This repository has been archived by the owner on Feb 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.h
222 lines (205 loc) · 6.23 KB
/
board.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
#pragma once
#include <cassert>
#include <iomanip>
#include <iostream>
class board {
public:
using board_t = uint64_t;
using row_t = uint16_t;
using tile_t = uint8_t;
using reward_t = int;
board(board_t rhs = 0u) : raw_(rhs) {}
board(const board &) = default;
board &operator=(const board &) = default;
~board() = default;
bool operator==(const board &rhs) const { return raw_ == rhs.raw_; }
bool operator!=(const board &rhs) const { return !(*this == rhs); }
public:
row_t operator[](size_t i) const { return (raw_ >> (i << 4u)) & 0xffff; }
tile_t operator()(size_t i) const { return (raw_ >> (i << 2u)) & 0x0f; }
void set(size_t i, tile_t e) {
raw_ =
(raw_ & ~(0x0full << (i << 2u))) | (board_t(e & 0x0full) << (i << 2u));
}
reward_t place(size_t pos, tile_t tile) {
assert(pos < 16 && tile > 0 && tile <= 3);
set(pos, tile);
return 0;
}
tile_t max_tile() const {
tile_t ret = 0;
for (size_t i = 0; i < 16; ++i) {
tile_t t = operator()(i);
if (t > ret)
ret = t;
}
return ret;
}
public:
reward_t slide(unsigned opcode) {
switch (opcode & 0b11) {
case 0:
return slide_up();
case 1:
return slide_right();
case 2:
return slide_down();
case 3:
return slide_left();
default:;
}
return -1;
}
private:
reward_t slide_left() {
board_t cur = 0u, prev = raw_;
reward_t score = 0;
score += lookup::find(operator[](0)).slide_left(cur, 0);
score += lookup::find(operator[](1)).slide_left(cur, 1);
score += lookup::find(operator[](2)).slide_left(cur, 2);
score += lookup::find(operator[](3)).slide_left(cur, 3);
raw_ = cur;
return (cur != prev) ? score : -1;
}
reward_t slide_right() {
mirror();
reward_t score = slide_left();
mirror();
return score;
}
reward_t slide_up() {
transpose();
reward_t score = slide_left();
mirror();
transpose();
flip();
return score;
}
reward_t slide_down() {
transpose();
mirror();
reward_t score = slide_left();
transpose();
flip();
return score;
}
private:
/**
* swap row and column
* +------------------------+ +------------------------+
* | 2 8 128 4| | 2 8 2 4|
* | 8 32 64 256| | 8 32 4 2|
* | 2 4 32 128| ----> | 128 64 32 8|
* | 4 2 8 16| | 4 256 128 16|
* +------------------------+ +------------------------+
*/
void transpose() {
raw_ = (raw_ & 0xf0f00f0ff0f00f0full) |
((raw_ & 0x0000f0f00000f0f0ull) << 12u) |
((raw_ & 0x0f0f00000f0f0000ull) >> 12u);
raw_ = (raw_ & 0xff00ff0000ff00ffull) |
((raw_ & 0x00000000ff00ff00ull) << 24u) |
((raw_ & 0x00ff00ff00000000ull) >> 24u);
}
/**
* horizontal reflection
* +------------------------+ +------------------------+
* | 2 8 128 4| | 4 128 8 2|
* | 8 32 64 256| | 256 64 32 8|
* | 2 4 32 128| ----> | 128 32 4 2|
* | 4 2 8 16| | 16 8 2 4|
* +------------------------+ +------------------------+
*/
void mirror() {
raw_ = ((raw_ & 0x000f000f000f000full) << 12u) |
((raw_ & 0x00f000f000f000f0ull) << 4u) |
((raw_ & 0x0f000f000f000f00ull) >> 4u) |
((raw_ & 0xf000f000f000f000ull) >> 12u);
}
/**
* vertical reflection
* +------------------------+ +------------------------+
* | 2 8 128 4| | 4 2 8 16|
* | 8 32 64 256| | 2 4 32 128|
* | 2 4 32 128| ----> | 8 32 64 256|
* | 4 2 8 16| | 2 8 128 4|
* +------------------------+ +------------------------+
*/
void flip() {
raw_ = ((raw_ & 0x000000000000ffffull) << 48u) |
((raw_ & 0x00000000ffff0000ull) << 16u) |
((raw_ & 0x0000ffff00000000ull) >> 16u) |
((raw_ & 0xffff000000000000ull) >> 48u);
}
private:
struct lookup {
using board_t = board::board_t;
using row_t = board::row_t;
using tile_t = board::tile_t;
using reward_t = board::reward_t;
lookup() {
static row_t cur = 0u;
raw = cur++;
// left
left = raw;
reward_left = mv_left(left);
}
static const lookup &find(size_t row) {
static const lookup cache[65536];
return cache[row];
}
static reward_t mv_left(row_t &row) {
reward_t reward = 0u;
tile_t elem[4] = {static_cast<tile_t>((row >> 0u) & 0x0f),
static_cast<tile_t>((row >> 4u) & 0x0f),
static_cast<tile_t>((row >> 8u) & 0x0f),
static_cast<tile_t>((row >> 12u) & 0x0f)};
size_t m = 4;
for (size_t c = 0; c < 3; ++c) {
tile_t rc = elem[c], rcn = elem[c + 1];
if (rc == 0u && rcn != 0u) {
m = c;
elem[c] = rcn;
break;
} else if (rc <= 2u && rc + rcn == 3u) {
m = c;
elem[c] = 3u;
reward = 3u;
break;
} else if (rc > 2u && rc == rcn) {
m = c;
elem[c] = ++rc;
reward = 3u * (1 << (rc - 3));
break;
}
}
for (size_t c = m + 1; c < 4; ++c)
elem[c] = c == 3u ? 0u : elem[c + 1];
row = ((elem[0] << 0u) | (elem[1] << 4u) | (elem[2] << 8u) |
(elem[3] << 12u));
return reward;
}
reward_t slide_left(board_t &board, size_t i) const {
board |= board_t(left) << (i << 4);
return reward_left;
}
row_t raw, left;
reward_t reward_left;
};
public:
friend std::ostream &operator<<(std::ostream &out, const board &b) {
out << "+------------------------+" << std::endl;
for (size_t i = 0; i < 4; ++i) {
out << "|" << std::dec;
for (size_t j = 0; j < 4; ++j) {
const tile_t t = b(i * 4 + j);
out << std::setw(6) << (t <= 3 ? t : ((1 << (t - 1)) - (1 << (t - 3))));
}
out << "|" << std::endl;
}
out << "+------------------------+" << std::endl;
return out;
}
private:
board_t raw_;
};