-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.c
2719 lines (2269 loc) · 48.1 KB
/
chess.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
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
/*
+----
| chess.c
|
| christopher w bowron
|
|
| copyright 1999 by christopher bowron
|
| -= digital dreamland 1999 =-
| ...we came, we played chess, we left...
|
+----
| - todo -
| get a name
| heuristics to get better moves to top of move list
| save the better moves from alpha beta to a list
| to inspect the next time we need a move
|
| greater depth
| book openings
|
| - DONE -
| fix memory leaks - no wasted memory!
| fix pawn promotion - kludged
| manage own memory - no dynamic malloc/free
| print best search result found
| variables as single int using masking
| fix enpassant
| fix random loss of pieces
| speed up incheckp
+----
*/
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <time.h>
#include "chess.h"
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
#define PIECECHAR(p) (" PNBRQKpnbrqk"[p])
#define NILMOVE (makemove(makesquare(8,8),makesquare(8,8)))
#define DRAWREP (makemove(makesquare(9,9),makesquare(9,9))) /* (-1) */
#define NPIECES (13)
#define BLACK (-1)
#define WHITE (1)
#define CHECKMATE (3000)
#define STALEMATE (1000)
#define MAXDEPTH (6)
#define loopboard {int x, y; for(x=0;x<8;x++) for(y=0;y<8;y++)
#define endloop }
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
#define getx(s) ( (s) & 0x0f )
#define gety(s) ( (s) >> 4 )
#define gets1(m) ( (m) & 0xff )
#define gets2(m) ( ((m) >> 8) & 0xff)
#define setpiece(m, p) ( (*m) += ( (p) << 16 ) )
#define setcheck(m) ( (*m) |= ( 1 << 31 ) )
#define setmate(m) ( (*m) |= ( 3 << 30 ) )
#define getpiece(m) ( (m) >> 16 )
#define makemove(s1,s2) ( ((s2) << 8) | (s1) )
#define makesquare(x,y) ( ((y) << 4) | (x))
#define moveequal(m1, m2) ( (m1) == (m2) )
#define squareequal(s1, s2) ( (s1) == (s2) )
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
#define whiteoo makemove (makesquare (4, 0), makesquare (6, 0))
#define whiteooo makemove (makesquare (4, 0), makesquare (2, 0))
#define blackoo makemove (makesquare (4, 7), makesquare (6, 7))
#define blackooo makemove (makesquare (4, 7), makesquare (2, 7))
#define wthruoo makemove (makesquare(4,0),makesquare(5,0))
#define bthruoo makemove (makesquare(4,7),makesquare(5,7))
#define wthruooo makemove (makesquare(4,0),makesquare(3,0))
#define bthruooo makemove (makesquare(4,7),makesquare(3,7))
#define A1 makesquare(0,0)
#define G1 makesquare(7,0)
#define A8 makesquare(0,7)
#define G8 makesquare(7,7)
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
chessboard board =
{
{ wr, wn, wb, wq, wk, wb, wn, wr },
{ wp, wp, wp, wp, wp, wp, wp, wp },
{ empty, empty, empty, empty, empty, empty, empty, empty },
{ empty, empty, empty, empty, empty, empty, empty, empty },
{ empty, empty, empty, empty, empty, empty, empty, empty },
{ empty, empty, empty, empty, empty, empty, empty, empty },
{ bp, bp, bp, bp, bp, bp, bp, bp },
{ br, bn, bb, bq, bk, bb, bn, br }
};
piece bigboard[12][12] =
{
{ empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty},
{ empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty},
{ empty,empty,wr, wn, wb, wq, wk, wb, wn, wr,empty,empty},
{ empty,empty, wp, wp, wp, wp, wp, wp, wp, wp,empty,empty },
{ empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty},
{ empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty},
{ empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty},
{ empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty},
{ empty,empty, bp, bp, bp, bp, bp, bp, bp, bp,empty,empty },
{ empty,empty, br, bn, bb, bq, bk, bb, bn, br,empty,empty },
{ empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty},
{ empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty},
};
#define getxy(x,y) ( bigboard[(y)+2][(x)+2] )
#define setxy(x,y,p) ( bigboard[(y)+2][(x)+2] = (p) )
#define offboardp(x,y) ( ((x)&0xfff8) | ((y)&0xfff8) )
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
unsigned int movecount=0;
movelist *moves[2]={NULL,NULL};
int movedepth[2];
int knightdx[]={2, 2,-2,-2};
int knightdy[]={1,-1,-1, 1};
move bestarray[MAXDEPTH];
struct variables globals = { 1, 1, 1, 1, 8, 8, 1 };
int computercontrolled [] = { 0, 0 };
square bking = makesquare (4,7);
square wking = makesquare (4,0);
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* dont use dynamic memory for movelists */
#define STACKSIZE (unsigned)(1<<15)
movelist movestack[STACKSIZE];
char used[STACKSIZE];
int stackpointer=0;
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
#define newlist() (NULL)
movelist *newmovelist()
{
/*return malloc(sizeof(movelist));*/
int t = stackpointer;
int t2 = t;
while (used[t])
{
t++;
t %= STACKSIZE;
if (t2 == t)
{
printf("out of memory\n");
exit(255);
}
}
stackpointer = (t+1) % STACKSIZE;
used[t]=1;
return &movestack[t];
}
int meminuse()
{
int count, x;
count=0;
for(x=0;x<STACKSIZE;x++)
{
if (used[x])
count++;
}
return count;
}
void freemovelist(movelist *m)
{
/*free(m);*/
int t = (int) m - (int) movestack;
t /= sizeof(movelist);
used[t] = 0;
}
void push (movelist **list, move m)
{
movelist *new = newmovelist();
new->data = m;
new->next = *list;
*list = new;
return;
}
move pop (movelist **list)
{
movelist *temp=*list;
move m;
if (temp == NULL)
return NILMOVE;
m = temp->data;
*list = temp->next;
freemovelist(temp);
return m;
}
movelist *reverse(movelist *list)
{
movelist *res=newlist();
move m;
while(list)
{
m = pop(&list);
push(&res, m);
}
return res;
}
int length(movelist *list)
{
int c=0;
while(list)
{
c++;
list=list->next;
}
return c;
}
void deletelist(movelist *list)
{
movelist *t;
while (list)
{
t = list;
list=list->next;
freemovelist(t);
}
}
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
void printsquare (square s)
{
printf("(%d, %d) ", getx(s), gety(s));
}
void printmove (move m)
{
printsquare (gets1(m));
printsquare (gets2(m));
if (getpiece(m) <= NPIECES)
printf("%c", PIECECHAR( getpiece(m)) );
if ((m&(3<<30))==(3<<30))
printf(" ++");
else if (m&(1<<31))
printf(" +");
printf("\n");
}
/* print list in coordinate notation */
void printlist (movelist *list)
{
move m;
if (list == NULL)
return;
m = list->data;
printmove(m);
printlist (list->next);
}
/* print list in algebraic notation */
void printalist (movelist *list)
{
move m;
char *s;
int c=0;
while (list)
{
c++;
m = list->data;
s = algebraic(m);
printf("%s", s);
if (c%10 == 0) printf("\n");
free(s);
list = list->next;
}
if (c%10 != 0) printf("\n");
}
void printboardw()
{
int x,y;
printf(" a b c d e f g h \n");
printf(" +---+---+---+---+---+---+---+---+\n");
for (y=7;y>=0;y--)
{
printf("%d |", y+1);
for (x=0;x<=7;x++)
{
printf(" %c |", PIECECHAR( getxy (x, y) ) );
}
printf(" %d \n", y+1);
printf(" +---+---+---+---+---+---+---+---+\n");
}
printf(" a b c d e f g h \n");
fflush(stdout);
}
void printboardb()
{
int x,y;
printf(" h g f e d c b a \n");
printf(" +---+---+---+---+---+---+---+---+\n");
for (y=0;y<=7;y++)
{
printf("%d |", y+1);
for (x=7;x>=0;x--)
{
printf(" %c |", PIECECHAR( getxy (x, y) ) );
}
printf(" %d \n", y+1);
printf(" +---+---+---+---+---+---+---+---+\n");
}
printf(" h g f e d c b a \n");
fflush(stdout);
}
void printboard(int color)
{
if (color == WHITE)
printboardw();
else
printboardb();
}
void printsearchresults(int res, int color)
{
int x;
char *s;
int max = (color == WHITE) ? movedepth[0] : movedepth[1];
printf("\nSearch Outcome : Adv %d = ", res);
for (x=max; x>0 ;x--)
{
s = algebraic(bestarray[x]);
printf("%s", s);
free(s);
}
printf("\n");
return;
}
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
/* stuff to do before we can play our game */
void initialize ()
{
/* our movelist stack setup */
printf("Allocating %d MoveLists\n", sizeof(used) / sizeof(*used));
memset(used, 0, sizeof(used));
/* random seed */
srandom(time(0));
}
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
int getcolor (piece p)
{
if (p==0) return 0;
if (p<=wk) return WHITE;
return BLACK;
}
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
int clearpathp(square s1, square s2)
{
int x1, x2, y1, y2, dx, dy, x, y;
x1 = getx(s1);
x2 = getx(s2);
y1 = gety(s1);
y2 = gety(s2);
dx = x2-x1;
dy = y2-y1;
if (dy != 0)
{
if (dy > 0) dy = 1;
else dy = -1;
}
if (dx !=0)
{
if (dx > 0) dx = 1;
else dx = -1;
}
for (x=x1+dx, y=y1+dy; ; x+=dx, y+=dy)
{
if ( ( x == x2 ) && ( y == y2) )
return 1;
if ( offboardp (x, y) )
return 1;
if ( getxy(x, y) != empty )
return 0;
}
return 1; /* shouldnt get here though */
}
int pcheck(move m)
{
int color, direction, start;
int hor, ver;
int x1, x2, y1, y2;
x1 = getx(gets1(m));
x2 = getx(gets2(m));
y1 = gety(gets1(m));
y2 = gety(gets2(m));
hor = x2 - x1;
ver = y2 - y1;
direction = color = getcolor(getxy( x1, y1) );
start = (color == WHITE) ? 1 : 6;
/* can move forward one square */
if ( (getxy ( x2, y2) == empty) &&
(hor == 0) &&
(direction == ver) )
return 1;
/* move forward 2 squares on first move */
if ( (getxy ( x2, y2) == empty) &&
(getxy ( x2, y2 - direction) == empty) &&
(hor == 0) &&
(direction * 2 == ver) &&
( y1 == start))
return 1;
/* attack diagonally */
if ( (direction == ver) &&
(( hor == 1) || (hor == -1)) &&
(getcolor (getxy (x2, y2)) == -color))
return 1;
/* white enpassant */
if ( (direction == ver) &&
(( hor == 1) || (hor == -1)) &&
( y2 == 5 ) &&
( color == WHITE ) &&
( x2 == globals.wenpassant ) )
return 1;
/* black enpassant */
if ( (direction == ver) &&
(( hor == 1) || (hor == -1)) &&
( y2 == 2 ) &&
( color == BLACK ) &&
( x2 == globals.benpassant ) )
return 1;
return 0;
}
int ncheck(move m)
{
int hor = getx(gets2(m)) - getx(gets1(m));
int ver = gety(gets2(m)) - gety(gets1(m));
if ( (abs(hor) == 2) && ( abs(ver) == 1))
return 1;
if ( (abs(ver) == 2) && ( abs(hor) == 1))
return 1;
return 0;
}
int bcheck(move m)
{
int hor = getx(gets2(m)) - getx(gets1(m));
int ver = gety(gets2(m)) - gety(gets1(m));
if ( ( ( hor == ver ) || ( hor == -ver ) ) &&
( clearpathp (gets1(m), gets2(m)) ))
return 1;
return 0;
}
int rcheck(move m)
{
int hor = getx(gets2(m)) - getx(gets1(m));
int ver = gety(gets2(m)) - gety(gets1(m));
if ( (hor == 0) && (clearpathp(gets1(m), gets2(m)) ) )
return 1;
if ( (ver == 0) && (clearpathp(gets1(m), gets2(m)) ) )
return 1;
return 0;
}
int qcheck(move m)
{
return (rcheck(m)||bcheck(m));
}
int kcheck(move m)
{
int hor = getx(gets2(m)) - getx(gets1(m));
int ver = gety(gets2(m)) - gety(gets1(m));
if ( ( abs(hor) <= 1 ) && ( abs(ver) <= 1 ) )
return 1;
/* white o-o */
if ( (moveequal ( m , whiteoo )) &&
(clearpathp ( gets1(m), makesquare(7,0))) &&
(globals.wcanoo) &&
(!incheckp (WHITE) ) &&
(!checkchecker( wthruoo, WHITE)) )
return 1;
/* white o-o-o */
if ( (moveequal ( m , whiteooo )) &&
(clearpathp ( gets1(m), makesquare(0,0) )) &&
(globals.wcanooo) &&
(!incheckp (WHITE) ) &&
(!checkchecker( wthruooo, WHITE)) )
return 1;
/* black o-o */
if ( (moveequal ( m , blackoo )) &&
(clearpathp ( gets1(m), makesquare(7,7) )) &&
(globals.bcanoo) &&
(!incheckp (BLACK) ) &&
(!checkchecker( bthruoo, BLACK)) )
return 1;
/* black o-o-o */
if ( (moveequal ( m , blackooo )) &&
(clearpathp ( gets1(m), makesquare(0,7) )) &&
(globals.bcanooo) &&
(!incheckp (BLACK) ) &&
(!checkchecker( bthruooo, BLACK )) )
return 1;
return 0;
}
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
int checkmatep(int color)
{
return (incheckp(color) && (mobility(color)==0));
}
int stalematep(int color)
{
return (!incheckp(color) && (mobility(color)==0));
}
int gameoverp ()
{
movelist *list;
if ( globals. turn == WHITE )
{
if (checkmatep (WHITE))
{
list = validmoves(WHITE);
if (list != NULL)
{
deletelist(list);
return 0;
}
printf ("white checkmated\n");
return 1;
}
if (stalematep (WHITE))
{
list = validmoves(WHITE);
if (list != NULL)
{
deletelist(list);
return 0;
}
printf ("white stalemated\n");
return 1;
}
if (incheckp (WHITE))
{
printf ("white in check\n");
}
}
if ( globals.turn == BLACK)
{
if (checkmatep (BLACK))
{
list = validmoves(BLACK);
if (list != NULL)
{
deletelist(list);
return 0;
}
printf("black checkmated\n");
return 1;
}
if (stalematep (BLACK))
{
list = validmoves(BLACK);
if (list != NULL)
{
deletelist(list);
return 0;
}
printf("black stalemated\n");
return 1;
}
if (incheckp (BLACK) )
{
printf("black in check\n");
}
}
return 0;
}
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
movelist *pmoves(square loc)
{
int color = getcolor(getxy(getx(loc), gety(loc)));
int x = getx(loc);
int direction = color;
int y = gety(loc);
int start = (color == WHITE) ? 1 : 6;
movelist *res = newlist();
if (getxy(x, y + direction) == empty)
{
push(&res, makemove(loc, makesquare(x, y+direction)));
}
if ((getxy(x, y+2*direction) == empty) &&
(y == start)&&
(getxy(x, y+direction) == empty))
{
push(&res, makemove(loc, makesquare(x,y+2*direction)));
}
if (getcolor (getxy (x+1, y+direction)) == -color)
push(&res, makemove(loc, makesquare(x+1,y+direction)));
if (getcolor (getxy (x-1, y+direction)) == -color)
push(&res, makemove(loc, makesquare(x-1,y+direction)));
if ( (y+1==5) &&
( color == WHITE ) &&
( x+1 == globals.wenpassant ))
push(&res, makemove(loc, makesquare(x+1,y+direction)));
if (( y-1 == 2 ) &&
( color == BLACK ) &&
( x-1 == globals.benpassant ) )
push(&res, makemove(loc, makesquare(x-1,y+direction)));
return res;
}
movelist *nmoves(square loc)
{
movelist *list = newlist();
int x = getx(loc);
int y = gety(loc);
int i;
int x2,y2,color=getcolor(getxy(x,y));
move m;
for (i=0;i<4;i++)
{
x2 = x+knightdx[i];
y2 = y+knightdy[i];
if (!offboardp(x2,y2))
{
m = makemove(loc, makesquare(x2,y2));
if (getcolor(getxy(x2,y2))!=color&&
!checkchecker(m,color))
push(&list, makemove ( loc, makesquare ( x2, y2 ) ) );
}
x2 = x+knightdy[i];
y2 = y+knightdx[i];
if (!offboardp(x2,y2))
{
m = makemove(loc, makesquare(x2,y2));
if (getcolor(getxy(x2,y2))!=color&&
!checkchecker(m,color))
push(&list, makemove ( loc, makesquare ( x2, y2 ) ) );
}
}
return list;
}
movelist *bmoves(square loc)
{
movelist *list = newlist();
int s1, s2, s3, s4;
int x, y, dx, dy;
int color = getcolor(getxy(getx(loc),gety(loc)));
int c1, c2, c3, c4;
s1 = s2 = s3 = s4 = 1;
x = getx(loc);
y = gety(loc);
for (dx = 1, dy = 1; ; dx++, dy++)
{
if (offboardp (x + dx, y + dy))
s1 = 0;
if (offboardp (x + dx, y - dy))
s2 = 0;
if (offboardp (x - dx, y + dy))
s3 = 0;
if (offboardp (x - dx, y - dy))
s4 = 0;
if ( (s1 == s2) && (s2 == s3) && (s3 == s4) && (s4 == 0) )
break;
c1 = getcolor(getxy(x+dx, y+dy));
c2 = getcolor(getxy(x+dx, y-dy));
c3 = getcolor(getxy(x-dx, y+dy));
c4 = getcolor(getxy(x-dx, y-dy));
if (s1)
{
if (c1 == color)
s1 = 0;
else if (c1 == 0)
push(&list, makemove(loc, makesquare(x+dx,y+dy)));
else if (c1 == -color)
{
push(&list, makemove(loc, makesquare(x+dx,y+dy)));
s1 = 0;
}
}
if (s2)
{
if (c2 == color)
s2 = 0;
else if (c2 == 0)
push(&list, makemove(loc, makesquare(x+dx,y-dy)));
else if (c2 == -color)
{
push(&list, makemove(loc, makesquare(x+dx,y-dy)));
s2 = 0;
}
}
if (s3)
{
if (c3 == color)
s3 = 0;
else if (c3 == 0)
push(&list, makemove(loc, makesquare(x-dx,y+dy)));
else if (c3 == -color)
{
push(&list, makemove(loc, makesquare(x-dx,y+dy)));
s3 = 0;
}
}
if (s4)
{
if (c4 == color)
s4 = 0;
else if (c4 == 0)
push(&list, makemove(loc, makesquare(x-dx,y-dy)));
else if (c4 == -color)
{
push(&list, makemove(loc, makesquare(x-dx,y-dy)));
s4 = 0;
}
}
}
return list;
}
movelist *rmoves(square loc)
{
movelist *list = newlist();
int s1, s2, s3, s4;
int x, y, dx, dy;
int color = getcolor(getxy(getx(loc),gety(loc)));
int c1, c2, c3, c4;
s1 = s2 = s3 = s4 = 1;
x = getx(loc);
y = gety(loc);
for (dx = 1, dy = 1; ; dx++, dy++)
{
if (offboardp (x + dx, y))
s1 = 0;
if (offboardp (x - dx, y))
s2 = 0;
if (offboardp (x, y + dy))
s3 = 0;
if (offboardp (x, y - dy))
s4 = 0;
if ( (s1 == s2) && (s2 == s3) && (s3 == s4) && (s4 == 0) )
break;
c1 = getcolor(getxy(x+dx, y));
c2 = getcolor(getxy(x-dx, y));
c3 = getcolor(getxy(x, y+dy));
c4 = getcolor(getxy(x, y-dy));
if (s1)
{
if (c1 == color)
s1 = 0;
else if (c1 == 0)
push(&list, makemove(loc, makesquare(x+dx,y)));
else if (c1 == -color)
{
push(&list, makemove(loc, makesquare(x+dx,y)));
s1 = 0;
}
}
if (s2)
{
if (c2 == color)
s2 = 0;
else if (c2 == 0)
push(&list, makemove(loc, makesquare(x-dx,y)));
else if (c2 == -color)
{
push(&list, makemove(loc, makesquare(x-dx,y)));
s2 = 0;
}
}
if (s3)
{
if (c3 == color)
s3 = 0;
else if (c3 == 0)
push(&list, makemove(loc, makesquare(x,y+dy)));
else if (c3 == -color)
{
push(&list, makemove(loc, makesquare(x,y+dy)));
s3 = 0;
}
}
if (s4)
{
if (c4 == color)
s4 = 0;
else if (c4 == 0)
push(&list, makemove(loc, makesquare(x,y-dy)));
else if (c4 == -color)
{
push(&list, makemove(loc, makesquare(x,y-dy)));
s4 = 0;
}
}
}
return list;
}
movelist *qmoves(square loc)
{
movelist *list1, *list2;
list1 = bmoves(loc);
list2 = rmoves(loc);
while (list2)
{
push(&list1, pop(&list2));
}
return list1;
}
movelist *kmoves(square loc)
{
int color = getcolor(getxy(getx(loc), gety(loc)));
movelist *list = newlist();
int x = getx(loc);
int y = gety(loc);
int dx, dy;
move m;
for (dx=-1;dx<=1;dx++)
{
for (dy=-1;dy<=1;dy++)
{
if (x+dx==y+dy) continue;
m = makemove(loc, makesquare(x+dx,y+dy));
if (!offboardp(x+dx,y+dy)&&
!checkchecker(m,color)&&
getcolor(getxy(x+dx,y+dy))!=color)
push(&list, m);
}
}
if ((color==WHITE)&&squareequal(loc, gets1(whiteoo)))
{
if (globals.wcanoo&&
!incheckp(WHITE)&&
!checkchecker( wthruoo, WHITE)&&
clearpathp ( loc, makesquare(7,0)))
push(&list, whiteoo);
if (globals.wcanooo&&
!incheckp(WHITE)&&
!checkchecker( wthruooo, WHITE)&&
clearpathp ( loc, makesquare(0,0)))
push(&list, whiteooo);
}
if ((color==BLACK)&&squareequal(loc, gets1(blackoo)))
{
if (globals.bcanoo&&
clearpathp ( loc, makesquare(7,7)) &&
!incheckp(BLACK)&&
!checkchecker( bthruoo, BLACK ))
push(&list, blackoo);
if (globals.bcanooo&&
clearpathp ( loc, makesquare(0,7) ) &&
!incheckp(BLACK)&&
!checkchecker( bthruooo, BLACK ))
push(&list, blackooo);
}
return list;
}
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */
int pmovecount(square loc)
{
int color = getcolor(getxy(getx(loc), gety(loc)));
int x = getx(loc);
int direction = color;
int y = gety(loc);
int start = (color == WHITE) ? 1 : 6;
int res = 0;
move m;
m=makemove(loc, makesquare(x,y+direction));
if ((getxy(x, y + direction) == empty)&&
!checkchecker(m,color))
{
res ++;
}
m=makemove(loc, makesquare(x,y+2*direction));
if ((getxy(x, y+2*direction) == empty) &&
(y == start)&&
(getxy(x, y+direction) == empty)&&
!checkchecker(m,color))
{
res ++;
}
if (!offboardp(x+1,y+direction))
{
m=makemove(loc, makesquare(x+1,y+direction));
if ((getcolor (getxy (x+1, y+direction)) == -color)&&