-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChess_Game.C
755 lines (594 loc) · 23.4 KB
/
Chess_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
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
* Author: Taha Masood
* Name: Chess_Game.C
* This program allows users to play a game of chess.
*/
#include <python.h>
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
#include <string.h>
/*
* This C program is currently functional with standard input and standard output.
* This is to make it easier to compile and debug the C code for the game.
* To compile the complete GUI, comment out the function print_board and the locations for input.
* Insert the python code by either defining a new file or using static PyObject * {}.
* Redirect standard output to the Python GUI.
*/
/*
* The following function intializes a new structure called 'chess'.
* The structure is created to decrease the intialization of multiple two-dimensional arrays.
* The structure itself is used to keep track of chess pieces on an eight-by-eight chess board.
*/
struct chess
{
int board[8][8];
};
/*
* The following function is to determine the end of the game.
* The function returns in integer corresponding to the overall layout of the board.
* The value 3 indicates both kings are still within the game.
* The value 2 indicates that the black king is out of the game.
* The value 1 indicates that the white king is out of the game.
* The value 0 indicates that both kings are out of the game.
*/
int game_value(struct chess current)
{
int counter = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (current.board[i][j] == 5) counter ++;
if (current.board[i][j] == 12) counter += 2;
}
}
return counter;
}
/*
* The following function determines if a piece is black.
* The function returns true if the piece is black and false if the piece is white.
*/
bool is_black(struct chess current, int position)
{
int value = current.board[position/10][position%10];
if (value == 0) return false;
else if ((value == 1) || (value == 2) || (value == 3) || (value == 4) || (value == 5) || (value == 6)) return true;
else return false;
}
/*
* The following function determines if a piece is white.
* The function returns true if the piece is white and false if the piece is black.
*/
bool is_white(struct chess current, int position)
{
int value = current.board[position/10][position%10];
if (is_black(current, position)) return false;
else if (value == 0) return false;
else return true;
}
/*
* The following function determines if a piece exists at the indicated position.
* The function returns true if a piece exists at the indicated position and returns false otherwise.
*/
bool is_piece(struct chess current, int position)
{
if ((current.board[position/10][position%10]) != 0) return true;
else return false;
}
/*
* The following function determines if a move is valid for a bishop.
* The function determines if the end location is possible for a bishop.
* The function ensures no pieces are blocking the bishop's path and/or the final position does not hold a piece of the same colour.
* The function returns true the move is achievable and false otherwise.
*/
bool valid_bishop(struct chess current, int start, int end)
{
int movement = end - start;
int value = 0;
if ((movement%11) == 0)
{
value = movement/11;
if (value > 0)
{
for(int i = 1; i < value; i++)
{
if ((current.board[(start/10)+i][(start%10)+i]) != 0) return false;
}
}
if (value < 0)
{
for(int j = -1; j > value; j--)
{
if((current.board[(start/10)+j][(start%10)+j]) != 0) return false;
}
}
if((is_white(current, start)) && (is_black(current, end))) return true;
else if((is_black(current, start)) && (is_white(current, end))) return true;
else if((current.board[end/10][end%10]) == 0) return true;
else return false;
}
else if ((movement%9) == 0)
{
value = movement/9;
if (value > 0)
{
for(int i = 1; i < value; i++)
{
if ((current.board[(start/10)+i][(start%10)-i]) != 0) return false;
}
}
if (value < 0)
{
for(int j = -1; j > value; j--)
{
if((current.board[(start/10)+j][(start%10)-j]) != 0) return false;
}
}
if((is_white(current, start)) && (is_black(current, end))) return true;
else if((is_black(current, start)) && (is_white(current, end))) return true;
else if((current.board[end/10][end%10]) == 0) return true;
else return false;
}
else return false;
}
/*
* The following function determines if a move is valid for a king.
* The function determines if the end location is possible for a king.
* The function ensures no pieces are blocking the king's path and/or the final position does not hold a piece of the same colour.
* The function returns true the move is achievable and false otherwise.
*/
bool valid_king(struct chess current, int start, int end)
{
int movement = end - start;
if ((movement == 1) || (movement == -1) || (movement == 10) || (movement == -10) || (movement == 9) || (movement == -9) || (movement == 11) || (movement == -11))
{
if((is_black(current, start)) && (is_white(current, end))) return true;
else if((is_white(current, start)) && (is_black(current, end))) return true;
else if((current.board[end/10][end%10]) == 0) return true;
else return false;
}
else return false;
}
/*
* The following function determines if a move is valid for a knight.
* The function determines if the end location is possible for a knight.
* The function ensures the final position does not hold a piece of the same colour.
* The function returns true the move is achievable and false otherwise.
*/
bool valid_knight(struct chess current, int start, int end)
{
int movement = end - start;
if((movement == 21) || (movement == -21) || (movement == 19) || (movement == -19) || (movement == 12) || (movement == -12) || (movement == 8) || (movement == -8))
{
if((is_black(current, start)) && (is_white(current, end))) return true;
else if((is_white(current, start)) && (is_black(current, end))) return true;
else if((current.board[end/10][end%10]) == 0) return true;
else return false;
}
else return false;
}
/*
* The following function determines if a move is valid for a rook.
* The function determines if the end location is possible for a rook.
* The function ensures no pieces are blocking the rook's path and/or the final position does not hold a piece of the same colour.
* The function returns true the move is achievable and false otherwise.
*/
bool valid_rook(struct chess current, int start, int end)
{
int movement = (end - start);
int value = movement/10;
if (value > 0)
{
for(int i = 1; i < value; i++)
{
if((current.board[(start/10)+i][start%10]) != 0) return false;
}
if((is_black(current, start)) && ((is_white(current, end)) || ((current.board[end/10][end%10]) == 0))) return true;
else if((is_white(current, start)) && ((is_black(current, end)) || ((current.board[end/10][end%10]) == 0))) return true;
else return false;
}
else if (value < 0)
{
for(int i = -1; i > value; i--)
{
if((current.board[(start/10)+i][start%10]) != 0) return false;
}
if((is_black(current, start)) && ((is_white(current, end)) || ((current.board[end/10][end%10]) == 0))) return true;
else if((is_white(current, start)) && ((is_black(current, end)) || ((current.board[end/10][end%10]) == 0))) return true;
else return false;
}
else if (value == 0)
{
value == ((end%10)-(start%10));
if (value > 0)
{
for(int i = 1; i < value; i++)
{
if((current.board[(start/10)+i][start%10]) != 0) return false;
}
}
else if (value < 0)
{
for(int i = -1; i > value; i--)
{
if((current.board[(start/10)+i][start%10]) != 0) return false;
}
}
if ((is_black(current, start)) && ((is_white(current, end)) || ((current.board[end/10][end%10]) == 0))) return true;
else if ((is_white(current, start)) && ((is_black(current, end)) || ((current.board[end/10][end%10]) == 0))) return true;
else return false;
}
else if (is_white(current, start))
{
value = movement/10;
if (value > 0) return false;
if (value != 0)
{
for(int i = 1; i < value; i++)
{
if((current.board[(start/10)+i][start%10]) != 0) return false;
}
if((is_white(current, end)) || ((current.board[end/10][end%10]) == 0)) return true;
else return false;
}
if (value == 0)
{
value == ((end%10)-(start%10));
if (value > 0)
{
for(int i = 1; i < value; i++)
{
if((current.board[(start/10)+i][start%10]) != 0) return false;
}
}
else if (value < 0)
{
for(int i = -1; i > value; i--)
{
if((current.board[(start/10)+i][start%10]) != 0) return false;
}
}
if((is_white(current, end)) || ((current.board[end/10][end%10]) == 0)) return true;
else return false;
}
}
}
/*
* The following function determines if a move is valid for a queen.
* The function determines if the end location is possible for a queen.
* The function ensures no pieces are blocking the queen's path and/or the final position does not hold a piece of the same colour.
* The function returns true the move is achievable and false otherwise.
* The function evidently utilizes previously defined functions to reduce the complexity of the program.
*/
bool valid_queen(struct chess current, int start, int end)
{
if (valid_rook(current, start, end)) return true;
else if (valid_bishop(current, start, end)) return true;
else return false;
}
/*
* The following function determines if a move is valid for a white pawn.
* The function determines if the end location is possible for a white pawn.
* The function ensures no pieces are blocking the pawn's path and/or the final position does not hold a piece of the same colour.
* The function returns true the move is achievable and false otherwise.
*/
bool white_valid_pawn(struct chess current, int start, int end)
{
int movement = (end - start);
if ((movement == -10) && ((current.board[end/10][end%10]) == 0)) return true;
else if ((start%10 != (0 && 7)) && (movement == -9 || movement == -11) && (is_black(current, end))) return true;
else if ((start%10 == 0) && (movement == -9) && (is_black(current, end))) return true;
else if ((start%10 == 8) && (movement == -11) && (is_black(current, end))) return true;
else if (((start/10) == 6) && (movement == -20) && ((current.board[end/10][end%10]) == 0)) return true;
else return false;
}
/*
* The following function determines if a move is valid for a black pawn.
* The function determines if the end location is possible for a black pawn.
* The function ensures no pieces are blocking the pawn's path and/or the final position does not hold a piece of the same colour.
* The function returns true the move is achievable and false otherwise.
*/
bool black_valid_pawn(struct chess current, int start, int end)
{
int movement = (end - start);
if ((movement == 10) && ((current.board[end/10][end%10]) == 0)) return true;
if (((start%10) != (0 && 7)) && ((movement == 9) || (movement == 11)) && (is_white(current, end))) return true;
if ((start%10 == 0) && (movement == 11) && (is_white(current, end))) return true;
if ((start%10 == 8) && (movement == 9) && (is_white(current, end))) return true;
if (((start/10) == 1) && movement == 20 && ((current.board[end/10][end%10]) == 0)) return true;
return false;
}
/*
* This function determines if the move entered aligns with the correct colour turn.
* This function restricts a player to move opposite colour pieces.
* The function returns true if the move complies with the current turn, and false otherwise.
*/
bool turn(struct chess current, int position, int counter)
{
int determine = counter%2;
if((is_black(current, position)) && (determine == 1)) return true;
else if ((is_white(current, position)) && (determine == 0)) return true;
else return false;
}
/*
* This is one of the few non-boolean functions.
* This function physically moves a piece from position to another and returns a struct chess.
* Pointers can be used to update the current board setting and the following 'move_piece' function.
*/
struct chess move_piece(struct chess current, int start, int end)
{
int value = current.board[start/10][start%10];
current.board[end/10][end%10] = 0;
current.board[start/10][start%10] = 0;
current.board[end/10][end%10] = value;
return current;
}
/*
* The follwoing function is only for debugging and quick compilation.
* The fucntion prints the board out in the standard output.
* This function should be commented out when compiling with the Python GUI.
*/
void print_board(struct chess current)
{
int value = 0;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j ++)
{
value = current.board[i][j];
if (value == 0) printf("E ");
if (value == 1) printf("Rook_B ");
if (value == 2) printf("Knight_B ");
if (value == 3) printf("Bishop_B ");
if (value == 4) printf("Queen_B ");
if (value == 5) printf("King_B ");
if (value == 6) printf("Pawn_B ");
if (value == 7) printf("Pawn_W ");
if (value == 8) printf("Rook_W ");
if (value == 9) printf("Knight_W ");
if (value == 10) printf("Bishop_W ");
if (value == 11) printf("Queen_W ");
if (value == 12) printf("King_W ");
}
printf("\n\n");
}
printf("\n");
}
/*
* The following function determines if a piece is present at the indicated position.
* The function returns true if the piece exists, and false otherwise.
*/
bool piece(struct chess current, int position)
{
if ((current.board[position/10][position%10]) == 0) return false;
else return true;
}
/*
* The following function determines if a location exists in an eight by eight board.
* The function returns true if the position exists, and false otherwise.
*/
bool is_board(int position)
{
int value = position % 10;
int value_2 = position / 10;
if ((value > 7) || (value < 0)) return false;
else if((value_2 > 7) || (value_2 < 0)) return false;
else return true;
}
/*
* The following function determines if a pawn swap can occur.
* The function returns true if the move can occur, and false otherwise.
*/
bool pawn_swap(struct chess current, int position)
{
int value = current.board[position/10][position%10];
if ((value == 6) && ((position/10) == 7)) return true;
else if ((value == 7) && ((position/10) == 0)) return true;
else return false;
}
/*
* The following function executes a pawn swap.
* The function asks the user to input the value of the piece to be swapped to.
* The function then changes the pwan to the indicated piece.
*/
struct chess pawn_swap_complete(struct chess current, int position)
{
int value, color;
printf("What would you like to promote the pawn to?\n\n");
printf("Rook = 1\nKnight = 2\nBishop = 3\nQueen = 4\n");
scanf("%d", &value);
color = is_black(current, position);
if(color == 0)
{
if (value == 1) current.board[position/10][position%10] = 8;
if (value == 2) current.board[position/10][position%10] = 9;
if (value == 3) current.board[position/10][position%10] = 10;
if (value == 4) current.board[position/10][position%10] = 11;
}
else if(color == 1)
{
if (value == 1) current.board[position/10][position%10] = 1;
if (value == 2) current.board[position/10][position%10] = 2;
if (value == 3) current.board[position/10][position%10] = 3;
if (value == 4) current.board[position/10][position%10] = 4;
}
return current;
}
/*
* The following function determines if a move is valid.
* The function calls other functions specified on the piece entered.
* The function returns true if the move is valid and false otherwise.
*/
bool valid_move(struct chess current, int start, int end)
{
int piece = current.board[start/10][start%10];
if ((piece == 6) && (black_valid_pawn(current, start, end))) return true;
else if ((piece == 7) && (white_valid_pawn(current, start, end))) return true;
else if (((piece == 1) || (piece == 8)) && (valid_rook(current, start, end))) return true;
else if (((piece == 2) || (piece == 9)) && (valid_knight(current, start, end))) return true;
else if (((piece == 3) || (piece == 10)) && (valid_bishop(current, start, end))) return true;
else if (((piece == 4) || (piece == 11)) && (valid_queen(current, start, end))) return true;
else if (((piece == 5) || (piece == 12)) && (valid_king(current, start, end))) return true;
else return false;
}
/*
* The following function determines if the white king is in check.
* The function calls other functions specified on the piece entered.
* The function returns true if the white king is not in check and false otherwise.
*/
bool white_king(struct chess current)
{
int final = 0, i = 0;
for (int t = 0; t < 8; t++)
{
for (int k = 0; k < 8; k++)
{
if(current.board[t][k] == 12) final = (t*10)+k;
}
}
int final_2 = 0;
for (int t = 0; t < 8; t++)
{
for (int k = 0; k < 8; k++)
{
final_2 = ((t*10)+k);
if(valid_move(current, final_2, final)) return false;
}
}
return true;
}
/*
* The following function determines if the black king is in check.
* The function calls other functions specified on the piece entered.
* The function returns true if the black king is not in check and false otherwise.
*/
bool black_king(struct chess current)
{
int final = 0, i = 0;
for (int t = 0; t < 8; t++)
{
for (int k = 0; k < 8; k++)
{
if(current.board[t][k] == 5) final = (t*10)+k;
}
}
int final_2 = 0;
for (int t = 0; t < 8; t++)
{
for (int k = 0; k < 8; k++)
{
final_2 = ((t*10)+k);
if(valid_move(current, final_2, final)) return false;
}
}
return true;
}
/*
* The following function determines if the black king is in check mate.
* The function calls other functions specified on the piece entered.
* The function returns true if the black king is not in check mate and false otherwise.
*/
bool black_king_check(struct chess current)
{
int flag = 0;
struct chess *board_temporary;
struct chess temporary;
board_temporary = &temporary;
for (int j = 0; j < 64; j++)
{
for (int i = 0; i < 64; i++)
{
*board_temporary = move_piece(current, j, i);
if(black_king(temporary)) flag ++;
}
}
if (flag == 0) return false;
else return true;
}
/*
* The following function determines if the white king is in check mate.
* The function calls other functions specified on the piece entered.
* The function returns true if the white king is not in check mate and false otherwise.
*/
bool white_king_check(struct chess current)
{
int flag = 0;
struct chess *board_temporary;
struct chess temporary;
board_temporary = &temporary;
for (int j = 0; j < 64; j++)
{
for (int i = 0; i < 64; i++)
{
*board_temporary = move_piece(current, j, i);
if(white_king(temporary)) flag ++;
}
}
if (flag == 0) return false;
else return true;
}
/*
* The following is the main function, which executes the game.
* The main function intializes a blank chess board and begins a game.
* The game is ended when one of the two kings is in check mate.
* Alongside printing the board each move, the main function also outputs user prompts.
* To compile with the Python GUI, redirect the outputs to the Python file.
*/
int main(void)
{
struct chess current = {1,2,3,4,5,3,2,1,6,6,6,6,6,6,6,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7,7,7,7,7,7,7,7,8,9,10,11,12,10,9,8};
struct chess *board;
struct chess *board_temporary;
struct chess temporary;
board_temporary = &temporary;
board = ¤t;
int start = 0, end = 0, counter = 0, colour;
while (black_king_check(current) && white_king_check(current))
{
printf("Please enter in the starting location:\n");
scanf("%d", &start);
printf("Please enter in the ending location:\n");
scanf("%d", &end);
colour = counter % 2;
*board_temporary = move_piece(current, start, end);
if(current.board[start/10][start%10] == 0) printf("\nNo piece at this position!\n\n");
else
{
if(is_board(start))
{
if(is_board(end))
{
if(turn(current, start, counter))
{
if(((colour == 0) && (white_king(temporary))) || ((colour == 1) && (black_king(temporary))))
{
if (valid_move(current, start, end))
{
*board = move_piece(current, start, end);
if (pawn_swap(current, end))
{
*board = pawn_swap_complete(current, end);
}
printf("\n");
print_board(current);
counter++;
}
else printf("\nThe move entered is invalid!\n\n");
}
else printf("\nThis move does not move your King out of check!\n");
}
else printf("\nWrong Player!\n\n");
}
else printf("\nThe final position is not on the board!\n\n");
}
else printf("\nThe intial position is not on the board!\n\n");
}
if((black_king(current)) == 0) printf("\nBlack King is in check!\n\n");
if((white_king(current)) == 0) printf("\nWhite King is in check!\n\n");
}
if (black_king_check(current) == 0 ) printf("Game Over, White Wins!\n");
else if (white_king_check(current) == 0) printf("Game Over, Black Wins!\n");
else printf("Time to Debug!\n");
return 0;
}