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
|
public class ChessEngineUCI : ChessEngine
{
private char[] buffer;
private string moves;
private string[] options;
private bool waiting_for_move;
public ChessEngineUCI (string[] options)
{
this.options = options;
buffer = new char[0];
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 ()
{
if (moves != "")
write_line ("position startpos moves" + moves);
else
write_line ("position startpos");
waiting_for_move = true;
write_line ("go wtime 30000 btime 30000");
}
public override void report_move (ChessMove move)
{
moves += " " + move.get_engine ();
}
public override void undo ()
{
if (waiting_for_move)
write_line ("stop");
waiting_for_move = false;
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;
debug ("Read from engine: '%s'", line);
string[] tokens = line.split (" ");
if (tokens.length > 0)
{
switch (tokens[0])
{
case "id":
break;
case "uciok":
if (tokens.length != 1)
warning ("Unexpected arguments on uciok: %s", line);
configure ();
break;
case "readyok":
if (tokens.length != 1)
warning ("Unexpected arguments on readyok: %s", line);
ready = true;
break;
case "bestmove":
if (tokens.length < 2)
warning ("No move with bestmove: %s", line);
debug ("Engine moves %s", tokens[1]);
waiting_for_move = false;
moved (tokens[1]);
break;
case "info":
break;
case "option":
break;
default:
warning ("Unknown command: '%s'", line);
break;
}
}
buffer = buffer[offset+1:buffer.length];
}
}
private void configure ()
{
foreach (var o in options)
write_line ("setoption %s".printf (o));
write_line ("isready");
}
}
|