~ubuntu-branches/ubuntu/precise/gnome-games/precise-proposed

« back to all changes in this revision

Viewing changes to glchess/src/chess-engine-uci.vala

  • Committer: Package Import Robot
  • Author(s): Rodrigo Moya
  • Date: 2011-05-30 13:32:04 UTC
  • mfrom: (1.3.4)
  • mto: (163.1.3 precise)
  • mto: This revision was merged to the branch mainline in revision 143.
  • Revision ID: package-import@ubuntu.com-20110530133204-celaq1v1dsxc48q1
Tags: upstream-3.0.2
ImportĀ upstreamĀ versionĀ 3.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
public class ChessEngineUCI : ChessEngine
 
2
{
 
3
    private char[] buffer;
 
4
    private string moves;
 
5
    private string[] options;
 
6
    private bool waiting_for_move;
 
7
 
 
8
    public ChessEngineUCI (string[] options)
 
9
    {
 
10
        this.options = options;
 
11
        buffer = new char[0];
 
12
        moves = "";
 
13
        starting.connect (start_cb);
 
14
    }
 
15
 
 
16
    private void start_cb ()
 
17
    {
 
18
        write_line ("uci");
 
19
    }
 
20
 
 
21
    public override void start_game ()
 
22
    {
 
23
        write_line ("ucinewgame");
 
24
    }
 
25
 
 
26
    public override void request_move ()
 
27
    {
 
28
        if (moves != "")
 
29
            write_line ("position startpos moves" + moves);
 
30
        else
 
31
            write_line ("position startpos");
 
32
        waiting_for_move = true;
 
33
        write_line ("go wtime 30000 btime 30000");
 
34
    }
 
35
 
 
36
    public override void report_move (ChessMove move)
 
37
    {
 
38
        moves += " " + move.get_engine ();
 
39
    }
 
40
 
 
41
    public override void undo ()
 
42
    {
 
43
        if (waiting_for_move)
 
44
            write_line ("stop");
 
45
        waiting_for_move = false;
 
46
        moves = moves.slice (0, moves.last_index_of (" "));
 
47
    }
 
48
 
 
49
    public override void process_input (char[] data)
 
50
    {
 
51
        /* Copy new data */
 
52
        int current = buffer.length;
 
53
        buffer.resize ((int) (buffer.length + data.length));
 
54
        for (int i = 0; i < data.length; i++)
 
55
            buffer[current + i] = data[i];
 
56
 
 
57
        /* Parse lines */
 
58
        while (true)
 
59
        {
 
60
            int offset;
 
61
 
 
62
            for (offset = 0; offset < buffer.length && buffer[offset] != '\n'; offset++);
 
63
            if (offset >= buffer.length)
 
64
                return;
 
65
 
 
66
            buffer[offset] = '\0';
 
67
            string line = (string) buffer;
 
68
 
 
69
            debug ("Read from engine: '%s'", line);
 
70
            
 
71
            string[] tokens = line.split (" ");
 
72
            if (tokens.length > 0)
 
73
            {
 
74
                switch (tokens[0])
 
75
                {
 
76
                case "id":
 
77
                    break;
 
78
 
 
79
                case "uciok":
 
80
                    if (tokens.length != 1)
 
81
                        warning ("Unexpected arguments on uciok: %s", line);
 
82
 
 
83
                    configure ();
 
84
                    break;
 
85
 
 
86
                case "readyok":
 
87
                    if (tokens.length != 1)
 
88
                        warning ("Unexpected arguments on readyok: %s", line);
 
89
 
 
90
                    ready = true;
 
91
                    break;
 
92
 
 
93
                case "bestmove":
 
94
                    if (tokens.length < 2)
 
95
                        warning ("No move with bestmove: %s", line);
 
96
                    debug ("Engine moves %s", tokens[1]);
 
97
                    waiting_for_move = false;
 
98
                    moved (tokens[1]);
 
99
                    break;
 
100
 
 
101
                case "info":
 
102
                    break;
 
103
 
 
104
                case "option":
 
105
                    break;
 
106
 
 
107
                default:
 
108
                    warning ("Unknown command: '%s'", line);
 
109
                    break;
 
110
                }
 
111
            }
 
112
 
 
113
            buffer = buffer[offset+1:buffer.length];
 
114
        }
 
115
    }
 
116
 
 
117
    private void configure ()
 
118
    {
 
119
        foreach (var o in options)
 
120
            write_line ("setoption %s".printf (o));
 
121
        write_line ("isready");
 
122
    }
 
123
}