Need assistance with connect 4 game?

I need to create a win condition for my connect 4 game but have realised that there's a tonne of ways to win connect 4 and have no idea how to create an algorithm to check all these possibilities. Can anyone assist me as i am still pretty new to c++ and have been stuck on this for hours? Thanks in advance :)
THIS IS UPDATED! I have added column limits, i have added a clear screen to neaten things u a bit and have got an incomplete draw condition which won't work for some reason.
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

int main()
{
	bool menuLoop = false;
	string a;
	string name1, name2;
	int columnNumber;
	int columnCount1 = 5, columnCount2 = 5, columnCount3 = 5, columnCount4 = 5, columnCount5 = 5, columnCount6 = 5, columnCount7 = 5;
	int randNum1;
	int turn = 0;
	int c = 0;
	char carryOn;
	bool winFlag1 = false, winFlag2 = false;
	bool columnFull = true;
	cout << "Welcome to connect 4!" << endl;
	cout << "Do you want to look at the rules and description or start the game ?" << endl;
	while (menuLoop == false)
	{
		cout << "Please type in rules / desc or start!" << endl;
		cin >> a;
		if (a == "desc" || a == "DESC" || a == "rules" || a == "RULES")
		{
			cout << "Connect 4 is a two player board game where there are discs that consist of yellow or the colour red," << endl;
			cout << "the aim of the game is to match 4 discs of the same colour diagonally, vertically or horizontally.You drop discs in a 7x6 board." << endl;
			cout << "1) Each player must have one turn each time." << endl;
			cout << "2) Once you select a column you cannot change your mind." << endl;
			cout << "Please type in start to start the game!" << endl;
			cin >> a;
			menuLoop = true;
		}
		if (a == "start" || a == "START")
		{
			menuLoop = true;
			char verticalGrid[6][7];

			for (int i = 0; i <= 6; i++)
			{
				verticalGrid[0][i] = ' ';
			}
			for (int i = 0; i <= 6; i++)
			{
				verticalGrid[1][i] = ' ';
			}
			for (int i = 0; i <= 6; i++)
			{
				verticalGrid[2][i] = ' ';
			}
			for (int i = 0; i <= 6; i++)
			{
				verticalGrid[3][i] = ' ';
			}
			for (int i = 0; i <= 6; i++)
			{
				verticalGrid[4][i] = ' ';
			}
			for (int i = 0; i <= 6; i++)
			{
				verticalGrid[5][i] = ' ';
			}



			cout << "Please enter player 1's name." << endl;
			cin >> name1;
			cout << "Please enter player 2's name." << endl;
			cin >> name2;

			cout << "Player " << name1 << " goes first!" << endl;

			while (turn < 42 && winFlag1 == false && winFlag2 == false)
			{
				columnFull = false;
				if (turn % 2 == 0)
				{
				
					cout << "Player " << name1 << ", please select the column number you would like to drop your disc from!" << endl;
				}
				else
				{
					
					cout << "Player " << name2 << ", please select the column number you would like to drop your disc from!" << endl;
				}

			
				cin >> columnNumber;
				if ((columnNumber <= 0) || (columnNumber >= 8))
				{
					columnFull = true;
				}

				if (turn % 2 == 0)
				{
					if (columnNumber == 1)
					{
						if (columnCount1 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false) 
						{
							verticalGrid[columnCount1][0] = 'X';
							columnCount1--;
						}
					}
					else if (columnNumber == 2)
					{
						if (columnCount2 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false)
						{
							verticalGrid[columnCount2][1] = 'X';
							columnCount2--;
						}
					}
					else if (columnNumber == 3)
					{
						if (columnCount3 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false)
						{
							verticalGrid[columnCount3][2] = 'X';
							columnCount3--;
						}
					}
					else if (columnNumber == 4)
					{
						if (columnCount4 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false)
						{
							verticalGrid[columnCount4][3] = 'X';
							columnCount4--;
						}
					}
					else if (columnNumber == 5)
					{
						if (columnCount5 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false)
						{
							verticalGrid[columnCount5][4] = 'X';
							columnCount5--;
						}
					}
					else if (columnNumber == 6)
					{
						if (columnCount6 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false)
						{
							verticalGrid[columnCount6][5] = 'X';
							columnCount6--;
						}
					}
					else if (columnNumber == 7)
					{
						if (columnCount7 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false)
						{
							verticalGrid[columnCount7][6] = 'X';
							columnCount7--;
						}
					}
					else if (isalpha(columnNumber))
					{
						columnFull = true;
					}
				}
				else
				{
					if (columnNumber == 1)
					{
						if (columnCount1 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false)
						{
							verticalGrid[columnCount1][0] = 'O';
							columnCount1--;
						}
					}
					else if (columnNumber == 2)
					{
						if (columnCount2 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false)
						{
							verticalGrid[columnCount2][1] = 'O';
							columnCount2--;
						}
					}
					else if (columnNumber == 3)
					{
						if (columnCount3 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false)
						{
							verticalGrid[columnCount3][2] = 'O';
							columnCount3--;
						}
					}
					else if (columnNumber == 4)
					{
						if (columnCount4 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false)
						{
							verticalGrid[columnCount4][3] = 'O';
							columnCount4--;
						}
					}
					else if (columnNumber == 5)
					{
						if (columnCount5 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false)
						{
							verticalGrid[columnCount5][4] = 'O';
							columnCount5--;
						}
					}
					else if (columnNumber == 6)
					{
						if (columnCount6 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false)
						{
							verticalGrid[columnCount6][5] = 'O';
							columnCount6--;
						}
					}
					else if (columnNumber == 7)
					{
						if (columnCount7 == -1)
						{
							cout << "Please select another column as this one is full!" << endl;
							columnFull = true;
						}
						if (columnFull == false)
						{
							verticalGrid[columnCount7][6] = 'O';
							columnCount7--;
						}
					}
					else if (isalpha(columnNumber))
					{
						columnFull = true;
					}

				}
				if (columnFull == false) {
					for (int j = 0; j <= 5; j++)
					{
						cout << "+--+--+--+--+--+--+--+" << endl;

						for (int i = 0; i <= 6; i++)
						{
							cout << "| " << verticalGrid[j][i];
						}
						cout << "|" << endl;
					}
					cout << "+--+--+--+--+--+--+--+" << endl;
				}
				if (turn % 2 == 0)   // this code is used to switch to each players go (probably an easier that i will sort out later)
				{
					randNum1 = 0;
				}
				else
				{
					randNum1 = 1;
				}


				//checks horizontal

				if (columnFull == false)
				{
					turn++;
				}

				

				cout << "Press any key to continue!" << endl;
				cin >> carryOn;

				if (isalnum(carryOn)) 
				{
					system("cls");
				}
			}
				
		}
	}
			if (turn == 41) 
				{
					cout << "The game has ended in a draw!" << endl;
				}
	return 1;
}



Last edited on
Nested for loops are the way to go.
You can practice them on that list of for loops found at line 38.
Just at tip:
You can compact lines 166 to 214 to just a few lines.

1
2
3
4
5
6
7
8
9
10
11
for(unsigned int j = 0; j <= 5; j++)
{
	cout << "+--+--+--+--+--+--+--+" << endl;

	for (int i = 0; i <= 6; i++)
	{
		cout << "| " << verticalGrid[j][i];
	}
	cout << "|" << endl;
}
cout << "+--+--+--+--+--+--+--+" << endl;


You can apply that priciple at many places in your code. That way its much smaller, readable and its easier to make changes.
Thanks :)
There are only 1 ways to win... 4 touching tiles in a straight line, right?
Also, you can only have a win that involves the last piece placed.

So you just need to check the last piece placed for 3 more tokens that touch it in a straight line (check left, right, up, down, and diagonal). You must do this after every piece is played. Remember that hitting the edge of the board without a win is a not-win scenario, and don't iterate off the board with a crash to check nonexistent tiles there, just fail that check (no win) and move on.




Last edited on
What a lovely idea. Do you mind if I post some code too ?

Problem1: Line 72
while (turn < 42)
- When turn = 42 your code will end.

Problem2:
You might want to add this to your code since the users can type 0, 8 and 9.
1
2
3
4
				while ((columnNumber <=0) || (columnNumber >=8))
				{
					cin >> columnNumber;
				}


Problem3:
I don't know what this code does or why it's there. Add some comments.
It's not needed unless you add a computer player later.

1
2
3
4
5
6
7
8
				if (turn % 2 == 0)
				{
					randNum1 = 0;
				}
				else
				{
					randNum1 = 1;
				}


Problem4: Sorry it's late and I have to go but problem 4 is that the players can choose #1 every time and it doesn't throw an error. need to add some code for that check.

Fix those, post your updated code and maybe I can focus on your question.
To give you a partial answer, I'd create a function and call that to check if there was a winner.
@hhjkkh10

You also need to look at having more functions in your code. There is a bit of a rule that functions should be less than 40 LOC, including main. Having functions is a basic type of abstraction, it aids understanding. Also functions should do 1 conceptual thing.

Some candidates for functions:

Display menu;
Body of a loop
Body of an if, else if or else

But this is all after you get the hang of for loops to reduce your code.

Basically if you find yourself writing almost the same thing over and over, then there is a better way :+)
Last edited on
Thanks will defo post an updated version once i have figured it out :)
I have made an updated version look above at OP.
well I personally don't like the clear screen but it's not my program so I can live with it.
The part I really don't like is having to type a number and enter in order to continue.

New problem that didn't exist before... at line 324
I'm not sure why but if I type a letter such as 'a" the program goes into a loop and i can't play anymore.

1
2
3
4
5
6
7
8
				cout << "Press any key to continue!" << endl;
				cin >> carryOn;

				if (isalnum(carryOn)) 
				{
					system("cls");
				}



I did test putting in all ones for the column and that is working.

I'll try and get on in a hour and give some advise on testing for a winner.

So far what I came up with is you only have to test the column or row after the 4th play.
you only need to check the diagonal after the 1st players 6th turn.
next you have to determine the column and row for the last piece played.
you test the whole column for 4 together and the whole row.
The diagonal is giving me a bit of trouble thinking about, since if you play the 4th piece on row 1, 2, 3, or 5, 6 or 7 you only have to check the diagonal in one direction.
Last edited on
I didn't get to work on that tonight but thought I'd share with you what i did earlier, based on your 1st code post.

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// http://www.cplusplus.com/forum/general/215125/

// Needs code to check for winner

#include <conio.h>
#include <cstdlib>
#include <dos.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <cmath>
#include <iomanip>
#include <string>
using namespace std;
int Background = 0;
void Color(string color);

// use color in your output
void Color(string color){
    int colorNum;
	/**/ if (color=="Lwhite") colorNum=15;
    else if (color=="Red"   ) colorNum=12;
    else if (color=="Cyan"  ) colorNum=11;
    else if (color=="Gray"  ) colorNum= 8;
    else if (color=="Reset" ) colorNum= 7;
    else if (color=="Green" ) colorNum= 2;
    else if (color=="Blue" )  colorNum= 1;
  
//Sets text color and applies background
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (colorNum + (Background * 16)));
}
// use color in your output

int CheckNumber ()
{
int columnNumberX=0;
	cin >> columnNumberX;
	while ((columnNumberX <=0) || (columnNumberX >=8))
		{
			Color("Green"); cout << "A valid number is from 1 - 7, Please try again: ";
			cin >> columnNumberX;
		}
		Color("Lwhite"); 
//		cout << columnNumberX << endl; // testing
		return 	columnNumberX;
}

int main()
{
	bool menuLoop = false;
	string a;
	string name1, name2;
	int columnNumber;
	int columnCount1 = 5, columnCount2 = 5, columnCount3 = 5, columnCount4 = 5, columnCount5 = 5, columnCount6 = 5, columnCount7 = 5;
	int randNum1;
	int turn = 0;
	Color("Lwhite"); cout << "Welcome to connect 4!" << endl;
	cout << "Do you want to look at the rules and description or start the game ?" << endl;
	while (menuLoop == false)
	{
		cout << "Please type in rules / desc or start!" << endl;
		cin >> a;
		if (a == "desc" || a == "DESC" || a == "rules" || a == "RULES")
		{
			cout << "Connect 4 is a two player board game where there are discs that consist of yellow or the colour red," << endl;
			cout << "the aim of the game is to match 4 discs of the same colour diagonally, vertically or horizontally.You drop discs in a 7x6 board." << endl;
			cout << "1) Each player must have one turn each time." << endl;
			cout << "2) Once you select a column you cannot change your mind." << endl;
			cout << "Please type in start to start the game!" << endl;
			cin >> a;
			menuLoop = true;
		}
		if (a == "start" || a == "START")
		{
			menuLoop = true;
			char verticalGrid[6][7];

			for (int i = 0; i <= 6; i++)
			{
				verticalGrid[0][i] = ' ';
			}
			for (int i = 0; i <= 6; i++)
			{
				verticalGrid[1][i] = ' ';
			}
			for (int i = 0; i <= 6; i++)
			{
				verticalGrid[2][i] = ' ';
			}
			for (int i = 0; i <= 6; i++)
			{
				verticalGrid[3][i] = ' ';
			}
			for (int i = 0; i <= 6; i++)
			{
				verticalGrid[4][i] = ' ';
			}
			for (int i = 0; i <= 6; i++)
			{
				verticalGrid[5][i] = ' ';
			}



			cout << "Please enter player 1's name." << endl;
			cin >> name1; // Red
			cout << "Please enter player 2's name." << endl;
			cin >> name2; // Blue

			cout << "Player "; 	Color("Red"); cout << name1; Color("Lwhite"); cout << " goes first!" << endl;

			while (turn < 42)
			{
			
				if (turn % 2 == 0)
				{
//					cout << "Player " << name1 << ", please select the column number you would like to drop your disc from!" << endl;
					cout << "Player ";  Color("Red"); cout << name1; Color("Lwhite"); cout << ", please select the column number you would like to drop your disc from!" << endl;
				}
				else
				{
//					cout << "Player " << name2 << ", please select the column number you would like to drop your disc from!" << endl;
					cout << "Player ";  Color("Cyan"); cout << name2; Color("Lwhite"); cout << ", please select the column number you would like to drop your disc from!" << endl;
				}

				columnNumber=CheckNumber();



				if (turn % 2 == 0)
				{
// 				cout << columnNumber;  // testing

					if (columnNumber == 1)
					{
						verticalGrid[columnCount1][0] = 'X';
						columnCount1--;
					}
					else if (columnNumber == 2)
					{
						verticalGrid[columnCount2][1] = 'X';
						columnCount2--;
					}
					else if (columnNumber == 3)
					{
						verticalGrid[columnCount3][2] = 'X';
						columnCount3--;
					}
					else if (columnNumber == 4)
					{
						verticalGrid[columnCount4][3] = 'X';
						columnCount4--;
					}
					else if (columnNumber == 5)
					{
						verticalGrid[columnCount5][4] = 'X';
						columnCount5--;
					}
					else if (columnNumber == 6)
					{
						verticalGrid[columnCount6][5] = 'X';
						columnCount6--;
					}
					else if (columnNumber == 7)
					{
						verticalGrid[columnCount7][6] = 'X';
						columnCount7--;
					}
					
				}
				else
				{
					if (columnNumber == 1)
					{
						verticalGrid[columnCount1][0] = 'O';
						columnCount1--;
					}
					else if (columnNumber == 2)
					{
						verticalGrid[columnCount2][1] = 'O';
						columnCount2--;
					}
					else if (columnNumber == 3)
					{
						verticalGrid[columnCount3][2] = 'O';
						columnCount3--;
					}
					else if (columnNumber == 4)
					{
						verticalGrid[columnCount4][3] = 'O';
						columnCount4--;
					}
					else if (columnNumber == 5)
					{
						verticalGrid[columnCount5][4] = 'O';
						columnCount5--;
					}
					else if (columnNumber == 6)
					{
						verticalGrid[columnCount6][5] = 'O';
						columnCount6--;
					}
					else if (columnNumber == 7)
					{
						verticalGrid[columnCount7][6] = 'O';
						columnCount7--;
					}

				}


					for(unsigned int j = 0; j <= 5; j++)
					{
						
						Color("Lwhite");cout << "+--+--+--+--+--+--+--+" << endl;
					
						for (int i = 0; i <= 6; i++)
						{
							Color("Lwhite");cout << "| ";
							if (verticalGrid[j][i] == 'X')
								{Color("Red"); cout <<verticalGrid[j][i];}
							else 
								{Color("Cyan");	cout <<verticalGrid[j][i];}
						}
						Color("Lwhite");cout << "|" << endl;
					}
					Color("Lwhite");cout << "+--+--+--+--+--+--+--+" << endl;
					

				turn++;
			}
			
			// End game if no winner.
			
			
		}
	}

	return 1;
}

Topic archived. No new replies allowed.