@@ -111,15 +111,16 @@ private double getUtility() {
111
111
112
112
private double simulate (final LargeBoard board , int player ) {
113
113
int numberOfMovesPlayed = board .movesPlayed ;
114
- while (! board .isDecided () ) {
114
+ while (board .result () == 0 ) {
115
115
final int possibilities [] = new int [9 ];
116
116
int movesToPlay = 0 ;
117
117
for (int position = 0 ; position < 81 ; position ++) {
118
118
if (board .canPlay (position )) {
119
119
possibilities [movesToPlay ] = position ;
120
120
movesToPlay ++;
121
- if (board .result (player )) {
122
- return player == 1 ? 1 : 0 ;
121
+ final int result = board .result ();
122
+ if (result != 0 ) {
123
+ return result == player ? 1 : 0 ;
123
124
}
124
125
}
125
126
}
@@ -178,9 +179,10 @@ public String toString() {
178
179
}
179
180
180
181
class LargeBoard {
182
+ public static final int FULL = (1 << 10 ) - 1 ;
181
183
int movesPlayed ;
182
- //todo: check if the board is occupied entirely
183
- int largeBoard , largeCaptures ;
184
+ int largeBoard , largeCaptures , largeOccupied ;
185
+ //todo: won't work. Persisted moves are never -1
184
186
int moves [] = new int [81 ];
185
187
final Board boards [] = new Board [9 ];
186
188
@@ -199,44 +201,59 @@ public void play(final int player, final int p) {
199
201
final int pRow = previousMove / 27 , pCol = (previousMove % 9 ) / 3 ;
200
202
assert bRow == pRow && bRow == pCol ;
201
203
}
202
- boards [bRow * 3 + bCol ].play (player , row * 3 + col );
204
+ final int position = bRow * 3 + bCol ;
205
+ assert (largeOccupied & (1 << position )) == 0 ;
206
+ boards [position ].play (player , row * 3 + col );
207
+ if (boards [position ].occupied == FULL ) {
208
+ largeOccupied = largeOccupied | (1 << position );
209
+ }
203
210
movesPlayed ++;
204
211
}
205
212
206
213
public void undo () {
207
214
movesPlayed --;
208
215
final int p = moves [movesPlayed ];
209
- final int bRow = p / 27 , bCol = (p % 9 ) / 3 ;
210
216
final int row = (p / 9 ) % 3 , col = p % 3 ;
211
- final int position = row * 3 + col ;
212
- boards [bRow * 3 + bCol ].undo (position );
213
- final int bitFlipped = ~(1 << position );
217
+ final int bRow = p / 27 , bCol = ( p % 9 ) / 3 ;
218
+ boards [bRow * 3 + bCol ].undo (row * 3 + col );
219
+ final int bitFlipped = ~(1 << ( bRow * 3 + bCol ) );
214
220
largeBoard = largeBoard & bitFlipped ;
215
221
largeCaptures = largeCaptures & bitFlipped ;
222
+ largeOccupied = largeOccupied & bitFlipped ;
216
223
}
217
224
218
- public boolean result (final int player ) {
225
+ public int result () {
226
+ int firstScore = 0 , secondScore = 0 ;
219
227
for (int i = 0 ; i < 3 ; i ++) {
220
228
for (int j = 0 ; j < 3 ; j ++) {
221
229
final int bit = 1 << (i * 3 + j );
222
230
if (boards [bit ].result (1 ) == 1 ) {
223
231
largeBoard = largeBoard | bit ;
232
+ firstScore ++;
224
233
largeCaptures = largeCaptures | bit ;
234
+ largeOccupied = largeOccupied | bit ;
225
235
} else if (boards [bit ].result (2 ) == 2 ) {
236
+ secondScore ++;
226
237
largeCaptures = largeCaptures | bit ;
238
+ largeOccupied = largeOccupied | bit ;
239
+ } else if (boards [bit ].occupied == FULL ) {
240
+ largeOccupied = largeOccupied | bit ;
227
241
}
228
242
}
229
243
}
230
- return Board .evaluateBoard (player , largeBoard , largeCaptures ) == player ;
231
- }
232
-
233
- @ Override
234
- public String toString () {
235
- return "LargeBoard{" +
236
- "largeBoard=" + largeBoard +
237
- ", largeCaptures=" + largeCaptures +
238
- ", boards=" + Arrays .deepToString (boards ) +
239
- '}' ;
244
+ if (firstScore > 4 ) {
245
+ return 1 ;
246
+ } else if (secondScore > 4 ) {
247
+ return 2 ;
248
+ } else if (Board .evaluateBoard (1 , largeBoard , largeCaptures ) == 1 ) {
249
+ return 1 ;
250
+ } else if (Board .evaluateBoard (2 , largeBoard , largeCaptures ) == 2 ) {
251
+ return 2 ;
252
+ } else if (largeOccupied == FULL ) {
253
+ return firstScore > secondScore ? 1 : (secondScore > firstScore ? 2 : 0 );
254
+ } else {
255
+ return 0 ;
256
+ }
240
257
}
241
258
242
259
public boolean canPlay (final int p ) {
@@ -249,12 +266,17 @@ public boolean canPlay(final int p) {
249
266
return false ;
250
267
}
251
268
}
252
- return (boards [bRow * 3 + bCol ].occupied & (1 << (row * 3 + col ))) == 0 ;
269
+ return ((largeOccupied & (1 << (bRow * 3 + bCol ))) == 0 )
270
+ && (boards [bRow * 3 + bCol ].occupied & (1 << (row * 3 + col ))) == 0 ;
253
271
}
254
272
255
- //todo: check for win by 4
256
- public boolean isDecided () {
257
- return result (1 ) || result (2 );
273
+ @ Override
274
+ public String toString () {
275
+ return "LargeBoard{" +
276
+ "largeBoard=" + largeBoard +
277
+ ", largeCaptures=" + largeCaptures +
278
+ ", boards=" + Arrays .deepToString (boards ) +
279
+ '}' ;
258
280
}
259
281
}
260
282
0 commit comments