-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
168 lines (157 loc) · 3.38 KB
/
board.cpp
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
#include "board.hpp"
#include <algorithm>
Board::Board() {
this->load(startpos);
}
Board::Board(std::string pos) {
if(!this->load(pos)) {
throw std::invalid_argument("Invalid position");
}
}
bool Board::load(std::string pos) {
if(pos.size() != WIDTH*HEIGHT+HEIGHT-1)
return false;
int idx = 0;
for(int i = 0; i < HEIGHT; i++, idx++) {
for(int j = 0; j < WIDTH; j++, idx++) {
if(pos[idx]=='.' || i==0)
empty_column[j] = i+1-(i==0)+((pos[idx]=='.')&&i==0);
board[i][j] = pos[idx];
}
if(idx != pos.size() && pos[idx] != '/')
return false;
}
return true;
}
bool Board::validate(std::string pl, std::string* tp) const {
if(full())
return false;
if(tp != nullptr)
tp->clear();
int cnt[pl.size()];
for(auto& c: cnt)
c = 0;
for(int r = 0; r < HEIGHT; r++) {
for(int c = 0; c < WIDTH; c++) {
char p = board[r][c];
if(r > 0 && p == '.' && board[r-1][c] != '.')
return false;
if(p != '.') {
if(pl.find(p) == std::string::npos)
return false;
cnt[pl.find(p)]++;
if(r < 3) {
bool down = true;
for(int i = r; i < HEIGHT && i-r < 4; i++) {
if(board[i][c] != p)
down = false;
}
if(down)
return false;
}
int hor = -1;
for(int i = c; i >= 0 && board[r][i] == p; i--) {
hor++;
if(hor == 4)
return false;
}
for(int i = c; i < WIDTH && board[r][i] == p; i++) {
hor++;
if(hor == 4)
return false;
}
int backslash = -1;
for(int i = 0; c-i >= 0 && r-i >= 0 && board[r-i][c-i] == p; i++) {
backslash++;
if(backslash == 4)
return false;
}
for(int i = 0; c+i < WIDTH && r+i < HEIGHT && board[r+i][c+i] == p; i++) {
backslash++;
if(backslash == 4)
return false;
}
int slash = -1;
for(int i = 0; c-i >= 0 && r+i < HEIGHT && board[r+i][c-i] == p; i++) {
slash++;
if(slash == 4)
return false;
}
for(int i = 0; c+i < WIDTH && r-i >= 0 && board[r-i][c+i] == p; i++) {
slash++;
if(slash == 4)
return false;
}
}
}
}
int c = cnt[0];
if(tp != nullptr)
tp->push_back(pl[0]);
for(int i = 1; i < pl.size(); i++) {
if(cnt[i] < c) {
c = cnt[i];
if(tp != nullptr) {
tp->clear();
tp->push_back(pl[i]);
}
} else if(cnt[i] == c) {
if(tp != nullptr)
tp->push_back(pl[i]);
}
}
for(auto& cn: cnt) {
if(cn-1 > c) {
if(tp != nullptr)
tp->clear();
return false;
}
}
return true;
}
void Board::print(std::ostream& os) const {
for(int i = 0; i < WIDTH; i++) {
os << i+1 << ' ';
}
os << '\n';
for(auto& r: board) {
for(auto& c: r) {
os << c << ' ';
}
os << '\n';
}
os << std::flush;
}
void Board::print_out() const {
print(std::cout);
}
bool Board::move(int c, char p) {
if(c < 0 || c >= WIDTH || empty_column[c] == 0)
return false;
board[empty_column[c]-1][c] = p;
empty_column[c]--;
moves_hist.push_back(c);
return true;
}
bool Board::unmove() {
if(moves_hist.empty())
return false;
int m = moves_hist.back();
moves_hist.pop_back();
board[empty_column[m]][m] = '.';
empty_column[m]++;
return true;
}
bool Board::full() const {
return std::all_of(empty_column, empty_column+WIDTH, [](const auto& e){return e == 0;});
}
bool Board::empty() const {
return std::all_of(empty_column, empty_column+WIDTH, [](const auto& e){return e == HEIGHT;});
}
int Board::left() const {
int sum = 0;
for(auto& e: empty_column) {
sum += e;
}
return sum;
}