-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.c
393 lines (380 loc) · 14.6 KB
/
game.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
#include "types.h"
#include "output.h"
#include "ingamemenu.h"
#include "scores.h"
#include "computer_mode.h"
#include "endgame.h"
int isFull(configurations config, char board[][config.width]) // function to check whether board is full or not, return 1 if full
{
int counter = 0;
for (int j = 0; j < config.width; j++)
{
if (board[0][j] == ' ') // if any of the top row is empty increase counter
{
counter++;
}
}
if (counter == 0)
{
return 1; // the game board is full (ended)
}
else
{
return 0; // the board is not full
}
}
int isColumnAvaliable(int move, configurations config, char board[][config.width]) // move is the column's index
{
if (move == -1) // in-game menu option
{
return 1;
}
if (board[0][move] == ' ') // If the top element in the row is empty then the column is avaliable
{
return 1;
}
else // the column is not avaliable
{
red_color();
printf("This column is not avaliable!\n");
reset_color();
return 0;
}
}
void dotheMove(int move, configurations config, char board[][config.width], char symbol, int moves_stack[], int *counter, int *undo, int redos_stack[], int *count_redos , int mode, player p1, player p2, player computer)
// move is column's index, counter is for moves_stack
{
if (move == -1) // The player chose to access in-game menu
{
inGameMenu(move, config, board, moves_stack, counter, undo, redos_stack, count_redos, mode, p1, p2, computer);
return;
}
for (int i = config.height - 1 ; i >= 0; i--)
{
// if the board place (which is the ith in the desired column) is empty, put the piece in the empty place
if (board[i][move] == ' ')
{
board[i][move] = symbol;
break;
}
}
}
void play(configurations config, char board[][config.width], player p1, player p2, player computer, int mode, int load, int moves_count, int moves_stack[])
// Moves_stack -> Array has all moves "columns" of the game (of both players)
{
int move; // move of the player each turn
clock_t timenow=time(NULL);// Time at the start of the game
int hours,minutes,seconds;// Time format variables
int full; // variable to check if board is full or not
int checkeven = 0; // to check if it's player's one turn or player two's turn
if (load == 1 && moves_count % 2 == 0)
{
checkeven = 0;
}
else if (load == 1 && moves_count % 2 == 1)
{
checkeven = 1;
}
char symbol; // symbol played
int printed_number; // number printed
system("cls"); // clear the commandline interface
if (mode == 1) //player
{
printArray(config, board, p1, p2); // print board (empty)
yellow_color();
printf("Time = 00 : 00 : 00 [HH:MM:SS]\n");
reset_color();
}
else // computer
{
printArray(config, board, p1, computer);
yellow_color();
printf("Time = 00 : 00 : 00 [HH:MM:SS]\n");
reset_color();
}
int undo = 0; // check if the user made undo
int redos_stack[config.width*config.height]; // all undos are in the redos_stack
memset(redos_stack, -1, sizeof(redos_stack)); // set all stack to -1
int count_redos = 0; // counter for redos
if (load == 0)
{
p1.score = 0;
p2.score = 0;
computer.score = 0;
}
int score = 0;
int computer_move_index;
while(full == 0)
{
if (mode == 1)
{
if (checkeven % 2 == 0) // even
{
// therefore it's player one's turn
symbol = p1.symbol;
score = p1.score;
printed_number = p1.id;
}
else
{
// therefore it's player two's turn
symbol = p2.symbol;
score = p2.score;
printed_number = p2.id;
}
}
else if (mode == 2) // vs computer
{
if (moves_count % 2 == 0) // player turn
{
symbol = p1.symbol;
score = p1.score;
printed_number = p1.id;
}
else // computer's move
{
symbol = computer.symbol;
score = computer.score;
printed_number = computer.id;
}
}
if (printed_number != 4) // this is the computer's turn
{
do
{
green_color();
printf("To access in-game menu (Undo/Redo/Save/Quit) -> Enter zero\n");
reset_color();
if (printed_number == 1)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), p1.color);
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), p2.color);
}
printf("Player %d turn...\nEnter Column: ", printed_number);
scanf("%d", &move);
reset_color();
while(getc(stdin) != '\n'); // remove the buffer
} while(move > config.width || move < 0 || (!isColumnAvaliable(move - 1, config, board)));
dotheMove(move - 1, config, board, symbol, moves_stack, &moves_count, &undo, redos_stack, &count_redos, mode, p1, p2, computer);
// (move - 1) is the chosen column's index
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), computer.color);
printf("Computer's turn..\n");
reset_color();
computersMove(&computer_move_index, config, board, symbol);
}
system("cls"); // clear the command line interface
if (move != 0)
{
if (printed_number == 4)
{
moves_stack[moves_count] = computer_move_index - 1;
moves_count++;
}
else
{
moves_stack[moves_count] = move - 1;
moves_count++;
}
}
if (undo == 1) // if the user made undo:
{
if (mode == 2) // computer mode
{
redos_stack[count_redos] = moves_stack[moves_count - 1]; // push the computer's move to redos stack, after undo
redos_stack[++count_redos] = moves_stack[moves_count - 2]; // push player's move to redos stack after undo
count_redos++;
isConnect4(config, board, moves_stack[moves_count - 1], &computer.score, moves_stack, moves_count, 'O', 1, 2);
isConnect4(config, board, moves_stack[moves_count - 2], &score, moves_stack, moves_count, 'X', 1, 2);
moves_stack[moves_count - 1] = -1; // pop last 2 move from the stack
moves_stack[moves_count - 2] = -1;
moves_count -= 2; // decrease moves count by 2
}
else // player mode
{
redos_stack[count_redos] = moves_stack[moves_count - 1]; // push the last undo - ed move to redos_stack
if (printed_number == 1)
{
isConnect4(config, board, moves_stack[moves_count - 1], &p2.score, moves_stack, moves_count, 'O', 1, 1);
// to_undo = 1
}
else if (printed_number == 2)
{
isConnect4(config, board, moves_stack[moves_count - 1], &p1.score, moves_stack, moves_count, 'X', 1, 1); // to_undo = 1
}
count_redos++; // increase redo count by one
moves_count--; // decrease move count by one
moves_stack[moves_count] = -1; // pop last move from the stack
}
}
else if (undo == 3) // can't redo error (there was no undo)
{
if (mode == 1)
{
checkeven--; // don't increase checkeven
}
}
else if (undo != 2) // if the user didn't make redo nor undo
{
count_redos = 0; // set count to zero
memset(redos_stack, -1, sizeof(redos_stack)); // set all stack to -1
if (mode == 1)
{
isConnect4(config, board, move - 1, &score, moves_stack, moves_count, symbol, 0, 1); // checks if the move caused
// scoring points, then edits the score to higher value
}
else if (mode == 2 && printed_number == 1)
{
isConnect4(config, board, move - 1, &score, moves_stack, moves_count, symbol, 0, 2); // checks if the move caused
// scoring points, then edits the score to higher value
}
else if (mode == 2 && printed_number == 4)
{
isConnect4(config, board, computer_move_index - 1, &score, moves_stack, moves_count, symbol, 0, 2);
// checks if the move caused scoring points, then edits the score to higher value
}
}
else if (undo == 2) // if the user made redo
{
if (mode == 1) // player vs player
{
isConnect4(config, board, redos_stack[count_redos - 1], &score, moves_stack, moves_count, symbol, 0, 1);
// checks if the redo-ed move caused points increase
redos_stack[count_redos - 1] = -1;
count_redos--; // decrease count by one
}
else // computer mode
{
isConnect4(config, board, redos_stack[count_redos - 1], &score, moves_stack, moves_count, 'X', 3, 2);
// to_undo = 3 to access the player's move
isConnect4(config, board, redos_stack[count_redos - 2], &computer.score, moves_stack, moves_count, 'O', 0, 2);
// checks if the redo-ed move caused points increase
redos_stack[count_redos - 2] = -1;
redos_stack[count_redos - 1] = -1; // the used redos_stack element is set to -1 again
count_redos -=2 ; // decrease count by TWO
}
}
undo = 0;
if (mode == 1) // player
{
printArray(config, board, p1, p2); // print board
}
else // computer
{
printArray(config, board, p1, computer);
}
full = isFull(config, board);
if (full == 1) // if board is full
{
break;
}
time_t time2 = time(NULL); // time after the turn
int time_taken = difftime(time2,timenow); // time different between the start and the end of the loop
seconds = time_taken % 60;
minutes = time_taken % 3600 / 60;
hours = time_taken / 3600;
pink_color();
printf("Time = %02d : %02d : %02d [HH:MM:SS]\n", hours, minutes, seconds);
reset_color();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), p1.color);
printf("Number Of Moves Of Player One: %d", (moves_count / 2) + (moves_count % 2));
reset_color();
if (mode == 1)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), p2.color);
printf("\tNumber Of Moves Of Player Two: %d\n", moves_count / 2);
reset_color();
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), computer.color);
printf("\tNumber Of Moves Of Computer: : %d\n", moves_count / 2);
reset_color();
}
if (printed_number == 1)
{
// update player one's score
p1.score = score;
}
else if (printed_number == 2)
{
// update player two's score
p2.score = score;
}
else
{
computer.score = score;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), p1.color);
printf("Player %d's score = %d", p1.id, p1.score);
reset_color();
if (mode == 1)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), p2.color);
printf("\t\t\tPlayer %d's score = %d\n", p2.id, p2.score);
}
else if (mode == 2)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), computer.color);
printf("\t\t\tComputer's score = %d\n", computer.score);
}
reset_color();
checkeven++; // increase checkeven by one
}
pink_color();
printf("Time = %02d : %02d : %02d [HH:MM:SS]\n", hours, minutes, seconds);
reset_color();
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), p1.color);
printf("Number Of Moves Of Player One: %d", (moves_count / 2) + (moves_count % 2));
reset_color();
if (mode == 1)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), p1.color);
printf("\tNumber Of Moves Of Player Two: %d\n", moves_count / 2);
reset_color();
}
else
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), computer.color);
printf("\tNumber Of Moves Of Computer: : %d\n", moves_count / 2);
reset_color();
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), p1.color);
printf("Player %d's score = %d", p1.id, p1.score);
reset_color();
if (mode == 1)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), p2.color);
printf("\t\t\tPlayer %d's score = %d\n", p2.id, p2.score);
}
else if (mode == 2)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), computer.color);
printf("\t\t\tComputer's score = %d\n", computer.score);
}
reset_color();
declareWinner(p1, p2, computer, mode, config.highscores, 1);
system("PAUSE");
}
void chooseMode(int game_mode, configurations config, char board[][config.width], player p1, player p2, player computer)
{
int moves_stack[config.width * config.height];
// Moves_stack -> Array has all moves "columns" of the game (of both players)
memset(moves_stack, -1, sizeof(moves_stack)); // Initializing the moves stack to negative ones
switch (game_mode)
{
case 1:
play(config, board, p1, p2, computer, 1, 0, 0, moves_stack); // int mode = 1, = vs player mode // load = 0 // moves_count = 0
break;
case 2:
play(config, board, p1, p2, computer, 2, 0, 0, moves_stack); // int mode = 2, = computer mode // load = 0 // moves_count = 0
break;
default:
break;
}
}