forked from online-go/score-estimator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathestimator.h
111 lines (90 loc) · 2.69 KB
/
estimator.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
#ifndef ESTIMATOR_H
#define ESTIMATOR_H
/* vim: set tabstop=4 expandtab foldmethod=marker: */
#ifdef DEBUG
# include <iostream>
using namespace std;
#endif
#define MAX_WIDTH 25
#define MAX_HEIGHT 25
#define MAX_SIZE (MAX_WIDTH*MAX_HEIGHT)
namespace score_estimator {
enum Color {
EMPTY = 0,
BLACK = 1,
WHITE = -1
};
inline Color other(Color c) { return c == BLACK ? WHITE : BLACK; }
class Point {
public:
int x,y;
Point(){}
Point(int _x, int _y=0) : x(_x), y(_y) {}
bool operator==(const Point &p) const { return x == p.x && y == p.y; }
bool operator!=(const Point &p) const { return x != p.x || y != p.y; }
};
template<int SIZE>
class Vec_t {
public:
Point points[SIZE];
int size;
public:
Vec_t() {
size = 0;
}
Point operator[](const int &i) const { return points[i]; }
Point& operator[](const int &i) { return points[i]; }
void push(const Point &p) {
points[size++] = p;
}
Point remove(int idx) {
Point ret = points[idx];
points[idx] = points[--size];
return ret;
}
};
typedef Vec_t<MAX_SIZE> Vec;
typedef Vec_t<4> NeighborVec;
enum Result {
OK = 0,
ILLEGAL = 1,
};
class Goban {
public:
int width;
int height;
int board[MAX_HEIGHT][MAX_WIDTH];
int do_ko_check;
Point possible_ko;
Goban();
Goban(const Goban &other);
Goban estimate(Color player_to_move, int trials, float tolerance);
Point generateMove(Color player, int trials, float tolerance);
inline int operator[](const Point &p) const { return board[p.y][p.x]; }
inline int& operator[](const Point &p) { return board[p.y][p.x]; }
int score();
void setSize(int width, int height);
void clearBoard();
void play_out_position(Color player_to_move);
Result place_and_remove(Point move, Color player, Vec &possible_moves);
private:
void init();
void get_neighbors(const Point &pt, NeighborVec &output);
bool has_liberties(const Point &pt);
int remove_group(Point move, Vec &possible_moves);
bool is_eye(Point move, Color player);
bool is_territory(Point pt, Color player);
void fill_territory(Point pt, Color player);
void synchronize_tracking_counters(int track[MAX_HEIGHT][MAX_WIDTH], Goban &visited, Point &p);
#ifdef DEBUG
public:
void print();
Point pointFromStr(const char *str);
void showBoard();
#endif
};
#ifdef DEBUG
ostream& operator<<(ostream &o, const Point &pt);
#endif
} /* namespace baduk */
#endif /* ESTIMATOR_H */