-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf.c
317 lines (292 loc) · 11.6 KB
/
cf.c
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
#include <stdio.h>
//initializes and prints board
void initBoard(char board[7][8]); //Initializes board
void printBoard(char board[7][8]); //printsBoard
//controls playerOne movements
void gameMechanics(char board[7][8], char playerOne, char playerTwo);
//check general conditions for winning
int checkFull(char board[7][8]);// checks if board is Full
int checkWin(char board[7][8], char tokenChanger); //checks if a player has won
//Ai methods
void aiMechanics(char board[7][8], int row, int col, char playerOne, char playerTwo); //controls basic AI movements
int aiThreeInRow(char board[7][8], char playerOne);//if playerOne has three in row, AI will try to block
void aiFirst(char board[7][8], char playerTwo, char playerOne); //if AI goes first
void initBoard(char board[7][8]) //Initializes board
{
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 8; j++) {
char c = 'A' + j - 1; //As loop iterates, char c = 'A' through 'G'
if (j == 0 && i == 0) //Empty Space at (0, 0)
board[i][j] = ' ';
else if (i == 0)
board[i][j] = c; //initializes column names (A-G)
else if (j == 0)
board[i][j] = 7 - i; //Initializes row numbers
else
board[i][j] = '.'; //Everything else is '.'
}
}
}
void printBoard(char board[7][8]) //Prints Board
{
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 8; j++) {
if (i != 0 && j == 0)
printf(" %d ", board[i][j]);
else
printf(" %c ", board[i][j]);
}
printf("\n");
}
printf("\n");
}
void gameMechanics(char board[7][8], char playerOne,
char playerTwo)//Manages game mechanics such as token placement and turns
{
printBoard(board);
char columnLetter;
int letterConversion;
while (checkWin(board, playerTwo) == 0) //checks that neither player has won
{
if (checkWin(board, playerOne) == 1) {
printf("\033[0;32m"); //Changing colour to Green
printf("Congratualations, You Win!");
printf("\033[0;0m");//Resets colour
break;
}
if (checkFull(board) == 0) { //if board isn't full
checkFull(board);
printf("Choose a column between A and G to place your token: ");
scanf(" %c", &columnLetter);
printf("\n");
letterConversion = (columnLetter - 'A') + 1;//Converts letter input to int
while (letterConversion < 1 || letterConversion > 7) //Checking for valid Column letter
{
printf("\033[0;31m"); //sets colour to red
printf("Please select a valid column letter\n");
printf("\033[0m");//resets colour
printf("Enter a letter between A and G: ");
scanf(" %c", &columnLetter);
letterConversion = (columnLetter - 'A') + 1;
}
while (board[1][letterConversion] != '.') //checks is top entry of column is '.' and not a player token
{
printf("\033[0;31m"); //sets colour to red
printf("Column is full, please select a column that isn't full\n");
printf("\033[0m");//resets colour
printf("Enter a column between A and G that isn't full: ");
scanf(" %c", &columnLetter);
printf("\n");
letterConversion = (columnLetter - 'A') + 1;
}
letterConversion = (columnLetter - 'A') + 1;
if (board[6][letterConversion] == '.') //if column is empty
{
board[6][letterConversion] = playerOne; // p1 places token in row #1
aiMechanics(board, 5, letterConversion, playerOne, playerTwo);//5 = row #2
} else {
for (int k = 1; k < 7; k++) { //iterates down the column for the last '.'
if (board[k][letterConversion] == 'X' || board[k][letterConversion] == 'O') {
board[k - 1][letterConversion] = playerOne;
aiMechanics(board, k - 2, letterConversion, playerOne, playerTwo);
break;
}
}
}
printBoard(board);
} else {
printf("\033[0;33m");
printf("It's a draw, Game Over");
printf("\033[0;0m");
break;
}
}
if (checkWin(board, playerOne) == 1) {
printf("\033[0;32m"); //Changing colour to Green
printf("Congratualations, You Win!");
printf("\033[0;0m");//Resets colour
} else if (checkWin(board, playerTwo) == 1) {
printf("\033[0;36m"); //Changing colour to cyan
printf("AI Wins");
printf("\033[0;0m");//Resets colour
}
}
void aiFirst(char board[7][8], char playerTwo, char playerOne) { //if AI goes first
board[6][6] = playerTwo;
gameMechanics(board, playerOne, playerTwo);
}
void aiMechanics(char board[7][8], int row, int col, char playerOne, char playerTwo) {
if (aiThreeInRow(board, playerOne) == 0) {
if (board[6][col] == '.') {
board[6][col] = playerTwo; // p2 places token in row #1
checkWin(board, playerTwo);
} else if (board[1][col] == '.') {
for (int k = 1; k < 7; k++) { //iterates down the column for the last '.'
if (board[k][col] == 'X' || board[k][col] == 'O') {
board[k - 1][col] = playerTwo;
checkWin(board, playerTwo);
break;
}
}
} else {
for (int i = 1; i < 8; i++) {
if (board[1][i] == '.') {
for (int k = 6; k > 0; k--) { //iterates down the column for the last '.'
if (board[k][i] == '.') {
board[k][i] = playerTwo;
checkWin(board, playerTwo);
break;
}
}
}
break;
}
}
}
if (aiThreeInRow(board, playerOne) == 1) {
if (col < 7) {
if (board[6][col + 1] == '.') {
board[6][col + 1] = playerTwo; // p2 places token in row #1
checkWin(board, playerTwo);
} else if (board[1][col + 1] == '.') {
for (int k = 1; k < 7; k++) { //iterates down the column for the last '.'
if (board[k][col + 1] == 'X' || board[k][col + 1] == 'O') {
board[k - 1][col + 1] = playerTwo;
checkWin(board, playerTwo);
break;
}
}
} else {
for (int i = 1; i < 8; i++) {
if (board[1][i] == '.') {
for (int k = 6; k > 0; k--) { //iterates down the column for the last '.'
if (board[k][i] == '.') {
board[k][i] = playerTwo;
checkWin(board, playerTwo);
break;
}
}
}//
break;
}
}
} else {
if (board[6][col] == '.') {
board[6][col] = playerTwo; // p2 places token in row #1
checkWin(board, playerTwo);
} else if (board[1][col] == '.') {
for (int k = 1; k < 7; k++) { //iterates down the column for the last '.'
if (board[k][col] == 'X' || board[k][col + 1] == 'O') {
board[k - 1][col] = playerTwo;
checkWin(board, playerTwo);
break;
}
}
} else {
for (int i = 1; i < 8; i++) {
if (board[1][i] == '.') {
for (int k = 6; k > 0; k--) { //iterates down the column for the last '.'
if (board[k][i] == '.') {
board[k][i] = playerTwo;
checkWin(board, playerTwo);
break;
}
}
}//
break;
}
}
}
}
}
int aiThreeInRow(char board[7][8], char playerOne) {
for (int i = 1; i < 7; i++) //Checks Horizontal
{
for (int j = 1; j < 5; j++)
if (board[i][j] == playerOne)
if (board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2])
return 1;
}
for (int i = 1; i < 7; i++)//Checks Diagonal
{
for (int j = 1; j < 8; j++) {
if (board[i][j] == playerOne) {
if (board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2])
return 1;
if (board[i][j] == board[i - 1][j + 1] && board[i][j] == board[i - 2][j + 2])
return 1;
}
}
}
return 0;
}
int checkWin(char board[7][8], char tokenChanger) //Checks if there is a winner
{
for (int i = 1; i < 7; i++) //Checks Horizontal
{
for (int j = 1; j < 5; j++)
if (board[i][j] == tokenChanger)
if (board[i][j] == board[i][j + 1] && board[i][j] == board[i][j + 2] && board[i][j] == board[i][j + 3])
return 1;
}
for (int j = 1; j < 8; j++) //Checks Vertical
{
for (int i = 6; i >= 3; i--)
if (board[i][j] == tokenChanger)
if (board[i][j] == board[i - 1][j] && board[i][j] == board[i - 2][j] && board[i][j] == board[i - 3][j])
return 1;
}
for (int i = 1; i < 7; i++)//Checks Diagonal
{
for (int j = 1; j < 8; j++) {
if (board[i][j] == tokenChanger) {
if (board[i][j] == board[i + 1][j + 1] && board[i][j] == board[i + 2][j + 2] &&
board[i][j] == board[i + 3][j + 3])
return 1;
if (board[i][j] == board[i - 1][j + 1] && board[i][j] == board[i - 2][j + 2] &&
board[i][j] == board[i - 3][j + 3])
return 1;
}
}
}
return 0;
}
int checkFull(char board[7][8])// Checks if board is full
{
for (int i = 1; i <
8; i++) //iterates through the second row from top (row under column Letters) to see if any coordinates contains '.'
{
if (board[1][i] == '.')
return 0;
}
return 1;
}
int main() {
char playerOne, playerTwo;
char board[7][8];
initBoard(board);
printf("\033[0;36m"); //sets colour to cyan
printf("\nWelcome to Connect Four!\n\n");
printf("\033[0;33m");//sets colour to yellow
printf("**NOTE: All Inputs Should be Capitalized**\n\n");
printf("\033[0m");//resets colour
printf("X goes first and O goes second\n");
printf("Choose your token (X or O): ");
scanf("%c", &playerOne);
while (playerOne != 'X' && playerOne != 'O') //Checks for Valid Token
{
printf("\033[0;31m"); //sets colour to red
printf("Please select a valid token\n");
printf("\033[0m");//resets colour
printf("Choose your token (X or O): \n");
scanf(" %c", &playerOne);
}
if (playerOne == 'X') {
playerTwo = 'O';
gameMechanics(board, playerOne, playerTwo); //Helper method to manage token placement and turns
} else {
playerTwo = 'X';
aiFirst(board, playerTwo, playerOne);
}
return 0;
}