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
| Unit UChessEngine;
Implementation
uses UInterpreteur
// Exemple d'implémentation pour un moteur d'échecs
type
TonEvtWrite = Procedure(StrValues : String) of Object;
TChessEngine = class
private
FInterpreter: TCommandInterpreter;
FPosition: string;
FThinking: Boolean;
FOnWriteLn : TonEvtWriteln;
FOnWrite : TonEvtWrite ;
// Handlers des commandes
function HandleUCI(const Params: TArray<string>): Boolean;
function HandleIsReady(const Params: TArray<string>): Boolean;
function HandlePosition(const Params: TArray<string>): Boolean;
function HandleGo(const Params: TArray<string>): Boolean;
function HandleStop(const Params: TArray<string>): Boolean;
function HandleQuit(const Params: TArray<string>): Boolean;
function HandleSetOption(const Params: TArray<string>): Boolean;
function HandleDebug(const Params: TArray<string>): Boolean;
Procedure _WriteLn(StrValues : String);
Procedure _Write(StrValues : String);
public
constructor Create;
destructor Destroy; override;
procedure InitializeCommands;
Function ProcessInput(const Input: string) : String;
procedure MainLoop;
property OnWriteLn : TonEvtWriteln read FonWriteln write FonWriteln;
property OnWrite : TonEvtWrite read FonWrite write FonWrite;
end;
implementation
{ TChessEngine }
constructor TChessEngine.Create;
begin
inherited Create;
FInterpreter := TCommandInterpreter.Create;
OnWriteLn := FInterpreter.OnWriteLn;
FPosition := 'startpos';
FThinking := False;
InitializeCommands;
end;
destructor TChessEngine.Destroy;
begin
FInterpreter.Free;
inherited Destroy;
end;
procedure TChessEngine.InitializeCommands;
begin
// Commandes UCI standard
FInterpreter.RegisterCommand('uci', HandleUCI, [],
'Initialise le protocole UCI');
FInterpreter.RegisterCommand('isready', HandleIsReady, [],
'Vérifie si le moteur est prêt');
FInterpreter.RegisterCommand('position', HandlePosition, [
TCommandParam.Create('type', ptString, True, '', 'startpos ou fen'),
TCommandParam.Create('moves', ptString, False, '', 'liste des coups')
], 'Définit la position');
FInterpreter.RegisterCommand('go', HandleGo, [
TCommandParam.Create('depth', ptInteger, False, '0', 'profondeur de recherche'),
TCommandParam.Create('time', ptInteger, False, '0', 'temps en millisecondes')
], 'Lance la recherche');
FInterpreter.RegisterCommand('stop', HandleStop, [],
'Arrête la recherche');
FInterpreter.RegisterCommand('quit', HandleQuit, [],
'Quitte le programme', ['exit', 'q']);
FInterpreter.RegisterCommand('setoption', HandleSetOption, [
TCommandParam.Create('name', ptString, True, '', 'nom de l\'option'),
TCommandParam.Create('value', ptString, True, '', 'valeur de l\'option')
], 'Définit une option');
FInterpreter.RegisterCommand('debug', HandleDebug, [
TCommandParam.Create('mode', ptBoolean, False, 'false', 'active/désactive le debug')
], 'Mode debug');
end;
function TChessEngine.HandleUCI(const Params: TArray<string>): Boolean;
begin
_WriteLn('id name MonMoteur 1.0');
_WriteLn('id author VotreNom');
_WriteLn('uciok');
Result := True;
end;
function TChessEngine.HandleIsReady(const Params: TArray<string>): Boolean;
begin
_WriteLn('readyok');
Result := True;
end;
function TChessEngine.HandlePosition(const Params: TArray<string>): Boolean;
begin
if Length(Params) > 1 then
begin
FPosition := Params[1];
_WriteLn('Position définie: ' + FPosition);
end;
Result := True;
end;
function TChessEngine.HandleGo(const Params: TArray<string>): Boolean;
begin
FThinking := True;
_WriteLn('Recherche en cours...');
// Ici tu lanceras votre algorithme de recherche
_WriteLn('bestmove e2e4');
FThinking := False;
Result := True;
end;
function TChessEngine.HandleStop(const Params: TArray<string>): Boolean;
begin
if FThinking then
begin
FThinking := False;
_WriteLn('Recherche interrompue');
end;
Result := True;
end;
function TChessEngine.HandleQuit(const Params: TArray<string>): Boolean;
begin
_WriteLn('Au revoir!');
Result := False; // Indique qu'il faut arrêter la boucle principale
end;
function TChessEngine.HandleSetOption(const Params: TArray<string>): Boolean;
begin
if Length(Params) >= 3 then
_WriteLn(Format('Option %s définie à %s', [Params[1], Params[2]]));
Result := True;
end;
function TChessEngine.HandleDebug(const Params: TArray<string>): Boolean;
begin
if Length(Params) > 1 then
FInterpreter.DebugMode := LowerCase(Params[1]) = 'true';
_WriteLn('Mode debug: ' + BoolToStr(FInterpreter.DebugMode, True));
Result := True;
end;
Function TChessEngine.ProcessInput(const Input: string) : Boolean;
begin
Result := FInterpreter.ExecuteCommand(Input);
end;
procedure TChessEngine.MainLoop;
var
Input: string;
Continue: Boolean;
begin
Continue := True;
_WriteLn('Moteur d''échecs - Tapez "help" pour l''aide');
while Continue do
begin
_Write('> ');
ReadLn(Input);
if LowerCase(Trim(Input)) = 'help' then
_WriteLn(FInterpreter.GetHelp)
else
Continue := ProcessInput(Input);
end;
end;
Procedure TChessEngine._WriteLn(StrValue : String) ;
Begin
If Assigned(FonWriteln) Then
FonWriteln(StrValue);
End;
end. |
Partager