-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoff_precoded.cpp
237 lines (212 loc) · 6.58 KB
/
off_precoded.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
#include "Utils.h"
#include "off.h"
#include "Pos2All.h"
#include "TreeDebug.h"
#include "Debug.h"
#define FFK
void InitNewFF() {
cerr << "Init New FF...";
void InitCountTables();
void InitFlipTables();
InitCountTables();
InitFlipTables();
cerr << "Done\n";
}
// the square is occupied, or something else is wrong, we shouldn't have come here
int BadFF(const CHAB& hab) {
QSSERT(0);
return -kInfinity;
}
int BadFFM(const CHABM& habm) {
QSSERT(0);
return -kInfinity;
}
int BadCF() {
QSSERT(0);
return -kInfinity;
}
void ClearFfs(TFF *ffs[7][7]) {
int left, right;
for (left=0; left<7; left++)
for (right=0; right<7; right++)
ffs[left][right]=BadFF;
}
void SetFfs(TFF *ffs[7][7], TFF* fftable[], int length, int loc, bool fMover) {
int config;
int size=lengthToSize(length);
int left, right;
for (config=0; config<size; config++) {
if (NFlipped(config, length, loc, fMover, left, right))
fftable[config]=BadFF;
else
fftable[config]=ffs[left][right];
}
}
void ClearFFM(TFFM *ffs[7][7]) {
int left, right;
for (left=0; left<7; left++)
for (right=0; right<7; right++)
ffs[left][right]=BadFFM;
}
void SetFFMs(TFFM *ffs[7][7], TFFM* fftable[], int length, int loc, bool fMover) {
int config;
int size=lengthToSize(length);
int left, right;
for (config=0; config<size; config++) {
if (NFlipped(config, length, loc, fMover, left, right))
fftable[config]=BadFFM;
else
fftable[config]=ffs[left][right];
}
}
// return the value of the game from Black's point of view.
// hab.alpha and hab.beta are from Black's point of view
// hab.nEmpty is current
// hab.nDiscDiff is from Black's point of view
#if CHECK_NODES
int AB_BlackToMove_(const CHAB& hab) {
#else
int AB_BlackToMove(const CHAB& hab) {
#endif
nSNodesQuick++;
if (hab.nEmpty==1) {
return hab.nDiscDiff+emptyHead.next->Terminal_Black();
}
else {
CHAB habSub;
int best,value;
CEmpty* pem;
int nFlipped;
best=-kInfinity;
// does black have a move?
habSub.nEmpty=hab.nEmpty-1;
habSub.alpha=-hab.beta;
habSub.beta=-hab.alpha;
for (pem=emptyHead.next; pem!=emEOL; pem=pem->next) {
nFlipped=(pem->Count_Black)();
if (nFlipped) {
habSub.nDiscDiff=-(nFlipped+nFlipped+1+hab.nDiscDiff);
pem->Remove();
value=-pem->AB_Black(habSub);
pem->Add();
if (value>best) {
best=value;
if (value>hab.beta)
return value;
if (value>-habSub.beta)
habSub.beta=-value;
}
}
}
if (best!=-kInfinity)
return best;
// does white have a move?
nSNodesQuick++;
habSub.alpha=hab.alpha;
habSub.beta=hab.beta;
best=kInfinity;
for (pem=emptyHead.next; pem!=emEOL; pem=pem->next) {
nFlipped=(pem->Count_White)();
if (nFlipped) {
habSub.nDiscDiff=hab.nDiscDiff-(nFlipped+nFlipped+1);
pem->Remove();
value=pem->AB_White(habSub);
pem->Add();
if (value<best) {
best=value;
if (value<hab.alpha)
return value;
if (value<habSub.beta)
habSub.beta=value;
}
}
}
if (best!=kInfinity)
return best;
else
return hab.nDiscDiff;
}
}
#if CHECK_NODES
int AB_BlackToMove(const CHAB& hab) {
char sBoardText[NN+1];
int value=AB_BlackToMove_(hab);
CheckValue(GetText(sBoardText),hab.alpha,hab.beta,true,value);
return value;
}
#endif
// return the value of the game from White's point of view
// hab.alpha and hab.beta are from White's point of view
// hab.nEmpty is current
// hab.nDiscDiff is from White's point of view
#if CHECK_NODES
int AB_WhiteToMove_(const CHAB& hab) {
#else
int AB_WhiteToMove(const CHAB& hab) {
#endif
nSNodesQuick++;
if (hab.nEmpty==1) {
return hab.nDiscDiff+(emptyHead.next->Terminal_White)();
}
else {
CHAB habSub;
int best,value;
CEmpty* pem;
int nFlipped;
best=-kInfinity;
// does white have a move?
habSub.nEmpty=hab.nEmpty-1;
habSub.alpha=-hab.beta;
habSub.beta=-hab.alpha;
for (pem=emptyHead.next; pem!=emEOL; pem=pem->next) {
nFlipped=(pem->Count_White)();
if (nFlipped) {
habSub.nDiscDiff=-(nFlipped+nFlipped+1+hab.nDiscDiff);
pem->Remove();
value=-pem->AB_White(habSub);
pem->Add();
if (value>best) {
best=value;
if (value>hab.beta)
return value;
if (value>-habSub.beta)
habSub.beta=-value;
}
}
}
if (best!=-kInfinity)
return best;
// does black have a move?
habSub.alpha=hab.alpha;
habSub.beta=hab.beta;
best=kInfinity;
for (pem=emptyHead.next; pem!=emEOL; pem=pem->next) {
nFlipped=(pem->Count_Black)();
if (nFlipped) {
habSub.nDiscDiff=hab.nDiscDiff-(nFlipped+nFlipped+1);
pem->Remove();
value=pem->AB_Black(habSub);
pem->Add();
if (value<best) {
best=value;
if (value<hab.alpha)
return value;
if (value<habSub.beta)
habSub.beta=value;
}
}
}
if (best!=kInfinity)
return best;
else
return hab.nDiscDiff;
}
}
#if CHECK_NODES
int AB_WhiteToMove(const CHAB& hab) {
char sBoardText[NN+1];
int value=AB_WhiteToMove_(hab);
CheckValue(GetText(sBoardText),hab.alpha,hab.beta,false,value);
return value;
}
#endif