-
Notifications
You must be signed in to change notification settings - Fork 1
/
rules.c
546 lines (481 loc) · 20.9 KB
/
rules.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
/****************************************************************************
* Chess Library
*
* Alexandria University, Faculty of Engineering
* First year, Fall 2015
* Computer and Systems Engineering Department
*
* Programming I Final Project
*
* Marwan Tammam <[email protected]>
* Ahmed Yakout <[email protected]>
*
***************************************************************************/
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include "chess.h"
bool
isValid(int move2[]){
if (move2[1] == 8 || move2[1] == 9)
{
return castling(move2);
}
if (islower(board[move2[0]][move2[1]]) && islower(board[move2[2]][move2[3]]))
return false;
if (isupper(board[move2[0]][move2[1]]) && isupper(board[move2[2]][move2[3]]))
return false;
if ((islower(board[move2[0]][move2[1]]) && turn != 1) || (isupper(board[move2[0]][move2[1]]) && turn != -1))
return false;
switch (board[move2[0]][move2[1]]){
case 'P': case 'p':
if (isValidPawn(move2))
{
if (move2[4] != 0 || move2[2] == 0 || move2[2] == 7)
{
if (move2[4] != 0 && (move2[2] == 0 || move2[2] == 7))
{
board[move2[0]][move2[1]] = turn == 1 ? move2[4] : toupper(move2[4]);
return true;
}
}
return true;
}
else
return false;
case 'K': case 'k':
return isValidKing(move2);
case 'N': case 'n':
return (isValidKnight(move2));
case 'B': case 'b':
return (isValidBishop(move2));
case 'R': case 'r':
return (isValidRook(move2));
case 'Q': case 'q':
return (isValidQueen(move2));
default:
return false;
}
}
bool
isValidRook(int move[])
{
int i;
if ( move[1] == move[3]){
//***************** vertical move ****************
if( move[0] > move[2]){
// *********** forward **************
for ( i = move[0]-1 ; i > move[2] ; i-- ){
if ( board[i][move[1]] != WHT && board[i][move[1]] != BLK ) {
// if true that means there is a piece in the way.
return false;
}
}
return true;
}
else if ( move[0] < move[2] ){
// *********** backward ****************
for ( i = move[0]+1 ; i < move[2] ; i++ ){
if ( board[i][move[1]] != WHT && board[i][move[1]] != BLK) {
// if true that means there is a piece in the way.
return false;
}
}
return true;
}
else return false;
// *********** invalid move ****************
}
else if ( move[0] == move[2]){
//***************** Horizontal move ****************
if( move[1] > move[3]){
// ************** left ***************
for ( i = move[1]-1 ; i > move[3] ; i-- ){
if ( board[move[0]][i] != WHT && board[move[0]][i] != BLK){
// if true that means there is a piece in the way.
return false;
}
}
return true;
}
else if ( move[1] < move[3] ){
// ************** right ***************
for ( i = move[1]+1 ; i < move[3] ; i++ ){
if ( board[move[0]][i] != WHT && board[move[0]][i] != BLK){
// if true that means there is a piece in the way.
return false;
}
}
return true;
}
else return false;
}
else return false;
// ******************** invalid move ********************
}
bool
isValidBishop(int move[]){
int i;
if ( abs(move[0]-move[2]) == abs(move[1]-move[3]) ){
//***************** Valid Diagonal move ************
if( move[0] < move[2] && move[1] < move[3]){
// *********** backward - right **************
for ( i = 1 ; i < move[2]-move[0] ; i++ ){
if ( board[move[0]+i][move[1]+i] != WHT && board[move[0]+i][move[1]+i] != BLK ) {
// if true that means there is a piece in the way.
return false;
}
}
return true;
}
else if( move[0] < move[2] && move[1] > move[3]){
// *********** backward - left ************
for ( i = 1 ; i < move[2]-move[0] ; i++ ){
if ( board[move[0]+i][move[1]-i] != WHT && board[move[0]+i][move[1]-i] != BLK ) {
// if true that means there is a piece in the way.
return false;
}
}
return true;
}
else if( move[0] > move[2] && move[1] < move[3]){
// *********** forward - right **************
for ( i = 1 ; i < move[0]-move[2] ; i++ ){
if ( board[move[0]-i][move[1]+i] != WHT && board[move[0]-i][move[1]+i] != BLK ) {
// if true that means there is a piece in the way.
return false;
}
}
return true;
}
else if( move[0] > move[2] && move[1] > move[3]){
// *********** Forward - left **************
for ( i = 1 ; i < move[0]-move[2] ; i++ ){
if ( board[move[0]-i][move[1]-i] != WHT && board[move[0]-i][move[1]-i] != BLK ) {
// if true that means there is a piece in the way.
return false;
}
}
return true;
}
else return false;
// *********** invalid move ****************
}
else return false;
// ******************** invalid diagonal move ********************
}
bool
isValidQueen(int move[]){
if ( isValidBishop(move) || isValidRook(move) ) return true;
else return false;
}
bool
isValidKnight(int move[]){
if ( move[2] == move[0]-2 && move[3] == move[1]+1 ) return true;
else if( move[2] == move[0]+2 && move[3] == move[1]+1 ) return true;
else if( move[2] == move[0]-2 && move[3] == move[1]-1 ) return true;
else if( move[2] == move[0]+2 && move[3] == move[1]-1 ) return true;
else if( move[2] == move[0]-1 && move[3] == move[1]+2 ) return true;
else if( move[2] == move[0]+1 && move[3] == move[1]+2 ) return true;
else if( move[2] == move[0]-1 && move[3] == move[1]-2 ) return true;
else if( move[2] == move[0]+1 && move[3] == move[1]-2 ) return true;
else return false;
}
bool
isValidKing(int move[]){
if ( abs(move[2] - move[0]) == 1 && abs(move[3] - move[1]) == 1 ) return true; // diagonal move
else if ( abs(move[2] - move[0]) == 1 && move[3] - move[1] == 0 ) return true; // vertical move
else if ( move[2] - move[0] == 0 && abs(move[3] - move[1]) == 1 ) return true; // horizontal move
else return false;
}
bool
isValidPawn(int move[]){
if ( turn == 1 ){
// player1 turn
if ( move[0]-move[2] == 1 && abs(move[1]-move[3]) == 1 ){
// Valid Diagonal move (forward) ( must check for a piece to be taken out
if (board[move[2]][move[3]] != WHT && board[move[2]][move[3]] != BLK) return true;
// a piece will be taken out, therefore move is valid.
else return false;
// there is no piece to be taken out, therefore move is invalid!
}
else if ( move[1] == move [3] && move[0] > move[2]){
// vertical (forward) move is valid (can't take out a piece)
if (move[0] == 6){
// can move 2 steps or 1 step
if ( move[0]-move[2] == 2){
// two step
if ((board[move[2]+1][move[3]] != WHT && board[move[2]+1][move[3]] != BLK) || (board[move[2]][move[3]] != WHT && board[move[2]][move[3]] != BLK)) return false;
else return true;
}
else if (move[0]-move[2] == 1){
// one step
if (board[move[2]][move[3]] != WHT && board[move[2]][move[3]] != BLK) return false;
else return true;
}
else return false;
}
else{
// can move 1 step (forward) only
if ( move[0] - move[2] == 1 ){
if ( board[move[2]][move[3]] != WHT && board[move[2]][move[3]] != BLK ) return false;
else return true;
}
else return false;
}
}
else return false;
}
else if ( turn == -1){
// player2 turn
if ( move[2]-move[0] == 1 && abs(move[1]-move[3]) == 1 ){
// Valid Diagonal move (backward) ( must check for a piece to be taken out
if (board[move[2]][move[3]] != WHT && board[move[2]][move[3]] != BLK) return true;
// a piece will be taken out, therefore move is valid.
else return false;
// there is no piece to be taken out, therefore move is invalid!
}
else if ( move[1] == move [3] && move[0] < move[2]){
// vertical (Backward) move is valid (can't take out a piece)
if (move[0] == 1){
// can move 2 steps or 1 step
if ( move[2]-move[0] == 2){
// two step
if ((board[move[2]-1][move[3]] != WHT && board[move[2]-1][move[3]] != BLK) || (board[move[2]][move[3]] != WHT && board[move[2]][move[3]] != BLK)) return false;
else return true;
}
else if (move[2]-move[0] == 1){
// one step
if (board[move[2]][move[3]] != WHT && board[move[2]][move[3]] != BLK) return false;
return true;
}
else return false;
}
else{
// can move 1 step (backward) only
if ( move[2]-move[0] == 1 ){
if (board[move[2]][move[3]] != WHT && board[move[2]][move[3]] != BLK) return false;
else return true;
}
else return false;
}
}
else return false;
}
else return false;
// invalid move or Promotion!
}
/* *********************************************************************************************
* ******************************** "Check" Functions ******************************************
* *********************************************************************************************
*/
bool
isCheck(void) {
if (knightAttack() || bishopAttack() || pawnAttack() || rookAttack() || kingAttack()) {
return true;
} else return false;
}
bool
rookAttack(void) {
int i;
char attacker1 = turn == -1 ? 'r' : 'R';
char attacker2 = turn == -1 ? 'q' : 'Q';
int kingPos[2];
kingPos[0] = turn == -1 ? blackKingPos[0] : whiteKingPos[0];
kingPos[1] = turn == -1 ? blackKingPos[1] : whiteKingPos[1];
// vertical
// forward
for (i = kingPos[0] - 1; i >= 0; i--) {
if (board[i][kingPos[1]] == attacker1) return true;
else if (board[i][kingPos[1]] == attacker2) return true;
else if (board[i][kingPos[1]] == WHT || board[i][kingPos[1]] == BLK) continue;
else break;
}
// backward
for (i = (kingPos[0] + 1); i <= 7; i++) {
if (board[i][kingPos[1]] == attacker1) return true;
else if (board[i][kingPos[1]] == attacker2) return true;
else if (board[i][kingPos[1]] == WHT || board[i][kingPos[1]] == BLK) continue;
else break;
}
// horizontal
// left
for (i = kingPos[1] - 1; i >= 0; i--) {
if (board[kingPos[0]][i] == attacker1) return true;
else if (board[kingPos[0]][i] == attacker2) return true;
else if (board[kingPos[0]][i] == WHT || board[kingPos[0]][i] == BLK) continue;
else break;
}
// right
for (i = kingPos[1] + 1; i <= 7; i++) {
if (board[kingPos[0]][i] == attacker1) return true;
else if (board[kingPos[0]][i] == attacker2) return true;
else if (board[kingPos[0]][i] == WHT || board[kingPos[0]][i] == BLK) continue;
else break;
}
return false; // no threats
}
bool
bishopAttack(void) {
int i;
char attacker1 = turn == -1 ? 'b' : 'B';
char attacker2 = turn == -1 ? 'q' : 'Q';
int kingPos[2];
kingPos[0] = turn == -1 ? blackKingPos[0] : whiteKingPos[0];
kingPos[1] = turn == -1 ? blackKingPos[1] : whiteKingPos[1];
// diagonal moves
// diagonal (forward - right) // pawn may check!
for (i = 1; i <= 7; i++) {
if (kingPos[0] - i < 0 || kingPos[1] + i > 7) break; // board's edge reached
if (board[kingPos[0] - i][kingPos[1] + i] == attacker1) return true;
else if (board[kingPos[0] - i][kingPos[1] + i] == attacker2) return true;
else if (board[kingPos[0] - i][kingPos[1] + i] == WHT || board[kingPos[0] - i][kingPos[1] + i] == BLK) continue;
else break;
}
// diagonal (backward - left)
for (i = 1; i <= 7; i++) {
if (kingPos[0] + i > 7 || kingPos[1] - i < 0) break; // board's edge reached
if (board[kingPos[0] + i][kingPos[1] - i] == attacker1) return true;
else if (board[kingPos[0] + i][kingPos[1] - i] == attacker2) return true;
else if (board[kingPos[0] + i][kingPos[1] - i] == WHT || board[kingPos[0] + i][kingPos[1] - i] == BLK) continue;
else break;
}
// diagonal (forward - left) // pawn may check!
for (i = 1; i <= 7; i++) {
if (kingPos[0] - i < 0 || kingPos[1] - i < 0) break; // board's edge reached
if (board[kingPos[0] - i][kingPos[1] - i] == attacker1) return true;
else if (board[kingPos[0] - i][kingPos[1] - i] == attacker2) return true;
else if (board[kingPos[0] - i][kingPos[1] - i] == WHT || board[kingPos[0] - i][kingPos[1] - i] == BLK) continue;
else break;
}
// diagonal (backward - right)
for (i = 1; i <= 7; i++) {
if (kingPos[0] + i > 7 || kingPos[1] + i > 7) break; // board's edge reached
if (board[kingPos[0] + i][kingPos[1] + i] == attacker1) return true;
else if (board[kingPos[0] + i][kingPos[1] + i] == attacker2) return true;
else if (board[kingPos[0] + i][kingPos[1] + i] == WHT || board[kingPos[0] + i][kingPos[1] + i] == BLK) continue;
else break;
}
return false; // no threats
}
bool
pawnAttack(void) {
char attacker = turn == -1 ? 'p' : 'P';
int kingPos[2];
kingPos[0] = turn == -1 ? blackKingPos[0] : whiteKingPos[0];
kingPos[1] = turn == -1 ? blackKingPos[1] : whiteKingPos[1];
if (islower(attacker))
{
if (board[kingPos[0] + 1][kingPos[1] - 1] == attacker || board[kingPos[0] + 1][kingPos[1] + 1] == attacker) return true; // check
else return false; // no threats
}
else
{
if (board[kingPos[0] - 1][kingPos[1] - 1] == attacker || board[kingPos[0] - 1][kingPos[1] + 1] == attacker) return true; // check
else return false; // no threats
}
}
bool
knightAttack(void) {
char attacker = turn == -1 ? 'n' : 'N';
int kingPos[2];
kingPos[0] = turn == -1 ? blackKingPos[0] : whiteKingPos[0];
kingPos[1] = turn == -1 ? blackKingPos[1] : whiteKingPos[1];
if (board[kingPos[0] - 2][kingPos[1] + 1] == attacker) return true; //check
else if (board[kingPos[0] + 2][kingPos[1] + 1] == attacker) return true;
else if (board[kingPos[0] - 2][kingPos[1] - 1] == attacker) return true;
else if (board[kingPos[0] + 2][kingPos[1] - 1] == attacker) return true;
else if (board[kingPos[0] - 1][kingPos[1] + 2] == attacker) return true; //check
else if (board[kingPos[0] + 1][kingPos[1] + 2] == attacker) return true;
else if (board[kingPos[0] - 1][kingPos[1] - 2] == attacker) return true;
else if (board[kingPos[0] + 1][kingPos[1] - 2] == attacker) return true;
else return false; // no threats
}
bool
kingAttack(void) {
char attacker = turn == -1 ? 'k' : 'K';
int kingPos[2];
kingPos[0] = turn == -1 ? blackKingPos[0] : whiteKingPos[0];
kingPos[1] = turn == -1 ? blackKingPos[1] : whiteKingPos[1];
// diagonal moves
if (board[kingPos[0] - 1][kingPos[1] - 1] == attacker || board[kingPos[0] - 1][kingPos[1] + 1] == attacker) return true; // check
if (board[kingPos[0] + 1][kingPos[1] - 1] == attacker || board[kingPos[0] + 1][kingPos[1] + 1] == attacker) return true; // check
// vertical
if (board[kingPos[0]][kingPos[1] - 1] == attacker || board[kingPos[0]][kingPos[1] + 1] == attacker) return true; // check
if (board[kingPos[0] - 1][kingPos[1]] == attacker || board[kingPos[0] + 1][kingPos[1]] == attacker) return true; // check
else return false;
}
/*******************************************************************************
*/
bool
isCheckMate(int n){
//n => turn
int i, j, k, l;
int possibleMoves[5];
for ( i = 0 ; i < 8 ; i++ ){
for ( j = 0 ; j < 8 ; j++ ){
if ( n == 1 ? islower(board[i][j]) : isupper(board[i][j]) ){
for ( k = 0 ; k < 8 ; k++ ){
for ( l = 0 ; l < 8 ; l++){
possibleMoves[0] = i;
possibleMoves[1] = j;
possibleMoves[2] = k;
possibleMoves[3] = l;
possibleMoves[4] = (tolower(board[i][j]) == 'p' && (k == 0 || k == 7)) ? 'q' : 0;
if (isValid(possibleMoves)) {
char killed = tempMove(possibleMoves);
int x = 0;
if (isCheck())
{
x = 1;
}
tempUndo(possibleMoves, killed);
if (x == 0)
return false;
}
}
}
}
}
}
return true;
}
bool
castling(int move[]){
// (0-0) kingSide , (0-0-0) queenSide
int i;
int move2[5] = {0};
move2[0] = turn == 1 ? whiteKingPos[0] : blackKingPos[0];
move2[1] = turn == 1 ? whiteKingPos[1] : blackKingPos[1];
move2[2] = turn == 1 ? whiteKingPos[0] : blackKingPos[0];
if (isCheck()) return false;
if(move[1] == 8)
{
if((turn == 1 ? r77Moved : R07Moved) == 1 || (turn == 1 ? whiteKingMoved : blackKingMoved) == 1) return false;
for ( i = 1 ; i <= 2 ; i++) {
move2[3] = move2[1] + i;
if (board[turn == 1 ? 7 : 0][4+i] != WHT && board[turn == 1 ? 7 : 0][4+i] != BLK) return false;
tempMove(move2);
if(isCheck()) {
tempUndo(move2, WHT);
return false;
}
else tempUndo(move2, WHT);
}
}
else if (move[1] == 9)
{
if((turn == 1 ? r70Moved : R00Moved) == 1 || (turn == 1 ? whiteKingMoved : blackKingMoved) == 1) return false;
for ( i = 1 ; i <= 3 ; i++) {
move2[3] = move2[1] - i;
if (board[turn == 1 ? 7 : 0][4-i] != WHT && board[turn == 1 ? 7 : 0][4-i] != BLK) return false;
tempMove(move2);
if(isCheck()) {
tempUndo(move2, WHT);
return false;
}
else tempUndo(move2, WHT);
}
}
return true;
}