-
Notifications
You must be signed in to change notification settings - Fork 1
/
gameflow.c
728 lines (678 loc) · 18 KB
/
gameflow.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#include "gameflow.h"
/*
* Save board row to a string
*/
char* boardRowToString(int row_num){
char str[8];
char* new_str = calloc(9, sizeof(char));
validate(new_str, "boardRowToString");
for (int i = 0; i < BOARD_SIZE; i++){
if (board[row_num][i] == EMPTY){
str[i] = '_';
}
else{
str[i] = board[row_num][i];
}
}
strncpy(new_str, str, 8);
new_str[8] = '\0';
return new_str;
}
/*
* Update board from a given string representing a certain row
*/
void updateBoardFromString(char* curr_row, int row_num){
for (int i = 0; i < BOARD_SIZE; i++){
if (curr_row[i] == WHITE_B){
board[row_num][i] = WHITE_B;
}
else if (curr_row[i] == WHITE_K){
board[row_num][i] = WHITE_K;
if (white_kingpos == NULL){
white_kingpos = calloc(1, sizeof(position));
validate(white_kingpos, "updateBoardFromString");
}
initPosition(white_kingpos, i + 97, row_num + 1);
}
else if (curr_row[i] == WHITE_N){
board[row_num][i] = WHITE_N;
}
else if (curr_row[i] == WHITE_P){
board[row_num][i] = WHITE_P;
}
else if (curr_row[i] == WHITE_Q){
board[row_num][i] = WHITE_Q;
}
else if (curr_row[i] == WHITE_R){
board[row_num][i] = WHITE_R;
}
else if (curr_row[i] == BLACK_B){
board[row_num][i] = BLACK_B;
}
else if (curr_row[i] == BLACK_K){
board[row_num][i] = BLACK_K;
if (black_kingpos == NULL){
black_kingpos = calloc(1, sizeof(position));
validate(black_kingpos, "updateBoardFromString");
}
initPosition(black_kingpos, i + 97, row_num + 1);
}
else if (curr_row[i] == BLACK_N){
board[row_num][i] = BLACK_N;
}
else if (curr_row[i] == BLACK_P){
board[row_num][i] = BLACK_P;
}
else if (curr_row[i] == BLACK_Q){
board[row_num][i] = BLACK_Q;
}
else if (curr_row[i] == BLACK_R){
board[row_num][i] = BLACK_R;
}
else{
board[row_num][i] = EMPTY;
}
}
}
/*
* Loading game settings from a given file
*/
int loadSettingsFromFile(char* fp, int print_bit){
xmlDocPtr doc;
xmlNodePtr cur;
xmlChar* key;
doc = xmlReadFile(fp, NULL, 0);
if (doc == NULL) {
print_message(WRONG_FILE_NAME);
return 0;
}
cur = xmlDocGetRootElement(doc);
cur = cur->xmlChildrenNode;
while (cur != NULL) {
// Exctracting the <next_turn> element from the XML file
if ((!xmlStrcmp(cur->name, (const xmlChar *)"next_turn"))){
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key != NULL){
if (!strcmp((char*)key, "White")){
next_player = 'W';
}
else{
next_player = 'B';
}
}
xmlFree(key);
}
// Exctracting the <game_mode> element from the XML file
if ((!xmlStrcmp(cur->name, (const xmlChar *)"game_mode"))){
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key != NULL){
game_mode = key[0] - '0';
}
xmlFree(key);
}
// Exctracting the <difficulty> element from the XML file
if ((!xmlStrcmp(cur->name, (const xmlChar *)"difficulty"))){
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key != NULL){
if (!strcmp((char*)key, "best")){
minimax_depth = 0;
}
else{
minimax_depth = key[0] - '0';
}
}
xmlFree(key);
}
// Exctracting the <user_color> element from the XML file
if ((!xmlStrcmp(cur->name, (const xmlChar *)"user_color"))){
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key != NULL){
if (!strcmp((char*)key, "White")){
user_color = 'W';
}
else{
user_color = 'B';
}
}
xmlFree(key);
}
// Exctracting the <board> element from the XML file
if ((!xmlStrcmp(cur->name, (const xmlChar *)"board"))){
cur = cur->xmlChildrenNode;
while (cur != NULL) {
//Extracting board children - rows
if ((!xmlStrcmp(cur->name, (const xmlChar *)"row_8"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
updateBoardFromString((char*)key, 7);
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"row_7"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
updateBoardFromString((char*)key, 6);
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"row_6"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
updateBoardFromString((char*)key, 5);
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"row_5"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
updateBoardFromString((char*)key, 4);
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"row_4"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
updateBoardFromString((char*)key, 3);
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"row_3"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
updateBoardFromString((char*)key, 2);
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"row_2"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
updateBoardFromString((char*)key, 1);
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"row_1"))) {
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
updateBoardFromString((char*)key, 0);
xmlFree(key);
}
cur = cur->next;
}
}
if (cur != NULL){
cur = cur->next;
}
}
if (print_bit){
print_board();
}
xmlFreeDoc(doc);
xmlCleanupParser();
return 1;
}
/*
* Saving game settings and board to an XML file in a proper format
*/
void saveGameStateToFile(char* fp){
xmlDocPtr doc = NULL; /* document pointer */
xmlNodePtr game_node = NULL, board_node = NULL; /* node pointers */
char* np_color;
char* up_color;
char diff_str[2];
char gm_str[2];
if (next_player == 'W'){
np_color = "White";
}
else{
np_color = "Black";
}
if (user_color == 'W'){
up_color = "White";
}
else{
up_color = "Black";
}
sprintf(gm_str, "%d", game_mode);
doc = xmlNewDoc(BAD_CAST "1.0");
game_node = xmlNewNode(NULL, BAD_CAST "game");
xmlDocSetRootElement(doc, game_node);
xmlNewChild(game_node, NULL, BAD_CAST "next_turn", BAD_CAST np_color);
xmlNewChild(game_node, NULL, BAD_CAST "game_mode", BAD_CAST gm_str);
if (game_mode == 1){
xmlNewChild(game_node, NULL, BAD_CAST "difficulty", BAD_CAST "");
xmlNewChild(game_node, NULL, BAD_CAST "user_color", BAD_CAST "");
}
else{
// if minimax_depth equals best depth
if (!minimax_depth){
xmlNewChild(game_node, NULL, BAD_CAST "difficulty", BAD_CAST "best");
}
else{
sprintf(diff_str, "%d", minimax_depth);
xmlNewChild(game_node, NULL, BAD_CAST "difficulty", BAD_CAST diff_str);
}
xmlNewChild(game_node, NULL, BAD_CAST "user_color", BAD_CAST up_color);
}
board_node = xmlNewNode(NULL, BAD_CAST "board");
xmlAddChild(game_node, board_node);
for (int i = 0; i < BOARD_SIZE; i++){
char buf[6];
sprintf(buf, "row_%d", BOARD_SIZE - i);
char* temp_row = boardRowToString(7 - i);
xmlNewChild(board_node, NULL, BAD_CAST buf, BAD_CAST temp_row);
free(temp_row);
}
xmlSaveFormatFileEnc(fp, doc, "UTF-8", 1);
xmlFreeDoc(doc);
xmlCleanupParser();
}
/*
* Updating board after a legal move
*/
int updateBoard(move* current_move, char playing_color){
if (!isValidAndLegalPiece(current_move->current_pos, playing_color,1)){
return 0;
}
if (isLegalMove(current_move, playing_color)){
actualBoardUpdate(current_move, board, playing_color,white_kingpos,black_kingpos);
}
else {
print_message(ILLEGAL_MOVE);
return 0;
}
return 1;
}
/*
* Maintaining a gaming mode
*/
void gamingMode() {
int player1 = 0;
int player2 = 0;
char* u_color;
if (game_mode == 1){
if (next_player == 'W'){
player1 = 1;
}
else{
player2 = 1;
}
}
else{
if (next_player == user_color){
player1 = player2 = 1;
}
else{
player1 = player2 = 0;
}
}
// Getting the colors for a player vs. AI mode
if (user_color == 'W') {
u_color = "White";
}
else {
u_color = "Black";
}
char** input;
char playing_color = playingColor(player1, player2);
int w_k_found = 0; int b_k_found = 0;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (w_k_found && b_k_found) {
break;
}
if (board[i][j] == WHITE_K) {
white_kingpos = calloc(1, sizeof(position));
validate(white_kingpos, "gamingMode");
initPosition(white_kingpos, j + 97, i + 1);
w_k_found = 1;
}
else if (board[i][j] == BLACK_K){
black_kingpos = calloc(1, sizeof(position));
validate(black_kingpos, "gamingMode");
initPosition(black_kingpos, j + 97, i + 1);
b_k_found = 1;
}
}
}
int game_over = gameOver(1, OppositeColor(playing_color));
while (!game_over) {
if (player1 || player2) {
if (player1 && player2){
printf("%s player - enter your move:\n", u_color);
}
else{
if (player1){
print_message("White player - enter your move:\n");
}
else{
print_message("Black player - enter your move:\n");
}
}
playing_color = playingColor(player1, player2);
input = getInput();
if (!strcmp(input[0], "quit")){
FreeInput(input);
free(input);
free(white_kingpos);
free(black_kingpos);
exit(0);
}
else if (!strcmp(input[0], "move")) { //extracting player's move from input
position* current_pos = createPos(input[1]);
move* current_move = createMoveFromInput(current_pos, input[3], input[4]);
int board_updated = updateBoard(current_move, playing_color);
freeMoves(current_move);
if (board_updated) {
print_board();
if (player1 && player2){
player1 = player2 = 0;
}
else{
player1 = !player1;
player2 = !player2;
}
}
}
else if (!strcmp(input[0], "get_moves")){
position* current_pos = createPos(input[1]);
if (!isValidAndLegalPiece(current_pos, playing_color,1)){
freePositions(current_pos);
continue;
}
moves* possible_moves = getMovesFromPosition(playing_color, board, current_pos);
removeBadMoves(possible_moves, playing_color, board, white_kingpos, black_kingpos);
printMoves(possible_moves->head,0);
freeMoves(possible_moves->head);
free(possible_moves);
freePositions(current_pos);
}
else if (!strcmp(input[0], "get_best_moves")){
int d = (int)strtol(input[1], (char**)NULL, 10); // if d == 0 then the value of d should be "best"
int last_minimax_depth = minimax_depth;
minimax_depth = d;
moves* best_moves = getBestMoves(playing_color);
printMoves(best_moves->head,0);
minimax_depth = last_minimax_depth;
freeMoves(best_moves->head);
free(best_moves);
}
else if (!strcmp(input[0], "get_score")){
int d = (int)strtol(input[1], (char**)NULL, 10); // if d == 0 then the value of d should be "best"
int last_minimax_depth = minimax_depth;
minimax_depth = d;
position* current_pos = createPos(input[3]);
move* m = createMoveFromInput(current_pos, input[5], input[6]);
if (!isLegalMove(m, playing_color)){
print_message(ILLEGAL_MOVE);
freeMoves(m);
minimax_depth = last_minimax_depth;
continue;
}
int move_score = getMoveScore(playing_color, m);
minimax_depth = last_minimax_depth;
printf("%d\n", move_score);
freeMoves(m);
}
else if (!strcmp(input[0], "save")){
char* fp = input[1];
saveGameStateToFile(fp);
}
else {
print_message(ILLEGAL_COMMAND);
}
FreeInput(input);
free(input);
}
else{ //pc turn
playing_color = playingColor(player1, player2);
moves* best_moves = getBestMoves(playing_color);
move* computer_move = best_moves->head;
print_message("\nComputer: move ");
printMoves(computer_move,1);
actualBoardUpdate(computer_move, board, playing_color, white_kingpos, black_kingpos);
print_board();
freeMoves(best_moves->head);
free(best_moves);
player1 = player2 = 1;
}
// Checking if there was a check after a move
game_over = gameOver(1, playing_color);
if (!game_over){
if (isCheck(playing_color, board, white_kingpos, black_kingpos)){
print_message(CHECK);
}
}
}
free(white_kingpos);
free(black_kingpos);
}
/*
* Getting best possible moves with the minimax depth
*/
moves* getBestMoves(char playing_color){
time_t start_t, end_t;
double diff_t;
best_moves = calloc(1, sizeof(moves));
validate(best_moves, "getBestMoves");
moves * all_moves = getMoves(playing_color, board, white_kingpos, black_kingpos);
// DEBUG
move * head = all_moves->head;
int count = 0;
while (head != NULL){
count++;
head = head->next;
}
printf("number of moves %d", count);
// END DEBUG
head = all_moves->head;
int max_score = INT_MIN;
int score;
while (head != NULL){
time(&start_t);
score = getMoveScore(playing_color, head);
time(&end_t);
diff_t = difftime(end_t, start_t);
printf(" getMoveScore Execution time = %f\n", diff_t);
//found a better move than the current best moves
if (score > max_score){
max_score = score;
freeMoves(best_moves->head);
free(best_moves);
best_moves = calloc(1, sizeof(moves));
validate(best_moves, "getBestMoves");
concatMoves(best_moves, copyMove(head));
}
//found a move with an equal score of the best move - adds it to the best moves list
else if (score == max_score){
concatMoves(best_moves, copyMove(head));
}
head = head->next;
}
freeMoves(all_moves->head);
free(all_moves);
return best_moves;
}
/*
* Getting a given move's score.
*/
int getMoveScore(char playing_color, move* curr_move){
int score;
char board_cpy[BOARD_SIZE][BOARD_SIZE];
position black_king_pos_cpy;
position white_king_pos_cpy;
initPosition(&black_king_pos_cpy, black_kingpos->x, black_kingpos->y);
initPosition(&white_king_pos_cpy, white_kingpos->x, white_kingpos->y);
boardCopy(board, board_cpy);
actualBoardUpdate(curr_move, board_cpy, playing_color,&white_king_pos_cpy,&black_king_pos_cpy);
if (minimax_depth == 1) {
return scoringFunc(board_cpy, playing_color, white_kingpos, black_kingpos);
}
if (minimax_depth){
score = alphabeta(OppositeColor(playing_color), board_cpy, minimax_depth - 1, INT_MIN, INT_MAX, 0, &white_king_pos_cpy, &black_king_pos_cpy);
}
else{
minimax_depth = bestDepth(board_cpy, playing_color);
score = alphabeta(OppositeColor(playing_color), board_cpy, bestDepth(board_cpy, playing_color) - 1, INT_MIN, INT_MAX, 0, &white_king_pos_cpy, &black_king_pos_cpy);
minimax_depth = 0;
}
return score;
}
/*
* Maintaining settings mode
*/
void SettingsMode() {
init_board();
print_board();
print_message(ENTER_SETTINGS);
char** input = getInput();
while (strcmp(input[0], "quit")){
if (!strcmp(input[0], "game_mode")){
game_mode = (int)strtol(input[1], (char **)NULL, 10);
if (game_mode == 1){
print_message(TWO_PLAYERS_GAME_MODE);
}
else if (game_mode == 2){
print_message(PLAYER_VS_AI_GAME_MODE);
}
else{
game_mode = 1;
print_message(WRONG_GAME_MODE);
}
}
else if (!strcmp(input[0], "difficulty")){
if (game_mode == 2){
if (!strcmp(input[1], "depth")){
minimax_depth = (int)strtol(input[2], (char **)NULL, 10);
if (minimax_depth > 4 || minimax_depth < 1) {
minimax_depth = 1;
print_message(WRONG_MINIMAX_DEPTH);
}
}
else{
// Denoting difficulty best as minimax depth 0
minimax_depth = 0;
}
}
else{
minimax_depth = 1;
print_message(ILLEGAL_COMMAND);
}
}
else if (!strcmp("user_color", input[0])){
if (game_mode == 2){
if (!strcmp(input[1], "black")){
user_color = 'B';
}
else {
user_color = 'W';
}
}
else{
print_message(ILLEGAL_COMMAND);
}
}
else if (!strcmp(input[0], "load")){
char* fp = input[1];
loadSettingsFromFile(fp,1);
}
else if (!strcmp(input[0], "clear")){
clear_board();
}
else if (!strcmp(input[0], "next_player")){
if (!strcmp(input[1], "black")){
next_player = 'B';
}
else {
next_player = 'W';
}
}
else if (!strcmp(input[0], "rm")) {
char x = input[1][1];
char* digit_pos = input[1] + 3;
int y = (int)strtol(digit_pos, (char**)NULL, 10);
if ((x > 'h' || x<'a') || (y < 1 || y>8)) {
print_message(WRONG_POSITION);
}
else {
board[y - 1][x - 97] = EMPTY;
}
}
else if (!strcmp(input[0], "set")) {
char x = input[1][1];
char* digit_pos = input[1] + 3;
int y = (int)strtol(digit_pos, (char**)NULL, 10);
if ((x>'h' || x < 'a') || (y < 1 || y>8)) {
print_message(WRONG_POSITION);
}
else {
char a = input[2][0];
char b1 = input[3][0];
char b2 = input[3][1];
char orig_piece = board[y - 1][x - 97];
if (a == 'w') {
if (b1 == 'k') {
if (b2 == 'n'){
board[y - 1][x - 97] = WHITE_N;
}
else{
board[y - 1][x - 97] = WHITE_K;
}
}
else if (b1 == 'q') {
board[y - 1][x - 97] = WHITE_Q;
}
else if (b1 == 'r') {
board[y - 1][x - 97] = WHITE_R;
}
else if (b1 == 'b') {
board[y - 1][x - 97] = WHITE_B;
}
else {
board[y - 1][x - 97] = WHITE_P;
}
}
else {
if (b1 == 'k') {
if (b2 == 'n'){
board[y - 1][x - 97] = BLACK_N;
}
else{
board[y - 1][x - 97] = BLACK_K;
}
}
else if (b1 == 'q') {
board[y - 1][x - 97] = BLACK_Q;
}
else if (b1 == 'r') {
board[y - 1][x - 97] = BLACK_R;
}
else if (b1 == 'b') {
board[y - 1][x - 97] = BLACK_B;
}
else {
board[y - 1][x - 97] = BLACK_P;
}
}
int* piecesW; int* piecesB;
piecesW = piecesCounter('W');
piecesB = piecesCounter('B');
if ((((piecesW[0] > 2 || piecesB[0] > 2) || (piecesW[1] > 2 || piecesB[1] > 2)) || ((piecesW[2] > 2 || piecesB[2] > 2) || (piecesW[3] > 1 || piecesB[3] > 1)))
|| ((piecesW[4] > 1 || piecesB[4] > 1) || (piecesW[5] > 8 || piecesB[5] > 8))){
board[y - 1][x - 97] = orig_piece;
print_message(NO_PIECE);
}
free(piecesW);
free(piecesB);
}
}
else if (!strcmp(input[0], "print")) {
print_board(board);
}
else if (!strcmp(input[0], "start")){
FreeInput(input);
free(input);
//wrong board initialization
if (WrongInitialization()) {
input = getInput();
continue;
}
gamingMode();
return;
}
else {
print_message(ILLEGAL_COMMAND);
}
FreeInput(input);
free(input);
print_message(ENTER_SETTINGS);
input = getInput();
}
FreeInput(input);
free(input);
exit(0);
}