Add some enhancements and fixes in the winboard mod

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2168 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
brian-ch 2011-11-27 16:13:27 +00:00
parent d2dccb679c
commit 45a7900c4e
2 changed files with 81 additions and 44 deletions

View File

@ -20,7 +20,7 @@ type
EngineStringList: TStringList;
side : boolean; //Side to move true=human.
public
constructor Create;
constructor Create; override;
procedure CreateUserInterface(); override;
procedure ShowUserInterface(AParent: TWinControl); override;
procedure HideUserInterface(); override;
@ -47,11 +47,11 @@ procedure TWinboardChessModule.CreateUserInterface;
begin
textEnginePatch := TStaticText.Create(nil);
textEnginePatch.SetBounds(20, 20, 180, 50);
textEnginePatch.Caption := 'Full patch to the engine(with args)';
textEnginePatch.Caption := 'Full patch to the engine';
editEnginePatch := TEdit.Create(nil);
editEnginePatch.SetBounds(200, 20, 150, 50);
editEnginePatch.Text := 'gnuchess -x';
editEnginePatch.Text := 'gnuchess';
end;
procedure TWinboardChessModule.ShowUserInterface(AParent: TWinControl);
@ -100,4 +100,6 @@ end;
initialization
RegisterChessModule(TWinboardChessModule.Create);
finalization
vwinboardConn.stopEngine;
end.

View File

@ -11,7 +11,7 @@ unit winboardConn;
interface
uses
Classes, SysUtils, Process, StdCtrls, chessgame;
Classes, SysUtils, Forms, Process, StdCtrls, chessgame;
type
moveInCoord = array[1..2] of TPoint;
@ -27,31 +27,73 @@ type
private
procedure readFromPipe;
function extractMove : string;
function letterToNumber(num:String) : String;
function numberToLetter(n : integer) : String;
function pieceToLetter(piece : TChessTile) : String;
function letterToNumber(num:String) : Integer;
function numberToLetter(n : integer) : String;
procedure detectEngine(path : String);
end;
var
engineProcess : TProcess;
outputText : String;
vwinboardConn : TwinboardConn;
algebraicInput: boolean;
engineMoveComIndex : integer;
engineMoveCommand : array[1..4,1..2] of String;
implementation
procedure TwinboardConn.startEngine(path : String);
begin
engineMoveCommand[1,1]:='gnuchess 5.07';
engineMoveCommand[1,2]:='My move is: ';
engineMoveCommand[2,1]:='phalanx';
engineMoveCommand[2,2]:='my move is ';
engineMoveCommand[3,1]:='gnuchess 5.08';
engineMoveCommand[3,2]:='move ';
engineMoveCommand[4,1]:='gnuchess 6.0.1';
engineMoveCommand[4,2]:='My move is : ';
if engineProcess<>nil then
if engineProcess.Running then
stopEngine;
engineProcess:= TProcess.Create(nil);
engineProcess.CommandLine := path;
engineProcess.Options := engineProcess.Options + [poUsePipes];
engineProcess.Execute;
detectEngine(path);
end;
procedure TwinboardConn.detectEngine(path : String);
var engineOut : String;
extraCommand : String;
begin
//crafty only show his moves in abbreviated algebraic notation, it's not in
//the array 'engineMoveCommand' because this is not implemented yet.
if pos(path,'crafty')<> 0 then algebraicInput:=true;
sleep(50); //just to guarantee that the engine has already tell his initial output
readFromPipe;
if pos('GNU Chess 5.07',outputText)>0 then
engineMoveComIndex:=1;
if pos('Phalanx',outputText)>0 then
engineMoveComIndex:=2;
if pos('GNU Chess 5.08',outputText)>0 then
engineMoveComIndex:=3;
if pos('GNU Chess 6.0.1',outputText)>0 then
engineMoveComIndex:=4;
extraCommand:='xboard'+#13+#10;
EngineProcess.Input.Write(extraCommand[1], length(extraCommand));
sleep(100);
outputText:='';
end;
procedure TwinboardConn.stopEngine;
begin
if engineProcess.Running then
engineProcess.Free;
if engineProcess<>nil then
if engineProcess.Running then
engineProcess.Terminate(0);
engineProcess.free;
end;
procedure TwinboardConn.tellMove(move : String);
@ -67,8 +109,9 @@ var move : String;
begin
sleep(500);
repeat
Application.processMessages;
readFromPipe;
until length(outputText)>10;
until pos(engineMoveCommand[engineMoveComIndex][2],outputText)>0;
move := extractMove;
points := stringToCoord(move);
result:=points;
@ -89,22 +132,6 @@ begin
end;
function TwinboardConn.pieceToLetter(piece : TChessTile) : String;
begin
case piece of
ctWRook : result:= 'R';
ctBRook : result:= 'R';
ctWKnight : result:= 'N';
ctBKnight : result:= 'N';
ctWBishop : result:= 'B';
ctBBishop : result:= 'B';
ctWQueen : result:= 'Q';
ctBQueen : result:= 'Q';
ctWKing : result:= 'K';
ctBKing : result:= 'K';
end;
end;
function TwinboardConn.numberToLetter(n : integer) : String;
begin
case n of
@ -123,26 +150,31 @@ function TwinboardConn.stringToCoord(moveStr : String) : moveInCoord;
var move : moveInCoord;
begin
//need to verify castle here
//if moveStr[1] in ['R','N','B','Q','K'] then
// Delete(moveStr,1,1);
move[1].X:=strToInt(letterToNumber(moveStr[1]));
move[1].Y:=strToInt(moveStr[2]);
move[2].X:=strToInt(letterToNumber(moveStr[3]));
if moveStr[1] in ['P','R','N','B','Q','K'] then
Delete(moveStr,1,1);
move[1].X:=letterToNumber(moveStr[1]);
move[1].Y:=StrToInt(moveStr[2]);
if moveStr[3] in ['x','-'] then
Delete(moveStr,3,1);
if moveStr[4] in ['P','R','N','B','Q','K'] then
Delete(moveStr,4,1);
move[2].X:=letterToNumber(moveStr[3]);
move[2].Y:=strToInt(moveStr[4]);
result:=move;
end;
//Transform collum number in letter, ex.: 1=a, 3=c ....
function TwinboardConn.letterToNumber(num:String) : String;
//Transform collum letter to number
function TwinboardConn.letterToNumber(num:String) : Integer;
begin
case num[1] of
'a' : result:='1';
'b' : result:='2';
'c' : result:='3';
'd' : result:='4';
'e' : result:='5';
'f' : result:='6';
'g' : result:='7';
'h' : result:='8';
'a' : result:=1;
'b' : result:=2;
'c' : result:=3;
'd' : result:=4;
'e' : result:=5;
'f' : result:=6;
'g' : result:=7;
'h' : result:=8;
else result :=0;
end;
end;
@ -183,14 +215,17 @@ function TwinboardConn.extractMove : string;
var move : String;
i,p : integer;
begin
p := pos('My move is: ', outputText);
p:=p+12;
if p>0 then
p := pos(engineMoveCommand[engineMoveComIndex][2],outputText);
if p>=0 then
begin
p := p+length(engineMoveCommand[engineMoveComIndex][2]);
while outputText[p]<>#10 do
begin
move:= move+outputText[p];
inc(p);
end;
end;
result:=move;
outputText:='';
end;