-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlogic.pas
More file actions
713 lines (672 loc) · 26.9 KB
/
logic.pas
File metadata and controls
713 lines (672 loc) · 26.9 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
// В этом модуле реализованы правила шаматной игры: кто куда ходит, как бьёт и т.п.
unit logic;
interface
uses gamedata;
// флаги
//amValid=1; // только корректные ходы
// Составляет список возможных ходов для данной фигуры (в массив moves)
procedure GetAvailMoves(var board:TBoard;fromPos:byte;var moves:TMovesList);
// Отмечает поля, находящиеся под боем, а также определяет тип защищающих фигур
procedure CalcBeatable(var board:TBoard;out beatable:TBeatable);
// Выполняет ход: перемещает фигуру с поля from в поле target и производит необходимые изменения
procedure DoMove(var board:TBoard;from,target:byte);
// Возвращает состояние шаха: был ли поставлен шах последним ходом
function IsCheck(var b:TBoard):boolean;
// Performance profiling
procedure ResetCounters;
procedure LogCounters;
implementation
uses SysUtils,Apus.MyServis;
var
// performance counters
getAvailMovesCount:int64;
calcBeatableCount:int64;
doMoveCount:int64;
isCheckCount:int64;
{ procedure CalcBeatable2(var board:TBoard);
var
i,j,k,n,x,y:integer;
fig,figpos:array[1..32] of byte;
begin
fillchar(beatable,sizeof(beatable),0);
with board do begin
n:=0;
for x:=0 to 7 do
for y:=0 to 7 do
if field[x,y]>0 then begin
inc(n);
figpos[n]:=x+y shl 4;
case field[x,y] of
PawnWhite:fig[n]:=1;
KnightWhite:fig[n]:=2;
BishopWhite:fig[n]:=3;
RookWhite:fig[n]:=4;
QueenWhite:fig[n]:=5;
KingWhite:fig[n]:=6;
PawnBlack:fig[n]:=1+Black;
KnightBlack:fig[n]:=2+Black;
BishopBlack:fig[n]:=3+Black;
RookBlack:fig[n]:=4+Black;
QueenBlack:fig[n]:=5+Black;
KingBlack:fig[n]:=6+Black;
end;
end;
for i:=1 to n do
if fig[i] and $F=6 then begin
// if
end;
end;
end; }
procedure CalcBeatable(var board:TBoard;out beatable:TBeatable);
var
x,y,i:integer;
v,whitecnt,blackcnt:byte;
pieceMask:integer;
begin
with board do
for y:=0 to 7 do
for x:=0 to 7 do begin
v:=0;
beatable[x,y]:=0;
whitecnt:=0; blackcnt:=0;
pieceMask:=0;
// определить под боем ли поле x,y
// пешки
if (y>1) and (x>0) and (GetCell(x-1,y-1)=PawnWhite) then pieceMask:=1;
if (y>1) and (x<7) and (GetCell(x+1,y-1)=PawnWhite) then pieceMask:=1;
if pieceMask=0 then begin // кони
if (x>1) and (y>0) and (GetCell(x-2,y-1)=KnightWhite) then pieceMask:=2;
if (x>1) and (y<7) and (GetCell(x-2,y+1)=KnightWhite) then pieceMask:=2;
if (x<6) and (y>0) and (GetCell(x+2,y-1)=KnightWhite) then pieceMask:=2;
if (x<6) and (y<7) and (GetCell(x+2,y+1)=KnightWhite) then pieceMask:=2;
if (x>0) and (y>1) and (GetCell(x-1,y-2)=KnightWhite) then pieceMask:=2;
if (x<7) and (y>1) and (GetCell(x+1,y-2)=KnightWhite) then pieceMask:=2;
if (x>0) and (y<6) and (GetCell(x-1,y+2)=KnightWhite) then pieceMask:=2;
if (x<7) and (y<6) and (GetCell(x+1,y+2)=KnightWhite) then pieceMask:=2;
end;
if pieceMask=0 then begin
for i:=1 to 7 do begin // вправо-вверх
if (x+i>7) or (y+i>7) then break;
case GetCell(x+i,y+i) of
BishopWhite:pieceMask:=pieceMask or 4;
QueenWhite:pieceMask:=pieceMask or 16;
KingWhite:if i=1 then pieceMask:=pieceMask or 32;
end;
if CellOccupied(x+i,y+i) then break;
end;
for i:=1 to 7 do begin // вправо-вниз
if (x+i>7) or (y-i<0) then break;
case GetCell(x+i,y-i) of
BishopWhite:pieceMask:=pieceMask or 4;
QueenWhite:pieceMask:=pieceMask or 16;
KingWhite:if i=1 then pieceMask:=pieceMask or 32;
end;
if CellOccupied(x+i,y-i) then break;
end;
for i:=1 to 7 do begin // влево-вверх
if (x-i<0) or (y+i>7) then break;
case GetCell(x-i,y+i) of
BishopWhite:pieceMask:=pieceMask or 4;
QueenWhite:pieceMask:=pieceMask or 16;
KingWhite:if i=1 then pieceMask:=pieceMask or 32;
end;
if CellOccupied(x-i,y+i) then break;
end;
for i:=1 to 7 do begin // влево-вниз
if (x-i<0) or (y-i<0) then break;
case GetCell(x-i,y-i) of
BishopWhite:pieceMask:=pieceMask or 4;
QueenWhite:pieceMask:=pieceMask or 16;
KingWhite:if i=1 then pieceMask:=pieceMask or 32;
end;
if GetCell(x-i,y-i)<>0 then break;
end;
end;
if pieceMask and 7=0 then begin // поле не бито пешкой/конём/слоном
for i:=1 to 7 do begin // вправо
if (x+i>7) then break;
case GetCell(x+i,y) of
RookWhite:pieceMask:=pieceMask or 8;
QueenWhite:pieceMask:=pieceMask or 16;
KingWhite:if i=1 then pieceMask:=pieceMask or 32;
end;
if GetCell(x+i,y)<>0 then break;
end;
for i:=1 to 7 do begin // влево
if (x-i<0) then break;
case GetCell(x-i,y) of
RookWhite:pieceMask:=pieceMask or 8;
QueenWhite:pieceMask:=pieceMask or 16;
KingWhite:if i=1 then pieceMask:=pieceMask or 32;
end;
if GetCell(x-i,y)<>0 then break;
end;
for i:=1 to 7 do begin // вверх
if (y+i>7) then break;
case GetCell(x,y+i) of
RookWhite:pieceMask:=pieceMask or 8;
QueenWhite:pieceMask:=pieceMask or 16;
KingWhite:if i=1 then pieceMask:=pieceMask or 32;
end;
if GetCell(x,y+i)<>0 then break;
end;
for i:=1 to 7 do begin // вниз
if (y-i<0) then break;
case GetCell(x,y-i) of
RookWhite:pieceMask:=pieceMask or 8;
QueenWhite:pieceMask:=pieceMask or 16;
KingWhite:if i=1 then pieceMask:=pieceMask or 32;
end;
if GetCell(x,y-i)<>0 then break;
end;
end;
if pieceMask>0 then begin
beatable[x,y]:=White+1;
while pieceMask and 1=0 do begin
inc(beatable[x,y]); pieceMask:=pieceMask shr 1;
end;
end;
pieceMask:=0;
// определить под боем ли поле x,y
// пешки
if (y<6) and (x>0) and (GetCell(x-1,y+1)=PawnBlack) then pieceMask:=1;
if (y<6) and (x<7) and (GetCell(x+1,y+1)=PawnBlack) then pieceMask:=1;
if pieceMask=0 then begin // кони
if (x>1) and (y>0) and (GetCell(x-2,y-1)=KnightBlack) then pieceMask:=2;
if (x>1) and (y<7) and (GetCell(x-2,y+1)=KnightBlack) then pieceMask:=2;
if (x<6) and (y>0) and (GetCell(x+2,y-1)=KnightBlack) then pieceMask:=2;
if (x<6) and (y<7) and (GetCell(x+2,y+1)=KnightBlack) then pieceMask:=2;
if (x>0) and (y>1) and (GetCell(x-1,y-2)=KnightBlack) then pieceMask:=2;
if (x<7) and (y>1) and (GetCell(x+1,y-2)=KnightBlack) then pieceMask:=2;
if (x>0) and (y<6) and (GetCell(x-1,y+2)=KnightBlack) then pieceMask:=2;
if (x<7) and (y<6) and (GetCell(x+1,y+2)=KnightBlack) then pieceMask:=2;
end;
if pieceMask=0 then begin
for i:=1 to 7 do begin // вправо-вверх
if (x+i>7) or (y+i>7) then break;
case GetCell(x+i,y+i) of
BishopBlack:pieceMask:=pieceMask or 4;
QueenBlack:pieceMask:=pieceMask or 16;
KingBlack:if i=1 then pieceMask:=pieceMask or 32;
end;
if CellOccupied(x+i,y+i) then break;
end;
for i:=1 to 7 do begin // вправо-вниз
if (x+i>7) or (y-i<0) then break;
case GetCell(x+i,y-i) of
BishopBlack:pieceMask:=pieceMask or 4;
QueenBlack:pieceMask:=pieceMask or 16;
KingBlack:if i=1 then pieceMask:=pieceMask or 32;
end;
if CellOccupied(x+i,y-i) then break;
end;
for i:=1 to 7 do begin // влево-вверх
if (x-i<0) or (y+i>7) then break;
case GetCell(x-i,y+i) of
BishopBlack:pieceMask:=pieceMask or 4;
QueenBlack:pieceMask:=pieceMask or 16;
KingBlack:if i=1 then pieceMask:=pieceMask or 32;
end;
if CellOccupied(x-i,y+i) then break;
end;
for i:=1 to 7 do begin // влево-вниз
if (x-i<0) or (y-i<0) then break;
case GetCell(x-i,y-i) of
BishopBlack:pieceMask:=pieceMask or 4;
QueenBlack:pieceMask:=pieceMask or 16;
KingBlack:if i=1 then pieceMask:=pieceMask or 32;
end;
if CellOccupied(x-i,y-i) then break;
end;
end;
if pieceMask and 7=0 then begin // поле не бито пешкой/конём/слоном
for i:=1 to 7 do begin // вправо
if (x+i>7) then break;
case GetCell(x+i,y) of
RookBlack:pieceMask:=pieceMask or 8;
QueenBlack:pieceMask:=pieceMask or 16;
KingBlack:if i=1 then pieceMask:=pieceMask or 32;
end;
if CellOccupied(x+i,y) then break;
end;
for i:=1 to 7 do begin // влево
if (x-i<0) then break;
case GetCell(x-i,y) of
RookBlack:pieceMask:=pieceMask or 8;
QueenBlack:pieceMask:=pieceMask or 16;
KingBlack:if i=1 then pieceMask:=pieceMask or 32;
end;
if CellOccupied(x-i,y) then break;
end;
for i:=1 to 7 do begin // вверх
if (y+i>7) then break;
case GetCell(x,y+i) of
RookBlack:pieceMask:=pieceMask or 8;
QueenBlack:pieceMask:=pieceMask or 16;
KingBlack:if i=1 then pieceMask:=pieceMask or 32;
end;
if CellOccupied(x,y+i) then break;
end;
for i:=1 to 7 do begin // вниз
if (y-i<0) then break;
case GetCell(x,y-i) of
RookBlack:pieceMask:=pieceMask or 8;
QueenBlack:pieceMask:=pieceMask or 16;
KingBlack:if i=1 then pieceMask:=pieceMask or 32;
end;
if CellOccupied(x,y-i) then break;
end;
end;
if pieceMask>0 then begin
beatable[x,y]:=beatable[x,y] or (Black+$8);
while pieceMask and 1=0 do begin
inc(beatable[x,y],$8); pieceMask:=pieceMask shr 1;
end;
end;
end;
end;
procedure GetAvailMoves(var board:TBoard;fromPos:byte;var moves:TMovesList);
var
i,j,x,y:integer;
v,color,color2,m,n,l:byte;
pb:PByte;
beatable:TBeatable;
procedure AddMove(var pb:PByte;m:byte); inline;
begin
inc(pb);
pb^:=m;
end;
procedure Finish(var moves:TMovesList;pb:PByte); inline;
begin
moves[0]:=UIntPtr(pb)-UIntPtr(@moves);
end;
begin
with board do begin
pb:=@moves;
x:=fromPos and $F;
y:=fromPos shr 4;
v:=GetPieceType(x,y);
color:=GetPieceColor(x,y);
if v=Pawn then begin
if color=White then begin
// белая пешка
if (y<7) and (GetCell(x,y+1)=0) then AddMove(pb,fromPos+$10);
if (y=1) and (GetCell(x,2)=0) and (GetCell(x,3)=0) then AddMove(pb,fromPos+$20);
if (x>0) and (GetCell(x-1,y+1) and ColorMask=Black) then AddMove(pb,fromPos+$10-1);
if (x<7) and (GetCell(x+1,y+1) and ColorMask=Black) then AddMove(pb,fromPos+$10+1);
// взятие на проходе
if (y=4) and (x>0) and (GetCell(x-1,y)=PawnBlack) and
(lastTurnFrom=fromPos+$20-1) and (lastTurnTo=fromPos-1) then AddMove(pb,fromPos+$10-1);
if (y=4) and (x<70) and (GetCell(x+1,y)=PawnBlack) and
(lastTurnFrom=fromPos+$20+1) and (lastTurnTo=fromPos+1) then AddMove(pb,fromPos+$10+1);
end else begin
// черная пешка
if (y>0) and (GetCell(x,y-1)=0) then AddMove(pb,fromPos-$10);
if (y=6) and (GetCell(x,5)=0) and (GetCell(x,4)=0) then AddMove(pb,fromPos-$20);
if (x>0) and (GetCell(x-1,y-1) and ColorMask=White) then AddMove(pb,fromPos-$10-1);
if (x<7) and (GetCell(x+1,y-1) and ColorMask=White) then AddMove(pb,fromPos-$10+1);
// взятие на проходе
if (y=3) and (x>0) and (GetCell(x-1,y)=PawnWhite) and
(lastTurnFrom=fromPos-$20-1) and (lastTurnTo=fromPos-1) then AddMove(pb,fromPos-$10-1);
if (y=3) and (x<70) and (GetCell(x+1,y)=PawnWhite) and
(lastTurnFrom=fromPos-$20+1) and (lastTurnTo=fromPos+1) then AddMove(pb,fromPos-$10+1);
end;
Finish(moves,pb);
exit;
end;
if v=Knight then begin
if (x>0) and (y>1) and (GetCell(x-1,y-2) and ColorMask<>color) then AddMove(pb,fromPos-$20-1);
if (x<7) and (y>1) and (GetCell(x+1,y-2) and ColorMask<>color) then AddMove(pb,fromPos-$20+1);
if (x>0) and (y<6) and (GetCell(x-1,y+2) and ColorMask<>color) then AddMove(pb,fromPos+$20-1);
if (x<7) and (y<6) and (GetCell(x+1,y+2) and ColorMask<>color) then AddMove(pb,fromPos+$20+1);
if (x>1) and (y>0) and (GetCell(x-2,y-1) and ColorMask<>color) then AddMove(pb,fromPos-$10-2);
if (x>1) and (y<7) and (GetCell(x-2,y+1) and ColorMask<>color) then AddMove(pb,fromPos+$10-2);
if (x<6) and (y>0) and (GetCell(x+2,y-1) and ColorMask<>color) then AddMove(pb,fromPos-$10+2);
if (x<6) and (y<7) and (GetCell(x+2,y+1) and ColorMask<>color) then AddMove(pb,fromPos+$10+2);
Finish(moves,pb);
exit;
end;
if v=King then begin
// нельзя ходить на поле под боем
if color=White then color2:=Black else color2:=White;
SetCell(x,y,0); // временно уберём короля
CalcBeatable(board,beatable);
SetCell(x,y,King+color);
if (x>0) and (y>0) and
(GetCell(x-1,y-1) and ColorMask<>color) and (beatable[x-1,y-1] and color2=0) then AddMove(pb,fromPos-$10-1);
if (x>0) and (y<7) and
(GetCell(x-1,y+1) and ColorMask<>color) and (beatable[x-1,y+1] and color2=0) then AddMove(pb,fromPos+$10-1);
if (x<7) and (y>0) and
(GetCell(x+1,y-1) and ColorMask<>color) and (beatable[x+1,y-1] and color2=0) then AddMove(pb,fromPos-$10+1);
if (x<7) and (y<7) and
(GetCell(x+1,y+1) and ColorMask<>color) and (beatable[x+1,y+1] and color2=0) then AddMove(pb,fromPos+$10+1);
if (x>0) and (GetCell(x-1,y) and ColorMask<>color) and (beatable[x-1,y] and color2=0) then AddMove(pb,fromPos-1);
if (x<7) and (GetCell(x+1,y) and ColorMask<>color) and (beatable[x+1,y] and color2=0) then AddMove(pb,fromPos+1);
if (y>0) and (GetCell(x,y-1) and ColorMask<>color) and (beatable[x,y-1] and color2=0) then AddMove(pb,fromPos-$10);
if (y<7) and (GetCell(x,y+1) and ColorMask<>color) and (beatable[x,y+1] and color2=0) then AddMove(pb,fromPos+$10);
// Рокировка...
if (x=4) and (y=0) and (color=White) and (rFlags and $F<3) then begin
// Возможна рокировка хотя бы в одну сторону
// длинная рокировка
if (rFlags and 1=0) and (GetCell(1,0) or GetCell(2,0) or GetCell(3,0)=0) and
((beatable[2,0] or beatable[3,0] or beatable[4,0]) and Black=0) then
AddMove(pb,fromPos-2);
// короткая рокировка
if (rFlags and 2=0) and (GetCell(5,0) or GetCell(6,0)=0) and
((beatable[4,0] or beatable[5,0] or beatable[6,0]) and Black=0) then
AddMove(pb,fromPos+2);
end;
if (x=4) and (y=7) and (color=Black) and (rFlags and $F0<$30) then begin
// Возможна рокировка хотя бы в одну сторону
// длинная рокировка
if (rFlags and $10=0) and (GetCell(1,7) or GetCell(2,7) or GetCell(3,7)=0) and
((beatable[2,7] or beatable[3,7] or beatable[4,7]) and White=0) then
AddMove(pb,fromPos-2);
// короткая рокировка
if (rFlags and $20=0) and (GetCell(5,7) or GetCell(6,7)=0) and
((beatable[4,7] or beatable[5,7] or beatable[6,7]) and White=0) then
AddMove(pb,fromPos+2);
end;
Finish(moves,pb);
exit;
end;
if v in [Rook,Queen] then begin
// ладья
m:=fromPos;
for i:=x+1 to 7 do begin
if GetCell(i,y) and ColorMask=color then break;
m:=m+1;
AddMove(pb,m);
if GetCell(i,y)<>0 then break;
end;
m:=fromPos;
for i:=x-1 downto 0 do begin
if GetCell(i,y) and ColorMask=color then break;
m:=m-1;
AddMove(pb,m);
if GetCell(i,y)<>0 then break;
end;
m:=fromPos;
for i:=y+1 to 7 do begin
if GetCell(x,i) and ColorMask=color then break;
m:=m+$10;
AddMove(pb,m);
if GetCell(x,i)<>0 then break;
end;
m:=fromPos;
for i:=y-1 downto 0 do begin
if GetCell(x,i) and ColorMask=color then break;
m:=m-$10;
AddMove(pb,m);
if GetCell(x,i)<>0 then break;
end;
end;
if v in [Bishop,Queen] then begin
// Слон
m:=fromPos;
n:=x; l:=y;
for i:=1 to 7 do begin
inc(n); inc(l);
m:=m+$10+1;
if (n>7) or (l>7) or (GetCell(n,l) and ColorMask=color) then break;
AddMove(pb,m);
if GetCell(n,l)<>0 then break;
end;
m:=fromPos;
n:=x; l:=y;
for i:=1 to 7 do begin
dec(n); inc(l);
m:=m+$10-1;
if (n>7) or (l>7) or (GetCell(n,l) and ColorMask=color) then break;
AddMove(pb,m);
if GetCell(n,l)<>0 then break;
end;
m:=fromPos;
n:=x; l:=y;
for i:=1 to 7 do begin
inc(n); dec(l);
m:=m-$10+1;
if (n>7) or (l>7) or (GetCell(n,l) and ColorMask=color) then break;
AddMove(pb,m);
if GetCell(n,l)<>0 then break;
end;
m:=fromPos;
n:=x; l:=y;
for i:=1 to 7 do begin
dec(n); dec(l);
m:=m-$10-1;
if (n>7) or (l>7) or (GetCell(n,l) and ColorMask=color) then break;
AddMove(pb,m);
if GetCell(n,l)<>0 then break;
end;
end;
end;
Finish(moves,pb);
end;
procedure DoMove(var board:TBoard;from,target:byte);
var
x,y,nx,ny:integer;
v,piece:byte;
begin
with board do begin
x:=from and $F;
y:=from shr 4;
nx:=target and $F;
ny:=target shr 4;
// Move piece from (x,y) to (nx,xy)
v:=GetCell(x,y);
SetCell(x,y,0);
lastPiece:=GetCell(nx,ny);
if not CellIsEmpty(nx,ny) then SetFlag(movBeat)
else ClearFlag(movBeat);
SetCell(nx,ny,v);
// пешка -> ферзь
if (v=PawnWhite) and (ny=7) then SetCell(nx,ny,QueenWhite);
if (v=PawnBlack) and (ny=0) then SetCell(nx,ny,QueenBlack);
// пешка на проходе
if (ny=2) and (GetCell(nx,ny+1)=PawnWhite) and (LastTurnFrom=nx+(ny-1) shl 4)
and (LastTurnTo=nx+(ny+1) shl 4) then begin
SetCell(nx,ny+1,0);
SetFlag(movBeat);
lastPiece:=PawnWhite;
end;
if (ny=5) and (GetCell(nx,ny-1)=PawnBlack) and (LastTurnFrom=nx+(ny+1) shl 4)
and (LastTurnTo=nx+(ny-1) shl 4) then begin
SetCell(nx,ny-1,0);
SetFlag(movBeat);
lastPiece:=PawnBlack;
end;
piece:=v and PieceMask;
if piece=King then begin
// король сделал ход - рокировка в будущем невозможна
if v and ColorMask=White then begin
rFlags:=rFlags or $4;
wKingPos:=target;
end else begin
rFlags:=rFlags or $40;
bKingPos:=target;
end;
// Рокировка
if (x=4) then begin
if (nx=2) then begin // в длинную сторону
SetCell(3,y,GetCell(0,y)); // ладья
SetCell(0,y,0);
if y=0 then rFlags:=rFlags or $8
else rFlags:=rFlags or $80;
end else
if (nx=6) then begin // в короткую сторону
SetCell(5,y,GetCell(7,y)); // ладья
SetCell(7,y,0);
if y=0 then rFlags:=rFlags or $8
else rFlags:=rFlags or $80;
end;
end;
end;
if piece=Rook then
if x=0 then begin
if (y=0) and (v and ColorMask=White) then rFlags:=rFlags or 1;
if (y=7) and (v and ColorMask=Black) then rFlags:=rFlags or $10;
end else
if x=7 then begin
if (y=0) and (v and ColorMask=White) then rFlags:=rFlags or 2;
if (y=7) and (v and ColorMask=Black) then rFlags:=rFlags or $20;
end;
LastTurnFrom:=from;
LastTurnTo:=target;
end;
end;
// проверяет был ли поставлен шах последним ходом и устанавливает флаг шаха если был
function IsCheck(var b:TBoard):boolean;
var
i,j,k:integer;
fl:boolean;
label check;
begin
with b do begin
if WhiteTurn then
for i:=0 to 7 do
for j:=0 to 7 do
if GetCell(i,j)=KingWhite then begin
// ладьей или ферзем вверх
for k:=j+1 to 7 do
if GetCell(i,k) in [QueenBlack,RookBlack] then goto check
else if CellOccupied(i,k) then break;
// ладьей или ферзем вправо
for k:=i+1 to 7 do
if GetCell(k,j) in [QueenBlack,RookBlack] then goto check
else if GetCell(k,j)>0 then break;
// ладьей или ферзем влево
for k:=i-1 downto 0 do
if GetCell(k,j) in [QueenBlack,RookBlack] then goto check
else if GetCell(k,j)>0 then break;
// ладьей или ферзем вниз
for k:=j-1 downto 0 do
if GetCell(i,k) in [QueenBlack,RookBlack] then goto check
else if GetCell(i,k)>0 then break;
// слоном или ферзем вверх-вправо
for k:=1 to 7 do
if (i+k>7) or (j+k>7) then break else
if GetCell(i+k,j+k) in [QueenBlack,BishopBlack] then goto check
else if GetCell(i+k,j+k)>0 then break;
// слоном или ферзем вверх-влево
for k:=1 to 7 do
if (i-k<0) or (j+k>7) then break else
if GetCell(i-k,j+k) in [QueenBlack,BishopBlack] then goto check
else if GetCell(i-k,j+k)>0 then break;
// слоном или ферзем вниз-вправо
for k:=1 to 7 do
if (i+k>7) or (j-k<0) then break else
if GetCell(i+k,j-k) in [QueenBlack,BishopBlack] then goto check
else if GetCell(i+k,j-k)>0 then break;
// слоном или ферзем вниз-влево
for k:=1 to 7 do
if (i-k<0) or (j-k<0) then break else
if GetCell(i-k,j-k) in [QueenBlack,BishopBlack] then goto check
else if GetCell(i-k,j-k)>0 then break;
// конём
if i-1>=0 then begin
if (j-2>=0) and (GetCell(i-1,j-2)=KnightBlack) then goto check;
if (j+2<=7) and (GetCell(i-1,j+2)=KnightBlack) then goto check;
end;
if i-2>=0 then begin
if (j-1>=0) and (GetCell(i-2,j-1)=KnightBlack) then goto check;
if (j+1<=7) and (GetCell(i-2,j+1)=KnightBlack) then goto check;
end;
if i+1<=7 then begin
if (j-2>=0) and (GetCell(i+1,j-2)=KnightBlack) then goto check;
if (j+2<=7) and (GetCell(i+1,j+2)=KnightBlack) then goto check;
end;
if i+2<=7 then begin
if (j-1>=0) and (GetCell(i+2,j-1)=KnightBlack) then goto check;
if (j+1<=7) and (GetCell(i+2,j+1)=KnightBlack) then goto check;
end;
// Пешкой
if (j<6) then begin
if (i>0) and (GetCell(i-1,j+1)=pawnBlack) then goto check;
if (i<7) and (GetCell(i+1,j+1)=pawnBlack) then goto check;
end;
end;
// Если ход черных - проверить черного короля
if not WhiteTurn then
for i:=0 to 7 do
for j:=0 to 7 do
if GetCell(i,j)=KingBlack then begin
// ладьей или ферзем вниз
for k:=j-1 downto 0 do
if GetCell(i,k) in [QueenWhite,RookWhite] then goto check
else if GetCell(i,k)>0 then break;
// ладьей или ферзем вправо
for k:=i+1 to 7 do
if GetCell(k,j) in [QueenWhite,RookWhite] then goto check
else if GetCell(k,j)>0 then break;
// ладьей или ферзем влево
for k:=i-1 downto 0 do
if GetCell(k,j) in [QueenWhite,RookWhite] then goto check
else if GetCell(k,j)>0 then break;
// ладьей или ферзем вверх
for k:=j+1 to 7 do
if GetCell(i,k) in [QueenWhite,RookWhite] then goto check
else if GetCell(i,k)>0 then break;
// слоном или ферзем вниз-вправо
for k:=1 to 7 do
if (i+k>7) or (j-k<0) then break else
if GetCell(i+k,j-k) in [QueenWhite,BishopWhite] then goto check
else if GetCell(i+k,j-k)>0 then break;
// слоном или ферзем вниз-влево
for k:=1 to 7 do
if (i-k<0) or (j-k<0) then break else
if GetCell(i-k,j-k) in [QueenWhite,BishopWhite] then goto check
else if GetCell(i-k,j-k)>0 then break;
// слоном или ферзем вверх-вправо
for k:=1 to 7 do
if (i+k>7) or (j+k>7) then break else
if GetCell(i+k,j+k) in [QueenWhite,BishopWhite] then goto check
else if GetCell(i+k,j+k)>0 then break;
// слоном или ферзем вверх-влево
for k:=1 to 7 do
if (i-k<0) or (j+k>7) then break else
if GetCell(i-k,j+k) in [QueenWhite,BishopWhite] then goto check
else if GetCell(i-k,j+k)>0 then break;
// конём
if i-1>=0 then begin
if (j-2>=0) and (GetCell(i-1,j-2)=KnightWhite) then goto check;
if (j+2<=7) and (GetCell(i-1,j+2)=KnightWhite) then goto check;
end;
if i-2>=0 then begin
if (j-1>=0) and (GetCell(i-2,j-1)=KnightWhite) then goto check;
if (j+1<=7) and (GetCell(i-2,j+1)=KnightWhite) then goto check;
end;
if i+1<=7 then begin
if (j-2>=0) and (GetCell(i+1,j-2)=KnightWhite) then goto check;
if (j+2<=7) and (GetCell(i+1,j+2)=KnightWhite) then goto check;
end;
if i+2<=7 then begin
if (j-1>=0) and (GetCell(i+2,j-1)=KnightWhite) then goto check;
if (j+1<=7) and (GetCell(i+2,j+1)=KnightWhite) then goto check;
end;
// Пешкой
if (j>1) then begin
if (i>0) and (GetCell(i-1,j-1)=pawnWhite) then goto check;
if (i<7) and (GetCell(i+1,j-1)=pawnWhite) then goto check;
end;
end;
end;
exit;
check:
b.SetFlag(movCheck);
end;
procedure ResetCounters;
begin
getAvailMovesCount:=0;
calcBeatableCount:=0;
doMoveCount:=0;
isCheckCount:=0;
end;
procedure LogCounters;
begin
{$IFDEF PERFCOUNT}
LogMessage('Logic counters: GAM=%d, CB=%d, DM=%d, IC=%d',
[getAvailMovesCount,calcBeatableCount,doMoveCount,isCheckCount]);
{$ENDIF}
end;
end.