Simple Connect 4 Program

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;
      
   }

}
Surely you have more requirements than just "use structures"? That's like telling someone to build a car and saying the only rule is that they have to use screws somehow.
Last edited on
Right, sorry we're supposed to use "a game struct that contains a pointer to the board put on the heap and the players’ piece selection".

Right now, I have the board set up as a 2d dynamic array. I just don't get the benefits of using a structure.
If you use a structure, you could have multiple games at once quite easily (e.g. a server). However, for this specific case, it is a simple matter of good practice.
Topic archived. No new replies allowed.