-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_connect4.c
558 lines (448 loc) · 13.5 KB
/
project_connect4.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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
* Creators: Fasih Hasan, Syed Ukkashah, Muhammad Ali
* Desc: A game called connect 4. First player to create a sequence of 4 characters in a row in a 6 by 7 grid will win
*/
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include "connect4.h"
#define BUFFER_SIZE 10
#define SAVE_BUFFER_SIZE 50
#define f_name "wins.txt"
#define f_save "saves.csv"
#define P1 'R'
#define P2 'B'
#define X '.'
#define blue 1
#define red 4
#define white 15
#define l_yellow 14
// default settings
const int rows = 6, cols = 7;
// input flag to handle input commands
bool input_flag = true;
int main()
{
int player, col;
bool valid = true, win = false;
char player_char, **grid = create_game_grid();
start_menu();
// game loop
while (!win) {
// check at the start if draw
// this was done to avoid out of bound errors mid game
if (game_draw_status(grid)) {
// display end screen for DRAW
game_draw_display(grid, player);
return 0; // end
}
// for each player
for (player = 0; player < 2; player++) {
// loop until valid input
do {
update_frame(grid);
if (!valid && !input_flag) printf("-- Enter appropriate column number or command --\n\n");
input_flag = false;
col = user_input(grid, &player);
valid = validate_move(grid, col);
} while (!valid);
// set to grid
set_grid(grid, col, player);
// check if win or draw
win = game_win_status(grid, player);
if (win)
break;
}
}
// display screen for WIN of player
game_end_display(grid, player);
// reset color to default
set_color(white);
return 0; // end
} // end main()
void start_menu() {
system("cls");
// Print "WELCOME" with different colors for each character
set_color(10); // Green for 'W'
printf(" W");
set_color(9); // Blue for 'E'
printf("E");
set_color(14); // Yellow for 'L'
printf("L");
set_color(11); // Pink for 'C'
printf("C");
set_color(13); // Purple for 'O'
printf("O");
set_color(12); // Red for 'M'
printf("M");
set_color(15); // Reset color to light yellow for the rest of the characters
printf("E ");
// Print "TO CONNECT 4!" in red
set_color(12); // Red for 'T'
printf("T");
set_color(15); // Reset color to light yellow for the rest of the characters
printf("O ");
set_color(4); // Red for 'C'
printf("CONNECT ");
set_color(12); // Red for '4'
printf("4");
set_color(15); // Reset text color to light yellow
printf("!\n\n");
// Set text color to red for "AFU STUDIOS"
set_color(4);
printf("%24s", "AFU STUDIOS\n\n");
// Reset text color to light yellow
set_color(15);
// Print controls with different colors
printf("%-12s%s\n", "", "CONTROLS:\n");
set_color(10); // Green for 'r'
printf("%-12s%s\n", "", "r - reset");
set_color(13); // Purple for 's'
printf("%-12s%s\n", "", "s - save");
set_color(14); // Yellow for 'l'
printf("%-12s%s\n", "", "l - load");
set_color(4); // Red for 'e'
printf("%-12s%s\n", "", "e - exit");
// Reset text color to light yellow
set_color(15);
// Set text color to gold
set_color(14);
printf("\n\n");
printf("%27s\n", "CONNECT 4 RULES\n\n");
set_color(14); // Reset text color
// Print rules with color formatting
printf("1. The game is played on a vertically standing grid with 6 rows and 7 columns.\n");
// set_color(4); // Red color for 'red'
printf("2. Player red (");
set_color(4); // Reset text color
printf("R");
set_color(14); // Red color for 'red'
printf(") and Player blue (");
set_color(1); // Blue color for 'blue'
printf("B");
set_color(14); // Red color for 'red'
printf(") take turns to drop their colored discs from the top into any of the\n");
printf(" seven columns.\n");
printf("3. The disc will then occupy the lowest available space within the chosen column.\n");
printf("4. The game continues until one of the players achieves four of their discs\n");
printf(" consecutively in a row, either horizontally, vertically, or diagonally.\n");
printf("5. The player who connects four discs in a row first wins the game!\n");
set_color(15); // Reset text color
printf("\n Press any key to continue");
// Blinking effect
int blink = 1;
while (!_kbhit()) {
// Toggle visibility by moving the cursor to the beginning of the line
printf("\r");
if (blink) {
printf(" Press any key to continue");
} else {
printf(" "); // Print spaces to clear the line
}
blink = !blink;
// Wait for a short time to control the blinking speed
Sleep(500);
}
fflush(stdin);
// Clear the line after a key is pressed
printf("\r ");
}
char **create_game_grid()
{
// creates double pointer for number of rows
char **grid = (char **)malloc(sizeof(char *) * rows);
// creates a data block for the double pointer
char *block = (char *)malloc(sizeof(char) * rows*cols);
// set the pointer locations for the grid
for (int i = 0; i < rows; i++) grid[i] = &block[i*cols];
// initialize the grid
initialize_grid(grid);
return grid;
} // end create_game_grid()
void initialize_grid(char **grid)
{
// initializing the grid with X
for (int i = 0; i < rows; i++) memset(grid[i], X, cols);
} // end initialize_grid()
void set_color(int color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
} // end set_color()
void color_player(char player)
{
switch (player) {
case P1:
set_color(red);
printf("%c", player); // Red color for P1
break;
case P2:
set_color(blue);
printf("%c", player); // Blue color for P2
break;
case X:
set_color(white);
printf("%c", player); // White color for X
}
set_color(l_yellow);
} // end color_player()
void print_grid(char **grid)
{
// default color scheme
set_color(l_yellow);
printf("%-4c", ' ');
for (int i = 0; i < cols; i++) {
printf("%-4d", i+1);
}
printf("\n");
printf("%-2c\\", ' ');
for (int i = 2; i <= cols*4; i++) (i & 1) ? printf("%c", '=') : printf("%c", '-');
printf("/");
printf("\n");
for (int i = 0; i < rows; i++) {
printf("%c | ", i+'a');
for (int j = 0; j < cols; j++) {
if (j == cols - 1) {
color_player(grid[i][j]);
printf(" |");
break;
}
color_player(grid[i][j]);
printf("%-3c", ' ');
}
if (i == rows - 1) continue;
printf("\n");
printf("%3c", '|');
for (int i = 2; i < cols*4; i++) printf("%c", ' ');
printf(" |\n");
}
printf("\n");
printf("%-2c/", ' ');
for (int i = 2; i <= cols*4; i++) (i & 1) ? printf("%c", '=') : printf("%c", '-');
printf("\\\n\n");
} // end print_grid(Player **)
void update_frame(char **grid)
{
// clear screen and print grid
system("cls");
print_grid(grid);
} // end update_frame()
int user_input(char **grid, int *player)
{
char input[BUFFER_SIZE];
printf("Enter column number for Player %d: ", *player+1);
// using fgets as a safer input than scanf or gets
// it will also prevent game crashes when user enters nothing
fgets(input, BUFFER_SIZE, stdin);
// removing trailing new line
input[strcspn(input, "\n")] = 0;
// exit if user inputs 'e' or 'E'
if (!strcmp("e", input) || !strcmp("E", input)) {
printf("%24s", "---- Exiting ----");
set_color(white);
Sleep(300);
exit(EXIT_SUCCESS);
}
// saving wins and showing wins if input is 's' or 'S'
else if (!strcmp("s", input) || !strcmp("S", input)) {
if (check_empty_grid(grid)) {
printf("%24s", "---- Empty Grid! ----");
Sleep(200);
} else {
printf("%24s", "---- Saving ----");
Sleep(500);
save_game(grid, *player);
}
input_flag = true;
return -1;
}
// loading game if input is 'l' or 'L'
else if (!strcmp("l", input) || !strcmp("L", input)) {
printf("%24s", "---- Loading ----");
Sleep(200);
*player = load_game(grid, *player);
input_flag = true;
return -1;
}
// restarting game if input is 'r' or 'R'
else if (!strcmp("r", input) || !strcmp("R", input)) {
printf("%24s", "---- Resetting ----");
Sleep(500);
initialize_grid(grid);
*player = 0;
input_flag = true;
return -1;
}
return atoi(input)-1;
} // user_input(int)
bool validate_move(char **grid, int col)
{
// range check and overflow check
return (col < cols && col >= 0) ? grid[0][col] == X : false;
} // end validate_move()
int get_first_empty(char **grid, int col)
{
int i = rows-1;
for (i; i >= 0 && grid[i][col] != X; i--);
// loop from bottom until first non player character found
return i;
} // end get_first_empty()
void set_grid(char **grid, int col, int player)
{
// get row where empty slot
int row = get_first_empty(grid, col);
grid[row][col] = (player) ? P1 : P2;
} // end set_grid()
bool game_win_status(char **grid, int player)
{
// getting row of current player character
char player_char = (player) ? P1 : P2;
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
// down
if (row + 3 < rows) {
if (grid[row][col] == player_char && grid[row+1][col] == player_char && grid[row+2][col] == player_char && grid[row+3][col] == player_char)
return true;
}
// right
if (col + 3 < cols) {
if (grid[row][col] == player_char && grid[row][col+1] == player_char && grid[row][col+2] == player_char && grid[row][col+3] == player_char)
return true;
}
// left
if (col - 3 >= 0) {
if (grid[row][col] == player_char && grid[row][col-1] == player_char && grid[row][col-2] == player_char && grid[row][col-3] == player_char)
return true;
}
// up right
if (col + 3 < cols && row - 3 >= 0) {
if (grid[row][col] == player_char && grid[row-1][col+1] == player_char && grid[row-2][col+2] == player_char && grid[row-3][col+3] == player_char)
return true;
}
// up left
if (col - 3 >= 0 && row - 3 >= 0) {
if (grid[row][col] == player_char && grid[row-1][col-1] == player_char && grid[row-2][col-2] == player_char && grid[row-3][col-3] == player_char)
return true;
}
// down right
if (col + 3 < cols && row + 3 < rows) {
if (grid[row][col] == player_char && grid[row+1][col+1] == player_char && grid[row+2][col+2] == player_char && grid[row+3][col+3] == player_char)
return true;
}
// down left
if (col - 3 >= 0 && row + 3 < rows) {
if (grid[row][col] == player_char && grid[row+1][col-1] == player_char && grid[row+2][col-2] == player_char && grid[row+3][col-3] == player_char)
return true;
}
}
}
return false;
} // end game_win_status()
bool game_draw_status(char **grid)
{
// if top row is not full return false
for (int i = 0; i < cols; i++) if (grid[0][i] == X) return false;
return true;
} // end game_draw_status()
void game_draw_display(char **grid, int player)
{
update_frame(grid);
printf("DRAW");
display_wins(-1);
} // game_draw_display()
void game_end_display(char **grid, int player)
{
update_frame(grid);
set_color((player) ? 4 : 1);
printf("%13c", ' ');
printf("%s ", (player) ? "RED" : "BLUE");
set_color(l_yellow);
printf("WINS!\n");
display_wins(player);
} // game_end_display()
int *save_win(int player)
{
char line[BUFFER_SIZE];
int *wins = (int *)calloc(2, sizeof(int));
//reading wins from file
FILE *f_ptr = fopen(f_name, "r+");
// if file exists
if (f_ptr != NULL) {
for (int i = 0;!feof(f_ptr); i++) {
// reading win from file
fgets(line, BUFFER_SIZE, f_ptr);
// trimming string
line[strcspn(line, "\n")] = 0;
// adding into win of the player
// ignoring if -1 (DRAW)
wins[i] = atoi(line) + (player != -1 && i == player);
}
fclose(f_ptr);
} else wins[player] = 1; // setting to 1 if no file exists
// ending if DRAW
if (player == -1) return wins;
// writing new wins in file
f_ptr = fopen(f_name, "w");
// if file can be opened
if (f_ptr != NULL) {
// writing the wins in to the file
fprintf(f_ptr, "%d\n%d", wins[0], wins[1]);
fclose(f_ptr);
}
return wins;
} // end save_win()
void display_wins(int player)
{
// get wins from the file
int *wins = save_win(player);
for (int i = 1; i >= 0; i--) {
printf("\n%6cTotal ", ' ');
set_color(i ? red : blue);
printf("%4s ", i ? "RED" : "BLUE");
set_color(l_yellow);
printf(" wins: %2d", wins[i]);
}
// to not immediately close the program at the end
Sleep(5000);
} // end display_wins()
int load_game(char **grid, int player) {
FILE *fp = fopen(f_save, "r");
if (fp == NULL) {
printf("%24s", "---- No Saves ----");
Sleep(300);
return player;
}
char save_str[SAVE_BUFFER_SIZE];
// retrieve saved game data from file
fgets(save_str, SAVE_BUFFER_SIZE, fp);
// copy each row into grid
for (int i = 0; i < rows; i++)
strncpy(grid[i], &save_str[i*cols], cols);
// return the player turn
return save_str[strlen(save_str)-1]-'0';
} // load_game()
bool check_empty_grid(char **grid)
{
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
if (grid[i][j] == P1 || grid[i][j] == P2) return false;
return true;
} // end check_empty_grid()
void save_game(char **grid, int player)
{
FILE *fp = fopen(f_save, "w");
char save_str[SAVE_BUFFER_SIZE];
memset(save_str, 0, SAVE_BUFFER_SIZE);
// using strncat to concat to limit the upper bound
for (int i = 0; i < rows; i++) strncat(save_str, grid[i], cols);
// convert char int to string
char player_str[3];
sprintf(player_str, ",%d", player);
// append player to string
strcat(save_str, player_str);
// save array data to file
fputs(save_str, fp);
fclose(fp);
} // end save_game()