2013-07-07 21:14:29 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010-2013 Robert Ancell
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation, either version 2 of the License, or (at your option) any later
|
|
|
|
* version. See http://www.gnu.org/copyleft/gpl.html the full text of the
|
|
|
|
* license.
|
|
|
|
*/
|
|
|
|
|
2010-12-08 07:18:30 +00:00
|
|
|
public class ChessEngineUCI : ChessEngine
|
|
|
|
{
|
|
|
|
private char[] buffer;
|
2011-01-25 11:44:15 +00:00
|
|
|
private string moves;
|
2011-01-09 21:36:38 +00:00
|
|
|
private string[] options;
|
2011-01-25 12:10:00 +00:00
|
|
|
private bool waiting_for_move;
|
|
|
|
|
2013-07-21 17:12:43 +00:00
|
|
|
public ChessEngineUCI (string binary, string[] args, string[] options)
|
2010-12-08 07:18:30 +00:00
|
|
|
{
|
2013-05-12 00:49:23 +00:00
|
|
|
base (binary, args);
|
2011-01-09 21:36:38 +00:00
|
|
|
this.options = options;
|
2010-12-08 07:18:30 +00:00
|
|
|
buffer = new char[0];
|
2011-01-25 11:44:15 +00:00
|
|
|
moves = "";
|
2010-12-08 07:18:30 +00:00
|
|
|
starting.connect (start_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void start_cb ()
|
|
|
|
{
|
|
|
|
write_line ("uci");
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void start_game ()
|
|
|
|
{
|
|
|
|
write_line ("ucinewgame");
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void request_move ()
|
|
|
|
{
|
2011-01-25 11:44:15 +00:00
|
|
|
if (moves != "")
|
|
|
|
write_line ("position startpos moves" + moves);
|
|
|
|
else
|
|
|
|
write_line ("position startpos");
|
2011-01-25 12:10:00 +00:00
|
|
|
waiting_for_move = true;
|
2010-12-08 07:18:30 +00:00
|
|
|
write_line ("go wtime 30000 btime 30000");
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void report_move (ChessMove move)
|
|
|
|
{
|
2011-01-25 11:44:15 +00:00
|
|
|
moves += " " + move.get_engine ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void undo ()
|
|
|
|
{
|
2011-01-25 12:10:00 +00:00
|
|
|
if (waiting_for_move)
|
|
|
|
write_line ("stop");
|
|
|
|
waiting_for_move = false;
|
2011-01-25 11:44:15 +00:00
|
|
|
moves = moves.slice (0, moves.last_index_of (" "));
|
2010-12-08 07:18:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void process_input (char[] data)
|
|
|
|
{
|
|
|
|
/* Copy new data */
|
|
|
|
int current = buffer.length;
|
|
|
|
buffer.resize ((int) (buffer.length + data.length));
|
|
|
|
for (int i = 0; i < data.length; i++)
|
|
|
|
buffer[current + i] = data[i];
|
|
|
|
|
|
|
|
/* Parse lines */
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
for (offset = 0; offset < buffer.length && buffer[offset] != '\n'; offset++);
|
|
|
|
if (offset >= buffer.length)
|
|
|
|
return;
|
|
|
|
|
|
|
|
buffer[offset] = '\0';
|
|
|
|
string line = (string) buffer;
|
|
|
|
|
2011-01-01 01:42:50 +00:00
|
|
|
debug ("Read from engine: '%s'", line);
|
2010-12-08 07:18:30 +00:00
|
|
|
|
|
|
|
string[] tokens = line.split (" ");
|
|
|
|
if (tokens.length > 0)
|
|
|
|
{
|
|
|
|
switch (tokens[0])
|
|
|
|
{
|
|
|
|
case "id":
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "uciok":
|
|
|
|
if (tokens.length != 1)
|
2011-01-01 01:42:50 +00:00
|
|
|
warning ("Unexpected arguments on uciok: %s", line);
|
2011-01-09 21:36:38 +00:00
|
|
|
|
2010-12-08 07:18:30 +00:00
|
|
|
configure ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "readyok":
|
|
|
|
if (tokens.length != 1)
|
2011-01-01 01:42:50 +00:00
|
|
|
warning ("Unexpected arguments on readyok: %s", line);
|
2010-12-08 07:18:30 +00:00
|
|
|
|
|
|
|
ready = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "bestmove":
|
|
|
|
if (tokens.length < 2)
|
2011-01-01 01:42:50 +00:00
|
|
|
warning ("No move with bestmove: %s", line);
|
|
|
|
debug ("Engine moves %s", tokens[1]);
|
2011-01-25 12:10:00 +00:00
|
|
|
waiting_for_move = false;
|
2010-12-08 07:18:30 +00:00
|
|
|
moved (tokens[1]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "info":
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "option":
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-01-01 01:42:50 +00:00
|
|
|
warning ("Unknown command: '%s'", line);
|
2010-12-08 07:18:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = buffer[offset+1:buffer.length];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void configure ()
|
|
|
|
{
|
2011-01-09 21:36:38 +00:00
|
|
|
foreach (var o in options)
|
|
|
|
write_line ("setoption %s".printf (o));
|
2010-12-08 07:18:30 +00:00
|
|
|
write_line ("isready");
|
|
|
|
}
|
|
|
|
}
|