-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChess.c
More file actions
531 lines (444 loc) · 12.8 KB
/
Copy pathChess.c
File metadata and controls
531 lines (444 loc) · 12.8 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
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
#include "Chess.h"
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "controlsv2.h"
#include <limits.h>
/* Pawn promotion options */
char* new_pawn_options[NUM_NEW_PAWN_OPTIONS] = {QUEEN, BISHOP, ROOK, KNIGHT};
char new_pawn_keys[NUM_NEW_PAWN_OPTIONS] = {QUEEN_W, BISHOP_W, ROOK_W, KNIGHT_W};
/*SDL main items*/
int quit, cancel;
int main(int argc, char* args[]){
char set_cmd[MAX_CMD_LENGTH], color_param[6], user_color[6] = WHITE, curr_color[6] = WHITE;
char load_file[MAX_CMD_LENGTH], computer_best_move[MAX_CMD_LENGTH];
int depth_param, param, move_ret_val, depth = DEFAULT_DEPTH;
int game_mode = DEFAULT_GAME_MODE, start = 0, restart = 1, i;
char** board;
/* Allocate game board */
board = (char**) malloc(sizeof(char*)*BOARD_SIZE);
if (board == NULL){
perror("Error: standard function malloc has failed");
exit(0);
}
for (i = 0; i < BOARD_SIZE; i++){
board[i] = (char*) malloc(sizeof(char)*BOARD_SIZE);
if (board[i] == NULL){
perror("Error: standard function malloc has failed");
exit(0);
}
}
/* Initiate the gui component */
if ((argc == 2) && (!strcmp(args[1], GUI_CMD))){
/* Place all pieces on the board */
init_board_(board);
main_gui(board);
free_board(&board);
return 0;
}
/* Main loop */
if ((argc == 1) || (!strcmp(args[1], CONSOLE_CMD))){
while(restart){
restart = 0;
start = 0;
depth = DEFAULT_DEPTH;
game_mode = DEFAULT_GAME_MODE;
strcpy(user_color, WHITE);
strcpy(curr_color, WHITE);
init_board_(board);
print_board_(board);
print_message(SETTINGS_START);
scanf("%s",set_cmd);
/****************************settings state******************************/
while(!start){
/* Choosing Game Mode */
if(!strcmp(set_cmd,"game_mode")){
scanf("%d", ¶m);
if (param == TWO_PLAYER){
game_mode = param;
printf("Running game in 2 players mode\n");
}
else if (param == PLAYER_VS_COMP){
game_mode = param;
printf("Running game in player vs. AI mode\n");
}
else
print_message(WRONG_GAME_MODE);
}
/* Setting MINIMAX depth */
else if(!strcmp(set_cmd,"difficulty")){
if (game_mode == TWO_PLAYER){
fgets(set_cmd, MAX_CMD_LENGTH, stdin);
print_message(ILLEGAL_COMMAND);
}
else{
scanf("%s", set_cmd);
if (!strcmp(set_cmd, "depth")){
scanf("%d", &depth_param);
if ((depth_param < MIN_DEPTH) || (depth_param > MAX_DEPTH)){
print_message(WRONG_MINIMAX_DEPTH);
}
else{
depth = depth_param;
}
}
else if (!strcmp(set_cmd, "best")){
depth = MAX_DEPTH+1;
}
else{
fgets(set_cmd, MAX_CMD_LENGTH, stdin);
print_message(ILLEGAL_COMMAND);
}
}
}
/* Set player's color */
else if(!strcmp(set_cmd,"user_color")){
scanf("%s", user_color);
if (game_mode == TWO_PLAYER){
print_message(ILLEGAL_COMMAND);
}
else if ((!strcmp(color_param, BLACK)) || (!strcmp(color_param, WHITE))){
strcpy(user_color, color_param);
strcpy(curr_color, color_param);
}
else{
print_message(ILLEGAL_COMMAND);
}
}
else if(!strcmp(set_cmd, "load")){
scanf("%s", load_file);
if (!load_board(board, load_file, &game_mode, user_color, curr_color, &depth)){
print_message(WRONG_FILE_NAME);
}
else{
print_board_(board);
}
}
else if(!strcmp(set_cmd, "print")){
print_board_(board);
}
else if(!strcmp(set_cmd,"start")){
start = 1;
break;
}
else if(!strcmp(set_cmd,"quit")){
free_board(&board);
return 0;
}
else{
fgets(set_cmd, MAX_CMD_LENGTH, stdin);
print_message(ILLEGAL_COMMAND);
}
scanf("%s",set_cmd);
}
/****************************game state***************************/
start = 0;
restart = 0;
/* Player VS Player */
if (game_mode == TWO_PLAYER){
while(!restart){
move_ret_val = user_turn(board, curr_color, game_mode, depth);
/* Restart game */
if (move_ret_val == RESTART_RET){
restart = RESTART_RET;
start = 0;
}
/* Player moved */
else{
print_board_(board);
/* Player checkmates opponent (wins)*/
if (move_ret_val == MATE_RET){
printf("Mate! %s player wins the game\n", curr_color);
break;
}
/* Player checks opponent*/
else if (move_ret_val == CHECK_RET){
printf("Check!\n");
}
/* Change to opponent player */
strcpy(curr_color, opponent_color(curr_color));
}
}
}
else if (game_mode == PLAYER_VS_COMP){
while(!restart){
/* User's move */
if (!strcmp(user_color, curr_color)){
move_ret_val = user_turn(board, user_color, game_mode, depth);
}
/* Computer's move */
else{
move_ret_val = computer_turn(board, curr_color, depth, computer_best_move);
puts(computer_best_move);
}
if (move_ret_val == RESTART_RET){
restart = RESTART_RET;
start = 0;
}
else{
print_board_(board);
if (move_ret_val == MATE_RET){
printf("Mate! %s player wins the game!\n", curr_color);
break;
}
else if (move_ret_val == CHECK_RET){
printf("Check!\n");
}
strcpy(curr_color, opponent_color(curr_color));
}
}
}
}
}
free_board(&board);
return 0;
}
/* User's turn and its options */
int user_turn(char** board, char *player_color, int game_mode, int depth){
move* all_moves, *my_move;
loc pos;
char user_cmd[MAX_CMD_LENGTH], save_file[MAX_CMD_LENGTH];
int num_moves = 0, user_move_val = ILLEGAL_RET;
while(user_move_val == ILLEGAL_RET){
printf("%s player - ", player_color);
print_message(USER_MOVE);
scanf("%s",user_cmd);
if(!strcmp(user_cmd,"quit")){
free_board(&board);
exit(0);
}
else if(!strcmp(user_cmd,"restart")){
user_move_val = RESTART_RET;
}
/* Simulate user's move */
else if(!strcmp(user_cmd, "move")){
my_move = (move*)malloc(sizeof(move));
if (my_move == NULL){
perror("Error: standard function malloc has failed");
exit(0);
}
if (init_move(my_move) == ILLEGAL_RET){
free(my_move);
print_message(ILLEGAL_COMMAND);
}
else{
user_move_val = user_move(board, player_color, my_move);
free(my_move);
if (user_move_val == ILLEGAL_RET){
print_message(ILLEGAL_COMMAND);
}
}
}
/* Get moves for location specified by user */
else if(!strcmp(user_cmd, "get_moves")){
init_pos(&pos);
num_moves = 0;
all_moves = (move*)malloc(sizeof(move));
if (all_moves == NULL){
perror("Error: standard function malloc has failed");
exit(0);
}
/* Location is invalid - not my color, or empty square */
if ((!is_valid_pos(pos)) || (get_location_moves(board, pos, &all_moves, &num_moves, player_color, 1) == ILLEGAL_RET)){
free(all_moves);
print_message(ILLEGAL_COMMAND);
}
else{
print_moves(all_moves, num_moves);
free(all_moves);
}
}
else if(!strcmp(user_cmd, "save")){
scanf("%s", save_file);
if (!save_board(board, save_file, game_mode, player_color, depth))
print_message(WRONG_FILE_NAME);
}
else{
fgets(user_cmd, MAX_CMD_LENGTH, stdin);
print_message(ILLEGAL_COMMAND);
}
}
return user_move_val;
}
/* Computer's turn, makes best move */
int computer_turn(char** board, char *computer_color, int depth, char* computer_best_move){
int best_move_index;
move* all_moves;
all_moves = (move*)malloc(sizeof(move));
if (all_moves == NULL){
perror("Error: standard function malloc has failed");
exit(0);
}
/* Find best move */
best_move_index = minimax(board, &all_moves, depth, depth, computer_color, INT_MIN, INT_MAX, 1);
make_move(board, &(all_moves[best_move_index]));
sprintf(computer_best_move, "Computer: move <%c,%d> to <%c,%d>\n",(char)all_moves[best_move_index].from.x+97 , all_moves[best_move_index].from.y+1,
(char)all_moves[best_move_index].to.x+97 , all_moves[best_move_index].to.y+1);
free(all_moves);
/* Computer checkmates opponent */
if (is_player_checkmated_opponent(board, computer_color))
return MATE_RET;
/* Computer checks opponent */
else if (is_player_checked_opponent(board, computer_color))
return CHECK_RET;
return OK_RET;
}
/* User's turn, makes a move */
int user_move(char** board, char *player_color, move* my_move){
/* Check if move is valid */
if (validate_move(board, my_move, player_color) == ILLEGAL_RET){
return ILLEGAL_RET;
}
/* Make the move */
make_move(board, my_move);
/* Player checkmates opponent */
if (is_player_checkmated_opponent(board, player_color))
return MATE_RET;
/* Player checks opponent */
else if (is_player_checked_opponent(board, player_color))
return CHECK_RET;
return OK_RET;
}
/* Init position variable from user's input */
void init_pos(loc* my_move){
char* tok, start_param[6];
/* getting the user input */
scanf("%s", start_param);
tok = strtok (start_param," <,>");
(*my_move).x = ((int)(tok[0])-97);
tok = strtok (NULL, " <,>");
(*my_move).y = atoi(tok)-1;
}
/* Init move variable from user's input */
int init_move(move* my_move){
char* tok, start_param[6], next_param[MAX_CMD_LENGTH], to[4], new_pawn[MAX_CMD_LENGTH];
int i = 0;
/* getting the user input */
scanf("%s", start_param);
tok = strtok (start_param," <,>");
my_move->from.x = ((int)(tok[0])-97);
tok = strtok (NULL, " <,>");
my_move->from.y = atoi(tok)-1;
/* ignore the "to" part */
scanf("%s", to);
scanf("%s", next_param);
tok = strtok (next_param,"<,>");
my_move->to.x = ((int)(tok[0])-97);
tok = strtok (NULL,"<,>");
my_move->to.y = atoi(tok)-1;
tok = strtok (NULL, "<,>");
fgets(new_pawn, MAX_CMD_LENGTH, stdin);
my_move->promotion = EOL;
new_pawn[strlen(new_pawn)-1] = EOL;
if (strlen(new_pawn)){
for (i = 0; i < NUM_NEW_PAWN_OPTIONS; i++){
if (!strcmp(new_pawn+1, new_pawn_options[i])){
my_move->promotion = new_pawn_keys[i];
return OK_RET;
}
}
}
/* new_pawn entered isn't a valid option */
if (strlen(new_pawn)){
return ILLEGAL_RET;
}
return OK_RET;
}
/* Check if position is within board range */
int is_valid_pos(loc location){
if((location.x < 0) || (location.x > (BOARD_SIZE-1)) || (location.y < 0) || (location.y > (BOARD_SIZE-1)))
return 0;
return 1;
}
/* Validates if a move is legal, doesn't eat piece of same color */
int is_legal_move(char** board, loc location, char* color){
char piece = board[location.x][location.y];
if (piece == EMPTY)
return 1;
if (!strcmp(color, BLACK)){
/* White eats black */
if(piece == tolower(piece))
return 1;
}
if (!strcmp(color, WHITE)){
/* Black eats white */
if(piece == toupper(piece))
return 1;
}
return 0;
}
/* Checks if a move eats a piece */
int is_eat_move(char** board, loc location, char* color){
char piece = board[location.x][location.y];
if (piece == EMPTY)
return 0;
if (!strcmp(color, BLACK)){
if(piece == tolower(piece))
return 1;
}
if (!strcmp(color, WHITE)){
if(piece == toupper(piece))
return 1;
}
return 0;
}
/* Check if a user's move is available */
int validate_move(char** board, move* my_move, char* player_color){
move* all_moves;
int num_moves = 0, i = 0;
all_moves = (move*)malloc(sizeof(move));
if (all_moves == NULL){
perror("Error: standard function malloc has failed");
exit(0);
}
if (!(is_valid_pos(my_move->from)) || !(is_valid_pos(my_move->to))){
free(all_moves);
return ILLEGAL_RET;
}
/* If promotion needed but not chosen - inits promotion to default option */
if (my_move->promotion == EOL){
if (tolower(board[my_move->from.x][my_move->from.y]) == PAWN_W){
if ((my_move->to.y == 0) || (my_move->to.y == BOARD_SIZE-1)){
if (!strcmp(player_color, WHITE)){
my_move->promotion = DEFAULT_PROMOTION;
}
else
my_move->promotion = toupper(DEFAULT_PROMOTION);
}
}
}
/* Calculate all of user's possible moves */
if (get_location_moves(board, my_move->from, &all_moves, &num_moves, player_color, 1) == ILLEGAL_RET){
free(all_moves);
return ILLEGAL_RET;
}
/* Check if move exists in all possible moves */
for (i = 0; i < num_moves; i++){
/* Move exists */
if (((all_moves+i)->to.x == my_move->to.x) && ((all_moves+i)->to.y == my_move->to.y) &&
((all_moves+i)->promotion == tolower(my_move->promotion))){
free(all_moves);
return OK_RET;
}
}
free(all_moves);
return ILLEGAL_RET;
}
/* Make move on board*/
void make_move(char** board, move* my_move){
/* This is a pawn promotion move*/
if (my_move->promotion != EOL){
/* Black pawn promoted*/
if (my_move->to.y == 0){
board[my_move->to.x][my_move->to.y] = toupper(my_move->promotion);
}
/* White pawn promoted*/
else{
board[my_move->to.x][my_move->to.y] = tolower(my_move->promotion);
}
}
/* Regular move*/
else
board[my_move->to.x][my_move->to.y] = board[my_move->from.x][my_move->from.y];
board[my_move->from.x][my_move->from.y] = EMPTY;
}