-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAltChess.c
More file actions
1792 lines (1574 loc) · 64.3 KB
/
AltChess.c
File metadata and controls
1792 lines (1574 loc) · 64.3 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
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <conio.h>
#include <malloc.h>
// Using gotoxy(x,y) which sets the cursor on entered x and y coordinates
#define gotoxy(x,y) printf ( "\033[%d;%dH", (y), (x) )
// Definition for each piece
typedef struct piece
{
char name[25] ;
int priority ;
int rowpos ;
int colpos ;
int color ;
// 1 stands for white, 0 for black
} piece ;
typedef struct square
{
char item[15] ;
piece *content ;
} square ;
typedef struct move
{
int r, c ;
// r stands for row, c for column
struct move *next ;
} move ;
// stores all the possible moves for the current piece to check if the move entered by the user is a valid move
typedef struct moveStack
{
move *top ;
} moveStack ;
typedef struct piecesLeft
{
char pieceLeft[15] ;
struct piecesLeft *next ;
} piecesLeft ;
// Record of all the remaining pieces for a player
typedef struct leftStack
{
piecesLeft *top ;
} leftStack ;
piece pieces[8] = {
// { name, priority, row position, column position, color }
{ "Pawn 1 (W)", 1, 3, 0, 1 },
{ "Knight (W)", 3, 3, 1, 1 },
{ "King (W)", 5, 3, 2, 1 },
{ "Pawn 2 (W)", 1, 3, 3, 1 },
{ "Pawn 1 (B)", 1, 0, 0, 0 },
{ "Knight (B)", 3, 0, 1, 0 },
{ "King (B)", 5, 0, 2, 0 },
{ "Pawn 2 (B)", 1, 0, 3, 0 },
} ;
// 0=WPawn1, 1=WKnight, 2=WKing, 3=WPawn2, 4=BPawn1, 5=BKnight, 6=BKing, 7=BPawn2
char empty[10] = " " ;
square board[4][4] ;
int stalemateCtr=0, WPoints = 0, BPoints = 0 ;
// This function takes a string and a number and prints that string that number of times
void printStr ( char c[], int times )
{
for ( int i=0 ; i<times ; i++ )
printf ( "%s", c ) ;
}
// Overloading earlier function for different use cases
void printStr2 ( char c1[], int times1, char c2[], int times2 )
{
for ( int i=0 ; i<times1 ; i++ )
printf ( "%s", c1 ) ;
for ( int i=0 ; i<times2 ; i++ )
printf ( "%s", c2 ) ;
}
void printStr3 ( char c1[], int t1, char c2[], int t2, char c3[], int t3 )
{
for ( int i=0 ; i<t1 ; i++ )
printf ( "%s", c1 ) ;
for ( int i=0 ; i<t2 ; i++ )
printf ( "%s", c2 ) ;
for ( int i=0 ; i<t3 ; i++ )
printf ( "%s", c3 ) ;
}
// Instructions for proceeding with the game
void instructions ()
{
printStr2 ( "\n", 1, "\t", 9 ) ;
printf ( "INTRODUCTION\n" ) ;
printStr2 ( "\t", 9, "-", 12 ) ;
printf ( "\n\tAlt Chess is a 2-player game which is an alternate take on the game of chess. " ) ;
printf ( "It consists of a 4X4 chessboard with 2 pawns, 1 knight and 1 king with \n\tthe priority of pawn being the " ) ;
printf ( "\tlowest and king being the highest.\n" ) ;
printf ( "\tThe objective is to kill as many opponent pieces as we can and get points according to the priority of the killed piece.\n" ) ;
printStr2 ( "\n", 1, "\t", 9 ) ;
printf ( "INSTRUCTIONS\n" ) ;
printStr2 ( "\t", 9, "-", 12 ) ;
printf ( "\n" ) ;
printf ( "\t\t\t1. White always moves first, and players get alternate turns. Only one piece can be moved at a time.\n" ) ;
printf ( "\t\t\t2. Pieces cannot move through other pieces (though the knight can jump over other pieces), " ) ;
printf ( "and can never move onto a square with one of \n\t\t\t their own pieces.\n" ) ;
printf ( "\t\t\t3. The pawn is promoted to the rook of the same color once it reaches the opposite side of the board.\n" ) ;
printf ( "\t\t\t4. Pawns can move only in the forward direction. They can only move to diagonal blocks towards the forward direction for " ) ;
printf ( "taking opponents.\n" ) ;
printf ( "\t\t\t5. Knight can move in a shape like L. Two spaces in forward, backward or side-to-side followed by a right or left turn.\n" ) ;
printf ( "\t\t\t6. The rook moves in a continuous line forward, backward or side-to-side.\n" ) ;
printf ( "\t\t\t7. King can move in all directions, one square at a time.\n" ) ;
printf ( "\t\t\t8. For your convenience, we have also displayed the possible moves you can make.\n" ) ;
printf ( "\t\t\t9. Killing the king does not mean the end of the game. The game ends only when the pieces of the same color are left on " ) ;
printf ( "the board.\n" ) ;
printf ( "\t\t\t10. If no piece is killed after 12 moves by any player and only one piece for each player is left, the condition of " ) ;
printf ( "stalemate is achieved,\n\t\t\t i.e the player with more points will be declared the winner.\n" ) ;
printf ( "\t\t\t11. The priority of pieces is as followed:\n" ) ;
printf ( "\t\t\t\t~King: Strongest, Points on killing King = 5\n" ) ;
printf ( "\t\t\t\t~Rook: Intermediate, Points on killing Rook = 4\n" ) ;
printf ( "\t\t\t\t~Knight: Intermediate, Points on killing Knight = 3\n" ) ;
printf ( "\t\t\t\t~Pawn: Weakest, Points on killing Pawn = 1\n" ) ;
printStr2 ( "\n", 3, "\t", 8 ) ;
printf ( "*****Press any key to continue.***** \n" ) ;
getch () ;
system( "cls" ) ;
}
// Records points of both teams
void scorecounter ( int newrpos, int newcpos )
{
if ( board[newrpos][newcpos].content ) //If something needs to be killed
{
if( board[newrpos][newcpos].content->color == 1 )
BPoints += board[newrpos][newcpos].content->priority ;
else if( board[newrpos][newcpos].content->color == 0 )
WPoints += board[newrpos][newcpos].content->priority ;
}
}
// Displays points of both teams
void scoreboard ()
{
printStr2 ( "\n", 2, "\t", 9 ) ;
printf ( "The current scores are:\n" ) ;
printStr ( "\t", 12 ) ;
printf ( "*Team White = %d\n", WPoints ) ;
printStr ( "\t", 12 ) ;
printf ( "*Team Black = %d\n", BPoints ) ;
}
// Stores all possible moves for current piece to check if move entered by the user is a valid move
void pushInMoveStack ( moveStack *stack, int row, int column )
{
move *toadd = (move*)malloc(sizeof(move)) ;
toadd->c = column ;
toadd->r = row ;
toadd->next=NULL ;
if ( !((*stack).top) )
(*stack).top = toadd ;
else
{
toadd->next = (*stack).top ;
(*stack).top=toadd ;
}
}
// New Record of all the remaining pieces for a player
void pushInLeftStack ( leftStack *stack, char toAdd[] )
{
piecesLeft *toadd = (piecesLeft*)malloc(sizeof(piecesLeft)) ;
strcpy ( toadd->pieceLeft, toAdd ) ;
toadd->next=NULL ;
if ( !((*stack).top) )
(*stack).top = toadd ;
else
{
toadd->next = (*stack).top ;
(*stack).top=toadd ;
}
}
// Function to empty a stack of moves
void emptyMoveStack ( moveStack *stack )
{
(*stack).top=NULL ;
}
int remainingCtr ( leftStack *stack )
{
piecesLeft *ctr = (*stack).top ;
int count=0 ;
while (ctr)
{
count++ ;
ctr=ctr->next ;
}
return count ;
}
// Deleting killed pieces from left stack
void delFromLeftStack ( leftStack *stack, char toDel[] )
{
piecesLeft *finder = (*stack).top ;
if ( strcmp(finder->pieceLeft, toDel)==0 )
{
piecesLeft *todel = finder ;
(*stack).top = ((*stack).top)->next ; ;
free(todel) ;
}
else
{
while ( finder->next )
{
if ( strcmp(finder->next->pieceLeft, toDel)==0 )
{
piecesLeft *todel = finder->next ;
finder->next=finder->next->next ;
free(todel) ;
}
if ( finder->next )
finder=finder->next ;
}
}
}
// Function to check if a piece is still alive
int pieceExists ( leftStack *stack, char toFind[] )
{
piecesLeft *finder = (*stack).top ;
while ( finder )
{
if ( strcmp(finder->pieceLeft, toFind)==0 )
return 1 ;
finder=finder->next ;
}
return 0 ;
// Function returns 1 if piece is alive, 0 if it is not
}
// Function to change a value in a stack
void updateLeftStack ( leftStack *stack, char before[], char after[] )
{
piecesLeft *finder = (*stack).top ;
while ( finder )
{
if ( strcmp(finder->pieceLeft, before)==0 )
{
strcpy ( finder->pieceLeft, after ) ;
break ;
}
finder = finder->next ;
}
}
// Function to check if an entered move is valid
int checkIfValidMove ( moveStack *stack, int row, int col )
{
move *finder=stack->top ;
while ( finder )
{
if ( finder->c==col && finder->r==row )
return 1 ;
finder=finder->next ;
}
return 0 ;
// Function returns 1 if there is a valid move, 0 if it is not
}
// Creating a new board and assigning starting positions to the pieces
void createboard ( leftStack *black, leftStack *white )
{
for ( int i=0 ; i<4 ; i++ )
{
for ( int j=0 ; j<4 ; j++ )
{
board[i][j].content=NULL ;
strcpy ( board[i][j].item, empty ) ;
}
}
for ( int i=0 ; i<4 ; i++ )
{
board[3][i].content=&(pieces[i]) ;
strcpy ( board[3][i].item, pieces[i].name ) ;
board[0][i].content=&(pieces[i+4]) ;
strcpy ( board[0][i].item, pieces[i+4].name ) ;
}
for ( int i=0 ; i<8 ; i++ )
{
if ( i<4 )
// Adding white pieces to white stack
pushInLeftStack ( white, pieces[i].name ) ;
else
// Adding black pieces to black stack
pushInLeftStack ( black, pieces[i].name ) ;
}
}
// Displays current state of the board for a game
void displayboard ()
{
printStr3 ( "\n", 2, "\t", 5, " ", 10 ) ;
printStr3 ( " 0", 1, "\t", 3, " 1", 1 ) ;
printStr3 ( "\t", 3, " 2", 1, "\t", 3 ) ;
printf ( " 3" ) ;
printStr3 ( "\t", 3, " ", 1, "\n", 1 ) ;
printStr3 ( "\n", 1, "\t", 5, "-", 97 ) ;
printf ( "\n" ) ;
for ( int i=0 ; i<4 ; i++ )
{
printf ( "\t\t\t\t%d\t", i ) ;
printf ( "|" ) ;
for ( int j=0 ; j<4 ; j++ )
printf ( "\t%s\t|", board[i][j].item ) ;
printStr3 ( "\n", 1, "\t", 5, "-", 97 ) ;
printf ( "\n" ) ;
}
}
// Displays available pieces for Player 1 (White)
void displayPiecesP1 ()
{
for ( int i=1 ; i<=4 ; i++ )
{
if ( pieces[i-1].rowpos != -1 )
{
printStr ( "\t", 9 ) ;
printf ( "%d. %s\n", i, pieces[i-1].name ) ;
}
}
printf ( "\n" ) ;
}
// Displays available pieces for Player 2 (Black)
void displayPiecesP2 ()
{
for ( int i=5 ; i<=8 ; i++ )
{
if ( pieces[i-1].rowpos != -1 )
{
printStr ( "\t", 9 ) ;
printf ( "%d. %s\n", i-4, pieces[i-1].name ) ;
}
}
printf ( "\n" ) ;
}
// Function to check if a piece is at one of the edges of the board
int checkedgepiece ( int a )
{
if ( a==0 )
return 1 ;
else if ( a==3 )
return 2 ;
else
return 0 ;
// Function returns 1 for left edge, 2 for right edge, 0 if not an edge piece
}
// Function to check if an enemy is present at an entered position
int findenemy ( int r, int c, int piececolor )
{
if ( board[r][c].content && board[r][c].content->color != piececolor )
return 1 ;
else
return 0 ;
// Function returns 1 if enemy is present on valid move, 0 if no enemy is present on valid move
}
// Message to enter full screen mode is displayed
void fullScreenMessage ()
{
printStr2 ( "\n", 5, "\t", 9 ) ;
printf ( "Please enter full screen\n" ) ;
printStr3 ( "\t", 9, "~", 24, "\n", 1 ) ;
printStr2 ( "\n", 10, "\t", 9 ) ;
printf ( "Press any key to continue" ) ;
getch () ;
system ( "cls" ) ;
}
// Shows valid moves for all pieces
int showvalidmovesforuser ( moveStack *stack, piece a , int val )
{
int flag=0 ;
// Code for white pawn movement
if ( strcmp ( a.name, "Pawn 1 (W)" )==0 || strcmp ( a.name, "Pawn 2 (W)" )==0 )
{
// Forward movement
if ( a.rowpos-1 >= 0 && strcmp ( board[a.rowpos-1][a.colpos].item, empty ) == 0 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-1, a.colpos ) ;
}
pushInMoveStack( stack, a.rowpos-1, a.colpos ) ;
flag=1 ;
}
// For pawn present at left edge (view in direction of forward movement)
if ( checkedgepiece ( a.colpos ) == 1 )
{
// For attacking enemy using diagonal movement
if ( a.colpos+1 >= 0 && a.rowpos-1 >= 0 && findenemy(a.rowpos-1, a.colpos+1, a.color)==1 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-1, a.colpos+1 ) ;
}
pushInMoveStack( stack, a.rowpos-1, a.colpos+1 ) ;
flag=1 ;
}
}
// For pawn present at right edge (view in direction of forward movement)
else if ( checkedgepiece ( a.colpos ) == 2 )
{
// For attacking enemy using diagonal movement
if ( a.colpos-1 >= 0 && a.rowpos-1 >= 0 && findenemy(a.rowpos-1, a.colpos-1, a.color)==1 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-1, a.colpos-1 ) ;
}
pushInMoveStack( stack, a.rowpos-1, a.colpos-1 ) ;
flag=1 ;
}
}
// For pawn present anywhere except the edges
else
{
// For attacking enemy using diagonal movement
if ( a.colpos+1 >= 0 && a.rowpos-1 >= 0 && findenemy(a.rowpos-1, a.colpos+1, a.color)==1 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-1, a.colpos+1 ) ;
}
pushInMoveStack( stack, a.rowpos-1, a.colpos+1 ) ;
flag=1 ;
}
// For attacking enemy using diagonal movement
if ( a.colpos-1 >= 0 && a.rowpos-1 >= 0 && findenemy(a.rowpos-1, a.colpos-1, a.color)==1 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-1, a.colpos-1 ) ;
}
pushInMoveStack( stack, a.rowpos-1, a.colpos-1 ) ;
flag=1 ;
}
}
}
// Code for black pawn movement
else if ( strcmp ( a.name, "Pawn 1 (B)" )==0 || strcmp ( a.name, "Pawn 2 (B)" )==0 ) // Code for pawn movement
{
// Forward Movement
if ( a.rowpos+1 <= 3 && strcmp ( board[a.rowpos+1][a.colpos].item, empty ) == 0 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+1, a.colpos ) ;
}
pushInMoveStack( stack, a.rowpos+1, a.colpos ) ;
flag=1 ;
}
// For pawn present at left edge (view in direction of forward movement)
if ( checkedgepiece ( a.colpos ) == 2 )
{
// For attacking enemy using diagonal movement
if ( a.colpos-1 <= 3 && a.rowpos+1 <= 3 && findenemy(a.rowpos+1, a.colpos-1, a.color)==1 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+1, a.colpos-1 ) ;
}
pushInMoveStack( stack, a.rowpos+1, a.colpos-1 ) ;
flag=1 ;
}
}
// For pawn present at right edge (view in direction of forward movement)
else if ( checkedgepiece ( a.colpos ) == 1 )
{
// For attacking enemy using diagonal movement
if ( a.colpos+1 <= 3 && a.rowpos+1 <= 3 && findenemy(a.rowpos+1, a.colpos+1, a.color)==1 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+1, a.colpos+1 ) ;
}
pushInMoveStack( stack, a.rowpos+1, a.colpos+1 ) ;
flag=1 ;
}
}
// For pawn present anywhere except the edges
else
{
// For attacking enemy using diagonal movement
if ( a.colpos+1 >= 0 && a.rowpos+1 <= 3 && findenemy(a.rowpos+1, a.colpos+1, a.color)==1 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+1, a.colpos+1 ) ;
}
pushInMoveStack( stack, a.rowpos+1, a.colpos+1 ) ;
flag=1 ;
}
// For attacking enemy using diagonal movement
if ( a.colpos-1 >= 0 && a.rowpos+1 >= 3 && findenemy(a.rowpos+1, a.colpos-1, a.color)==1 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+1, a.colpos-1 ) ;
}
pushInMoveStack( stack, a.rowpos+1, a.colpos-1 ) ;
flag=1 ;
}
}
}
// Code for King movement
else if ( strcmp ( a.name, "King (W)" )==0 || strcmp ( a.name, "King (B)" )==0 ) // Code for King movement
{
//Forward movement for white king, backward for black king
if ( a.rowpos-1 >= 0 && a.rowpos-1<4 && strcmp ( board[a.rowpos-1][a.colpos].item, empty ) == 0
|| a.rowpos-1 >= 0&& a.rowpos-1<4 && findenemy(a.rowpos-1, a.colpos, a.color)==1 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-1, a.colpos ) ;
}
pushInMoveStack( stack, a.rowpos-1, a.colpos ) ;
flag=1 ;
}
// If white king is present at left edge, black king is at right edge
if ( checkedgepiece ( a.colpos ) == 1 )
{
// Shows valid move one square ahead in the directions if either enemy is present or block is empty
if ( a.colpos+1 >= 0 && a.rowpos-1 >= 0 && a.colpos+1 < 4 && a.rowpos-1<4 && findenemy(a.rowpos-1, a.colpos+1, a.color)==1
|| a.colpos+1 >= 0 && a.rowpos-1 >= 0 && a.colpos+1 < 4 && a.rowpos-1<4 && strcmp ( board[a.rowpos-1][a.colpos+1].item, empty )==0 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-1, a.colpos+1 ) ;
}
pushInMoveStack( stack, a.rowpos-1, a.colpos+1 ) ;
flag=1 ;
}
}
// If white king is present at right edge, black king is at left side
else if ( checkedgepiece ( a.colpos ) == 2 )
{
// Shows valid move one square ahead in the directions if either enemy is present or block is empty
if ( a.colpos-1 >= 0 && a.rowpos-1 >= 0 && a.colpos-1 < 4 && a.rowpos-1 < 4&& findenemy(a.rowpos-1, a.colpos-1, a.color)==1
|| a.colpos-1 >= 0 && a.rowpos-1 >= 0 && a.colpos-1 < 4 && a.rowpos-1 < 4 && strcmp ( board[a.rowpos-1][a.colpos-1].item, empty )==0 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-1, a.colpos-1 ) ;
}
pushInMoveStack( stack, a.rowpos-1, a.colpos-1 ) ;
flag=1 ;
}
}
// If the king ( white or black ) is present anywhere except the edges
else
{
// Shows valid move one square ahead in the directions if either enemy is present or block is empty
if ( a.colpos+1 >= 0 && a.rowpos-1 >= 0 && a.colpos+1 < 4 && a.rowpos-1 <4 && findenemy(a.rowpos-1, a.colpos+1, a.color)==1
|| a.colpos+1 >= 0 && a.colpos+1 < 4 && a.rowpos-1 <4 && a.rowpos-1 >= 0 && strcmp ( board[a.rowpos-1][a.colpos+1].item, empty )==0 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-1, a.colpos+1 ) ;
}
pushInMoveStack( stack, a.rowpos-1, a.colpos+1 ) ;
flag=1 ;
}
// Shows valid move one square ahead in the directions if either enemy is present or block is empty
if ( a.colpos-1 >= 0 && a.rowpos-1 >= 0 && a.colpos-1 < 4 && a.rowpos-1 <4 && findenemy(a.rowpos-1, a.colpos-1, a.color)==1
|| a.colpos-1 >= 0 && a.rowpos-1 >= 0 && a.colpos-1 < 4 && a.rowpos-1 <4 && strcmp ( board[a.rowpos-1][a.colpos-1].item, empty )==0 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-1, a.colpos-1 ) ;
}
pushInMoveStack( stack, a.rowpos-1, a.colpos-1 ) ;
flag=1 ;
}
}
// Right move for white king, left move for black king
if ( a.colpos+1 >= 0 && a.colpos+1<4 && strcmp ( board[a.rowpos][a.colpos+1].item, empty ) == 0
|| a.colpos+1 >= 0 && a.colpos+1<4 && findenemy(a.rowpos, a.colpos+1, a.color)==1 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos, a.colpos+1 ) ;
}
pushInMoveStack( stack, a.rowpos, a.colpos+1 ) ;
flag=1 ;
}
// Left move for white king, right move for black king
if ( a.colpos-1 >= 0 && a.colpos-1<4 && strcmp ( board[a.rowpos][a.colpos-1].item, empty ) == 0
|| a.colpos-1 >= 0&& a.colpos-1<4 && findenemy(a.rowpos, a.colpos-1, a.color)==1 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos, a.colpos-1 ) ;
}
pushInMoveStack( stack, a.rowpos, a.colpos-1 ) ;
flag=1 ;
}
// Backward movement for white king, forward for white king
if ( a.rowpos+1 >= 0 && a.rowpos+1<4 && strcmp ( board[a.rowpos+1][a.colpos].item, empty ) == 0
|| a.rowpos+1 >= 0 && a.rowpos+1<4 && findenemy(a.rowpos+1, a.colpos,a.color)==1 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+1, a.colpos ) ;
}
pushInMoveStack( stack, a.rowpos+1, a.colpos ) ;
flag=1 ;
}
// If white king is present at left edge, black king at right edge
if ( checkedgepiece ( a.colpos ) == 1 )
{
// Shows valid move one square ahead in the directions if either enemy is present or block is empty
if ( a.colpos+1 >= 0 && a.rowpos+1 >= 0 && a.colpos+1 <4 && a.rowpos+1<4 && findenemy(a.rowpos+1, a.colpos+1, a.color)==1
|| a.colpos+1 >= 0 && a.rowpos+1 >= 0&& a.colpos+1 <4 && a.rowpos+1<4 && strcmp ( board[a.rowpos+1][a.colpos+1].item, empty )==0 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+1, a.colpos+1 ) ;
}
pushInMoveStack( stack, a.rowpos+1, a.colpos+1 ) ;
flag=1 ;
}
}
// If white king is present at right edge, black king at left edge
else if ( checkedgepiece ( a.colpos ) == 2 )
{
// Shows valid move one square ahead in the directions if either enemy is present or block is empty
if ( a.colpos-1 >= 0 && a.colpos-1<4 && a.rowpos+1 >= 0 && a.rowpos+1<4 && findenemy(a.rowpos+1, a.colpos-1, a.color)==1
|| a.colpos-1 >= 0&& a.colpos-1<4 && a.rowpos+1 >= 0 && a.rowpos+1<4 && strcmp ( board[a.rowpos+1][a.colpos-1].item, empty )==0 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+1, a.colpos-1 ) ;
}
pushInMoveStack( stack, a.rowpos+1, a.colpos-1 ) ;
flag=1 ;
}
}
// If king ( white or black ) is present anywhere except the edges
else
{
// Shows valid move one square ahead in the directions if either enemy is present or block is empty
if ( a.colpos+1 >= 0 && a.colpos+1<4 && a.rowpos+1 >= 0&& a.rowpos+1<4 && findenemy(a.rowpos+1, a.colpos+1, a.color)==1
|| a.colpos+1 >= 0 && a.colpos+1<4 && a.rowpos+1 >= 0&& a.rowpos+1<4 && strcmp ( board[a.rowpos+1][a.colpos+1].item, empty )==0 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+1, a.colpos+1 ) ;
}
pushInMoveStack( stack, a.rowpos+1, a.colpos+1 ) ;
flag=1 ;
}
// Shows valid move one square ahead in the directions if either enemy is present or block is empty
if ( a.colpos-1 >= 0 && a.rowpos+1 >= 0 && a.colpos-1 < 4 && a.rowpos+1 < 4 && findenemy(a.rowpos+1, a.colpos-1, a.color)==1
|| a.colpos-1 >= 0 && a.rowpos+1 >= 0 && a.colpos-1 < 4 && a.rowpos+1 < 4 && strcmp ( board[a.rowpos+1][a.colpos-1].item, empty )==0 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+1, a.colpos-1 ) ;
}
pushInMoveStack( stack, a.rowpos+1, a.colpos-1 ) ;
flag=1 ;
}
}
}
// Code for knight movement
else if ( strcmp ( a.name, "Knight (W)" )==0 || strcmp ( a.name, "Knight (B)" )==0 )
{
// 2 steps forward-1 step left movement for white knight, 2 steps backward-1 step right movement for black knight
if ( a.rowpos-2>= 0 && a.colpos-1 >= 0 && strcmp ( board[a.rowpos-2][a.colpos-1].item, empty ) == 0
|| a.rowpos-2 >= 0 && a.colpos-1 >= 0 && findenemy(a.rowpos-2, a.colpos-1, a.color)==1 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-2, a.colpos-1 ) ;
}
pushInMoveStack( stack, a.rowpos-2, a.colpos-1 ) ;
flag=1 ;
}
if ( a.rowpos-2 >= 0 && a.colpos+1 <= 3 && strcmp ( board[a.rowpos-2][a.colpos+1].item, empty ) == 0
|| a.rowpos-2 >= 0 && a.colpos+1 <= 3 && findenemy(a.rowpos-2, a.colpos+1, a.color)==1 )
// 2 steps forward-1 step right movement for white knight, 2 steps backward-1 step left movement for black knight
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-2, a.colpos+1 ) ;
}
pushInMoveStack( stack, a.rowpos-2, a.colpos+1 ) ;
flag=1 ;
}
if ( a.rowpos-1 >= 0 && a.colpos-2 >= 0 && strcmp ( board[a.rowpos-1][a.colpos-2].item, empty ) == 0
|| a.rowpos-1 >= 0 && a.colpos-2 >= 0 && findenemy(a.rowpos-1, a.colpos-2, a.color)==1 )
// 1 step forward-2 steps left movement for white knight, 1 step backward-2 steps right movement for black knight
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-1, a.colpos-2 ) ;
}
pushInMoveStack( stack, a.rowpos-1, a.colpos-2 ) ;
flag=1 ;
}
if ( a.rowpos-1 >= 0 && a.colpos+2 <= 3 && strcmp ( board[a.rowpos-1][a.colpos+2].item, empty ) == 0
|| a.rowpos-1 >= 0 && a.colpos+2 <= 3 && findenemy(a.rowpos-1, a.colpos+2, a.color)==1 )
// 1 step forward-2 steps right movement for white knight, 1 step backward-2 steps left movement for black knight
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos-1, a.colpos+2 ) ;
}
pushInMoveStack( stack, a.rowpos-1, a.colpos+2 ) ;
flag=1 ;
}
if ( a.rowpos+1 >= 0 && a.colpos-2 >= 0 && strcmp ( board[a.rowpos+1][a.colpos-2].item, empty ) == 0
|| a.rowpos+1 >= 0 && a.colpos-2 >= 0 && findenemy(a.rowpos+1, a.colpos-2, a.color)==1 )
// 1 step backward-2 steps left movement for white knight, 1 step forward-2 steps right movement for black knight
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+1, a.colpos-2 ) ;
}
pushInMoveStack( stack, a.rowpos+1, a.colpos-2 ) ;
flag=1 ;
}
if ( a.rowpos+1 >= 0 && a.colpos+2 <= 3 && strcmp ( board[a.rowpos+1][a.colpos+2].item, empty ) == 0
|| a.rowpos+1 >= 0 && a.colpos+2 <= 3 && findenemy(a.rowpos+1, a.colpos+2, a.color)==1 )
//1 step backward-2 steps right movement for white knight, 1 step forward-2 steps left movement for black knight
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+1, a.colpos+2) ;
}
pushInMoveStack( stack, a.rowpos+1, a.colpos+2 ) ;
flag=1 ;
}
if ( a.rowpos+2 >= 0 && a.colpos-1 >= 0 && strcmp ( board[a.rowpos+2][a.colpos-1].item, empty ) == 0
|| a.rowpos+2 >= 0 && a.colpos-1 >= 0 && findenemy(a.rowpos+2, a.colpos-1, a.color)==1 )
// 2 steps backward-1 step left movement for white knight, 2 steps forward-1 step right movement for black knight
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+2, a.colpos-1 ) ;
}
pushInMoveStack( stack, a.rowpos+2, a.colpos-1 ) ;
flag=1 ;
}
if ( a.rowpos+2 >= 0 && a.colpos+1 <= 3 && strcmp ( board[a.rowpos+2][a.colpos+1].item, empty ) == 0
|| a.rowpos+2 >= 0 && a.colpos+1 <= 3 && findenemy(a.rowpos+2, a.colpos+1, a.color)==1 )
// 2 steps backward-1 step right movement for white knight, 2 steps forward-1 step left movement for black knight
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos+2, a.colpos+1 ) ;
}
pushInMoveStack( stack, a.rowpos+2, a.colpos+1 ) ;
flag=1 ;
}
}
// This code applies to pawns promoted to rook
else if ( strcmp ( a.name, "Rook 1 (W)")==0 || strcmp ( a.name, "Rook 2 (W)")==0 || strcmp ( a.name, "Rook 1 (B)")==0
|| strcmp ( a.name, "Rook 2 (B)")==0 )
{
for ( int i=a.colpos-1 ; i>=0 ; i-- )
if (findenemy ( a.rowpos, i, a.color )==1 || strcmp ( board[a.rowpos][i].item, empty ) == 0 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos, i ) ;
}
pushInMoveStack( stack, a.rowpos, i ) ;
flag=1 ;
if ( findenemy ( a.rowpos, i, a.color )==1 )
break;
}
for ( int i=a.colpos+1 ; i<=3 ; i++ )
if (findenemy ( a.rowpos, i, a.color )==1 || strcmp ( board[a.rowpos][i].item, empty ) == 0 )
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", a.rowpos, i ) ;
}
pushInMoveStack( stack, a.rowpos, i ) ;
flag=1 ;
if ( findenemy ( a.rowpos, i, a.color )==1 )
break;
}
for ( int j=a.rowpos-1 ; j>=0 ; j-- )
if ( findenemy ( j, a.colpos, a.color )==1 || strcmp ( board[j][a.colpos].item, empty ) == 0)
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", j, a.colpos ) ;
}
pushInMoveStack( stack, j, a.colpos ) ;
flag=1 ;
if ( findenemy ( j, a.colpos, a.color )==1 )
break;
}
for ( int j=a.rowpos+1 ; j<=3 ; j++ )
if ( findenemy ( j, a.colpos, a.color )==1 || strcmp ( board[j][a.colpos].item, empty ) == 0)
{
if ( val==1 )
{
printStr ( "\t", 11 ) ;
printf ( "Position R:%d and C:%d \n", j, a.colpos ) ;
}
pushInMoveStack( stack, j, a.colpos ) ;
flag=1 ;
if ( findenemy ( j, a.colpos, a.color )==1 )
break;
}
}
return flag ;
}
// Function to move a piece to a given position
void movepiece ( int a, int newrpos, int newcpos, leftStack *stack )
{
stalemateCtr++ ;
// If something needs to be killed
if ( board[newrpos][newcpos].content )
{
printStr3 ( "\n", 1, "\t", 9, "~", 2 ) ;
printf ( "Killed %s!~~\n", board[newrpos][newcpos].content->name ) ;
// Killed piece deleted from record of remaining pieces for the player
delFromLeftStack( stack, board[newrpos][newcpos].item ) ;
stalemateCtr=0 ;
board[newrpos][newcpos].content->colpos=-1 ;
board[newrpos][newcpos].content->rowpos=-1 ;
}
strcpy ( board[pieces[a].rowpos][pieces[a].colpos].item, empty ) ;
board[pieces[a].rowpos][pieces[a].colpos].content=NULL ;
pieces[a].rowpos=newrpos ;
pieces[a].colpos=newcpos ;
strcpy ( board[pieces[a].rowpos][pieces[a].colpos].item, pieces[a].name ) ;
board[newrpos][newcpos].content=&(pieces[a]) ;
}
// Pawn promotes to rook once it reaches opposite end of the board
void promote ( piece *a, leftStack *stack )
{
if ( strcmp ( (*a).name, "Pawn 1 (W)" )==0 )
{
strcpy ( (*a).name, "Rook 1 (W)" ) ;
updateLeftStack ( stack, "Pawn 1 (W)", "Rook 1 (W)" ) ;
pieces[0].priority = 4 ;
}
else if ( strcmp ( (*a).name, "Pawn 2 (W)" )==0 )
{
strcpy ( (*a).name, "Rook 2 (W)" ) ;
updateLeftStack ( stack, "Pawn 2 (W)", "Rook 2 (W)" ) ;
pieces[3].priority = 4 ;
}
else if ( strcmp ( (*a).name, "Pawn 1 (B)" )==0 )
{
strcpy ( (*a).name, "Rook 1 (B)" ) ;
updateLeftStack ( stack, "Pawn 1 (B)", "Rook 1 (B)" ) ;
pieces[4].priority = 4 ;
}
else if ( strcmp ( (*a).name, "Pawn 2 (B)" )==0 )
{
strcpy ( (*a).name, "Rook 2 (B)" ) ;
updateLeftStack ( stack, "Pawn 2 (B)", "Rook 2 (B)" ) ;
pieces[7].priority = 4 ;
}
}
void declareWinner ( leftStack *black, leftStack *white )
{
// If only white pieces are present on the board
if ( remainingCtr(black)==0 )
{
printStr2 ( "\n", 9, "\t", 9 ) ;
printf ( "Team White wins! \n\n\n\n" ) ;
return ;
}
// If only black pieces are present on the board
else if ( remainingCtr(white)==0 )
{
printStr2 ( "\n", 9, "\t", 9 ) ;
printf ( "Team Black wins! \n\n\n\n" ) ;
return ;
}
else
{
if ( WPoints > BPoints )
{
printStr2 ( "\n", 9, "\t", 9 ) ;
printf ( "Team White wins! \n\n\n\n" ) ;
return ;
}
else if ( BPoints > WPoints )
{
printStr2 ( "\n", 9, "\t", 9 ) ;
printf ( "Team Black wins! \n\n\n\n" ) ;
return ;
}