/
alouette.pas
executable file
·237 lines (219 loc) · 8.02 KB
/
alouette.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
{**
@abstract(Programme principal du moteur d'échecs.)
Interface du moteur d'échecs.
}
program Alouette;
uses
{$IFDEF UNIX}
CWString, CThreads,
{$ENDIF}
Classes, SysUtils, Math,
Log, Chess, Move, Best, Utils, Perft, Trees;
const
CAppName = 'Alouette';
CAppVersion = '0.1.4';
CAppAuthor = 'Roland Chastain';
procedure Send(const AMessage: string; const AFlush: boolean = TRUE);
var
LList: TStringList;
i: integer;
begin
WriteLn(output, AMessage);
if AFlush then
Flush(output);
if Pos(LineEnding, AMessage) = 0 then
Log.Append(Concat('<- ', AMessage))
else
begin
LList := TStringList.Create;
LList.Text := AMessage;
for i := 0 to Pred(LList.Count) do
Log.Append(Concat('<- ', LList[i]));
LList.Free;
end;
end;
type
{** Fil d'exécution pour la recherche du meilleur coup. }
TSearchThread = class(TThread)
protected
procedure Execute; override;
end;
var
LPos: TPosition;
LFrc: boolean;
LMoveStr: string;
LTimeAv: cardinal;
LRandMove: boolean;
procedure TSearchThread.Execute;
var
LTime: cardinal;
begin
LTime := GetTickCount64;
LMoveStr := GetBestMove(LPos, LFrc, LTimeAv, LMoveStr, LRandMove);
LTime := GetTickCount64 - LTime;
if not Terminated then
begin
Send(Format('bestmove %s', [LMoveStr]));
Log.Append(Format('** Move computed in %0.3f s', [LTime / 1000]), TRUE);
end;
end;
const
CBoolStr: array[boolean] of string = ('false', 'true');
var
LCmd: string;
LIdx: integer;
LMTime, LWTime, LBTime, LMTG, LWInc, LBInc: integer;
LThread: TSearchThread;
LDepth: integer;
LFen: string;
LBookLine, LBookMove: string;
LBook: array[boolean] of TTreeList;
LBookName: array[boolean] of TFileName;
LColor: boolean;
LUseBook: boolean;
begin
Randomize;
LFrc := FALSE;
LRandMove := (ParamCount = 1) and ((ParamStr(1) = '-r') or (ParamStr(1) = '--random'));
LBookName[FALSE] := Concat(ExtractFilePath(ParamStr(0)), 'white.txt');
LBookName[TRUE] := Concat(ExtractFilePath(ParamStr(0)), 'black.txt');
for LColor := FALSE to TRUE do
begin
LBook[LColor] := TTreeList.Create;
if FileExists(LBookName[LColor]) then
LBook[LColor].LoadFromFileCompact(LBookName[LColor])
else
Log.Append(Format('** File not found: %s', [LBookName[LColor]]));
end;
LBookLine := '';
LUseBook := FALSE;
Send(Format('%s %s', [CAppName, CAppVersion]));
while not Eof do
begin
ReadLn(input, LCmd);
Log.Append(Concat('-> ', LCmd));
if LCmd = 'quit' then
Break
else
if LCmd = 'uci' then
begin
Send(Format('id name %s %s', [CAppName, CAppVersion]), FALSE);
Send(Format('id author %s', [CAppAuthor]), FALSE);
Send(Format('option name UCI_Chess960 type check default %s', [CBoolStr[LFrc]]), FALSE);
Send('uciok');
end else
if LCmd = 'isready' then
begin
Send('readyok');
end else
if LCmd = 'ucinewgame' then
LPos := CNewPos
else
if BeginsWith('position ', LCmd) then
begin
if WordPresent('startpos', LCmd) then
begin
LPos := EncodePosition;
LUseBook := not LRandMove;
end else
if WordPresent('fen', LCmd) then
begin
LFen := GetFen(LCmd);
LPos := EncodePosition(LFen, LFrc);
LUseBook := IsUsualStartPos(LFen) and not LRandMove;
end else
Log.Append(Format('** Unknown command: %s', [LCmd]));
LBookLine := '';
if WordPresent('moves', LCmd) then
for LIdx := 4 to WordsNumber(LCmd) do
begin
LMoveStr := GetWord(LIdx, LCmd);
if IsChessMove(LMoveStr) then
begin
if not Move.DoMove(LPos, LMoveStr) then
Log.Append(Format('** Impossible move: %s', [LMoveStr]));
LBookLine := Concat(LBookLine, ' ', LMoveStr);
end;
end;
end else
if BeginsWith('go', LCmd) then
begin
if IsGoCmd(LCmd, LWTime, LBTime, LWInc, LBinc) then // go wtime 60000 btime 60000 winc 1000 binc 1000
LTimeAv := IfThen(LPos.Side, LBTime div 100 + LBinc, LWTime div 100 + LWInc)
else
if IsGoCmd(LCmd, LWTime, LBTime, LMTG) then // go wtime 59559 btime 56064 movestogo 38
LTimeAv := IfThen(LPos.Side, LBTime div LMTG, LWTime div LMTG)
else
if IsGoCmd(LCmd, LWTime, LBTime) then // go wtime 600000 btime 600000
LTimeAv := IfThen(LPos.Side, LBTime div 100, LWTime div 100)
else
if IsGoCmd(LCmd, LMTime) then // go movetime 500
LTimeAv := LMTime
else
begin
LTimeAv := 1000;
Log.Append(Format('** Unknown command: %s', [LCmd]));
end;
if LUseBook then
begin
LBookMove := LBook[LPos.Side].FindMoveToPlay(Trim(LBookLine), TRUE);
if LBookMove <> '' then
begin
Log.Append(Format('** Book %s', [LBookMove]));
Send(Format('bestmove %s', [LBookMove]));
Continue;
end;
end;
LThread := TSearchThread.Create(TRUE);
with LThread do
begin
FreeOnTerminate := TRUE;
Priority := tpNormal;
Start;
end;
end else
if LCmd = 'stop' then
begin
if Assigned(LThread) then
LThread.Terminate;
Send(Format('bestmove %s', [LMoveStr]));
end else
if BeginsWith('setoption name UCI_Chess960 value ', LCmd) then
LFrc := WordPresent('true', LCmd)
else
if LCmd = 'board' then
Send(PosToText(LPos))
else
if LCmd = 'moves' then
DisplayLegalMoves(LPos)
else
if IsPerftCmd(LCmd, LDepth) then
Start(LPos, IfThen(LDepth < 5, LDepth, 5))
else
if LCmd = 'help' then
Send(
'UCI commands:' + LineEnding +
' go movetime <x>' + LineEnding +
' go wtime <x> btime <x>' + LineEnding +
' go wtime <x> btime <x> movestogo <x>' + LineEnding +
' go wtime <x> btime <x> winc <x> binc <x>' + LineEnding +
' isready' + LineEnding +
' position fen <fen> [moves ...]' + LineEnding +
' position startpos [moves ...]' + LineEnding +
' quit' + LineEnding +
' setoption name UCI_Chess960 value <true,false>' + LineEnding +
' stop' + LineEnding +
' uci' + LineEnding +
' ucinewgame' + LineEnding +
'Custom commands:' + LineEnding +
' board (display the current board)' + LineEnding +
' help' + LineEnding +
' moves (display legal moves for the current position)' + LineEnding +
' perft <x>'
)
else
Log.Append(Format('** Unknown command: %s', [LCmd]));
end;
LBook[FALSE].Free;
LBook[TRUE].Free;
end.