-
Notifications
You must be signed in to change notification settings - Fork 3
/
chessplayer.pas
279 lines (243 loc) · 7.18 KB
/
chessplayer.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
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
{ Autor: Jürgen Schlottke, Schönaich-C.-Str. 46, D-W 2200 Elmshorn
Tel. 04121/63109
Zweck : Demonstration der Schachprogrammierung unter Turbo-Pascal
Datum : irgendwann 1991, als PD freigegeben am 18.01.93
Version: ohne Versionsnummer
}
unit ChessPlayer;
interface
uses
ChessPlayerCore;
type
TExitCode = (ecSuccess, ecCheck, ecCheckmate, ecStalemate, ecError);
TChessPlayer = class
strict private
FIniPos,
FCurPos: TChessPosition;
FHalfmovesCount: integer;
FSecondList: TMoveList;
public
constructor Create;
destructor Destroy; override;
procedure SetPosition(const APos: string);
function PlayMove(const AMove: string): boolean;
function BestMoveIndex(const ABestValue: integer): integer;
function BestMove(out ACode: TExitCode): string; overload;
function BestMove: string; overload;
function BoardAsText(const APretty: boolean = TRUE): string;
function FEN: string;
function SetPositionFromMoves(const APos: string; const AMoves: array of string): string;
end;
implementation
uses
Classes, SysUtils, TypInfo, ChessPlayerTypes, Log;
const
STARTPOS = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
constructor TChessPlayer.Create;
begin
inherited Create;
FIniPos := TChessPosition.Create(STARTPOS);
FCurPos := TChessPosition.Create;
end;
destructor TChessPlayer.Destroy;
begin
FCurPos.Free;
FIniPos.Free;
inherited Destroy;
end;
procedure TChessPlayer.SetPosition(const APos: string);
function GetMoveCount(const APos: string): integer;
var
LFields: TStringList;
begin
LFields := TStringList.Create;
ExtractStrings([' '], [], pchar(APos), LFields);
if LFields.Count = 6 then
result := 2 * Pred(StrToInt(LFields[5])) + Ord(LFields[1] = 'b')
else
result := 0;
LFields.Free;
end;
begin
FCurPos.SetPosition(APos);
FHalfmovesCount := GetMoveCount(APos);
end;
function TChessPlayer.PlayMove(const AMove: string): boolean;
var
LAuxPos: TChessPosition;
LMove, LPromo, LFrom, LTo: integer;
begin
MoveToInt(AMove, LMove, LPromo);
LFrom := LMove div 100;
LTo := LMove mod 100;
FCurPos.GenerateMoves;
result := FCurPos.IsLegal(LMove);
if result then
begin
LAuxPos := TChessPosition.Create;
LAuxPos.SetPosition(FCurPos);
LAuxPos.MovePiece(LFrom, LTo, LPromo);
LAuxPos.activeColor := CBlack * LAuxPos.activeColor;
if LAuxPos.Check then
result := FALSE
else
begin
FCurPos.MovePiece(LFrom, LTo, LPromo);
Inc(FHalfmovesCount);
end;
LAuxPos.Free;
end;
end;
function TChessPlayer.BestMoveIndex(const ABestValue: integer): integer;
var
LAuxPos: TChessPosition;
LMaxValue: integer;
LSecondListCount: integer;
i, j: integer;
LLine1, LLine2: string;
begin
LAuxPos := TChessPosition.Create;
with FCurPos do
begin
LSecondListCount := 0;
for i := 1 to moveCount do
if firstList[i].FValue = ABestValue then
begin
Inc(LSecondListCount);
FSecondList[LSecondListCount].FFrom := firstList[i].FFrom;
FSecondList[LSecondListCount].FTo := firstList[i].FTo;
FSecondList[LSecondListCount].FValue := 0
end;
end;
LMaxValue := Low(integer);
result := 0;
LLine1 := '';
LLine2 := '';
for i := 1 to LSecondListCount do
begin
LAuxPos.SetPosition(FCurPos);
with LAuxPos do
begin
if chessboard[FSecondList[i].FFrom] = FIniPos.chessboard[FSecondList[i].FFrom] then
begin
Inc(FSecondList[i].FValue, 5);
if chessboard[FSecondList[i].FFrom] * activeColor = CPawn then
Inc(FSecondList[i].FValue, 2);
end;
if chessboard[FSecondList[i].FFrom] * activeColor = CKing then
if Abs(FSecondList[i].FTo - FSecondList[i].FFrom) = 20 then
Inc(FSecondList[i].FValue, 20)
else
Dec(FSecondList[i].FValue, 10);
if (FHalfmovesCount < 32) and (chessboard[FSecondList[i].FFrom] * activeColor in [CPawn, CBishop, CKnight]) then
Inc(FSecondList[i].FValue, 20);
if (FSecondList[i].FFrom div 10 = 1)
or (FSecondList[i].FFrom div 10 = 8)
or (FSecondList[i].FFrom mod 10 = 1)
or (FSecondList[i].FFrom mod 10 = 8) then
Inc(FSecondList[i].FValue, 2);
if (FSecondList[i].FTo div 10 = 1)
or (FSecondList[i].FTo div 10 = 8)
or (FSecondList[i].FTo mod 10 = 1)
or (FSecondList[i].FTo mod 10 = 8) then
Dec(FSecondList[i].FValue, 2);
end;
LAuxPos.SetPosition(FCurPos);
LAuxPos.MovePiece(FSecondList[i].FFrom, FSecondList[i].FTo, CQueen);
if LAuxPos.chessboard[FSecondList[i].FTo] = FIniPos.chessboard[FSecondList[i].FTo] then
Dec(FSecondList[i].FValue, 10);
LAuxPos.activeColor := CBlack * LAuxPos.activeColor;
LAuxPos.GenerateSimpleMoves;
with LAuxPos do
for j := 1 to moveCount do
begin
Inc(FSecondList[i].FValue);
if chessboard[firstList[j].FTo] <> CNil then
Inc(FSecondList[i].FValue);
end;
LAuxPos.activeColor := CBlack * LAuxPos.activeColor;
LAuxPos.GenerateSimpleMoves;
with LAuxPos do
for j := 1 to moveCount do
begin
Dec(FSecondList[i].FValue);
if chessboard[firstList[j].FTo] <> CNil then
Dec(FSecondList[i].FValue);
end;
{$IFDEF DEBUG}
with FSecondList[i] do
begin
LLine1 := LLine1 + Format('%6s', [MoveToStr(100 * FFrom + FTo)]);
LLine2 := LLine2 + Format('%6d', [FValue]);
end;
{$ENDIF}
if FSecondList[i].FValue >= LMaxValue then
begin
LMaxValue := FSecondList[i].FValue;
result := i;
end;
end;
{$IFDEF DEBUG}
TLog.Append(LLine1);
TLog.Append(LLine2);
{$ENDIF}
LAuxPos.Free;
end;
function TChessPlayer.BestMove(out ACode: TExitCode): string;
const
CCodeIllegal: array[boolean] of TExitCode = (ecStalemate, ecCheckmate);
CCodeLegal: array[boolean] of TExitCode = (ecSuccess, ecCheck);
var
i: integer;
LCheckBefore, LPromo: boolean;
begin
result := '0000';
i := FCurPos.BestEval(FCurPos.activeColor, 1, 32000);
i := BestMoveIndex(i);
if i > 0 then
begin
Inc(FHalfmovesCount);
LCheckBefore := FCurPos.Check;
with FSecondList[i] do
LPromo := FCurPos.MovePiece(FFrom, FTo, CQueen);
FCurPos.activeColor := CBlack * FCurPos.activeColor;
if FCurPos.Check then
ACode := CCodeIllegal[LCheckBefore]
else
begin
with FSecondList[i] do
result := MoveToStr(100 * FFrom + FTo);
if LPromo then
result := result + 'q';
FCurPos.activeColor := CBlack * FCurPos.activeColor;
ACode := CCodeLegal[FCurPos.Check];
end;
end else
ACode := ecError;
if not (ACode in [ecSuccess, ecCheck]) then
TLog.Append(Format('bestmove function exit code %d', [ACode]));
end;
function TChessPlayer.BestMove: string;
var
LCode: TExitCode;
begin
result := BestMove(LCode);
end;
function TChessPlayer.BoardAsText(const APretty: boolean): string;
begin
result := FCurPos.BoardAsText(APretty);
end;
function TChessPlayer.FEN: string;
begin
result := FCurPos.FEN;
end;
function TChessPlayer.SetPositionFromMoves(const APos: string; const AMoves: array of string): string;
var
i: integer;
begin
SetPosition(APos);
for i := Low(AMoves) to High(AMoves) do
PlayMove(AMoves[i]);
result := FEN;
end;
end.