-
Notifications
You must be signed in to change notification settings - Fork 3
/
PisqPipe.cs
299 lines (278 loc) · 10.5 KB
/
PisqPipe.cs
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/** functions that communicate with Piskvork manager through pipes */
/** don't modify this file */
using System;
using System.Threading;
abstract class GomocupInterface
{
/* information about a game - you should use these variables */
public int width, height; /* the board size */
public int info_timeout_turn = 30000; /* time for one turn in milliseconds */
public int info_timeout_match = 1000000000; /* total time for a game */
public int info_time_left = 1000000000; /* left time for a game */
public int info_max_memory = 0; /* maximum memory in bytes, zero if unlimited */
public int info_game_type = 1; /* 0:human opponent, 1:AI opponent, 2:tournament, 3:network tournament */
public bool info_exact5 = false; /* false:five or more stones win, true:exactly five stones win */
public bool info_renju = false; /* false:gomoku, true:renju */
public bool info_continuous = false; /* false:single game, true:continuous */
public int terminate; /* return from brain_turn when terminate>0 */
public int start_time; /* tick count at the beginning of turn */
public string dataFolder; /* folder for persistent files, can be null */
/* you have to implement these functions */
abstract public string brain_about { get; } /* copyright, version, homepage */
abstract public void brain_init(); /* create the board and call Console.WriteLine("OK"); or Console.WriteLine("ERROR Maximal board size is .."); */
abstract public void brain_restart(); /* delete old board, create new board, call Console.WriteLine("OK"); */
abstract public void brain_turn(); /* choose your move and call do_mymove(x,y); 0<=x<width, 0<=y<height */
abstract public void brain_my(int x, int y); /* put your move to the board */
abstract public void brain_opponents(int x, int y); /* put opponent's move to the board */
abstract public void brain_block(int x, int y); /* square [x,y] belongs to a winning line (when info_continuous is 1) */
abstract public int brain_takeback(int x, int y); /* clear one square; return value: 0:success, 1:not supported, 2:error */
abstract public void brain_end(); /* delete temporary files, free resources */
virtual public void brain_eval(int x, int y) { } /* display evaluation of square [x,y] */
private string cmd;
private AutoResetEvent event1;
private ManualResetEvent event2;
/** read a line from STDIN */
private void get_line()
{
cmd = Console.ReadLine();
if (cmd == null) Environment.Exit(0);
}
/** parse coordinates x,y */
private bool parse_coord2(string param, out int x, out int y)
{
string[] p = param.Split(',');
if (p.Length == 2 && int.TryParse(p[0], out x) && int.TryParse(p[1], out y) && x >= 0 && y >= 0)
return true;
x = y = 0;
return false;
}
/** parse coordinates x,y */
private bool parse_coord(string param, out int x, out int y)
{
return parse_coord2(param, out x, out y) && x < width && y < height;
}
/** parse coordinates x,y and player number z */
private void parse_3int_chk(string param, out int x, out int y, out int z)
{
string[] p = param.Split(',');
if (!(p.Length == 3 && int.TryParse(p[0], out x) && int.TryParse(p[1], out y) && int.TryParse(p[2], out z)
&& x >= 0 && y >= 0 && x < width && y < height))
x = y = z = 0;
}
/** return pointer to word after command if input starts with command, otherwise return NULL */
private static string get_cmd_param(string command, out string param)
{
param = "";
int pos = command.IndexOf(' ');
if (pos >= 0)
{
param = command.Substring(pos + 1).TrimStart(' ');
command = command.Substring(0, pos);
}
return command.ToLower();
}
/** send suggest */
protected void suggest(int x, int y)
{
Console.WriteLine("SUGGEST {0},{1}", x, y);
}
/** write move to the pipe and update internal data structures */
protected void do_mymove(int x, int y)
{
brain_my(x, y);
Console.WriteLine("{0},{1}", x, y);
}
/** main function for the working thread */
private void threadLoop()
{
for (; ; )
{
event1.WaitOne();
brain_turn();
event2.Set();
}
}
/** start thinking */
private void turn()
{
terminate = 0;
event2.Reset();
event1.Set();
}
/** stop thinking */
private void stop()
{
terminate = 1;
event2.WaitOne();
}
private void start()
{
start_time = Environment.TickCount;
stop();
if (width == 0)
{
width = height = 20;
brain_init();
}
}
/** do command cmd */
private void do_command()
{
string param, info;
int x, y, who, e;
switch (get_cmd_param(cmd, out param))
{
case "info":
switch (get_cmd_param(param, out info))
{
case "max_memory":
int.TryParse(info, out info_max_memory); break;
case "timeout_match":
int.TryParse(info, out info_timeout_match); break;
case "timeout_turn":
int.TryParse(info, out info_timeout_turn); break;
case "time_left":
int.TryParse(info, out info_time_left); break;
case "game_type":
int.TryParse(info, out info_game_type); break;
case "rule":
if (int.TryParse(info, out e))
{
info_exact5 = (e & 1) != 0;
info_continuous = (e & 2) != 0;
info_renju = (e & 4) != 0;
}
break;
case "folder":
dataFolder = info; break;
case "evaluate":
if (parse_coord(info, out x, out y)) brain_eval(x, y);
break;
/* unknown info is ignored */
}
break;
case "start":
if (!int.TryParse(param, out width) || width < 5)
{
width = 0;
Console.WriteLine("ERROR bad START parameter");
}
else
{
height = width;
start();
brain_init();
}
break;
case "rectstart":
if (!parse_coord2(param, out width, out height) || width < 5 || height < 5)
{
width = height = 0;
Console.WriteLine("ERROR bad RECTSTART parameters");
}
else
{
start();
brain_init();
}
break;
case "restart":
start();
brain_restart();
break;
case "turn":
start();
if (!parse_coord(param, out x, out y))
{
Console.WriteLine("ERROR bad coordinates");
}
else
{
brain_opponents(x, y);
turn();
}
break;
case "play":
start();
if (!parse_coord(param, out x, out y))
{
Console.WriteLine("ERROR bad coordinates");
}
else
{
do_mymove(x, y);
}
break;
case "begin":
start();
turn();
break;
case "about":
Console.WriteLine(brain_about);
break;
case "end":
stop();
brain_end();
Environment.Exit(0);
break;
case "board":
start();
for (; ; ) /* fill the whole board */
{
get_line();
parse_3int_chk(cmd, out x, out y, out who);
if (who == 1) brain_my(x, y);
else if (who == 2) brain_opponents(x, y);
else if (who == 3) brain_block(x, y);
else
{
if (!cmd.Equals("done", StringComparison.InvariantCultureIgnoreCase))
Console.WriteLine("ERROR x,y,who or DONE expected after BOARD");
break;
}
}
turn();
break;
case "takeback":
start();
string t = "ERROR bad coordinates";
if (parse_coord(param, out x, out y))
{
e = brain_takeback(x, y);
if (e == 0) t = "OK";
else if (e == 1) t = "UNKNOWN";
}
Console.WriteLine(t);
break;
default:
Console.WriteLine("UNKNOWN command");
break;
}
}
/** main function for AI console application */
public void main()
{
try
{
int dummy = Console.WindowHeight;
//ERROR, process started from the Explorer or command line
Console.WriteLine("MESSAGE Gomoku AI should not be started directly. Please install gomoku manager (http://sourceforge.net/projects/piskvork). Then enter path to this exe file in players settings.");
}
catch (System.IO.IOException)
{
//OK, process started from the Piskvork manager
}
event1 = new AutoResetEvent(false);
new Thread(threadLoop).Start();
event2 = new ManualResetEvent(true);
for (; ; )
{
get_line();
do_command();
}
}
static void Main(string[] args)
{
new GomocupEngine().main();
}
}