gnome-chess/engine/chess-engine-uci.vala

144 lines
4.4 KiB
Vala
Raw Normal View History

2014-01-07 02:19:07 +00:00
/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
2013-07-07 21:14:29 +00:00
* Copyright (C) 2010-2013 Robert Ancell
2016-03-15 17:12:58 +00:00
* Copyright (C) 2013-2016 Michael Catanzaro
2013-07-07 21:14:29 +00:00
*
* 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 3 of the License, or (at your option) any later
2013-07-07 21:14:29 +00:00
* version. See http://www.gnu.org/copyleft/gpl.html the full text of the
* license.
*/
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;
private string go_options;
2011-01-25 12:10:00 +00:00
private bool waiting_for_move;
public ChessEngineUCI (string binary, string[] args, uint delay_seconds, string[] options, string[] go_options)
{
base (binary, args, delay_seconds);
2011-01-09 21:36:38 +00:00
this.options = options;
this.go_options = string.joinv (" ", go_options);
buffer = new char[0];
2011-01-25 11:44:15 +00:00
moves = "";
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;
write_line ("go wtime 30000 btime 30000 %s".printf (go_options));
}
public override void report_move (ChessMove move)
{
2011-01-25 11:44:15 +00:00
moves += " " + move.get_engine ();
}
public override void do_undo ()
2011-01-25 11:44:15 +00:00
{
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 (" "));
}
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);
2015-02-23 13:07:42 +00:00
string[] tokens = line.split (" ");
if (tokens.length > 0)
{
switch (tokens[0])
{
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
configure ();
break;
case "readyok":
if (tokens.length != 1)
2011-01-01 01:42:50 +00:00
warning ("Unexpected arguments on readyok: %s", line);
break;
case "bestmove":
if (tokens.length < 2)
2011-01-01 01:42:50 +00:00
warning ("No move with bestmove: %s", line);
2015-07-19 15:37:39 +00:00
/*
* GNU Chess likes to report a move after receiving a stop command,
* and the UCI spec does not seem to prohibit this, so just discard
2015-07-19 15:37:39 +00:00
* the move if we were not expecting it. This commonly occurs when
* the game is over, or after performing undo.
*/
if (waiting_for_move)
{
debug ("Engine moves %s", tokens[1]);
waiting_for_move = false;
moved (tokens[1]);
}
else
{
2015-07-19 15:37:39 +00:00
debug ("Discarding engine move %s during human's turn", tokens[1]);
}
break;
}
}
buffer = buffer[offset+1:buffer.length];
}
}
private void configure ()
{
2011-01-09 21:36:38 +00:00
foreach (var o in options)
{
var line = o.split (" ");
var option_value = line[line.length - 1];
line = line[0:line.length-1];
var option_name = string.joinv (" ", line);
write_line ("setoption name %s value %s".printf (option_name, option_value));
}
write_line ("isready");
}
}