Connect4 Game Help

I am attempting to create a Connect4 game with a very basic interface. I have created a general template for the graphics and input of the game. I now need to create a function that will check to see if each player has 4 in a row following each turn. I have been thinking about how to do this and I cannot think of anything. Here is my code now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <stdio.h>
#include <conio.h>

using namespace std;

void setGrid(char grid[6][7]);
int newTurn(char grid[6][7], bool p1turn);
int setX(char grid[6][7], bool p1turn);
int setO(char grid[6][7], bool p1turn);
void refreshGrid(char grid[6][7]);
void titleStart();

int main()
{
    titleStart();
 char grid[6][7];
   setGrid(grid);
   bool p1turn = true;
   
   newTurn(grid, p1turn);
  
   
   getch();
   return 0;
}
void titleStart()
{
     cout << "--------------------------------------------------" << endl;
     cout << "---        CONNECT 4                           ---" << endl;
     cout << "---                  CONNECT 4                 ---" << endl;
     cout << "---                           CONNECT 4        ---" << endl;
     cout << "---             By: Matt Hintzke               ---" << endl;
     cout << "--------------------------------------------------" << endl;
     cout << "     >  Player 1 = O   -----   Player 2 = X  <      " << endl;
     getch();
     system("cls");
     }
int setX(char grid[6][7], bool p1turn)
{
 int pick;
 int i;
 cout << "Player 2, please select a column: " <<endl;
 cin >> pick;
 if(pick > 0 && pick < 8)
 { 
 for(i=0;i<6;i++)
 {
  if(grid[i][pick-1] == '-')
  {
   grid[i][pick-1] = 'X';
   break;
                   }
                 }
                 }else{ cout << "Please choose between 1 and 7" <<endl; getch(); system("cls"); setX(grid, p1turn); }
   p1turn = 1;
   newTurn(grid, p1turn);
   return 0;
}
int setO(char grid[6][7], bool p1turn)
{
 int pick;
 int i;
 cout << "Player 1, please select a column: " <<endl;
 cin >> pick;
 if(pick > 0 && pick < 8)
 { 
 for(i=0;i<6;i++)
 {
  if(grid[i][pick-1] == '-')
  {
   grid[i][pick-1] = 'O';
   break;
                   }
                 }
                 }else{ cout << "Please choose between 1 and 7" <<endl; getch(); system("cls"); setO(grid, p1turn); }
   p1turn = 0;
   newTurn(grid, p1turn);
   return 0;
}
int newTurn(char grid[6][7], bool p1turn)
{
  //  k++;
    refreshGrid(grid);
     switch (p1turn)
   {
          case 0:
               setX(grid, p1turn);
               break;
          case 1:
               setO(grid, p1turn);
               break;
          }
          return 1;
}
void setGrid(char grid[6][7]){
     int j,i;
    
     char empty = '-';
     for(i=5;i>=0;i--)
     {
     for(j=0;j<7;j++)
     {
        grid[i][j] = empty;
        cout << " " <<grid[i][j]<< " ";            
                     }
                     cout << endl;
                     }
        cout << " 1  2  3 " <<endl;
     }
void refreshGrid(char grid[6][7])
{
     int i,j;
     system("cls");
      for(i=5;i>=0;i--)
     {
     for(j=0;j<7;j++)
     {
        cout << " " <<grid[i][j]<< " ";            
                     }
                     cout << endl;
                     }
        cout << " 1  2  3  4  5  6  7" <<endl;
     }
     


So is there some algorithm or something I can use to check if there are 4 "X"s or "O"s in a row either diagonally, horizontally, or vertically? Any ideas would be great. Thanks
It's fairly simple.
If you wanted to check if tile x, y was first in a row of 4 "x"s, you'd use a for loop
1
2
3
bool row = true;
for(int i = 0; i < 4; i++) if( grid[x+i][y] != 'X' ) row = false;
//note that this does not show bounds checking. It could be done either outside or inside the loop. 

If you write the same algorithm for other directions, you should notice that they are all very similar. In fact, you could write a function, which, with different arguments, could check 4 elements in any direction. I suppose I should let you try to figure it out yourself. As a hint, I'll give the declaration of that function:
bool check4( char grid[6][7], int x, int y, int dx, int dy, char type );

Now, when you have this function, you could call it 4 times for each tile, but actually you only need to check 8 directions from the tile which was last played on. Is this clear, or is there something I should explain?
Hmm I am trying to understand your method. First, since you are changing the first part of the array where it is [x+i], doesn't that mean you will be checking values along a column, not a row? My cord system is like this:

* * * * * * * 7 across
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
6 down where grid[6][7] is grid[rows][columns]. So you are checking in different rows for a line of 4 of the same type in a single column, correct?

Also, I understand how to do it with horizontal and vertical now, but what about diagonally?
Well, which of them is x and which is y depends on how you wrote your function which prints the grid. It doesn't matter though.
For other directions only [x+i][y] part changes. It would be [x][y+i], [x-i][y], [x][y-i], [x+i][y+i], [x+i][y-i], [x-i][y-i] and [x-i][y+i].
Notice that they are all [x+dx*i][y+dy*i] where dx and dy can be -1, 0 or +1.
Ok I think that makes sense, I will try to give it a go.
Now, when you have this function, you could call it 4 times for each tile, but actually you only need to check 8 directions from the tile which was last played on. Is this clear, or is there something I should explain?


Above is correct. Just remember to handle the sides and corners as there is no need to waste time checking all 8 directions for tiles at the sides and corners.
Topic archived. No new replies allowed.