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
|
(********************************************************)
(* Example of a WinBoard-protocol driver, by H.G.Muller *)
(* Rewritten in Pascal by R. Chastain *)
(********************************************************)
uses
{$IFDEF UNIX}
CThreads, CWString,
{$ENDIF}
SysUtils, Classes, ChessPlayer, ChessPlayerTypes, Log;
const
// four different constants, with values for WHITE and BLACK that suit your engine
WHITE = 1;
BLACK = 2;
NONE = 0;
ANALYZE = 3;
// some value that cannot occur as a valid move
INVALID = 666;
// some parameter of your engine
MAXMOVES = 500; (* maximum game length *)
MAXPLY = 60; (* maximum search depth *)
OFF = 0;
ON_ = 1;
DEFAULT_FEN = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
type
TMOVE = integer; // in this example moves are encoded as an int
var
moveNr: integer = 0; // part of game state; incremented by MakeMove
gameMove: array[0..MAXMOVES-1] of TMOVE; // holds the game history
// Some routines your engine should have to do the various essential things
function MakeMove(const stm: integer; const move_: TMOVE): integer; forward; // performs move, and returns new side to move
procedure UnMake(const move_: TMOVE); forward; // unmakes the move;
function SetUp(const fen: string): integer; forward; // sets up the position from the given FEN, and returns the new side to move
procedure SetMemorySize(const n: integer); forward; // if n is different from last time, resize all tables to make memory usage below n MB
function MoveToText(const move_: TMOVE): string; forward; // converts the move from your internal format to text like e2e2, e1g1, a7a8q.
function ParseMove(const moveText: string): integer; forward; // converts a long-algebraic text move to your internal move format
function SearchBestMove(const stm, timeLeft, mps, timeControl, inc_, timePerMove: integer; var move_, ponderMove: TMOVE): integer; forward;
procedure PonderUntilInput(const stm: integer); forward; // Search current position for stm, deepening forever until there is input.
// Some global variables that control your engine's behavior
var
ponder,
randomize_,
postThinking,
resign, // engine-defined option
contemptFactor: integer; // likewise
procedure Send(const AMessage: string);
begin
ToLog({$I %FILE%}, {$I %LINE%}, Format('<< %s', [AMessage]));
WriteLn(AMessage);
Flush(output);
end;
type
TSender = class(TThread)
private
FText: string;
protected
procedure Execute; override;
public
constructor Create(const AText: string; const ACreateSuspended: boolean = TRUE);
end;
constructor TSender.Create(const AText: string; const ACreateSuspended: boolean);
begin
inherited Create(ACreateSuspended);
FreeOnTerminate := TRUE;
FText := AText;
end;
procedure TSender.Execute;
begin
Send(FText);
end;
function TakeBack(const n: integer): integer; // reset the game and then replay it to the desired point
var
last, stm, nr: integer;
begin
stm := SetUp('');
last := moveNr - n;
if last < 0 then last := 0;
for nr := 0 to Pred(moveNr) do stm := MakeMove(stm, gameMove[nr]);
result := stm;
end;
procedure PrintResult(const stm, score: integer);
begin
if score = 0 then Send('1/2-1/2');
if (score > 0) and (stm = WHITE) or (score < 0) and (stm = BLACK) then Send('1-0')
else Send('0-1');
end;
function main: integer;
var
stm: integer; // side to move
engineSide: integer = NONE; // side played by engine
timeLeft: integer;
mps, timeControl, inc_, timePerMove: integer; // time-control parameters, to be used by Search
maxDepth: integer; // used by search
move_, ponderMove: integer;
inBuf: string;
command: string;
score: integer;
min: integer;
sec: integer = 0;
//newStm: integer;
nping: integer;
sender: TSender;
label
noPonder;
begin
ToLog({$I %FILE%}, {$I %LINE%}, ParamStr(0), TRUE);
while {not EOF}TRUE do // infinite loop
begin
ToLog({$I %FILE%}, {$I %LINE%}, Format('stm=%d engineSide=%d', [stm, engineSide]));
Flush(output); // make sure everything is printed before we do something that might take time
if stm = engineSide then // if it is the engine's turn to move, set it thinking, and let it move
begin
score := SearchBestMove(stm, timeLeft, mps, timeControl, inc_, timePerMove, move_, ponderMove);
if move_ = INVALID then // game apparently ended
begin
engineSide := NONE; // so stop playing
PrintResult(stm, score);
end else
begin
stm := MakeMove(stm, move_); // assumes MakeMove returns new side to moves
gameMove[moveNr] := move_; // remember game
Inc(moveNr);
Send(Format('move %s', [MoveToText(move_)]));
(*
sender := TSender.Create(Format('move %s', [MoveToText(move_)]));
sender.Start;
*)
end;
end;
(*
Flush(output); // make sure everything is printed before we do something that might take time
// now it is not our turn (anymore)
if engineSide = ANALYZE then // in analysis, we always ponder the position
PonderUntilInput(stm)
else
if (engineSide <> NONE) and (ponder = ON_) and (moveNr <> 0) then // ponder while waiting for input
begin
if ponderMove = INVALID then // if we have no move to ponder on, ponder the position
PonderUntilInput(stm)
else
begin
newStm := MakeMove(stm, ponderMove);
PonderUntilInput(newStm);
UnMake(ponderMove);
end;
end;
*)
noPonder:
ReadLn(inBuf);
ToLog({$I %FILE%}, {$I %LINE%}, Format('>> %s', [inBuf]));
SScanf(inBuf, '%s', [@command]);
if command = 'quit' then
Break;
if command = 'force' then begin engineSide := NONE; Continue; end;
if command = 'analyze' then begin engineSide := ANALYZE; Continue; end;
if command = 'exit' then begin engineSide := NONE; Continue; end;
if command = 'otim' then goto noPonder; // do not start pondering after receiving time commands, as move will follow immediately
if command = 'time' then begin SScanf(inBuf, 'time %d', @timeLeft); goto noPonder; end;
if command = 'level' then
begin
if (SScanf(inBuf, 'level %d %d %d', [@mps, @min, @inc_]) = 3) // if this does not work, it must be min:sec format
or (SScanf(inBuf, 'level %d %d:%d %d', [@mps, @min, @sec, @inc_]) = 4) then
;
timeControl := 60 * min + sec;
timePerMove := -1;
Continue;
end;
if command = 'protover' then
begin
//Send('feature ping=1 setboard=1 colors=0 usermove=1 memory=1 debug=1 reuse=0 sigint=0 sigterm=0 myname="Moustique"');
Send('feature ping=1 setboard=1 usermove=1 memory=1 sigint=0 sigterm=0 myname="Moustique II 0.1"');
Send('feature done=1');
Continue;
end;
if command = 'option' then // setting of engine-define option; find out which
begin
(*
if(sscanf(inBuf+7, "Resign=%d", &resign) == 1) continue;
if(sscanf(inBuf+7, "Contempt=%d", &contemptFactor) == 1) continue;
continue;
*)
end;
if command = 'sd' then begin SScanf(inBuf, 'sd %d', [@maxDepth]); Continue; end;
if command = 'st' then begin SScanf(inBuf, 'st %d', [@timePerMove]); Continue; end;
if command = 'memory' then begin SetMemorySize(StrToIntDef(Copy(inBuf, 8), 0)); Continue; end;
if command = 'ping' then begin SScanf(inBuf, 'ping %d', [@nping]); Send(Format('pong %d', [nping])); Continue; end;
if command = 'new' then begin engineSide := BLACK; stm := Setup(DEFAULT_FEN); maxDepth := MAXPLY; randomize_ := OFF; Continue; end;
if command = 'setboard' then begin engineSide := NONE; stm := Setup(Copy(inBuf, 10)); Continue; end;
if command = 'easy' then begin ponder := OFF; Continue; end;
if command = 'hard' then begin ponder := ON_; Continue; end;
if command = 'undo' then begin stm := TakeBack(1); Continue; end;
if command = 'remove' then begin stm := TakeBack(2); Continue; end;
if command = 'go' then begin engineSide := stm; Continue; end;
if command = 'post' then begin postThinking := ON_; Continue; end;
if command = 'nopost' then begin postThinking := OFF; Continue; end;
if command = 'random' then begin randomize_ := ON_; Continue; end;
if command = 'hint' then begin if ponderMove <> INVALID then Send(Format('Hint: %s', [MoveToText(ponderMove)])); Continue; end;
if command = 'book' then Continue;
// ignored commands:
if command = 'xboard' then Continue;
if command = 'computer' then Continue;
if command = 'name' then Continue;
if command = 'ics' then Continue;
if command = 'accepted' then Continue;
if command = 'rejected' then Continue;
if command = 'variant' then Continue;
if command = '' then Continue;
if command = 'usermove' then
begin
move_ := ParseMove(Copy(inBuf, 10));
if move_ = INVALID then Send('Illegal move')
else
begin
stm := MakeMove(stm, move_);
ponderMove := INVALID;
Inc(moveNr);
gameMove[moveNr] := move_; // remember game
end;
Continue;
end;
ToLog({$I %FILE%}, {$I %LINE%}, Format('Unknown command: "%s"', [inBuf]));
end;
result := 0;
end;
var
LChessPlayer: TChessPlayer;
function MakeMove(const stm: integer; const move_: TMOVE): integer;
begin
ToLog({$I %FILE%}, {$I %LINE%}, Format('MakeMove(%d, %d)', [stm, move_]));
if LChessPlayer.PlayMove(move_) then
begin
if stm = WHITE then
result := BLACK
else
result := WHITE;
end else
ToLog({$I %FILE%}, {$I %LINE%}, Format('Coup refusé par le joueur d''échecs artificiel : %d', [move_]));
ToLog({$I %FILE%}, {$I %LINE%}, Format('MakeMove = %d', [result]));
end;
procedure UnMake(const move_: TMOVE);
begin
end;
function SetUp(const fen: string): integer;
begin
ToLog({$I %FILE%}, {$I %LINE%}, Format('SetUp(%s)', [fen]));
LChessPlayer.SetPosition(fen);
result := WHITE;
ToLog({$I %FILE%}, {$I %LINE%}, Format('SetUp = %d', [result]));
end;
procedure SetMemorySize(const n: integer);
begin
end;
function MoveToText(const move_: TMOVE): string;
begin
//Log(Format('%s MoveToText(%d)', [TimeToStr(Time), move_]));
result := ChessPlayerTypes.MoveToStr(move_);
end;
function ParseMove(const moveText: string): integer;
begin
//Log(Format('%s ParseMove(%s)', [TimeToStr(Time), moveText]));
result := ChessPlayerTypes.MoveToInt(moveText);
end;
function SearchBestMove(const stm, timeLeft, mps, timeControl, inc_, timePerMove: integer; var move_, ponderMove: TMOVE): integer;
begin
//Log(Format('%s SearchBestMove(...)', [TimeToStr(Time)]));
move_ := LChessPlayer.BestMove;
result := 0;
end;
procedure PonderUntilInput(const stm: integer);
begin
end;
begin
LChessPlayer := TChessPlayer.Create;
ExitCode := main;
LChessPlayer.Free;
end. |
Partager