I need assistance with my connect 4 game?

Whenever i input a letter as a column number my program goes crazy and starts printing out text again and again like its in an infinite loop. Would be awesome if someone could take a look at it for me. Thanks in advance :)
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
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

char playerXorO; 
char verticalGrid[6][7];
int columnNumber; // 
int columnCount[8] = { 5, 5, 5, 5, 5, 5, 5, 5};
bool columnFull = true;
char XorO[2] = { 'X', 'O' };

void boardCreate() 
{
	for (int j = 0; j <= 5; j++) // gives every element in the verticalGrid array the value of a blank space
	{
		for (int i = 0; i <= 6; i++)
		{
			verticalGrid[j][i] = ' ';
		}
	}
}

void columnSelect()
{
	for (int i = 1; i <= 7; i++) {		// for loop allows the user to select and drop a disc into a column in the gird
		if (columnNumber == i)
		{
			if (isalpha(columnNumber))
			{
				columnFull = true;
			}
			if (columnCount[i] == -1)
			{
				cout << "Please select another column as this one is full!" << endl;
				columnFull = true;
			}
			if (columnFull == false)
			{
				verticalGrid[columnCount[i]][i - 1] = playerXorO;
				columnCount[i]--;
			}
		}
	}
}

void columnPrint()
{
	if (columnFull == false) {				// prints out the gameboard
		for (int j = 0; j <= 5; j++)
		{
			cout << "+--+--+--+--+--+--+--+" << endl;

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

}

int main()
{
	
	string inputStart;
	string name1, name2, name; 
	
	int turn = 0;
	bool winFlag1 = false;
	int winFlagCount = 0, winFlagCount1 = 0, winFlagCount2 = 0, winFlagCount3 = 0;
	bool gameLoop = true;
	
	// prints out the text that tells the user about connect 4 and the rules
		cout << "Welcome to connect 4!" << endl;
		cout << "Connect 4 is a two player board game where there are discs that consist of the letter X or the letter O," << endl;
		cout << "the aim of the game is to match 4 discs of the same letter diagonally, vertically or horizontally.\nYou 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;

	while (gameLoop == true) {
		cout << "Please type in start to start the game!" << endl; // tells the user to type in start
		cin >> inputStart;

		if (inputStart == "start" || inputStart == "START")
		{
			gameLoop = false;
			
			boardCreate(); // calling the function that creates the board



			cout << "Please enter player 1's name." << endl; // collects the data for player 1 and 2's name
			cin >> name1;
			cout << "Please enter player 2's name." << endl;
			cin >> name2;

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

			while (turn < 42 && winFlag1 == false)
			{
				winFlagCount = 0;
				winFlagCount1 = 0;
				winFlagCount2 = 0;
				winFlagCount3 = 0;
				columnFull = false;
				if (turn % 2 == 0) // selection sequence allows the players to switch turns
				{
					name = name1;
					cout << name1 << ", please select the column number you would like to drop your disc from!" << endl;
					playerXorO = 'X';
				}
				else
				{
					name = name2;
					cout << name2 << ", please select the column number you would like to drop your disc from!" << endl;
					playerXorO = 'O';
				}


				cin >> columnNumber;

				if (columnNumber <= 0 || columnNumber >= 8) //if statement makes sure the user doesnt input an invalid column number
				{
					columnFull = true;
					cout << "That is an invalid column! Please pick a column from 1-7." << endl;
				}

				columnSelect(); // calling the function that allows discs to drop into the grid

				columnPrint(); // calling the function that prints out the game board



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

				//this for loop checks to see if the user has won diagonally
				for (int j = 0; j < 4; ++j)
				{
					for (int i = 5; i > 2; --i)
					{
						if (verticalGrid[j][i] == playerXorO)
						{
							winFlagCount++;
							if (verticalGrid[j + 1][i - 1] == playerXorO)
							{
								winFlagCount++;
								if (verticalGrid[j + 2][i - 2] == playerXorO)
								{
									winFlagCount++;
									if (verticalGrid[j + 3][i - 3] == playerXorO)
									{
										winFlagCount++;

										if (winFlagCount == 4) {
											cout << name << " wins congratulations!" << endl;
											winFlag1 = true;
											break;
										}
									}
								}
							}
						}
					}
				}
				//another loop that checks the other diagonal direction
				for (int j = 0; j < 4; ++j)
				{
					for (int i = 5; i > 2; --i)
					{
						if (verticalGrid[j][i - 3] == playerXorO)
						{
							winFlagCount1++;
							if (verticalGrid[j + 1][i - 2] == playerXorO)
							{
								winFlagCount1++;
								if (verticalGrid[j + 2][i - 1] == playerXorO)
								{
									winFlagCount1++;
									if (verticalGrid[j + 3][i] == playerXorO)
									{
										winFlagCount1++;

										if (winFlagCount1 == 4) {
											cout << name << " wins congratulations!" << endl;
											winFlag1 = true;
											break;
										}
									}
								}
							}
						}
					}
				}
				
				//this for loop checks to see if the user has won vertically
				for (int numColumn = 0; numColumn < 7; ++numColumn)
				{
					for (int check = 5; check > 2; --check)
					{
						if (verticalGrid[numColumn][check] == playerXorO)
						{
							winFlagCount2++;
							if (verticalGrid[numColumn][check - 1] == playerXorO)
							{
								winFlagCount2++;
								if (verticalGrid[numColumn][check - 2] == playerXorO)
								{
									winFlagCount2++;
									if (verticalGrid[numColumn][check - 3] == playerXorO)
									{
										winFlagCount2++;

										if (winFlagCount2 == 4) {
											cout << name << " wins congratulations!" << endl;
											winFlag1 = true;
											break;
										}
									}
								}
							}
						}
					}
				}

				//this for loop checks to see if the user has won horizontally
				for (int numRow = 0; numRow < 6; ++numRow)
				{
					for (int check1 = 6; check1 > 2; --check1)
					{
						if (verticalGrid[check1][numRow] == playerXorO)
						{
							winFlagCount3++;
							if (verticalGrid[check1 - 1][numRow] == playerXorO)
							{
								winFlagCount3++;
								if (verticalGrid[check1 - 2][numRow] == playerXorO)
								{
									winFlagCount3++;
									if (verticalGrid[check1 - 3][numRow] == playerXorO)
									{
										winFlagCount3++;
										if (winFlagCount3 == 4) {
											cout << name << " wins congratulations!" << endl;
											winFlag1 = true;
											break;
										}
									}
								}
							}
						}
					}
				}
				

				// these 2 if statements end the game as when turn = 45 the while loop doesn't run anymore
				if (winFlag1 == true)
				{
					turn = 45;
				}

				if (turn == 42)
				{
					cout << "The game has ended in a draw!" << endl;
					turn = 45;
				}



			}

		}
		else
		{

		}
	}
			
	return 1;
}
Last edited on
Hi,

Your columnNumber is of type int, so if you type in an alphabetical char, it will be equivalent to the ascii code of that char.

Some other problems:

You need to make more use of functions: there is too much code in main. Your comments describe quite well what should be in a function.

Avoid global variables, they will bite you in ass one day :+)

You can use for loops instead of all those nested if statements.

Good Luck !!

Please don't start a new topic about the same subject, just keep the same one going. It's a time waster for us, because people wind up making the same comments on both topics.

http://www.cplusplus.com/forum/general/215125/
Last edited on
There are multiple ways, but here is one.

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
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

char playerXorO; 
char verticalGrid[6][7];
char columnNumberX; // 
int columnNumber; // 
int columnCount[8] = { 5, 5, 5, 5, 5, 5, 5, 5};
bool columnFull = true;
char XorO[2] = { 'X', 'O' };

void boardCreate() 
{
	for (int j = 0; j <= 5; j++) // gives every element in the verticalGrid array the value of a blank space
	{
		for (int i = 0; i <= 6; i++)
		{
			verticalGrid[j][i] = ' ';
		}
	}
}

void columnSelect()
{
	for (int i = 1; i <= 7; i++) {		// for loop allows the user to select and drop a disc into a column in the gird
		if (columnNumber == i)
		{
			if (isalpha(columnNumber))
			{
				columnFull = true;
			}
			if (columnCount[i] == -1)
			{
				cout << "Please select another column as this one is full!" << endl;
				columnFull = true;
			}
			if (columnFull == false)
			{
				verticalGrid[columnCount[i]][i - 1] = playerXorO;
				columnCount[i]--;
			}
		}
	}
}

void columnPrint()
{
	if (columnFull == false) {				// prints out the gameboard
		for (int j = 0; j <= 5; j++)
		{
			cout << "+--+--+--+--+--+--+--+" << endl;

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

}

int main()
{
	
	string inputStart;
	string name1, name2, name; 
	
	int turn = 0;
	bool winFlag1 = false;
	int winFlagCount = 0, winFlagCount1 = 0, winFlagCount2 = 0, winFlagCount3 = 0;
	bool gameLoop = true;
	
	// prints out the text that tells the user about connect 4 and the rules
		cout << "Welcome to connect 4!" << endl;
		cout << "Connect 4 is a two player board game where there are discs that consist of the letter X or the letter O," << endl;
		cout << "the aim of the game is to match 4 discs of the same letter diagonally, vertically or horizontally.\nYou 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;

	while (gameLoop == true) {
		cout << "Please type in start to start the game!" << endl; // tells the user to type in start
		cin >> inputStart;

		if (inputStart == "start" || inputStart == "START")
		{
			gameLoop = false;
			
			boardCreate(); // calling the function that creates the board



			cout << "Please enter player 1's name." << endl; // collects the data for player 1 and 2's name
			cin >> name1;
			cout << "Please enter player 2's name." << endl;
			cin >> name2;

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

			while (turn < 42 && winFlag1 == false)
			{
				winFlagCount = 0;
				winFlagCount1 = 0;
				winFlagCount2 = 0;
				winFlagCount3 = 0;
				columnFull = false;
				if (turn % 2 == 0) // selection sequence allows the players to switch turns
				{
					name = name1;
					cout << name1 << ", please select the column number you would like to drop your disc from!" << endl;
					playerXorO = 'X';
				}
				else
				{
					name = name2;
					cout << name2 << ", please select the column number you would like to drop your disc from!" << endl;
					playerXorO = 'O';
				}

				cin >> columnNumberX;
				if ( (columnNumberX=='1') || (columnNumberX=='2') || (columnNumberX=='3') || (columnNumberX=='4') || (columnNumberX=='5') || (columnNumberX=='6') || (columnNumberX=='7') )
				{
				columnNumber=columnNumberX- 48;
				}
				else
				{
					columnFull = true;
					cout << "That is an invalid column! Please pick a column from 1-7." << endl;
				}

				columnSelect(); // calling the function that allows discs to drop into the grid

				columnPrint(); // calling the function that prints out the game board



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

				//this for loop checks to see if the user has won diagonally
				for (int j = 0; j < 4; ++j)
				{
					for (int i = 5; i > 2; --i)
					{
						if (verticalGrid[j][i] == playerXorO)
						{
							winFlagCount++;
							if (verticalGrid[j + 1][i - 1] == playerXorO)
							{
								winFlagCount++;
								if (verticalGrid[j + 2][i - 2] == playerXorO)
								{
									winFlagCount++;
									if (verticalGrid[j + 3][i - 3] == playerXorO)
									{
										winFlagCount++;

										if (winFlagCount == 4) {
											cout << name << " wins congratulations!" << endl;
											winFlag1 = true;
											break;
										}
									}
								}
							}
						}
					}
				}
				//another loop that checks the other diagonal direction
				for (int j = 0; j < 4; ++j)
				{
					for (int i = 5; i > 2; --i)
					{
						if (verticalGrid[j][i - 3] == playerXorO)
						{
							winFlagCount1++;
							if (verticalGrid[j + 1][i - 2] == playerXorO)
							{
								winFlagCount1++;
								if (verticalGrid[j + 2][i - 1] == playerXorO)
								{
									winFlagCount1++;
									if (verticalGrid[j + 3][i] == playerXorO)
									{
										winFlagCount1++;

										if (winFlagCount1 == 4) {
											cout << name << " wins congratulations!" << endl;
											winFlag1 = true;
											break;
										}
									}
								}
							}
						}
					}
				}
				
				//this for loop checks to see if the user has won vertically
				for (int numColumn = 0; numColumn < 7; ++numColumn)
				{
					for (int check = 5; check > 2; --check)
					{
						if (verticalGrid[numColumn][check] == playerXorO)
						{
							winFlagCount2++;
							if (verticalGrid[numColumn][check - 1] == playerXorO)
							{
								winFlagCount2++;
								if (verticalGrid[numColumn][check - 2] == playerXorO)
								{
									winFlagCount2++;
									if (verticalGrid[numColumn][check - 3] == playerXorO)
									{
										winFlagCount2++;

										if (winFlagCount2 == 4) {
											cout << name << " wins congratulations!" << endl;
											winFlag1 = true;
											break;
										}
									}
								}
							}
						}
					}
				}

				//this for loop checks to see if the user has won horizontally
				for (int numRow = 0; numRow < 6; ++numRow)
				{
					for (int check1 = 6; check1 > 2; --check1)
					{
						if (verticalGrid[check1][numRow] == playerXorO)
						{
							winFlagCount3++;
							if (verticalGrid[check1 - 1][numRow] == playerXorO)
							{
								winFlagCount3++;
								if (verticalGrid[check1 - 2][numRow] == playerXorO)
								{
									winFlagCount3++;
									if (verticalGrid[check1 - 3][numRow] == playerXorO)
									{
										winFlagCount3++;
										if (winFlagCount3 == 4) {
											cout << name << " wins congratulations!" << endl;
											winFlag1 = true;
											break;
										}
									}
								}
							}
						}
					}
				}
				

				// these 2 if statements end the game as when turn = 45 the while loop doesn't run anymore
				if (winFlag1 == true)
				{
					turn = 45;
				}

				if (turn == 42)
				{
					cout << "The game has ended in a draw!" << endl;
					turn = 45;
				}



			}

		}
		else
		{

		}
	}
			
	return 1;
}


Thanks for the tips and the code, i will defo check it out today thanks!
Last edited on
@SamuelAdams just thought i should tell you that there is a strange bug when the win condition doesnt work with your fix. It tends to only happen when there are a few turns left.
Last edited on
Basically this is what I added, and a few other lines to make this work.
It was added so that you wouldn't have that endless loop if they entered a char.
The only thing I can think of is that it checks to make sure it's a valid value.
It does not check if the column is full or anything else.


1
2
3
4
5
6
				cin >> columnNumberX;
				if ( (columnNumberX=='1') || (columnNumberX=='2') || (columnNumberX=='3') || (columnNumberX=='4') || (columnNumberX=='5') || (columnNumberX=='6') || (columnNumberX=='7') )
				{
				columnNumber=columnNumberX- 48;
				}

Why don't you make the Column a char instead?

@SamuelAdams solution is some thing to be avoided, IMO

There is a lot that need to be fixed up here, some of it was detailed in the other topic:

http://www.cplusplus.com/forum/general/215125/

@OP

See how time is being wasted here with this duplicate topic?
Topic archived. No new replies allowed.