-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReversi.cpp
260 lines (237 loc) · 6.15 KB
/
Reversi.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include <cstring>
#include <vector>
#include <algorithm>
#include "Reversi.h"
using namespace std;
int moveX[] = { -1,-1,-1, 0, 0, 1, 1, 1 };
int moveY[] = { -1, 0, 1,-1, 1,-1, 0, 1 };
Reversi::Reversi(void) {
}
Reversi::~Reversi(void) {
}
void Reversi::init() {
memset(this->mArry, eEMPTY, sizeof(this->mArry));
this->mArry[27] = eWHITE;
this->mArry[28] = eBLACK;
this->mArry[35] = eBLACK;
this->mArry[36] = eWHITE;
this->bBW = true;
this->steps = 0;
}
int Reversi::convertXY(int x, int y) {
return (x - 1) * 8 + (y - 1);
}
bool Reversi::isValid(int x, int y) {
if (x<1 || x>8 || y<1 || y>8)
return false;
else
return true;
}
int Reversi::check(int x, int y) {
if (isValid(x, y) && this->mArry[this->convertXY(x, y)] != eEMPTY && this->mArry[this->convertXY(x, y)] != ePLAYABLE)
return 0;
int eatCount = 0;
for (int i = 0; i<8; ++i) {
int newX = x + moveX[i], newY = y + moveY[i];
int otherCount = 0;
while (isValid(newX, newY)) {
char chessNow = this->mArry[this->convertXY(newX, newY)];
if (chessNow != eEMPTY && chessNow != ePLAYABLE) {
if ((this->bBW && chessNow == eBLACK) || (!this->bBW && chessNow == eWHITE)) {
eatCount += otherCount;
break;
}
else {
++otherCount;
newX += moveX[i];
newY += moveY[i];
}
}
else {
break;
}
}
}
return eatCount;
}
Reversi Reversi::operator=(Reversi rhs) {
this->bBW = rhs.bBW;
memcpy(this->mArry, rhs.mArry, sizeof(this->mArry));
return *this;
}
int Reversi::avaMoving() {
int result = 0;
for (int x = 1; x <= 8; ++x) {
for (int y = 1; y <= 8; ++y) {
if (this->mArry[this->convertXY(x, y)] == eEMPTY || this->mArry[this->convertXY(x, y)] == ePLAYABLE) {
if (this->check(x, y) != 0) {
++result;
}
}
}
}
return result;
}
bool Reversi::isEnd() {
bool flag = true;
for (int x = 1; x <= 8; ++x) {
for (int y = 1; y <= 8; ++y) {
// can be better
if (this->mArry[this->convertXY(x, y)] == eEMPTY || this->mArry[this->convertXY(x, y)] == ePLAYABLE) {
if (this->check(x, y) != 0) {
flag = false;
this->mArry[this->convertXY(x, y)] = ePLAYABLE;
}
else {
if (this->mArry[this->convertXY(x, y)] == this->ePLAYABLE)
this->mArry[this->convertXY(x, y)] = eEMPTY;
}
this->bBW = !this->bBW;
if (this->check(x, y) != 0) {
flag = false;
}
this->bBW = !this->bBW;
}
}
}
return flag;
}
bool Reversi::isBW() {
/**/
return this->bBW;
}
void Reversi::setBW(int x, int y) {
//flip
for (int i = 0; i<8; ++i) {
int newX = x + moveX[i], newY = y + moveY[i];
vector<int> flipList;
while (isValid(newX, newY)) {
char chessNow = this->mArry[this->convertXY(newX, newY)];
if (chessNow != eEMPTY && chessNow != ePLAYABLE) {
if ((this->bBW && chessNow == eBLACK) || (!this->bBW && chessNow == eWHITE)) {
//fliping
for (int k = 0; k < flipList.size(); ++k) {
this->mArry[flipList[k]] = this->mArry[flipList[k]] == eBLACK ? eWHITE : eBLACK;
}
break;
}
else {
//add to list
flipList.push_back(this->convertXY(newX, newY));
newX += moveX[i];
newY += moveY[i];
}
}
else {
break;
}
}
}
//
this->mArry[this->convertXY(x, y)] = this->bBW ? eBLACK : eWHITE;
bool oriBW = this->bBW;
this->bBW = !oriBW;
bool flag = false;
for (int x = 1; x <= 8; ++x) {
for (int y = 1; y <= 8; ++y) {
if (this->mArry[this->convertXY(x, y)] == eEMPTY || this->mArry[this->convertXY(x, y)] == ePLAYABLE) {
if (this->check(x, y) != 0) {
flag = true;
}
}
}
}
if (!flag)this->bBW = oriBW;
++this->steps;
}
char Reversi::getBW(int x, int y) {
return this->mArry[this->convertXY(x, y)];
}
int Reversi::getSteps() {
return this->steps;
}
int Reversi::BCount() {
int result = 0;
for (int i = 0; i < 64; ++i)
if (this->mArry[i] == eBLACK)
++result;
return result;
}
int Reversi::WCount() {
int result = 0;
for (int i = 0; i < 64; ++i)
if (this->mArry[i] == eWHITE)
++result;
return result;
}
// black base
float evalue(Reversi r) {
float value = r.BCount() - r.WCount();
//角佔領判斷
for (int i = 0; i < 4; ++i) {
char chessNow = r.getBW(corner[i][0], corner[i][1]);
if (chessNow == r.eBLACK) value += cornerVal;
else if (chessNow == r.eWHITE) value -= cornerVal;
}
//星位佔領判斷
for (int i = 0; i < 4; ++i) {
char chessNow = r.getBW(xsquare[i][0], xsquare[i][1]);
if (chessNow == r.eBLACK) value -= xsquareVal;
else if (chessNow == r.eWHITE) value += xsquareVal;
}
//c位佔領判斷
for (int i = 0; i < 8; ++i) {
char chessNow = r.getBW(csquare[i][0], csquare[i][1]);
if (chessNow == r.eBLACK) value -= csquareVal;
else if (chessNow == r.eWHITE) value += csquareVal;
}
//邊佔領判斷
for (int i = 3; i <= 6; ++i) {
char chessNow = r.getBW(1, i);
if (chessNow == r.eBLACK) value += sideVal;
else if (chessNow == r.eWHITE) value -= sideVal;
chessNow = r.getBW(8, i);
if (chessNow == r.eBLACK) value += sideVal;
else if (chessNow == r.eWHITE) value -= sideVal;
}
for (int i = 3; i <= 6; ++i) {
char chessNow = r.getBW(i, 1);
if (chessNow == r.eBLACK) value += sideVal;
else if (chessNow == r.eWHITE) value -= sideVal;
chessNow = r.getBW(i, 8);
if (chessNow == r.eBLACK) value += sideVal;
else if (chessNow == r.eWHITE) value -= sideVal;
}
//
return value;
}
// ai is black
float minmax(Reversi r, int depth, float alpha, float beta) {
if (r.isEnd()) {
return r.BCount() - r.WCount();
}
else if (depth == 0) {
return evalue(r);
}
// isEnd執行過 代表playable正確
Reversi tmp;
bool flag = true;
for (int x = 1; x <= 8 && flag; ++x) {
for (int y = 1; y <= 8 && flag; ++y) {
if (r.getBW(x, y) != r.ePLAYABLE)continue;
tmp = r;
tmp.setBW(x, y);
if (r.isBW()) {
alpha = std::max(minmax(tmp, depth - 1, alpha, beta), alpha);
if (beta <= alpha)
flag = false;
}
else {
beta = std::min(minmax(tmp, depth - 1, alpha, beta), beta);
if (beta <= alpha)
flag = false;
}
}
}
return (r.isBW() ? alpha : beta);
}