This repository was archived by the owner on Feb 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboard.hpp
More file actions
185 lines (172 loc) · 5.59 KB
/
board.hpp
File metadata and controls
185 lines (172 loc) · 5.59 KB
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
#pragma once
#include "dset.hpp"
#include <array>
#include <bitset>
#include <iostream>
#include <random>
#define BIT_TEST _Unchecked_test
const auto MASK_RIGHT = ~std::bitset<81>("100000000"
"100000000"
"100000000"
"100000000"
"100000000"
"100000000"
"100000000"
"100000000"
"100000000"),
MASK_LEFT = MASK_RIGHT << 1;
class Board {
public:
using board_t = std::bitset<81>;
constexpr size_t operator[](size_t p) const noexcept {
return static_cast<size_t>(board_[0][p]) +
static_cast<size_t>(board_[1][p]) * 2u;
}
bool place(size_t bw, size_t p) noexcept {
// assert(bw == 0 || bw == 1);
if (forbid_[bw].BIT_TEST(p)) {
return false;
}
// place
auto &forbid = forbid_[bw], &forbid_op = forbid_[1 - bw];
auto &board = board_[bw].set(p), &board_op = board_[1 - bw];
forbid.set(p);
forbid_op.set(p);
// union
auto &dset = dset_[bw], &dset_op = dset_[1 - bw];
const size_t dirlen = dir_len_[p];
for (size_t i = 0; i < dirlen; ++i) {
const size_t x = dir_[p][i];
if (board.BIT_TEST(x)) {
dset.unions(p, x);
}
}
// check current component
check_valid(dset, p, board, board_op, forbid, forbid_op);
// check neighbors
for (size_t i = 0; i < dirlen; ++i) {
const size_t x = dir_[p][i];
if (board_op.BIT_TEST(x)) {
check_valid(dset_op, x, board_op, board, forbid_op, forbid);
} else if (!board.BIT_TEST(x)) {
check_no_liberty(dset_op, x, board_op, board, forbid_op);
}
}
return true;
}
bool has_legal_move(size_t bw) const noexcept { return !forbid_[bw].all(); }
board_t get_legal_moves(size_t bw) const noexcept { return ~forbid_[bw]; }
board_t get_two_go() const noexcept { return ~(forbid_[0] | forbid_[1]); }
template <class PRNG>
inline size_t random_legal_move(size_t bw, PRNG &rng) const noexcept {
return random_move_from_board(~forbid_[bw], rng);
}
template <class PRNG>
size_t heuristic_legal_move(size_t bw, const board_t &init_two_go,
bool &is_two_go, PRNG &rng) const noexcept {
const auto &legal_init_two_go = init_two_go & (~forbid_[bw]);
if (legal_init_two_go.any()) {
is_two_go = true;
return random_move_from_board(legal_init_two_go, rng);
}
is_two_go = false;
return random_move_from_board(~forbid_[bw], rng);
}
template <class PRNG>
static size_t random_move_from_board(const board_t &valid,
PRNG &rng) noexcept {
// assert(valid.any());
const size_t index = rng() % valid.count();
size_t action = valid._Find_first();
for (size_t i = 0; i < index; ++i) {
action = valid._Find_next(action);
}
return action;
}
public:
friend std::ostream &operator<<(std::ostream &out, const Board &b) {
out << " A B C D E F G H J\n";
for (size_t i = 0; i < 9; ++i) {
out << 9 - i << ' ';
for (size_t j = 0; j < 9; ++j) {
out << ".XO"[b[i * 9 + j]] << ' ';
}
out << 9 - i << '\n';
}
out << " A B C D E F G H J\n\n";
return out;
}
private:
static void check_valid(const DisjointSet &dset, size_t p,
const board_t &board, const board_t &board_op,
board_t &forbid, board_t &forbid_op) noexcept {
// find component
const auto &component = dset.get_component(p);
// find liberty
const auto &liberty =
((component << 9) | (component >> 9) | ((component & MASK_RIGHT) << 1) |
((component & MASK_LEFT) >> 1)) &
~(board | board_op);
if (liberty.count() == 1) {
const size_t x = liberty._Find_first();
forbid_op.set(x);
check_no_liberty(dset, x, board, board_op, forbid);
}
}
static void check_no_liberty(const DisjointSet &dset, size_t x, board_t board,
const board_t &board_op,
board_t &forbid) noexcept {
board.set(x);
// find component
auto component = dset.get_component(x);
const size_t dirlen = dir_len_[x];
for (size_t i = 0; i < dirlen; ++i) {
const size_t y = dir_[x][i];
if (board.BIT_TEST(y)) {
component |= dset.get_component(y);
}
}
// find liberty
const auto &liberty =
((component << 9) | (component >> 9) | ((component & MASK_RIGHT) << 1) |
((component & MASK_LEFT) >> 1)) &
~(board | board_op);
if (liberty.none()) {
forbid.set(x);
}
// board.reset(x);
}
private:
board_t board_[2], forbid_[2];
DisjointSet dset_[2];
const static constexpr auto dir_ = []() constexpr {
std::array<std::array<size_t, 4>, 81> ret{};
for (size_t i = 0; i < 81; ++i) {
auto &ri = ret[i];
size_t j = 0;
if (i / 9 > 0) {
ri[j++] = i - 9;
}
if (i % 9 > 0) {
ri[j++] = i - 1;
}
if (i % 9 < 8) {
ri[j++] = i + 1;
}
if (i / 9 < 8) {
ri[j++] = i + 9;
}
}
return ret;
}
();
const static constexpr auto dir_len_ = []() constexpr {
std::array<size_t, 81> ret{};
for (size_t i = 0; i < 81; ++i) {
ret[i] = static_cast<size_t>(i / 9 > 0) + static_cast<size_t>(i % 9 > 0) +
static_cast<size_t>(i % 9 < 8) + static_cast<size_t>(i / 9 < 8);
}
return ret;
}
();
};