[txt-based game] Connect 4 with a twist

Hello everyone! Quick intro: I'm currently a Sophomore in Electrical Engineering at UTK, and I'm planning to get a minor in computer science. Anyway, I'm on spring break right now, and I started to learn about game developing. I only have the standard/basic c++ 102 course under my belt right now, but I'm really getting into self learning. I realize that I need to crawl before I walk and so fourth, so I started out my developing journey by making three simple console text-based games.

The first one was a simple hi-low guessing name that kinda got some rust off of my programming skills from freshman year. The second game was a states and capitals guessing game. It basically loaded a list of states and capitals from a file, randomly picked a state, and randomly picked a few capitals to guess from in a multiple choice format. When the user guesses correctly, the state/capital combination is removed from the array and a new list is written to a separate file. That gave me a solid refresher on file I/O.

-Connect 4 Twist-
The third game (which I just started today) works likes this.
Similarly to connect 4, you (drop) pieces onto a board from the top. Instead of two players and two colors, you instead have letters A, B, and C. The letters are generated randomly so you don't know which letter you will be available to use on a given turn. Getting a combination that lines up to ABC will win the game. Each letter placed by a player can be used by anyone to win. Each player also has the chance to use a Wildcard letter 'W' one time during the game that counts as any letter.

Finally getting to the point...
I was hoping someone out there could check out my code and look for bad habits/redundancies, etc... I'm trying to improve my programming skills in general in preparation for simple SDL 2d games, all constructive criticism is welcome. If you really hate my code, I'll just say it's because I'm EE not CS xD

Quick note: I know the code is lacking proper commenting, but I was just really in the zone spitting out code.


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
// Just3_Skeleton.cpp : main project file.

#include "stdafx.h"				//needed with visual c++
#include <iostream>				//basic in and out streams
#include <time.h>				//for setting RNG
#include <string>				//for names and such


char pieceLet[16];
char gameBoard[16];
unsigned int turnNum = 0;
std::string winLoc = "";
bool wildcardp1 = true;
bool wildcardp2 = true;

void generateTileArray(char pieceLet[])
{
	int pieceNum[16];
	srand(time(NULL));
	for (int i = 0; i < 16; i++)
	{
		pieceNum[i] = rand() % 3 + 1;
		switch (pieceNum[i])
		{
		case 1: pieceLet[i] = 'A';
			break;
		case 2: pieceLet[i] = 'B';
			break;
		case 3: pieceLet[i] = 'C';
			break;
		}
	}
}

void initGameBoard(char gameBoard[])
{
	std::cout << " ----------------------------------- \n"
				 "|        Welcome to ABC-3           |\n"
				 " ----------------------------------- \n"
				 "The goal of the game is to connect ABC\n"
				 "in any way possible. Letters are \n"
				 "generated randomly. W is a wildcard\n"
				 "That counts as any letter and can "
				 "only be used once.\n\n\n\n";
	for(int i = 0; i < 16; i++)
	{
		gameBoard[i] = ' ';
	}
}

void displayGameBoard(char gameBoard[])
{
	std::cout << "    ---------------\n"
				 "   | " << gameBoard[0] << " | " <<gameBoard[1] << " | " <<gameBoard[2] << " | " << gameBoard[3] <<" |\n"
				 "    ---------------\n"
				 "   | " << gameBoard[4] << " | " <<gameBoard[5] << " | " <<gameBoard[6] << " | " << gameBoard[7] <<" |\n"
				 "    ---------------\n"
				 "   | " << gameBoard[8] << " | " <<gameBoard[9] << " | " <<gameBoard[10] << " | " << gameBoard[11] <<" |\n"
				 "    ---------------\n"
				 "   | " << gameBoard[12] << " | " <<gameBoard[13] << " | " <<gameBoard[14] << " | " << gameBoard[15] <<" |\n"
				 "    ---------------\n"
				 "     1   2   3   4   ";
}

void takeTurn(char pieceLet[], char gameBoard[], int turnNum, bool& wildcardp1, bool& wildcardp2)
{
	char placeLoc = ' ';
	std::cout << "\n\nLetter: " << pieceLet[turnNum] <<std::endl;
place:
	std::cout << "Placement: ";
	std::cin >> placeLoc;
	if (turnNum %2 == 1)
	{
		if (wildcardp2 == false)
			goto noWildCard;
	}
	else if (turnNum %2 == 0)
	{
		if (wildcardp1 == false)
			goto noWildCard;
	}
	if (placeLoc == 'w' || placeLoc == 'W')
	{
		pieceLet[turnNum] = 'W';
		std::cout << "\nWildcard used!\n";
		if (turnNum %2 == 1)
		{
			wildcardp2 = false;
		}
		else if (turnNum %2 == 0)
		{
			wildcardp1 = false;
		}
		goto place;
	}
noWildCard:
	if (placeLoc != '1' && placeLoc != '2' && placeLoc != '3' && placeLoc != '4')
	{
		std::cout << "\nSorry, but " << placeLoc << " is not a valid choice.\n";
		goto place;
	}
	switch (placeLoc)
	{
	case '1': if (gameBoard[12] == ' ')
			{
				gameBoard[12] = pieceLet[turnNum];
			}
			else if (gameBoard[8] == ' ')
			{
				gameBoard[8] = pieceLet[turnNum];
			}
			else if (gameBoard[4] == ' ')
			{
				gameBoard[4] = pieceLet[turnNum];
			}
			else if (gameBoard[0] == ' ')
			{
				gameBoard[0] = pieceLet[turnNum];
			}
			else
			{
				std::cout << "Column is full! Pick another place to put your game piece.\n";
				goto place;
			}
			break;
	case '2': if (gameBoard[13] == ' ')
			{
				gameBoard[13] = pieceLet[turnNum];
			}
			else if (gameBoard[9] == ' ')
			{
				gameBoard[9] = pieceLet[turnNum];
			}
			else if (gameBoard[5] == ' ')
			{
				gameBoard[5] = pieceLet[turnNum];
			}
			else if (gameBoard[1] == ' ')
			{
				gameBoard[1] = pieceLet[turnNum];
			}
			else
			{
				std::cout << "Column is full! Pick another place to put your game piece.\n";
				goto place;
			}
			break;
	case '3': if (gameBoard[14] == ' ')
			{
				gameBoard[14] = pieceLet[turnNum];
			}
			else if (gameBoard[10] == ' ')
			{
				gameBoard[10] = pieceLet[turnNum];
			}
			else if (gameBoard[6] == ' ')
			{
				gameBoard[6] = pieceLet[turnNum];
			}
			else if (gameBoard[2] == ' ')
			{
				gameBoard[2] = pieceLet[turnNum];
			}
			else
			{
				std::cout << "Column is full! Pick another place to put your game piece.\n";
				goto place;
			}
			break;
	case '4': if (gameBoard[15] == ' ')
			{
				gameBoard[15] = pieceLet[turnNum];
			}
			else if (gameBoard[11] == ' ')
			{
				gameBoard[11] = pieceLet[turnNum];
			}
			else if (gameBoard[7] == ' ')
			{
				gameBoard[7] = pieceLet[turnNum];
			}
			else if (gameBoard[3] == ' ')
			{
				gameBoard[3] = pieceLet[turnNum];
			}
			else
			{
				std::cout << "Column is full! Pick another place to put your game piece.\n";
				goto place;
			}
			break;
	}
}
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
bool checkWin(char gameBoard[], std::string& winLoc)
{
	//check columns from top to bottom starting in column 1 and ending in column 4
	for(int k = 0; k < 4; k++)
	{
		int j = k;
		int x = 0;
		while( x < 2)
		{
		if (gameBoard[j] == 'A' || gameBoard[j] == 'W')
			{
				if (gameBoard[j+4] == 'B' || gameBoard[j+4] == 'W')
				{
					if (gameBoard[j+8] == 'C' || gameBoard[j+8] == 'W')
					{
						winLoc = "a column from top to bottom!!";
						return true;
					}
				}
			}
		j = j + 4;
		x++;
		}
	}

	//check columns from bottom to top starting in column 4 and ending in column 1
	for(int k = 15; k > 11; k--)
	{
		int j = k;
		int x = 0;
		while( x < 2)
		{
		if (gameBoard[j] == 'A' || gameBoard[j] == 'W')
			{
				if (gameBoard[j-4] == 'B' || gameBoard[j-4] == 'W')
				{
					if (gameBoard[j-8] == 'C' || gameBoard[j-8] == 'W')
					{
						winLoc = "a column from bottom to top!!";
						return true;
					}
				}
			}
		j = j - 4;
		x++;
		}
	}

	//check rows from left to right starting at row 1 and ending at row 4
	for (int k = 0; k <=12; k = k+4 )
	{
		int j = k;
		int x = 0;
		while(x < 2)
		{
			if (gameBoard[j] == 'A' || gameBoard[j] == 'W')
			{
				if (gameBoard[j+1] == 'B' || gameBoard[j+1] == 'W')
				{
					if (gameBoard[j+2] == 'C' || gameBoard[j+2] == 'W')
					{
						winLoc = "a row from left to right!!";
						return true;
					}
				}
			}
			j++;
			x++;
		}
	}

	//check rows from right to left starting at row 4 and ending at row 1
	for (int k = 15; k >= 3; k = k-4 )
	{
		int j = k;
		int x = 0;
		while(x < 2)
		{
			if (gameBoard[j] == 'A' || gameBoard[j] == 'W')
			{
				if (gameBoard[j-1] == 'B' || gameBoard[j-1] == 'W')
				{
					if (gameBoard[j-2] == 'C' || gameBoard[j-2] == 'W')
					{
						winLoc = "a row from right to left!!";
						return true;
					}
				}
			}
			j--;
			x++;
		}
	}
	
	//top left diagonal and bottom right diagonal going from bottom left to top right
	for(int k = 8; k <= 15 ; k += 5)
	{
		if (gameBoard[k] == 'A' || gameBoard[k] == 'W')
		{
			if (gameBoard[k-3] == 'B' || gameBoard[k-3] == 'W')
			{
				if (gameBoard[k-6] == 'C' || gameBoard[k-6] == 'W')
				{
					winLoc = "a diagonal from bottom-left to top-right!!";
					return true;
				}
			}
		}
	}

	//top left and bottom right diagonal going from top right to bottom left
	for(int k = 2; k <= 7 ; k += 5)
	{
		if (gameBoard[k] == 'A' || gameBoard[k] == 'W')
		{
			if (gameBoard[k+3] == 'B' || gameBoard[k+3] == 'W')
			{
				if (gameBoard[k+6] == 'C' || gameBoard[k+6] == 'W')
				{
					winLoc = "a diagonal from top-right to bottom-left!!";
					return true;
				}
			}
		}
	}

	//positive diagonal going from bottom left to top right
	for(int k = 12; k >= 9; k -= 3)
	{
		if (gameBoard[k] == 'A' || gameBoard[k] == 'W')
		{
			if (gameBoard[k-3] == 'B' || gameBoard[k-3] == 'W')
			{
				if (gameBoard[k-6] == 'C' || gameBoard[k-6] == 'W')
				{
					winLoc = "a diagonal from bottom-left to top-right!!";
					return true;
				}
			}
		}
	}

	//positive diagonal going from top-right to bottom-left
	for(int k = 3; k <= 6; k += 3)
	{
		if (gameBoard[k] == 'A' || gameBoard[k] == 'W')
		{
			if (gameBoard[k+3] == 'B' || gameBoard[k+3] == 'W')
			{
				if (gameBoard[k+6] == 'C' || gameBoard[k+6] == 'W')
				{
					winLoc = "a diagonal from top-right to bottom-left!!";
					return true;
				}
			}
		}
	}

	//top right and bottom left diagonal going from top-left to bottom-right
	for(int k = 1; k <= 4 ; k += 3)
	{
		if (gameBoard[k] == 'A' || gameBoard[k] == 'W')
		{
			if (gameBoard[k+5] == 'B' || gameBoard[k+5] == 'W')
			{
				if (gameBoard[k+10] == 'C' || gameBoard[k+10] == 'W')
				{
					winLoc = "a diagonal: top-left to bottom-right!!";
					return true;
				}
			}
		}
	}

	//top-right and bottom-left diagonal going from bottom-right to top-left
	for(int k = 14; k >= 11 ; k -= 3)
	{
		if (gameBoard[k] == 'A' || gameBoard[k] == 'W')
		{
			if (gameBoard[k-5] == 'B' || gameBoard[k-5] == 'W')
			{
				if (gameBoard[k-10] == 'C' || gameBoard[k-10] == 'W')
				{
					winLoc = "a diagonal: bottom-right to top-left!!";
					return true;
				}
			}
		}
	}

	//negative diagonal from top-left to bottom-right
	for(int k = 0; k <= 5; k += 5)
	{
		if (gameBoard[k] == 'A' || gameBoard[k] == 'W')
		{
			if (gameBoard[k+5] == 'B' || gameBoard[k+5] == 'W')
			{
				if (gameBoard[k+10] == 'C' || gameBoard[k+10] == 'W')
				{
					winLoc = "a diagonal from top-left to bottom-right!!";
					return true;
				}
			}
		}
	}

	//negative diagonal from bottom-right to top-left
	for(int k = 15; k >= 10; k -= 5)
	{
		if (gameBoard[k] == 'A' || gameBoard[k] == 'W')
		{
			if (gameBoard[k-5] == 'B' || gameBoard[k-5] == 'W')
			{
				if (gameBoard[k-10] == 'C' || gameBoard[k-10] == 'W')
				{
					winLoc = "a diagonal from bottom-right to top-left!!";
					return true;
				}
			}
		}
	}
	return false;
}

int main()
{	
	std::string player1 = "";
	std::string player2 = "";
	bool winner = false;
	char wait = ' ';
    generateTileArray(pieceLet);
	initGameBoard(gameBoard);
	std::cout << "Player 1 enter your name: ";
	std::cin >> player1;
	std::cout << "Player 2 enter your name: ";
	std::cin >> player2;
	std::cout << "\n\n\nTurn:     " << turnNum +1 << "\n"
				 "Player:   " << player1 << "\n"
				 "Wildcard: (availible)\n\n";
	displayGameBoard(gameBoard);

	//actual game loop
	while(turnNum < 16)
	{
		takeTurn(pieceLet, gameBoard, turnNum, wildcardp1, wildcardp2);
		winner = checkWin(gameBoard, winLoc);
		std::cout << "\n\n\n\n\n\n\n";
		if(winner == true)
		{
			break;
		}
		std::cout << "Turn:     " << turnNum + 2 << "\n"
				     "Player:   ";
		if ((turnNum + 2) % 2 == 0)
		{
			std::cout << player2 << "\n";
			if (wildcardp2 == true)
				std::cout << "Wildcard: (availible)\n\n";
			else
				std::cout << "Wildcard: (unavailible)\n\n";
		}
		else
		{
			std::cout << player1 << "\n";
			if (wildcardp1 == true)
				std::cout << "Wildcard: (available)\n\n";
			else
				std::cout << "Wildcard: (unavailable)\n\n";
		}
		displayGameBoard(gameBoard);
		turnNum++;
		std::cout << "\n\n\n\n";
	}
	std::cout << "\n\n\n\n";
	displayGameBoard(gameBoard);
	std::cout << "\n\n\n\n\n\n";
	if ((turnNum + 2) % 2 == 0)
	{
			std::cout << player2 << " won";
	}
	else
	{
			std::cout << player1 << " won";
	}
	std::cout << " by connecting " << winLoc;
	std::cin >> wait;
    return 0;
}
Topic archived. No new replies allowed.