-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhavannahgtp.h
224 lines (193 loc) · 11 KB
/
havannahgtp.h
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
#pragma once
#include "gtp.h"
#include "game.h"
#include "string.h"
#include "solver.h"
#include "solverab.h"
#include "solverpns.h"
#include "solverpns2.h"
#include "solverpns_tt.h"
#include "player.h"
#include "board.h"
#include "move.h"
struct TimeControl {
enum Method { PERCENT, EVEN, STATS };
Method method; //method to use to distribute the remaining time
double param; //param for the method, such as the percentage used or multiple of even
double game;
double move;
bool flexible; //whether time_per_move can be saved for future moves
int max_sims;
TimeControl(){
method = STATS;
param = 2;
game = 0;
move = 5;
flexible = true;
max_sims = 0;
}
string method_name(){
switch(method){
case PERCENT: return "percent";
case EVEN: return "even";
case STATS: return "stats";
default: return "WTF? unknown time control method";
}
}
};
class HavannahGTP : public GTPclient {
HavannahGame game;
public:
int verbose;
bool genmoveextended;
bool hguicoords;
bool colorboard;
TimeControl time;
double time_remain; //time remaining for this game
int mem_allowed;
bool allow_swap;
Player player;
SolverAB solverab;
SolverPNS solverpns;
SolverPNS2 solverpns2;
SolverPNSTT solverpnstt;
HavannahGTP(FILE * i = stdin, FILE * o = stdout, FILE * l = NULL){
GTPclient(i, o, l);
verbose = 1;
genmoveextended = false;
hguicoords = true;
colorboard = true;
time_remain = time.game;
mem_allowed = 1000;
allow_swap = false;
set_board();
newcallback("name", bind(&HavannahGTP::gtp_name, this, _1), "Name of the program");
newcallback("version", bind(&HavannahGTP::gtp_version, this, _1), "Version of the program");
newcallback("verbose", bind(&HavannahGTP::gtp_verbose, this, _1), "Set verbosity, 0 for quiet, 1 for normal, 2+ for more output");
newcallback("extended", bind(&HavannahGTP::gtp_extended, this, _1), "Output extra stats from genmove in the response");
newcallback("debug", bind(&HavannahGTP::gtp_debug, this, _1), "Enable debug mode");
newcallback("echo", bind(&HavannahGTP::gtp_echo, this, _1), "Return the arguments as the response");
newcallback("hguicoords", bind(&HavannahGTP::gtp_hguicoords, this, _1), "Switch coordinate systems to match HavannahGui");
newcallback("gridcoords", bind(&HavannahGTP::gtp_gridcoords, this, _1), "Switch coordinate systems to match Little Golem");
newcallback("colorboard", bind(&HavannahGTP::gtp_colorboard, this, _1), "Turn on or off the colored board");
newcallback("showboard", bind(&HavannahGTP::gtp_print, this, _1), "Show the board");
newcallback("print", bind(&HavannahGTP::gtp_print, this, _1), "Alias for showboard");
newcallback("dists", bind(&HavannahGTP::gtp_dists, this, _1), "Similar to print, but shows minimum win distances");
newcallback("zobrist", bind(&HavannahGTP::gtp_zobrist, this, _1), "Output the zobrist hash for the current move");
newcallback("clear_board", bind(&HavannahGTP::gtp_clearboard, this, _1), "Clear the board, but keep the size");
newcallback("clear", bind(&HavannahGTP::gtp_clearboard, this, _1), "Alias for clear_board");
newcallback("boardsize", bind(&HavannahGTP::gtp_boardsize, this, _1), "Clear the board, set the board size");
newcallback("swap", bind(&HavannahGTP::gtp_swap, this, _1), "Enable/disable swap: swap <0|1>");
newcallback("play", bind(&HavannahGTP::gtp_play, this, _1), "Place a stone: play <color> <location>");
newcallback("white", bind(&HavannahGTP::gtp_playwhite, this, _1), "Place a white stone: white <location>");
newcallback("black", bind(&HavannahGTP::gtp_playblack, this, _1), "Place a black stone: black <location>");
newcallback("undo", bind(&HavannahGTP::gtp_undo, this, _1), "Undo one or more moves: undo [amount to undo]");
newcallback("genmove", bind(&HavannahGTP::gtp_genmove, this, _1), "Generate a move: genmove [color] [time]");
newcallback("move_stats", bind(&HavannahGTP::gtp_move_stats, this, _1), "Output the move stats for the player tree as it stands now");
newcallback("player_solve", bind(&HavannahGTP::gtp_player_solve, this, _1), "Run the player, but don't make the move, and give solve output");
newcallback("player_solved", bind(&HavannahGTP::gtp_player_solved, this, _1), "Output whether the player solved the current node");
newcallback("player_hgf", bind(&HavannahGTP::gtp_player_hgf, this, _1), "Output an hgf of the current tree");
newcallback("player_load_hgf", bind(&HavannahGTP::gtp_player_load_hgf,this, _1), "Load an hgf generated by player_hgf");
newcallback("player_confirm", bind(&HavannahGTP::gtp_confirm_proof, this, _1), "Confirm the outcome of the current tree, for use after loading a proof tree");
newcallback("pv", bind(&HavannahGTP::gtp_pv, this, _1), "Output the principle variation for the player tree as it stands now");
newcallback("time", bind(&HavannahGTP::gtp_time, this, _1), "Set the time limits and the algorithm for per game time");
newcallback("player_params", bind(&HavannahGTP::gtp_player_params, this, _1), "Set the algorithm for the player, no args gives options");
newcallback("player_gammas", bind(&HavannahGTP::gtp_player_gammas, this, _1), "Load the gammas for weighted random from a file");
newcallback("patterns", bind(&HavannahGTP::gtp_patterns, this, _1), "List all legal moves plus their local pattern");
newcallback("all_legal", bind(&HavannahGTP::gtp_all_legal, this, _1), "List all legal moves");
newcallback("history", bind(&HavannahGTP::gtp_history, this, _1), "List of played moves");
newcallback("playgame", bind(&HavannahGTP::gtp_playgame, this, _1), "Play a list of moves");
newcallback("winner", bind(&HavannahGTP::gtp_winner, this, _1), "Check the winner of the game");
newcallback("havannah_winner", bind(&HavannahGTP::gtp_winner, this, _1), "Alias for winner");
newcallback("ab_solve", bind(&HavannahGTP::gtp_solve_ab, this, _1), "Solve with alpha-beta");
newcallback("ab_params", bind(&HavannahGTP::gtp_solve_ab_params, this, _1), "Set Parameters for alpha-beta");
newcallback("ab_stats", bind(&HavannahGTP::gtp_solve_ab_stats, this, _1), "Output the stats for the alpha-beta solver");
newcallback("ab_clear", bind(&HavannahGTP::gtp_solve_ab_clear, this, _1), "Stop the solver and release the memory");
newcallback("pns_solve", bind(&HavannahGTP::gtp_solve_pns, this, _1), "Solve with proof number search and an explicit tree");
newcallback("pns_params", bind(&HavannahGTP::gtp_solve_pns_params, this, _1), "Set Parameters for PNS");
newcallback("pns_stats", bind(&HavannahGTP::gtp_solve_pns_stats, this, _1), "Output the stats for the PNS solver");
newcallback("pns_clear", bind(&HavannahGTP::gtp_solve_pns_clear, this, _1), "Stop the solver and release the memory");
newcallback("pns2_solve", bind(&HavannahGTP::gtp_solve_pns2, this, _1), "Solve with proof number search and an explicit tree");
newcallback("pns2_params", bind(&HavannahGTP::gtp_solve_pns2_params, this, _1), "Set Parameters for PNS");
newcallback("pns2_stats", bind(&HavannahGTP::gtp_solve_pns2_stats, this, _1), "Output the stats for the PNS solver");
newcallback("pns2_clear", bind(&HavannahGTP::gtp_solve_pns2_clear, this, _1), "Stop the solver and release the memory");
newcallback("pnstt_solve", bind(&HavannahGTP::gtp_solve_pnstt, this, _1), "Solve with proof number search and a transposition table of fixed size");
newcallback("pnstt_params", bind(&HavannahGTP::gtp_solve_pnstt_params, this, _1), "Set Parameters for PNSTT");
newcallback("pnstt_stats", bind(&HavannahGTP::gtp_solve_pnstt_stats, this, _1), "Outupt the stats for the PNSTT solver");
newcallback("pnstt_clear", bind(&HavannahGTP::gtp_solve_pnstt_clear, this, _1), "Stop the solver and release the memory");
}
void set_board(bool clear = true){
player.set_board(game.getboard());
solverab.set_board(game.getboard());
solverpns.set_board(game.getboard());
solverpns2.set_board(game.getboard());
solverpnstt.set_board(game.getboard(), clear);
}
void move(const Move & m){
game.move(m);
player.move(m);
solverab.move(m);
solverpns.move(m);
solverpns2.move(m);
solverpnstt.move(m);
}
GTPResponse gtp_echo(vecstr args);
GTPResponse gtp_print(vecstr args);
GTPResponse gtp_zobrist(vecstr args);
string won_str(int outcome) const;
GTPResponse gtp_swap(vecstr args);
GTPResponse gtp_boardsize(vecstr args);
GTPResponse gtp_clearboard(vecstr args);
GTPResponse gtp_undo(vecstr args);
Move parse_move(const string & str);
string move_str(int x, int y, int hguic = -1);
string move_str(Move m, int hguic = -1);
GTPResponse gtp_all_legal(vecstr args);
GTPResponse gtp_history(vecstr args);
GTPResponse gtp_patterns(vecstr args);
GTPResponse play(const string & pos, int toplay);
GTPResponse gtp_playgame(vecstr args);
GTPResponse gtp_play(vecstr args);
GTPResponse gtp_playwhite(vecstr args);
GTPResponse gtp_playblack(vecstr args);
GTPResponse gtp_winner(vecstr args);
GTPResponse gtp_name(vecstr args);
GTPResponse gtp_version(vecstr args);
GTPResponse gtp_verbose(vecstr args);
GTPResponse gtp_extended(vecstr args);
GTPResponse gtp_colorboard(vecstr args);
GTPResponse gtp_hguicoords(vecstr args);
GTPResponse gtp_gridcoords(vecstr args);
GTPResponse gtp_debug(vecstr args);
GTPResponse gtp_dists(vecstr args);
GTPResponse gtp_time(vecstr args);
double get_time();
GTPResponse gtp_move_stats(vecstr args);
GTPResponse gtp_player_solve(vecstr args);
GTPResponse gtp_player_solved(vecstr args);
GTPResponse gtp_pv(vecstr args);
GTPResponse gtp_genmove(vecstr args);
GTPResponse gtp_player_params(vecstr args);
GTPResponse gtp_player_gammas(vecstr args);
GTPResponse gtp_player_hgf(vecstr args);
GTPResponse gtp_player_load_hgf(vecstr args);
GTPResponse gtp_confirm_proof(vecstr args);
string solve_str(int outcome) const;
string solve_str(const Solver & solve);
GTPResponse gtp_solve_ab(vecstr args);
GTPResponse gtp_solve_ab_params(vecstr args);
GTPResponse gtp_solve_ab_stats(vecstr args);
GTPResponse gtp_solve_ab_clear(vecstr args);
GTPResponse gtp_solve_pns(vecstr args);
GTPResponse gtp_solve_pns_params(vecstr args);
GTPResponse gtp_solve_pns_stats(vecstr args);
GTPResponse gtp_solve_pns_clear(vecstr args);
GTPResponse gtp_solve_pns2(vecstr args);
GTPResponse gtp_solve_pns2_params(vecstr args);
GTPResponse gtp_solve_pns2_stats(vecstr args);
GTPResponse gtp_solve_pns2_clear(vecstr args);
GTPResponse gtp_solve_pnstt(vecstr args);
GTPResponse gtp_solve_pnstt_params(vecstr args);
GTPResponse gtp_solve_pnstt_stats(vecstr args);
GTPResponse gtp_solve_pnstt_clear(vecstr args);
};