forked from Thorium/webchess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.inc
1317 lines (1140 loc) · 45.7 KB
/
chess.inc
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
<?php
// $Id: chess.inc,v 1.2 2004/11/09 09:32:16 dadij Exp $
# These php3 scripts are freeware.
# That means, you may use and change it freely for any purpose.
# However, please send an email to the author
# (http://www.mailchess.de/chessphpfeedback.html)
# if you use this script in any way.
# This software is provided 'as-is',
# without any express or implied warranty.
# In no event the author will be held liable
# for any damages arising from the use of this software.
# Please send comments or bug reports to http://www.mailchess.de/chessphpfeedback.html
# These variables are set before a move is verified or executed
$nkmove_from_Square = 0;
$nkmove_to_Square = 0;
$nkmove_promotion_figur = 0;
# These variables are set after each execution of a move
# They describe the status of position
$nkmove_ep_Square = 0;
$nkmove_turn = 1;
$nkmove_castling_ws = TRUE;
$nkmove_castling_wl = TRUE;
$nkmove_castling_bs = TRUE;
$nkmove_castling_bl = TRUE;
$nkmove_PlyNumber = 0;
# Variables for the move list
# Only used in move_count
$nkmove_list_from = array(200);
$nkmove_list_to = array(200);
$nkmove_list_count = 0;
# For creating the notation
$nkpgn_piece = "";
$nkpgn_from_digit = "";
$nkpgn_from_letter = "";
$nkpgn_capture = FALSE;
$nkpgn_square_list = array(8); // For writing Sge2 (if two or more pieces can enter a square)
# For handling of result when mate or so
$nkState = 0;
# How a piece can move
$nkab_Bishop = array(-9, -11, 9, 11);
$nkab_Rook = array(-1, 10, 1, -10);
$nkab_Knight = array(19, 21, 12, -8, -19, -21, -12, 8);
$nkab_Queen = array(-9, -11, 9, 11, -1, 10, 1, -10);
$nkab_King = array(-9, -11, 9, 11, -1, 10, 1, -10);
# Constants for get_GameState()
define("gsRunning" , 0);
define("gsMate" , 1);
define("gsStalemate" , 2);
define("gsCheck" , 3);
# The values of the pieces
define("WK" , 6); define("BK" , -6);
define("WQ" , 5); define("BQ" , -5);
define("WR" , 2); define("BR" , -2);
define("WB" , 3); define("BB" , -3);
define("WN" , 4); define("BN" , -4);
define("WP" , 1); define("BP" , -1);
# The values of the squares
define("sq_a1",21);define("sq_b1",22);define("sq_c1",23);define("sq_d1",24);
define("sq_a2",31);define("sq_b2",32);define("sq_c2",33);define("sq_d2",34);
define("sq_a3",41);define("sq_b3",42);define("sq_c3",43);define("sq_d3",44);
define("sq_a4",51);define("sq_b4",52);define("sq_c4",53);define("sq_d4",54);
define("sq_a5",61);define("sq_b5",62);define("sq_c5",63);define("sq_d5",64);
define("sq_a6",71);define("sq_b6",72);define("sq_c6",73);define("sq_d6",74);
define("sq_a7",81);define("sq_b7",82);define("sq_c7",83);define("sq_d7",84);
define("sq_a8",91);define("sq_b8",92);define("sq_c8",93);define("sq_d8",94);
define("sq_e1",25);define("sq_f1",26);define("sq_g1",27);define("sq_h1",28);
define("sq_e2",35);define("sq_f2",36);define("sq_g2",37);define("sq_h2",38);
define("sq_e3",45);define("sq_f3",46);define("sq_g3",47);define("sq_h3",48);
define("sq_e4",55);define("sq_f4",56);define("sq_g4",57);define("sq_h4",58);
define("sq_e5",65);define("sq_f5",66);define("sq_g5",67);define("sq_h5",68);
define("sq_e6",75);define("sq_f6",76);define("sq_g6",77);define("sq_h6",78);
define("sq_e7",85);define("sq_f7",86);define("sq_g7",87);define("sq_h7",88);
define("sq_e8",95);define("sq_f8",96);define("sq_g8",97);define("sq_h8",98);
# The board variable
# It is surrounded by "edge values"; empty squares are 0
$nkfirst_Position = array(
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
100,WR ,WN ,WB ,WQ ,WK ,WB ,WN ,WR ,100,
100,WP ,WP ,WP ,WP ,WP ,WP ,WP ,WP ,100,
100,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,100,
100,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,100,
100,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,100,
100,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,100,
100,BP ,BP ,BP ,BP ,BP ,BP ,BP ,BP ,100,
100,BR ,BN ,BB ,BQ ,BK ,BB ,BN ,BR ,100,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100);
$nkempty_Position = array(
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,
100,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,100,
100,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,100,
100,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,100,
100,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,100,
100,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,100,
100,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,100,
100,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,100,
100,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,100,
100,100,100,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100);
$nkBoard = array(120);
$nkNotation = "";
$pgn_Move = ""; // last move is inserted into the mail separately
# Only for debugging
function print_Position() {
global $nkBoard;
for ($nki = 11; $nki >= 0; $nki--) {
for ($nkj = 0; $nkj <= 9; $nkj++) {
$nkk = $nkBoard[$nki * 10 + $nkj];
if ($nkk != 100) {
if ($nkk == 0)
echo "--";
else if ($nkk > 0)
echo "+" . $nkk;
else
echo $nkk;
echo "|";
}
}
echo "<br>\n";
}
}
# help function for creating notation
function get_pgn_piece($nkaPiece) {
switch (Abs($nkaPiece)) {
case WK: return "K";
case WQ: return "Q";
case WR: return "R";
case WB: return "B";
case WN: return "N";
case WP: return "";
}
return "X";
}
# 0..63 to 21..98
function c0_21($nki) {
return ($nki >> 3) * 10 + ((($nki + 8) % 8)) + 21;
}
# 21..98 to 0..63
function c21_0($nki) {
return ((floor($nki / 10) - 2) << 3) + (($nki % 10) - 1);
}
# The following functions search the corresponding piece what can move to $nkfrom
# Used for checking whether a square is attacked
function search_Queen($nkfrom, $nkturn) {
global $nkBoard, $nkab_Queen, $nkpgn_square_list;
$nkretVal = 0;
for ($nki = 0; $nki <= 7; $nki++) {
$nkj = $nkfrom;
while (TRUE) {
$nkj = $nkj + $nkab_Queen[$nki];
if ($nkBoard[$nkj] == 100)
break;
if ($nkBoard[$nkj] == $nkturn * WQ) {
$nkpgn_square_list[$nkretVal] = $nkj;
$nkretVal++;
}
if ($nkBoard[$nkj] != 0)
break;
}
}
return $nkretVal;
}
function search_Rook($nkfrom, $nkturn) {
global $nkBoard, $nkab_Rook, $nkpgn_square_list;
$nkretVal = 0;
for ($nki = 0; $nki <= 3; $nki++) {
$nkj = $nkfrom;
while (TRUE) {
$nkj = $nkj + $nkab_Rook[$nki];
if ($nkBoard[$nkj] == 100)
break;
if ($nkBoard[$nkj] == $nkturn * WR) {
$nkpgn_square_list[$nkretVal] = $nkj;
$nkretVal++;
}
if ($nkBoard[$nkj] != 0)
break;
}
}
return $nkretVal;
}
function search_Bishop($nkfrom, $nkturn) {
global $nkBoard, $nkab_Bishop, $nkpgn_square_list;
$nkretVal = 0;
for ($nki = 0; $nki <= 3; $nki++) {
$nkj = $nkfrom;
while (TRUE) {
$nkj = $nkj + $nkab_Bishop[$nki];
if ($nkBoard[$nkj] == 100)
break;
if ($nkBoard[$nkj] == $nkturn * WB) {
$nkpgn_square_list[$nkretVal] = $nkj;
$nkretVal++;
}
if ($nkBoard[$nkj] != 0)
break;
}
}
return $nkretVal;
}
function search_Knight($nkfrom, $nkturn) {
global $nkBoard, $nkab_Knight, $nkpgn_square_list;
$nkretVal = 0;
for ($nki = 0; $nki <= 7; $nki++) {
$nkj = $nkfrom;
$nkj = $nkj + $nkab_Knight[$nki];
if ($nkBoard[$nkj] == $nkturn * WN) {
$nkpgn_square_list[$nkretVal] = $nkj;
$nkretVal++;
}
}
return $nkretVal;
}
function search_King($nkfrom, $nkturn) {
global $nkBoard, $nkab_King, $nkpgn_square_list;
$nkretVal = 0;
for ($nki = 0; $nki <= 7; $nki++) {
$nkj = $nkfrom;
$nkj = $nkj + $nkab_King[$nki];
if ($nkBoard[$nkj] == $nkturn * WK) {
$nkpgn_square_list[$nkretVal] = $nkj;
$nkretVal++;
}
}
return $nkretVal;
}
function search_capturePawn($nkfrom, $nkturn) {
global $nkBoard, $nkpgn_square_list;
$nkretVal = 0;
if ($nkBoard[$nkfrom + (-$nkturn * 9 )] == $nkturn * WP) {
$nkpgn_square_list[$nkretVal] = $nkj;
$nkretVal++;
}
if ($nkBoard[$nkfrom + (-$nkturn * 11)] == $nkturn * WP) {
$nkpgn_square_list[$nkretVal] = $nkj;
$nkretVal++;
}
return $nkretVal;
}
function search_Pawn($nkfrom, $nkturn) {
global $nkBoard, $nkpgn_square_list;
$nkretVal = 0;
if ($nkBoard[$nkfrom + (-$nkturn * 10)] == $nkturn * WP) {
$nkpgn_square_list[$nkretVal] = $nkj;
$nkretVal++;
}
else if ($nkBoard[$nkfrom + (-$nkturn * 10)] == 0)
if (
(($nkturn == 1) && (($nkfrom > sq_h3) && ($nkfrom < sq_a5))) ||
(($nkturn != 1) && (($nkfrom > sq_h4) && ($nkfrom < sq_a6)))
)
if ($nkBoard[$nkfrom + (-$nkturn * 20)] == $nkturn * WP) {
$nkpgn_square_list[$nkretVal] = $nkj;
$nkretVal++;
}
return $nkretVal;
}
# Check whether the king is attacked
# The turn is changed and it is looked for a opponent's piece what is able to reach this square
function attacked($nkfrom, $nkturn) {
if (
(search_King($nkfrom, -$nkturn) != 0)
|| (search_Queen($nkfrom, -$nkturn) != 0)
|| (search_Rook($nkfrom, -$nkturn) != 0)
|| (search_Bishop($nkfrom, -$nkturn) != 0)
|| (search_Knight($nkfrom, -$nkturn) != 0)
|| (search_CapturePawn($nkfrom, -$nkturn) != 0)
)
return TRUE;
else
return FALSE;
}
# A "free" square may not be a edge value (100) or a square where an own piece is placed
function is_Square_free($nkSquare, $nkturn) {
global $nkBoard;
if ($nkBoard[$nkSquare] == 100)
return FALSE;
if (
(($nkturn == 1) && ($nkBoard[$nkSquare] > 0)) ||
(($nkturn != 1) && ($nkBoard[$nkSquare] < 0))
)
return FALSE;
return TRUE;
}
# Adds a move to the list
function Move_to_List($nkfrom, $nkto) {
global $nkmove_list_count, $nkmove_list_to, $nkmove_list_from;
$nkmove_list_from[$nkmove_list_count] = $nkfrom;
$nkmove_list_to[$nkmove_list_count] = $nkto;
$nkmove_list_count++;
}
# The following functions check the squares where a piece from a certain square can move to
# They are added to the move list (it must still be verified whether the move is legal)
# If a $nkto is a legal square (instead of 0), the return value is TRUE if the move list contains this square
function get_QueenSquares($nkfrom, $nkturn, $nkto) {
global $nkBoard, $nkab_Queen;
for ($nki = 0; $nki <= 7; $nki++) {
$nkj = $nkfrom;
while (TRUE) {
$nkj = $nkj + $nkab_Queen[$nki];
if (!is_Square_free($nkj, $nkturn))
break;
Move_to_List($nkfrom, $nkj);
if ($nkj == $nkto)
return TRUE;
if ($nkBoard[$nkj] != 0)
break;
}
}
return FALSE;
}
function get_RookSquares($nkfrom, $nkturn, $nkto) {
global $nkBoard, $nkab_Rook;
for ($nki = 0; $nki <= 3; $nki++) {
$nkj = $nkfrom;
while (TRUE) {
$nkj = $nkj + $nkab_Rook[$nki];
if (!is_Square_free($nkj, $nkturn))
break;
Move_to_List($nkfrom, $nkj);
if ($nkj == $nkto)
return TRUE;
if ($nkBoard[$nkj] != 0)
break;
}
}
return FALSE;
}
function get_BishopSquares($nkfrom, $nkturn, $nkto) {
global $nkBoard, $nkab_Bishop;
for ($nki = 0; $nki <= 3; $nki++) {
$nkj = $nkfrom;
while (TRUE) {
$nkj = $nkj + $nkab_Bishop[$nki];
if (!is_Square_free($nkj, $nkturn))
break;
Move_to_List($nkfrom, $nkj);
if ($nkj == $nkto)
return TRUE;
if ($nkBoard[$nkj] != 0)
break;
}
}
return FALSE;
}
function get_KnightSquares($nkfrom, $nkturn, $nkto) {
global $nkab_Knight;
for ($nki = 0; $nki <= 7; $nki++) {
$nkj = $nkfrom;
$nkj = $nkj + $nkab_Knight[$nki];
if (is_Square_free($nkj, $nkturn)) {
Move_to_List($nkfrom, $nkj);
if ($nkj == $nkto)
return TRUE;
}
}
return FALSE;
}
function get_KingSquares($nkfrom, $nkturn, $nkto) {
global $nkBoard, $nkab_King;
global $nkmove_castling_ws, $nkmove_castling_wl, $nkmove_castling_bs, $nkmove_castling_bl;
for ($nki = 0; $nki <= 7; $nki++) {
$nkj = $nkfrom;
$nkj = $nkj + $nkab_King[$nki];
if (is_Square_free($nkj, $nkturn)) {
Move_to_List($nkfrom, $nkj);
if ($nkj == $nkto)
return TRUE;
}
}
# Additionally check castling possibilities
if ($nkturn == 1) {
if ($nkmove_castling_ws) {
if (
(($nkBoard[sq_e1] == WK) && ($nkBoard[sq_h1] == WR))
&& (($nkBoard[sq_f1] == 0) && (!attacked(sq_f1, $nkturn)))
&& (($nkBoard[sq_g1] == 0) && (!attacked(sq_g1, $nkturn)))
&& (!attacked(sq_e1, $nkturn))
) {
Move_to_List(sq_e1, sq_g1);
if (sq_g1 == $nkto)
return TRUE;
}
}
if ($nkmove_castling_wl) {
if (
(($nkBoard[sq_e1] == WK) && ($nkBoard[sq_a1] == WR))
&& ($nkBoard[sq_b1] == 0)
&& (($nkBoard[sq_d1] == 0) && (!attacked(sq_d1, $nkturn)))
&& (($nkBoard[sq_c1] == 0) && (!attacked(sq_c1, $nkturn)))
&& (!attacked(sq_e1, $nkturn))
) {
Move_to_List(sq_e1, sq_c1);
if (sq_c1 == $nkto)
return TRUE;
}
}
}
else {
if ($nkmove_castling_bs) {
if (
(($nkBoard[sq_e8] == BK) && ($nkBoard[sq_h8] == BR))
&& (($nkBoard[sq_f8] == 0) && (!attacked(sq_f8, $nkturn)))
&& (($nkBoard[sq_g8] == 0) && (!attacked(sq_g8, $nkturn)))
&& (!attacked(sq_e8, $nkturn))
) {
Move_to_List(sq_e8, sq_g8);
if (sq_g8 == $nkto)
return TRUE;
}
}
if ($nkmove_castling_bl) {
if (
(($nkBoard[sq_e8] == BK) && ($nkBoard[sq_a8] == BR))
&& ($nkBoard[sq_b8] == 0)
&& (($nkBoard[sq_d8] == 0) && (!attacked(sq_d8, $nkturn)))
&& (($nkBoard[sq_c8] == 0) && (!attacked(sq_c8, $nkturn)))
&& (!attacked(sq_e8, $nkturn))
) {
Move_to_List(sq_e8, sq_c8);
if (sq_c8 == $nkto)
return TRUE;
}
}
}
return FALSE;
}
function get_PawnSquares($nkfrom, $nkturn, $nkto) {
global $nkBoard, $nkmove_ep_Square;
# Check the square in front of the pawn
if ($nkBoard[$nkfrom + ($nkturn * 10)] == 0) {
Move_to_List($nkfrom, $nkfrom + ($nkturn * 10));
if ($nkfrom + ($nkturn * 10) == $nkto)
return TRUE;
# If the pawn is located on the second rank, check the next square too
if (
(($nkturn == 1) && (($nkfrom > sq_h1) && ($nkfrom < sq_a3)))
|| (($nkturn <> 1) && (($nkfrom > sq_h6) && ($nkfrom < sq_a8)))
)
if ($nkBoard[$nkfrom + ($nkturn * 20)] == 0) {
Move_to_List($nkfrom, $nkfrom + ($nkturn * 20));
if ($nkfrom + ($nkturn * 20) == $nkto)
return TRUE;
}
}
# Check capture possibilities
if ($nkturn == 1) {
if (
(($nkBoard[$nkfrom + ($nkturn * 9)] != 100) && ($nkBoard[$nkfrom + ($nkturn * 9)] < 0))
|| ($nkfrom + ($nkturn * 9) == $nkmove_ep_Square)
) {
Move_to_List($nkfrom, $nkfrom + ($nkturn * 9));
if ($nkfrom + ($nkturn * 9) == $nkto)
return TRUE;
}
if (
(($nkBoard[$nkfrom + ($nkturn * 11)] != 100) && ($nkBoard[$nkfrom + ($nkturn * 11)] < 0))
|| ($nkfrom + ($nkturn * 11) == $nkmove_ep_Square)
) {
Move_to_List($nkfrom, $nkfrom + ($nkturn * 11));
if ($nkfrom + ($nkturn * 11) == $nkto)
return TRUE;
}
}
else {
if (
(($nkBoard[$nkfrom + ($nkturn * 9)] != 100) && ($nkBoard[$nkfrom + ($nkturn * 9)] > 0))
|| ($nkfrom + ($nkturn * 9) == $nkmove_ep_Square)
) {
Move_to_List($nkfrom, $nkfrom + ($nkturn * 9));
if ($nkfrom + ($nkturn * 9) == $nkto)
return TRUE;
}
if (
(($nkBoard[$nkfrom + ($nkturn * 11)] != 100) && ($nkBoard[$nkfrom + ($nkturn * 11)] > 0))
|| ($nkfrom + ($nkturn * 11) == $nkmove_ep_Square)
) {
Move_to_List($nkfrom, $nkfrom + ($nkturn * 11));
if ($nkfrom + ($nkturn * 11) == $nkto)
return TRUE;
}
}
return FALSE;
}
# Returns the king's place
function get_King($nkwhite) {
global $nkBoard;
if ($nkwhite)
$nkj = WK;
else
$nkj = BK;
for ($nki = 0; $nki <= 119; $nki++) {
if ($nkBoard[$nki] == $nkj)
return $nki;
}
return -1;
}
# Checks whether a move is legal according to the set move variables
function is_Move_possible() {
global $nkBoard, $nkmove_list_count, $nkmove_turn, $nkmove_ep_Square, $nkmove_to_Square;
global $nkmove_from_Square, $nkpgn_piece, $nkpgn_from_digit, $nkpgn_from_letter, $nkpgn_capture, $nkpgn_square_list;
# Legal from square?
$nkf = $nkBoard[$nkmove_from_Square];
if (($nkf == 0) || ($nkf == 100))
return FALSE;
# The right colored piece on it?
if (
(($nkmove_turn == 1) && ($nkf < 0)) ||
(($nkmove_turn != 1) && ($nkf > 0))
)
return FALSE;
# Get the to squares of the corresponding piece
# The function returns TRUE if our to_square fits with one of them
$nkf = Abs($nkf);
$nkb = FALSE;
$nkmove_list_count = 0;
if ($nkf == WK)
$nkb = get_KingSquares($nkmove_from_Square, $nkmove_turn, $nkmove_to_Square);
else if ($nkf == WQ)
$nkb = get_QueenSquares($nkmove_from_Square, $nkmove_turn, $nkmove_to_Square);
else if ($nkf == WR)
$nkb = get_RookSquares($nkmove_from_Square, $nkmove_turn, $nkmove_to_Square);
else if ($nkf == WB)
$nkb = get_BishopSquares($nkmove_from_Square, $nkmove_turn, $nkmove_to_Square);
else if ($nkf == WN)
$nkb = get_KnightSquares($nkmove_from_Square, $nkmove_turn, $nkmove_to_Square);
else if ($nkf == WP)
$nkb = get_PawnSquares($nkmove_from_Square, $nkmove_turn, $nkmove_to_Square);
# Set piece to the new square and check whether own king is attacked
if ($nkb) {
$nkrv_f = $nkBoard[$nkmove_from_Square];
$nkrn_f = $nkBoard[$nkmove_to_Square];
$nkrep_f = $nkBoard[$nkmove_to_Square + (-$nkmove_turn * 10)];
$nkBoard[$nkmove_to_Square] = $nkBoard[$nkmove_from_Square];
$nkBoard[$nkmove_from_Square] = 0;
if (($nkf == WP) && ($nkmove_ep_Square == $nkmove_to_Square))
$nkBoard[$nkmove_to_Square + (-$nkmove_turn * 10)] = 0;
$nkRetVal = false;
$nki = get_King($nkmove_turn == 1);
if ($nki != -1)
$nkRetVal = !attacked($nki, $nkmove_turn);
$nkBoard[$nkmove_from_Square] = $nkrv_f;
$nkBoard[$nkmove_to_Square] = $nkrn_f;
$nkBoard[$nkmove_to_Square + (-$nkmove_turn * 10)] = $nkrep_f;
$nkpgn_piece = ""; // for adding a move to pgn notation
$nkpgn_from_digit = "";
$nkpgn_from_letter = "";
$nkpgn_capture = FALSE;
if ($nkRetVal) {
$nkpgn_piece = get_pgn_piece($nkBoard[$nkmove_from_Square]);
$nkpgn_capture = ($nkBoard[$nkmove_to_Square] != 0) || (($nkf == WP) && ($nkmove_ep_Square == $nkmove_to_Square));
$nki = 1;
switch (Abs($nkBoard[$nkmove_from_Square])) {
case WQ: $nki = search_Queen($nkmove_to_Square, $nkmove_turn);
break;
case WR: $nki = search_Rook($nkmove_to_Square, $nkmove_turn);
break;
case WB: $nki = search_Bishop($nkmove_to_Square, $nkmove_turn);
break;
case WN: $nki = search_Knight($nkmove_to_Square, $nkmove_turn);
break;
}
if ($nki > 1) {
$nkhl = c21_0($nkmove_from_Square) % 8;
$nkhr = floor(c21_0($nkmove_from_Square) / 8);
for ($nkj = 0; $nkj < $nki; $nkj++) {
if ($nkmove_from_Square != $nkpgn_square_list[$nkj]) {
$nkfile = c21_0($nkpgn_square_list[$nkj]) % 8;
$nkrank = floor(c21_0($nkpgn_square_list[$nkj]) / 8);
if ($nkhl == $nkfile)
$nkpgn_from_digit = floor(c21_0($nkmove_from_Square) / 8) + 1;
if ($nkhr == $nkrank)
$nkpgn_from_letter = chr((c21_0($nkmove_from_Square) % 8) + 97);
}
}
# neither on the same file nor the same rank (knight)
if ($nkpgn_from_digit == "")
$nkpgn_from_letter = chr((c21_0($nkmove_from_Square) % 8) + 97);
}
}
return $nkRetVal;
}
return FALSE;
}
# Execute a move according to the set move variables
# Sets ep-square and castling rights
# Changes the turn
function execute_Move() {
global $nkBoard, $nkmove_turn, $nkmove_promotion_figur, $nkmove_ep_Square, $nkmove_to_Square, $nkmove_from_Square;
global $nkmove_castling_ws, $nkmove_castling_wl, $nkmove_castling_bs, $nkmove_castling_bl, $nkpgn_castling, $nkmove_PlyNumber;
# Set from- and to-square
$nkBoard[$nkmove_to_Square] = $nkBoard[$nkmove_from_Square];
$nkPiece = Abs($nkBoard[$nkmove_from_Square]);
$nkBoard[$nkmove_from_Square] = 0;
$nkpgn_castling = "";
$nkep = $nkmove_ep_Square;
$nkmove_ep_Square = 0;
if ($nkPiece == WP) {
# if to-square is ep-square, remove pawn above
if ($nkep == $nkmove_to_Square)
$nkBoard[$nkmove_to_Square + (-$nkmove_turn * 10)] = 0;
# if promotion, set new piece
if ($nkmove_promotion_figur != 0)
$nkBoard[$nkmove_to_Square] = $nkmove_promotion_figur;
# set new ep_square if double step and an opponent's pawn is placed on the nearby square
if ($nkmove_from_Square + ($nkmove_turn * 20) == $nkmove_to_Square) {
$nkmove_ep_Square = $nkmove_from_Square + ($nkmove_turn * 10);
}
}
else if ($nkPiece == WK) {
# set rook and castling rights if necessary
if ($nkmove_from_Square == sq_e1) {
if ($nkmove_to_Square == sq_g1) {
$nkBoard[sq_h1] = 0;
$nkBoard[sq_f1] = WR;
$nkpgn_castling = "O-O";
}
else if ($nkmove_to_Square == sq_c1) {
$nkBoard[sq_a1] = 0;
$nkBoard[sq_d1] = WR;
$nkpgn_castling = "O-O-O";
}
$nkmove_castling_ws = FALSE;
$nkmove_castling_wl = FALSE;
}
else if ($nkmove_from_Square == sq_e8) {
if ($nkmove_to_Square == sq_g8) {
$nkBoard[sq_h8] = 0;
$nkBoard[sq_f8] = BR;
$nkpgn_castling = "O-O";
}
else if ($nkmove_to_Square == sq_c8) {
$nkBoard[sq_a8] = 0;
$nkBoard[sq_d8] = BR;
$nkpgn_castling = "O-O-O";
}
$nkmove_castling_bs = FALSE;
$nkmove_castling_bl = FALSE;
}
}
if (($nkmove_from_Square == sq_h1) || ($nkmove_to_Square == sq_h1))
$nkmove_castling_ws = FALSE;
if (($nkmove_from_Square == sq_a1) || ($nkmove_to_Square == sq_a1))
$nkmove_castling_wl = FALSE;
if (($nkmove_from_Square == sq_h8) || ($nkmove_to_Square == sq_h8))
$nkmove_castling_bs = FALSE;
if (($nkmove_from_Square == sq_a8) || ($nkmove_to_Square == sq_a8))
$nkmove_castling_bl = FALSE;
$nkmove_turn = -$nkmove_turn;
$nkmove_PlyNumber++;
}
# Converts e4 to 55, for example
# If it cannot be converted, return 0 (an illegal square)
function get_Square($nkFeld) {
if (strlen($nkFeld) != 2)
return 0;
$nkx = ord($nkFeld[0]) - 97;
$nky = $nkFeld[1];
if (($nkx < 0) || ($nkx > 7) || ($nky < 1) || ($nky > 8))
return 0;
$nkx = (($nky - 1) << 3) + $nkx;
$nkx = ($nkx >> 3) * 10 + ((($nkx + 8) % 8)) + 21;
if (($nkx < 21) || ($nkx > 98))
return 0;
return $nkx;
}
# Syntax must be "h7-h8Q", castling e1-g1
function check_MoveSyntax($nkMove) {
if ((strlen($nkMove) < 5) || (strlen($nkMove) > 6))
return FALSE;
if ($nkMove[2] != "-")
return FALSE;
return TRUE;
}
# Resets the move variables
function reset_Move_Vars() {
global $nkmove_turn, $nkmove_promotion_figur, $nkmove_ep_Square, $nkmove_to_Square, $nkmove_from_Square;
global $nkmove_castling_ws, $nkmove_castling_wl, $nkmove_castling_bs, $nkmove_castling_bl, $nkmove_PlyNumber;
$nkmove_from_Square = 0;
$nkmove_to_Square = 0;
$nkmove_ep_Square = 0;
$nkmove_promotion_figur = 0;
$nkmove_turn = 1;
$nkmove_castling_ws = TRUE;
$nkmove_castling_wl = TRUE;
$nkmove_castling_bs = TRUE;
$nkmove_castling_bl = TRUE;
$nkmove_PlyNumber = 0;
}
# Extracts from- and to-square from a move (e2-e4, e1-g1, h7-h8Q)
# and sets the move variables
# Then it checks whether the move is legal (it does not execute the move)
function set_Move_Vars($nkaMove) {
global $nkBoard, $nkmove_turn, $nkmove_promotion_figur, $nkmove_to_Square, $nkmove_from_Square;
if (!check_MoveSyntax($nkaMove))
return FALSE;
$nkmove_from_Square = get_Square(substr($nkaMove, 0, 2));
$nkmove_to_Square = get_Square(substr($nkaMove, 3, 2));
$nkmove_promotion_figur = 0;
# Check whether promotion piece is defined
if (
(Abs($nkBoard[$nkmove_from_Square]) == WP) &&
(($nkmove_to_Square > sq_h7) || ($nkmove_to_Square < sq_a2))
) {
if (strlen($nkaMove) < 6)
$nks_promotion_figur = "Q";
else
$nks_promotion_figur = substr($nkaMove, 5, 1);
if ($nks_promotion_figur == "Q")
$nkmove_promotion_figur = $nkmove_turn * WQ;
else if ($nks_promotion_figur == "R")
$nkmove_promotion_figur = $nkmove_turn * WR;
else if ($nks_promotion_figur == "B")
$nkmove_promotion_figur = $nkmove_turn * WB;
else if ($nks_promotion_figur == "N")
$nkmove_promotion_figur = $nkmove_turn * WN;
}
if (($nkmove_to_Square == 0) || ($nkmove_from_Square == 0) || (!is_Move_possible()))
return FALSE;
return TRUE;
}
# Counts all legal moves to check for mate and stalemate
function Move_Count() {
global $nkBoard, $nkmove_list_count, $nkmove_list_to, $nkmove_turn, $nkmove_ep_Square;
$nkRetVal = 0;
$nkKing_Place = get_King($nkmove_turn == 1);
if ($nkKing_Place != -1) {
# get the moves from all pieces on the board
for ($nki = 0; $nki <= 63; $nki++) {
$nkv = c0_21($nki);
$nkf = $nkBoard[$nkv];
if (
(($nkf != 0) && ($nkf != 100))
&& (!(($nkmove_turn == 1) && ($nkf < 0)))
&& (!(($nkmove_turn != 1) && ($nkf > 0)))
) {
$nkmove_list_count = 0;
if (Abs($nkf) == WK)
get_KingSquares($nkv, $nkmove_turn, 0);
else if (Abs($nkf) == WQ)
get_QueenSquares($nkv, $nkmove_turn, 0);
else if (Abs($nkf) == WR)
get_RookSquares($nkv, $nkmove_turn, 0);
else if (Abs($nkf) == WB)
get_BishopSquares($nkv, $nkmove_turn, 0);
else if (Abs($nkf) == WN)
get_KnightSquares($nkv, $nkmove_turn, 0);
else if (Abs($nkf) == WP)
get_PawnSquares($nkv, $nkmove_turn, 0);
for ($nkk = 0; $nkk < $nkmove_list_count; $nkk++) {
$nkn = $nkmove_list_to[$nkk];
# execute move on board after saving the old values
# do not forget ep, second part of castling is not necessary (only check for check)
$nkrep = $nkn + (-$nkmove_turn * 10);
$nkrep_f = $nkBoard[$nkn + (-$nkmove_turn * 10)];
$nkrn_f = $nkBoard[$nkn];
$nkrv_f = $nkBoard[$nkv];
$nkBoard[$nkn] = $nkBoard[$nkv];
$nkBoard[$nkv] = 0;
if ((Abs($nkf) == WP) && ($nkmove_ep_Square == $nkn))
$nkBoard[$nkn + (-$nkmove_turn * 10)] = 0;
# king in check? If not, add to legal moves
if (Abs($nkf) == WK)
$nkw = $nkn;
else
$nkw = $nkKing_Place;
if (!attacked($nkw, $nkmove_turn)) {
$nkRetVal++;
if (
(Abs($nkf) == WP) &&
(($nkn < sq_a2) || ($nkn > sq_h7))
)
# Promotion +3
$nkRetVal = $nkRetVal + 3;
}
# Restore board for next loop
$nkBoard[$nkn] = $nkrn_f;
$nkBoard[$nkv] = $nkrv_f;
$nkBoard[$nkrep] = $nkrep_f;
}
}
}
}
return $nkRetVal;
}
# Returns a value depended on mate, stalemate, check and nothing of all
//# The "view point" is the own side after execution a move - the opponent
function get_GameState() {
global $nkmove_turn;
# Running
$nkRetVal = 0;
$nkc = Move_Count();
if ($nkc == 0) {
# Mate
$nkRetVal = 1;
if (!attacked(get_King($nkmove_turn == 1), $nkmove_turn))
# Stalemate
$nkRetVal = 2;
}
else
if (attacked(get_King($nkmove_turn == 1), $nkmove_turn))
# Check
$nkRetVal = 3;
return $nkRetVal;
}
function State_to_String($nki) {
switch ($nki) {
case gsMate : return "Mate";
case gsStalemate : return "Stalemate";
case gsCheck : return "Check";
default : return "";
}
}
# Sets up the first position and resets the move variables
function set_first_Position() {
global $nkBoard, $nkfirst_Position, $nkNotation;
$nkNotation = "";
for ($nki = 0; $nki <= 119; $nki++)
$nkBoard[$nki] = $nkfirst_Position[$nki];
reset_Move_Vars();
}
# Reads a game an sets up the last (current) position
# $nkMoves are delivered as sql result
function read_Game($nkMoves) {
global $nkNotation;
set_first_Position();
$nkl = 0;
while ($nkrow = mysql_fetch_array ($nkMoves)) {
$nkl++;
if (!add_Move($nkrow["Move"]))
return FALSE;
if ($nkl > 7) {
$nkNotation = $nkNotation . "\n";
$nkl = 0;
}
}
return TRUE;
}
# Checks the syntax of move (e2-e4), initiate the variables and execute it if possible
function add_Move($nkMove) {
global $nkNotation, $nkState, $nkpgn_piece, $nkpgn_from_letter, $nkpgn_from_digit, $nkpgn_capture;
global $nkmove_promotion_figur, $nkpgn_castling, $pgn_Move, $nkmove_PlyNumber;
global $simplemove;
if (!set_Move_Vars($nkMove))
return FALSE;
execute_Move();
# Create pgn notation
$nkMoveNumber = "";
if ($nkmove_PlyNumber % 2) {
$nkMoveNumber = (floor($nkmove_PlyNumber / 2) + 1) . ".";
$nkMoveNumber1 = $nkMoveNumber;
}
else
$nkMoveNumber1 = (floor(($nkmove_PlyNumber - 1) / 2) + 1) . ". ... ";