/
simplego-frame.cpp
467 lines (414 loc) · 14.1 KB
/
simplego-frame.cpp
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include <wx/wx.h>
#include <wx/aboutdlg.h>
#include <wx/textfile.h>
#include <wx/fileconf.h>
#include <wx/stdpaths.h>
#ifndef __WXMSW__
#include <sys/resource.h>
#endif
#include "simplego-frame.h"
#include "simplego-panel.h"
#include "simplego-settings.h"
#include "simplego-statusbar.h"
// Frame constructor - read settings from config file, create the panel and add menus and status bar to it
SimpleGoFrame::SimpleGoFrame(const wxString& title, const wxPoint& pos, const wxSize& size, long style) : wxFrame(NULL, -1, title, pos, size, style)
{ panel = new SimpleGoPanel(this);
wxFileConfig config("simplego", wxEmptyString, wxStandardPaths::Get().GetUserDataDir());
config.Read("boardsize", &(panel->boardsize), 19);
config.Read("blackname", &blackname, "Black");
config.Read("whitename", &whitename, "White");
config.Read("blacklevel", &blacklevel, 0);
config.Read("whitelevel", &whitelevel, 0);
config.Read("timeout", &timeout, 3);
config.Read("komi", &komi, 6.5);
config.Read("suicide", &suicide, 0);
if(!(panel->boardsize>=2 && panel->boardsize<=19))
panel->boardsize = 19;
if(!(blacklevel>=0 && blacklevel<=10))
blacklevel = 0;
if(!(whitelevel>=0 && whitelevel<=10))
whitelevel = 0;
if(!(komi>=-1000 && komi<=1000))
komi = 6.5;
wxMenuBar* menubar = new wxMenuBar;
gamemenu = new wxMenu;
playmenu = new wxMenu;
gamemenu->Append(ID_NEW_GAME, wxT("&New game"));
gamemenu->Append(ID_BOARD_SIZE, wxT("&Board size..."));
gamemenu->Append(ID_SETTINGS, wxT("&Settings..."));
playmenu->Append(ID_PASS, wxT("&Pass"));
playmenu->Append(ID_GO_TO_MOVE, wxT("&Go to move..."));
playmenu->Append(ID_GNUGO, wxT("&Make GNU Go move"));
playmenu->Append(ID_RANDOM, wxT("&Random!"), "", wxITEM_CHECK);
gamemenu->Append(ID_SCORE_GAME, wxT("S&core game"));
gamemenu->Append(ID_LOAD_GAME, wxT("&Load game..."));
gamemenu->Append(ID_SAVE_GAME, wxT("&Save game..."));
gamemenu->AppendSeparator();
gamemenu->Append(ID_ABOUT, wxT("Ab&out..."));
#ifdef __WXMSW__
playmenu->Enable(ID_GNUGO, false);
#else
signal(SIGCHLD, SIG_IGN);
#endif
Connect(ID_NEW_GAME, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(SimpleGoFrame::NewGame));
Connect(ID_BOARD_SIZE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(SimpleGoFrame::GetBoard));
Connect(ID_SETTINGS, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(SimpleGoFrame::Settings));
Connect(ID_PASS, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(SimpleGoFrame::Pass));
Connect(ID_GO_TO_MOVE, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(SimpleGoFrame::GoToMove));
Connect(ID_GNUGO, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(SimpleGoFrame::GNUGoMove));
Connect(ID_SCORE_GAME, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(SimpleGoFrame::ScoreGame));
Connect(ID_LOAD_GAME, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(SimpleGoFrame::LoadGame));
Connect(ID_SAVE_GAME, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(SimpleGoFrame::SaveGame));
Connect(ID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(SimpleGoFrame::About));
menubar->Append(gamemenu, wxT("&Game"));
menubar->Append(playmenu, wxT("&Play"));
SetMenuBar(menubar);
int styles[1] = {wxSB_SUNKEN};
statusbar = new SimpleGoStatusBar(this);
SetStatusBar(statusbar);
PositionStatusBar();
statusbar->SetStatusStyles(1, styles);
SetSize(panel->boardsize);
panel->InitGame();
panel->UpdateBoard();
}
// Frame destructor - save settings in config file
SimpleGoFrame::~SimpleGoFrame()
{ wxFileConfig config("simplego", wxEmptyString, wxStandardPaths::Get().GetUserDataDir());
config.Write("boardsize", panel->boardsize);
config.Write("blackname", sgfload ? prevblackname : blackname);
config.Write("whitename", sgfload ? prevwhitename : whitename);
config.Write("blacklevel", blacklevel);
config.Write("whitelevel", whitelevel);
config.Write("timeout", timeout);
config.Write("komi", sgfload ? prevkomi : komi);
config.Write("suicide", suicide);
}
// Run GNU Go and have it play the next move
void SimpleGoFrame::MakeGNUGoMove()
{ int in[2], out[2], n;
char buf[256];
char data[256] = {0};
char str[256], strtime[32], strkomi[32], strlevel[32];
#ifndef __WXMSW__
pipe(in);
pipe(out);
if(fork()==0)
{ close(STDIN_FILENO);
close(STDOUT_FILENO);
dup2(in[0], STDIN_FILENO);
dup2(out[1], STDOUT_FILENO);
close(in[1]);
close(out[0]);
sprintf(str, "--%s-suicide", suicide || madesuicide ? "allow" : "forbid");
sprintf(strtime, "%d", timeout);
sprintf(strkomi, "%.1f", komi);
sprintf(strlevel, "%d", panel->curmove%2 ? whitelevel : blacklevel);
execlp("gnugo", "gnugo", "--mode", "gtp", "--chinese-rules", "--positional-superko", str, "--level", strlevel[0]=='0' ? "10" : strlevel, "--never-resign", "--komi", strkomi, "--clock", strtime, NULL);
}
close(in[0]);
close(out[1]);
sprintf(str, "boardsize %d\n", panel->boardsize);
write(in[1], str, strlen(str));
while(read(out[0], buf, 1) && buf[0] != '=');
for(int i=0; i<panel->curmove; i++)
{ if(panel->movelist[i].x==0&&panel->movelist[i].y==0)
sprintf(str, "play %s pass\n", i%2 ? "white" : "black");
else
sprintf(str, "play %s %c%d\n", i%2 ? "white" : "black", panel->movelist[i].x+'A'-1+(panel->movelist[i].x>8 ? 1 : 0), (panel->boardsize+1)-panel->movelist[i].y);
write(in[1], str, strlen(str));
while(read(out[0], buf, 1) && buf[0] != '=');
}
sprintf(str, "genmove %s\n", panel->curmove%2 ? "white" : "black");
write(in[1], str, strlen(str));
close(in[1]);
while(read(out[0], buf, 1))
if(buf[0] == '=')
{ while(n = read(out[0], buf, 255))
{ buf[n] = '\0';
strcat(data, buf);
}
if(data[1]>='A' && data[1]<='T' && data[2]>='1' && data[2]<='9')
{ int x = data[1]-'A'+1-(data[1]>'H' ? 1 : 0);
int y = (panel->boardsize+1)-atoi(data+2);
panel->MakeMove(x, y);
}
else
panel->MakePass();
}
close(out[0]);
#endif
}
// Run GNU Go and have it score the game
void SimpleGoFrame::MakeGNUGoScore()
{ int in[2], out[2], n;
char buf[256];
char data[2048] = {0};
char str[256];
memcpy(panel->gnugoboard, panel->board, BOARDMEMORYLEN);
#ifndef __WXMSW__
if(panel->ValidMoveExists())
{ pipe(in);
pipe(out);
if(fork()==0)
{ close(STDIN_FILENO);
close(STDOUT_FILENO);
dup2(in[0], STDIN_FILENO);
dup2(out[1], STDOUT_FILENO);
close(in[1]);
close(out[0]);
struct rlimit limit;
limit.rlim_cur = timeout;
setrlimit(RLIMIT_CPU, &limit);
sprintf(str, "--%s-suicide", suicide ? "allow" : "forbid");
execlp("gnugo", "gnugo", "--mode", "gtp", "--chinese-rules", str, NULL);
}
close(in[0]);
close(out[1]);
sprintf(str, "boardsize %d\n", panel->boardsize);
write(in[1], str, strlen(str));
while(read(out[0], buf, 1) && buf[0] != '=');
for(int i=1; i<=panel->boardsize; i++)
for(int j=1; j<=panel->boardsize; j++)
if(panel->board[i][j]!=EMPTY)
{ sprintf(str, "play %s %c%d\n", panel->board[i][j]==WHITE ? "white" : "black", i+'A'-1+(i>8 ? 1 : 0), (panel->boardsize+1)-j);
write(in[1], str, strlen(str));
while(read(out[0], buf, 1) && buf[0] != '=');
}
sprintf(str, "final_status_list dead\n");
write(in[1], str, strlen(str));
close(in[1]);
while(read(out[0], buf, 1))
if(buf[0] == '=')
{ while(n = read(out[0], buf, 255))
{ buf[n] = '\0';
strcat(data, buf);
}
for(int i=0; i<(int)strlen(data)-1; i++)
if(data[i]>='A' && data[i]<='T' && data[i+1]>='1' && data[i+1]<='9')
{ int x = data[i]-'A'+1-(data[i]>'H' ? 1 : 0);
int y = (panel->boardsize+1)-atoi(data+i+1);
panel->gnugoboard[x][y] = OPP(panel->board[x][y]);
}
}
close(out[0]);
}
#endif
panel->ScoreArea(panel->gnugoboard);
panel->gnugoscore = true;
int blackscore = 0, whitescore = 0;
for(int i=1; i<=panel->boardsize; i++)
for(int j=1; j<=panel->boardsize; j++)
if(panel->gnugoboard[i][j]==BLACK)
blackscore++;
else if(panel->gnugoboard[i][j]==WHITE)
whitescore++;
double fscore = blackscore - whitescore - komi;
if(fscore==0)
score = "Draw";
else if((int)fscore==fscore)
score = wxString::Format("%c+%.0f", fscore>0 ? 'B' : 'W', fabs(fscore));
else
score = wxString::Format("%c+%.1f", fscore>0 ? 'B' : 'W', fabs(fscore));
scoretooltip = wxString::Format("Area score: %d-%d", blackscore, whitescore);
panel->UpdateBoard();
}
// New game menu command
void SimpleGoFrame::NewGame(wxCommandEvent& event)
{ panel->InitGame();
panel->UpdateBoard();
}
// Board size... menu command
void SimpleGoFrame::GetBoard(wxCommandEvent& event)
{ panel->gnugopause = true;
int num = wxAtoi(wxGetTextFromUser("Enter the new board size, between 2 and 19:", "New board size", ""));
if(num>=2&&num<=19)
{ panel->boardsize = num;
panel->InitGame();
SetSize(num);
panel->UpdateBoard();
}
}
// Settings... menu command
void SimpleGoFrame::Settings(wxCommandEvent& event)
{ panel->gnugopause = true;
SimpleGoSettingsDialog dialog(this);
if(dialog.ShowModal()==1)
{ if(!(panel->curmove>=2 && panel->movelist[panel->curmove-1].x==0 && panel->movelist[panel->curmove-2].x==0))
panel->gnugopause = false;
panel->UpdateStatus();
}
}
// Pass menu command
void SimpleGoFrame::Pass(wxCommandEvent& event)
{ panel->MakePass();
}
// Go to move... menu command
void SimpleGoFrame::GoToMove(wxCommandEvent& event)
{ panel->gnugopause = true;
wxString input = wxGetTextFromUser(wxString::Format("Enter the move number to go to, between 0 and %d:", panel->totmove), "Go to move", "");
if(!input.IsSameAs(""))
{ int num = wxAtoi(input);
if(num>=0 && num<=panel->totmove)
{ panel->gnugopause = true;
panel->gnugoscore = false;
panel->curmove = num;
panel->UpdateBoard();
}
}
}
// GNU Go move menu command
void SimpleGoFrame::GNUGoMove(wxCommandEvent& event)
{ MakeGNUGoMove();
}
// Score game menu command
void SimpleGoFrame::ScoreGame(wxCommandEvent& event)
{ MakeGNUGoScore();
}
// Load game... menu command
void SimpleGoFrame::LoadGame(wxCommandEvent& event)
{ panel->gnugopause = true;
wxFileDialog LoadDialog(this, "Load Game", "", "", "*.sgf", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if(LoadDialog.ShowModal()==wxID_OK)
PlaySGF(LoadDialog.GetPath());
}
// Save game... menu command
void SimpleGoFrame::SaveGame(wxCommandEvent& event)
{ char str[15];
time_t rawtime;
time(&rawtime);
strftime(str, 15, "%y%m%d-1.sgf", localtime(&rawtime));
for(int i=2; wxFileExists(str); i++)
sprintf(str+6, "-%d.sgf", i);
panel->gnugopause = true;
wxFileDialog SaveDialog(this, "Save Game", "", str, "*.sgf", wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if(SaveDialog.ShowModal()==wxID_OK)
{ wxFile file(SaveDialog.GetPath(), wxFile::write);
wxString escblackname(blackname), escwhitename(whitename);
escblackname.Replace("\\", "\\\\");
escblackname.Replace("]", "\\]");
escwhitename.Replace("\\", "\\\\");
escwhitename.Replace("]", "\\]");
strftime(str, 15, "%Y-%m-%d", localtime(&rawtime));
file.Write(wxString::Format("(;FF[4]GM[1]SZ[%d]AP[Simple Go:%s]PB[%s]PW[%s]DT[%s]", panel->boardsize, VERSION, escblackname, escwhitename, str));
if((int)komi==komi)
file.Write(wxString::Format("KM[%.0f]", komi));
else
file.Write(wxString::Format("KM[%.1f]", komi));
if(panel->gnugoscore)
file.Write(wxString::Format("RE[%s]", score));
for(int i=0; i<panel->totmove; i++)
{ if(panel->movelist[i].x==0&&panel->movelist[i].y==0)
file.Write(wxString::Format(";%c[]", i%2 ? 'W' : 'B'));
else
file.Write(wxString::Format(";%c[%c%c]", i%2 ? 'W' : 'B', panel->movelist[i].x+'a'-1, panel->movelist[i].y+'a'-1));
}
file.Write(")");
file.Close();
}
}
// About... menu command
void SimpleGoFrame::About(wxCommandEvent& event)
{ wxAboutDialogInfo info;
info.SetName("Simple Go");
info.SetVersion(VERSION);
info.SetDescription("A simple implementation of the game Go\nby Curtis Bright");
info.SetWebSite("http://www.curtisbright.com/simplego/");
wxAboutBox(info);
}
// Set the window size
void SimpleGoFrame::SetSize(int boardsize)
{ if(boardsize>9)
SetClientSize(16*(boardsize+1), 16*(boardsize+1));
else
SetClientSize(160, 160);
}
// Load the game record found in the SGF file with given filename
void SimpleGoFrame::PlaySGF(wxString filename)
{ if(!wxFileExists(filename))
return;
if(!sgfload)
{ prevblackname = blackname;
prevwhitename = whitename;
prevkomi = komi;
}
else
{ blackname = prevblackname;
whitename = prevwhitename;
komi = prevkomi;
sgfload = false;
}
wxFile file(filename);
bool sizeset = false;
wxString str;
file.ReadAll(&str);
bool inpropvalue = false;
int j=0;
for(int i=0; i<str.Len(); i++)
{ if(str.GetChar(i)=='[')
inpropvalue = true;
else if(inpropvalue && str.GetChar(i)==']' && str.GetChar(i-1)!='\\')
inpropvalue = false;
if(inpropvalue || strchr(" \t\r\n\v\f", str.GetChar(i))==NULL)
str.SetChar(j++, str.GetChar(i));
if(!inpropvalue && str.GetChar(i)==')')
break;
}
str.Remove(j);
for(int i=0; i<str.Len(); i++)
{ wxString substr = str.Mid(i, 3);
if(substr.Cmp("SZ[")==0)
{ int num = wxAtoi(str.Mid(i+3, 2));
if(num>=2&&num<=19)
{ panel->boardsize = num;
panel->InitGame();
}
sizeset = true;
}
else if((substr.Find("B[")==1 || substr.Find("W[")==1) && !(substr.GetChar(0)>='A' && substr.GetChar(0)<='Z'))
{ if(!sizeset)
{ panel->boardsize = 19;
panel->InitGame();
sizeset = true;
}
if(str.Mid(i+2, 2).Cmp("[]")==0 || str.Mid(i+2, 4).Cmp("[tt]")==0)
panel->MakePassSGF();
else if(str.Mid(i+3, 1).Cmp("a")>=0 && str.Mid(i+3, 1).Cmp("s")<=0 && str.Mid(i+4, 1).Cmp("a")>=0 && str.Mid(i+4, 1).Cmp("s")<=0)
{ int x = str.Mid(i+3, 1).GetChar(0) - 'a' + 1;
int y = str.Mid(i+4, 1).GetChar(0) - 'a' + 1;
panel->MakeMoveSGF(x, y);
}
}
else if(substr.Cmp("PB[")==0)
{ blackname = "";
i += 3;
while(str.GetChar(i)!=']')
{ if(str.GetChar(i)=='\\')
i++;
blackname += str.GetChar(i++);
}
}
else if(substr.Cmp("PW[")==0)
{ whitename = "";
i += 3;
while(str.GetChar(i)!=']')
{ if(str.GetChar(i)=='\\')
i++;
whitename += str.GetChar(i++);
}
}
else if(substr.Cmp("KM[")==0)
{ int len = str.Mid(i+3).Find(']');
str.Mid(i+3, len).ToDouble(&komi);
}
}
file.Close();
SetSize(panel->boardsize);
if(panel->curmove>=2 && panel->movelist[panel->curmove-2].x==0 && panel->movelist[panel->curmove-1].x==0)
MakeGNUGoScore();
else
panel->UpdateBoard();
sgfload = true;
panel->gnugopause = true;
playmenu->Check(ID_RANDOM, false);
}