-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectFour.cpp
More file actions
323 lines (281 loc) · 9.32 KB
/
Copy pathConnectFour.cpp
File metadata and controls
323 lines (281 loc) · 9.32 KB
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
# include <iostream>
# include <random>
using namespace std;
void showBoard(char board[6][7]);
bool insert_to_board(char board[6][7], char player, int col);
bool board_is_full(char board[6][7]);
bool gameIsOver(char board [6][7]);
string rowSubArr(char board [6][7], int row, int start, int end);
string colSubArr(char board [6][7], int col, int start, int end);
bool hasDiagonalFour(char board[6][7]);
int generate_random();
void two_player();
void single_player();
int main() {
cout << "Welcome to my C++ Connect 4, Enter 1 for single player mode and 2 for two player mode: " << endl;
int n;
cin >> n;
switch (n)
{
case 1:
single_player();
break;
case 2:
two_player();
break;
default:
cout << "Invalid input, try again!" << endl;
break;
}
return 0;
}
void showBoard(char board [6][7]) {
cout << " ";
for(int i = 1; i <= 7; i++)
cout << i << " ";
cout << "\n------------------------------" << endl;
for(int i = 0; i < 6; i++) {
cout << " : ";
for(int j = 0; j < 7; j++) {
cout << ' ' << board[i][j] << ' ';
}
cout << endl;
}
cout << "------------------------------" << endl;
}
bool insert_to_board(char board[6][7], char player, int col) {
col--;
int i = 5;
while(i >= 0) {
if(board[i][col] != '-') {
i--;
}
else {
board[i][col] = player;
return true;
}
}
return false;
}
bool board_is_full(char board[6][7]) {
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 7; j++) {
if(board[i][j] == '-') {
return false;
}
}
}
return true;
}
bool gameIsOver(char board[6][7]) {
string player_R = "RRRR";
string player_Y = "YYYY";
for(int r = 0; r < 6; r++) {
for(int c = 0; c < 4; c++) {
if(rowSubArr(board, r, c, c+3) == player_R || rowSubArr(board, r, c, c+3) == player_Y) {
return true;
}
}
}
for(int c = 0; c < 7; c++) {
for(int r = 0; r < 3; r++) {
if(colSubArr(board, c, r, r + 3) == player_R || colSubArr(board, c, r, r + 3)== player_Y) {
return true;
}
}
}
return hasDiagonalFour(board);
}
string rowSubArr(char board [6][7], int row, int start, int end) {
string sub = "";
for(int i = start; i <= end; i++) {
sub += board[row][i];
}
return sub;
}
string colSubArr(char board [6][7], int col, int start, int end) {
string sub = "";
for(int i = start; i <= end; i++) {
sub += board[i][col];
}
return sub;
}
bool hasDiagonalFour(char board[6][7]) {
for(int i = 0; i < 3; i++) {
string diagonal = "";
diagonal = diagonal + board[i][i] + board[i+1][i+1] + board[i+2][i+2] + board[i+3][i+3];
if (diagonal == "RRRR" || diagonal == "YYYY") {
return true;
}
}
for(int i = 1; i < 3; i++) {
string diagonal = "";
diagonal = diagonal + board[i][i - 1] + board[i+1][i] + board[i+2][i+1] + board[i+3][i+2];
if (diagonal == "RRRR" || diagonal == "YYYY") {
return true;
}
}
for(int i = 1; i < 4; i++) {
string diagonal = "";
diagonal = diagonal + board[i-1][i] + board[i][i+1] + board[i+1][i+2] + board[i+2][i+3];
if (diagonal == "RRRR" || diagonal == "YYYY") {
return true;
}
}
for(int i = 2; i < 4; i++) {
string diagonal = "";
diagonal = diagonal + board[i-2][i] + board[i-1][i+1] + board[i][i+2] + board[i+1][i+3];
if (diagonal == "RRRR" || diagonal == "YYYY") {
return true;
}
}
string diagonal = "";
diagonal = diagonal + board[2][0] + board[3][1] + board[4][2] + board[5][3];
if (diagonal == "RRRR" || diagonal == "YYYY") {
return true;
}
diagonal = "";
diagonal = diagonal + board[0][3] + board[1][4] + board[2][5] + board[3][6];
if (diagonal == "RRRR" || diagonal == "YYYY") {
return true;
}
for(int i = 0; i < 3; i++) {
string diagonal = "";
int n = 7;
diagonal = diagonal + board[i][n - 1 - i] + board[i+1][n - 1 - (i+1)] + board[i+2][n - 1 - (i+2)] + board[i+3][n - 1 - (i+3)];
if (diagonal == "RRRR" || diagonal == "YYYY") {
return true;
}
}
for(int i = 1; i < 3; i++) {
string diagonal = "";
int n = 7;
diagonal = diagonal + board[i][n - 1 - (i-1)] + board[i+1][n - 1 - i] + board[i+2][n - 1 - (i+1)] + board[i+3][n - 1 - (i+2)];
if (diagonal == "RRRR" || diagonal == "YYYY") {
return true;
}
}
for(int i = 0; i < 3; i++) {
string diagonal = "";
int n = 6;
diagonal = diagonal + board[i][n - 1 - i] + board[i+1][n - 1 - (i+1)] + board[i+2][n - 1 - (i+2)] + board[i+3][n - 1 - (i+3)];
if (diagonal == "RRRR" || diagonal == "YYYY") {
return true;
}
}
for(int i = 0; i < 2; i++) {
string diagonal = "";
int n = 5;
diagonal = diagonal + board[i][n - 1 - i] + board[i+1][n - 1 - (i+1)] + board[i+2][n - 1 - (i+2)] + board[i+3][n - 1 - (i+3)];
if (diagonal == "RRRR" || diagonal == "YYYY") {
return true;
}
}
diagonal = "";
diagonal = diagonal + board[3][0] + board[2][1] + board[1][2] + board[0][3];
if (diagonal == "RRRR" || diagonal == "YYYY") {
return true;
}
diagonal = "";
diagonal = diagonal + board[5][3] + board[4][4] + board[3][5] + board[2][6];
if (diagonal == "RRRR" || diagonal == "YYYY") {
return true;
}
return false;
}
int generate_random()
{
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> dist(1, 7);
return dist(gen);
}
void single_player() {
cout << "Welcome to Connect4 single player mode" << endl;
cout << "You're always Red(R)" << endl;
char board [6][7];
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 7; j++) {
board[i][j] = '-';
}
}
int col;
char player = 'R';
bool gameover = false;
while (!gameover) {
if(board_is_full(board)) {
cout << "Draw, board is full and neither player R nor player Y has won!" << endl;
break;
}
//Computer's turn
if(player == 'Y') {
bool computer = insert_to_board(board, player, generate_random());
while(!computer) {
computer = insert_to_board(board, player, generate_random());
}
}
//Player's turn
else {
cout << "Player: " << player << ", enter the column in where to insert your piece: " << endl; showBoard(board);
cin >> col;
while(col < 1 || col > 7) {
cout << player << ": Invalid, choice for column! please re-enter:" << endl; showBoard(board);
cin >> col;
}
bool insert_success = insert_to_board(board, player, col);
while(!insert_success) {
cout << player << ": The column you chose is full, please select another: " << endl;
cin >> col;
insert_success = insert_to_board(board, player, col);
}
}
gameover = gameIsOver(board);
if(!gameover) {
player = (player == 'R') ? 'Y' : 'R';
}
else {
showBoard(board);
cout << "Game Over, player: " << player << " has won." << endl;
cout << "Run the code again to play again!" << endl;
}
}
}
void two_player() {
cout << "Welcome to Connect4 2 player mode!" << endl;
char board [6][7];
for(int i = 0; i < 6; i++) {
for(int j = 0; j < 7; j++) {
board[i][j] = '-';
}
}
int col;
char player = 'R';
bool gameover = false;
while (!gameover) {
if(board_is_full(board)) {
cout << "Draw, board is full and neither player R nor player Y has won!" << endl;
break;
}
cout << "Player: " << player << ", enter the column in where to insert your piece: " << endl; showBoard(board);
cin >> col;
while(col < 1 || col > 7){
cout << player << ": Invalid, choice for column! please re-enter:" << endl; showBoard(board);
cin >> col;
}
bool insert_success = insert_to_board(board, player, col);
while(!insert_success) {
cout << player << ": The column you chose is full, please select another: " << endl;
cin >> col;
insert_success = insert_to_board(board, player, col);
}
gameover = gameIsOver(board);
if(!gameover) {
player = (player == 'R') ? 'Y' : 'R';
}
else {
showBoard(board);
cout << "Game Over, player: " << player << " has won." << endl;
cout << "Run the code again to play again!" << endl;
}
}
}