-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrules.pas
99 lines (82 loc) · 2.67 KB
/
rules.pas
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
unit Rules;
interface
uses
Main;
function IsMoveLegal(const FromCell, ToCell: cellty): boolean;
procedure DoMove(const FromCell, ToCell: cellty);
function GameStateMessage: string;
procedure ResetGame;
function CurrPosFen: string;
implementation
uses
SysUtils, StrUtils, ChessTypes, Game, Fen, Language;
const
CSquareName: array[colty, rowty] of string[2] = (
('a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8'),
('b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8'),
('c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8'),
('d1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8'),
('e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8'),
('f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8'),
('g1', 'g2', 'g3', 'g4', 'g5', 'g6', 'g7', 'g8'),
('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8')
);
var
LGame: TChessGame;
function ChangeCastlingNotation(const AMove: string): string;
begin
if (AMove = 'e1g1') and LGame.IsLegal('e1h1') and LGame.IsCastling('e1h1') then begin result := 'e1h1'; end else
if (AMove = 'e1c1') and LGame.IsLegal('e1a1') and LGame.IsCastling('e1a1') then begin result := 'e1a1'; end else
if (AMove = 'e8g8') and LGame.IsLegal('e8h8') and LGame.IsCastling('e8h8') then begin result := 'e8h8'; end else
if (AMove = 'e8c8') and LGame.IsLegal('e8a8') and LGame.IsCastling('e8a8') then begin result := 'e8a8'; end else
result := AMove;
end;
function IsMoveLegal(const FromCell, ToCell: cellty): boolean;
var
LMove: string;
begin
LMove := Concat(CSquareName[FromCell.col, FromCell.row], CSquareName[ToCell.col, ToCell.row]);
LMove := ChangeCastlingNotation(LMove);
result := LGame.IsLegal(LMove);
end;
procedure DoMove(const FromCell, ToCell: cellty);
var
LMove: string;
begin
LMove := Concat(CSquareName[FromCell.col, FromCell.row], CSquareName[ToCell.col, ToCell.row]);
LMove := ChangeCastlingNotation(LMove);
LGame.DoMove(LMove);
end;
function GameStateMessage: string;
begin
case LGame.State of
csProgress:
result := Concat(
IfThen(LGame.Check, Concat(GetText(txCheck), ' '), ''),
IfThen(LGame.ActiveColor = pcWhite, GetText(txWhiteToMove), GetText(txBlackToMove))
);
csCheckmate:
result := Concat(
GetText(txCheckmate), ' ',
IfThen(LGame.ActiveColor = pcWhite, GetText(txBlackWins), GetText(txWhiteWins))
);
csStalemate:
result := GetText(txStalemate);
csDraw:
result := GetText(txDraw);
end;
end;
procedure ResetGame;
begin
LGame.Free;
LGame := TChessGame.Create(CFenStartPosition);
end;
function CurrPosFen: string;
begin
result := LGame.GetFen(FALSE);
end;
initialization
LGame := TChessGame.Create(CFenStartPosition);
finalization
LGame.Free;
end.