-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.h
194 lines (175 loc) · 4.56 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
#pragma once
#include <array>
#include <iostream>
#include <cstdio>
/**
* array-based board for 2048
*
* index (2-d form):
* [0][0] [0][1] [0][2] [0][3]
* [1][0] [1][1] [1][2] [1][3]
* [2][0] [2][1] [2][2] [2][3]
* [3][0] [3][1] [3][2] [3][3]
*
* index (1-d form):
* (0) (1) (2) (3)
* (4) (5) (6) (7)
* (8) (9) (10) (11)
* (12) (13) (14) (15)
*
*/
unsigned int fibo_seq[18] = {0, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584};
class board {
public:
board() : tile() {}
board(const board& b) = default;
board& operator =(const board& b) = default;
std::array<int, 4>& operator [](const int& i) { return tile[i]; }
const std::array<int, 4>& operator [](const int& i) const { return tile[i]; }
int& operator ()(const int& i) { return tile[i / 4][i % 4]; }
const int& operator ()(const int& i) const { return tile[i / 4][i % 4]; }
public:
bool operator ==(const board& b) const { return tile == b.tile; }
bool operator < (const board& b) const { return tile < b.tile; }
bool operator !=(const board& b) const { return !(*this == b); }
bool operator > (const board& b) const { return b < *this; }
bool operator <=(const board& b) const { return !(b < *this); }
bool operator >=(const board& b) const { return !(*this < b); }
public:
/**
* apply an action to the board
* return the reward gained by the action, or -1 if the action is illegal
*/
int sum_row(int r){
int sum = 0 ;
for (int c = 0; c < 4; c++){
sum += fibo_seq[ tile[r][c] ] ;
}
return sum ;
}
int sum_col(int c){
int sum = 0 ;
for (int r = 0; r < 4; r++){
sum += fibo_seq[ tile[r][c] ] ;
}
return sum ;
}
void get_score_dummy1(int* sum){
sum[0] = sum_row(0) + sum_row(1) ;
sum[1] = sum_col(2) + sum_col(3) ;
sum[2] = sum_row(2) + sum_row(3) ;
sum[3] = sum_col(0) + sum_col(1) ;
}
int move(const int& opcode) {
switch (opcode) {
case 0: return move_up();
case 1: return move_right();
case 2: return move_down();
case 3: return move_left();
default: return -1;
}
}
int move_left() {
board prev = *this;
int score = 0;
for (int r = 0; r < 4; r++) {
auto& row = tile[r];
int top = 0, hold = 0;
for (int c = 0; c < 4; c++) {
int tile = row[c];
if (tile == 0) continue;
row[c] = 0;
if (hold) {
if ( ( tile==1 && hold==1 ) ) {
row[top++] = ++tile;
score += fibo_seq[tile];
hold = 0;
}
else if (( tile>=1 && hold>=1 && abs(tile-hold)==1 ) ){
row[top++] = std::max(tile, hold)+1;
score += fibo_seq[ std::max(tile, hold)+1 ];
hold = 0;
}
else {
row[top++] = hold;
hold = tile;
}
} else {
hold = tile;
}
}
if (hold) tile[r][top] = hold;
}
return (*this != prev) ? score : -1;
}
int move_right() {
reflect_horizontal();
int score = move_left();
reflect_horizontal();
return score;
return score;
}
int move_up() {
rotate_right();
int score = move_right();
rotate_left();
return score;
}
int move_down() {
rotate_right();
int score = move_left();
rotate_left();
return score;
}
void transpose() {
for (int r = 0; r < 4; r++) {
for (int c = r + 1; c < 4; c++) {
std::swap(tile[r][c], tile[c][r]);
}
}
}
void reflect_horizontal() {
for (int r = 0; r < 4; r++) {
std::swap(tile[r][0], tile[r][3]);
std::swap(tile[r][1], tile[r][2]);
}
}
void reflect_vertical() {
for (int c = 0; c < 4; c++) {
std::swap(tile[0][c], tile[3][c]);
std::swap(tile[1][c], tile[2][c]);
}
}
/**
* rotate the board clockwise by given times
*/
void rotate(const int& r = 1) {
switch (((r % 4) + 4) % 4) {
default:
case 0: break;
case 1: rotate_right(); break;
case 2: reverse(); break;
case 3: rotate_left(); break;
}
}
void rotate_right() { transpose(); reflect_horizontal(); } // clockwise
void rotate_left() { transpose(); reflect_vertical(); } // counterclockwise
void reverse() { reflect_horizontal(); reflect_vertical(); }
public:
friend std::ostream& operator <<(std::ostream& out, const board& b) {
char buff[32];
out << "+------------------------+" << std::endl;
for (int r = 0; r < 4; r++) {
std::snprintf(buff, sizeof(buff), "|%6u%6u%6u%6u|",
fibo_seq[ b[r][0] ] , // use -2u (0xff...fe) to remove the unnecessary 1 for (1 << 0)
fibo_seq[ b[r][1] ] ,
fibo_seq[ b[r][2] ] ,
fibo_seq[ b[r][3] ] );
out << buff << std::endl;
}
out << "+------------------------+" << std::endl;
return out;
}
private:
std::array<std::array<int, 4>, 4> tile;
};