I've been working on a simple connect four c++ program, where two players play against each other (no A.I.)
It is the first time i've written a c++ program using matrixes.
For some reason whenever I run my program it does not seem to show the symbols i assigned for both players, nor does it stop after it is supposed to reach four-in-a-row
I've experimented with two different display functions, neither seem to be working
#include<iostream>
#include "lvp\matrix.h"
#include<conio.h>
using namespace std;
bool check(int a, int B)/>/>/>;
matrix<char>grid(6,7);
void display(/*matrix<char> &m*/);
int gravitydrop(int b, char player);
const char BRICK=177;
const int rowsize=6;
char player=1;
int main()
{
bool win=false;
int holdc=0; //drop value hold2
int holdinput; //hold
int numPlaced=0;
int count=0;
system("cls");
for(int a =0;a <= 5; a++){ //fill place with whitespace
for(int b = 0; b<=6; b++) //
grid[a][b] = ' '; //
}
cout<<"Instructions: There are two players in a game of connect four."<<endl;
cout<<"First to get four of their pieces in a linear line next to each other wins!"<<endl;
display();
_getch();
while (!win)
{
if (holdc!=-1)
{
if (player==1)
{
cout<<"Player one, where do you wish to place your piece?"<<endl;
player=2;
}
else
{
cout<<"Player two, where do you wish to place your piece?"<<endl;
player=1;
}
}
while(true)
{
if (numPlaced==42) break;
cin>>holdinput;
holdinput--;
if (holdinput<=6 && holdinput>=0)break;
else
cout<<"Please enter a valid number between 1 and 7"<<endl;
}
}
if(numPlaced==42)
_getch;
holdc=gravitydrop(holdinput,player);
if (holdc==-1)
cout<<"column full. please choose another number."<<endl;
else
{
win = check(holdc,holdinput);
numPlaced++;
system("cls");
display();
}
if(numPlaced==42)
{
cout<<"It's a tie"<<endl;
_getch;
return 0;
}
if (player==1)
cout<<"game won by player 1";
else
cout<<"game won by player 2";
_getch;
return 0;
}
/*void display(matrix<char> &m)
{
for(int r=0; r<m.numrows(); r++)
{
//horizontal line
for(int i=0; i<m.numcols()*2+1; i++)
cout<<BRICK;
cout<<endl;
//lines containing actual elements
for(int c=0; c<m.numcols(); c++)
{
cout<<BRICK<<m[r][c];
}
cout<<BRICK<<endl;
}
//horizontal line
for(int i=0; i<m.numcols()*2+1; i++)
cout<<BRICK;
cout<<endl;
//grid numbering
for(int c=0; c<m.numcols(); c++)
{
cout<<" "<<c;
}
cout<<endl;
}*/
int gravitydrop(int b, char player)
{
if(b >=0 && b<= 6)
{
if(grid[0][b] == ' ')
{
int i;
for(i = 0;grid[i][b] == ' ';i++)
if(i == 5)
{
grid[i][b] = player;
return i;
}
i--;
grid[i][b] =player;
return i;
}
else
return -1;
}
else
return -1;
}
bool check(int a, int B)/>/>/>
{
int vertcheck=1;
int horcheck=1;
int dia1check=1; // (\)
int dia2check=1; // (/)
int v;
int h;
for (v = a +1;grid[v][b]==player && v <= 5;v++,vertcheck++); //checks to see if the player is same for four in a row or greater.
for(v = a -1;grid[v][b] == player && v >= 0;v--,vertcheck++);
if(vertcheck >= 4)
return true;
for(h = b -1;grid[a][h] == player && h>= 0;h--,horcheck++); //checks "4" 4-in-a-column
for(h = b +1;grid[a][h] == player && h<= 6;h++,horcheck++);
if(horcheck>= 4)
return true;
for(v = a -1, h= b +1;grid[v][h] == player && v>=0 && h <= 6; dia2check++, v --, h++); //checks for (/)
for(v = a +1, h= b -1;grid[v][h] == player && v<=5 && h>=0; dia2check++, v ++, h--);
if(dia2check >= 4)
return true;
for(v = a -1, h= b -1;grid[v][h] == player && v>=0 && h >=0; dia1check++,v --,h --); //checks for (\)
for(v = a +1, h = b+1;grid[v][h] == player && v<=5 && h<=6;dia1check ++, v ++, h++);
if(dia1check >= 4)
return true;
return false;
}
void display(){
cout<<" 1 2 3 4 5 6 7\n";
for(int a = 0; a<= 5; a++)
{
for(int b =0; b <= 6; b++) cout<<char(218)<<char(196)<<char(191)<<" ";
cout<<endl;
for(int b =0; b <= 6; b++) cout<<char(179)<<grid[a][b]<<char(179)<<" ";
cout<<endl;
for(int b =0; b <= 6; b++) cout<<char(192)<<char(196)<<char(217)<<" ";
cout<<endl;
}
}
It would be a great help if someone could kindly point out why my pieces aren't displaying nor adding up correctly.

New Topic/Question
Reply



MultiQuote





|