Hi, I'm writing a connect 4 program. I have a couple of questions about the code i've been writing.
1.
I'm supposed to be using structures, but I really don't know how structures work, and reading the tutorials just makes things more difficult for me.
2.
In my code, i'm trying to check for a winner in 4 separate functions(Horizontal, Vertical, and 2 x Diagonal ways)
If my program finds two consecutive player pieces, it will add one to a counter. A problem that came out with was that the counter wont reset within the loop, so it will think that this scenario is a winner:
E E E K K E
E E E E E E
E K E E E E
K E E E E E
(E = Empty, K = Player's piece)
Sorry if it's ugly, I haven't had time to clean up some parts yet, but here's my check horizontal function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void check_horizontal_board(char&p_piece, char **board, int cols, int rows, int pieces, bool&gamerunning)
{
int count = 1;
if(count!=pieces)
{
for(int i=0; i<cols; i++)
for(int j=0; j<rows; j++)
{
if(board[i][j] == p_piece && board[i][j+1]== p_piece && board[i][j+1]!=' ' && board[i][j]!=' ')
{
count = count+1;
//cout << count << endl;
}
}
}
if(count == pieces)
{
gamerunning = false;
}
}
|