-
Notifications
You must be signed in to change notification settings - Fork 11
/
SearchGame.java
184 lines (176 loc) · 5.54 KB
/
SearchGame.java
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
// This software is copyright (c) 1996-2005 by
// John Tromp
// Insulindeweg 908
// 1095 DX Amsterdam
// Netherlands
// E-mail: john.tromp at gmail.com
//
// This notice must not be removed.
// This software must not be sold for profit.
// You may redistribute if your distributees have the
// same rights and restrictions.
import java.io.*;
import java.text.DecimalFormat;
public class SearchGame extends TransGame {
static final int BOOKPLY = 0; // full-width search up to this depth
static final int REPORTPLY = 2;
static int reportply = 2;
int history[][] = new int[2][SIZE1];
long nodes, msecs;
void inithistory()
{
for (int side=0; side<2; side++)
for (int i=0; i<(WIDTH+1)/2; i++)
for (int h=0; h<H1/2; h++)
history[side][H1*i+h] = history[side][H1*(WIDTH-1-i)+HEIGHT-1-h] =
history[side][H1*i+HEIGHT-1-h] = history[side][H1*(WIDTH-1-i)+h] =
4+Math.min(3,i) + Math.max(-1,Math.min(3,h)-Math.max(3-i,0))
+ Math.min(3,Math.min(i,h)) + Math.min(3,h);
}
int ab(int alpha, int beta)
{
nodes++;
if (nplies == SIZE-1) // one move left
return DRAW; // by assumption, player to move can't win
int side, otherside;
otherside = (side = nplies & 1) ^ 1;
long other = color[otherside];
int i,nav,av[] = new int[WIDTH];
long newbrd;
boolean winontop;
for (i = nav = 0; i < WIDTH; i++) {
newbrd = other | (1L << height[i]); // check opponent move
if (!islegal(newbrd))
continue;
winontop = islegalhaswon(other | (2L << height[i]));
if (haswon(newbrd)) { // immediate threat
if (winontop) // can't stop double threat
return LOSS;
nav = 0; // forced move
av[nav++] = i;
while (++i < WIDTH)
if (islegalhaswon(other | (1L << height[i])))
return LOSS;
break;
}
if (!winontop)
av[nav++] = i;
}
if (nav == 0)
return LOSS;
if (nplies == SIZE-2) // two moves left
return DRAW; // opponent has no win either
int score;
if (nav == 1) {
makemove(av[0]);
score = LOSSWIN-ab(LOSSWIN-beta,LOSSWIN-alpha);
backmove();
return score;
}
int ttscore = transpose();
if (ttscore != UNKNOWN) {
if (ttscore == DRAWLOSS) {
if ((beta = DRAW) <= alpha)
return ttscore;
} else if (ttscore == DRAWWIN) {
if ((alpha = DRAW) >= beta)
return ttscore;
} else return ttscore; // exact score
}
int hashindx = htindex;
int hashlock = lock;
long poscnt = posed;
int besti=0,j,l,sc;
int v,val;
score = LOSS;
for (i = 0; i < nav; i++) {
val = history[side][height[av[l = i]]];
for (j = i+1; j < nav; j++) {
v = history[side][height[av[j]]];
if (v > val) {
val = v; l = j;
}
}
for (j = av[l]; l>i; l--)
av[l] = av[l-1];
makemove(av[i] = j);
sc = LOSSWIN-ab(LOSSWIN-beta,LOSSWIN-alpha);
backmove();
if (sc > score) {
besti = i;
if ((score=sc) > alpha && nplies >= BOOKPLY && (alpha=sc) >= beta) {
if (score == DRAW && i < nav-1)
score = DRAWWIN;
if (besti > 0) {
for (i = 0; i < besti; i++)
history[side][height[av[i]]]--; // punish worse
history[side][height[av[besti]]] += besti;
}
break;
}
}
}
if (score == LOSSWIN-ttscore) // combine < and >
score = DRAW;
poscnt = posed - poscnt;
int work;
for (work=0; (poscnt>>=1) != 0; work++) ; // work=log #positions stored
transtore(hashindx, hashlock, score, work);
if (nplies <= reportply) {
System.out.println(toString() + "#-<=>+".charAt(score) + work);
}
return score;
}
int solve()
{
int i, side = nplies & 1, otherside = side ^ 1;
reportply = nplies + REPORTPLY;
nodes = 0L;
msecs = 1L;
if (haswon(color[otherside]))
return LOSS;
for (i = 0; i < WIDTH; i++)
if (islegalhaswon(color[side] | (1L << height[i])))
return WIN;
inithistory();
msecs = System.currentTimeMillis();
int score = ab(LOSS, WIN);
msecs = System.currentTimeMillis() + 1 - msecs; // prevent division by 0
return score;
}
public static void main(String argv[])
{
System.out.println("Fhourstones 3.1 (Java)");
System.out.println("Boardsize = " + WIDTH + "x" + HEIGHT);
System.out.println("Using " + TRANSIZE + " transposition table entries.");
SearchGame c4 = new SearchGame();
BufferedReader dis = new BufferedReader(new InputStreamReader(System.in));
DecimalFormat df = new DecimalFormat("######.###");
for (;;) {
String line=null;
try {
line = dis.readLine();
} catch (IOException e) {
System.out.println(e);
System.exit(0);
}
if (line == null)
break;
c4.reset();
for (int i=0; i < line.length(); i++)
c4.makemove(line.charAt(i) - '1');
System.out.println("\nSolving " + c4.nplies + "-ply position after "
+ c4.toString() + " . . .");
c4.emptyTT();
int result = c4.solve();
long poscnt = c4.posed;
int work;
for (work=0; (poscnt>>=1) != 0; work++) ; //work = log #transpositions
System.out.println("score = " + result + " (" +
"#-<=>+".charAt(result) + ") work = " + work);
System.out.println("" + c4.nodes + " pos / " + c4.msecs +
" msec = " + df.format((double)c4.nodes/c4.msecs) + " Kpos/sec");
System.out.println(c4.htstat());
}
}
}