@@ -111,16 +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 .result () == 0 ) {
115
- final int possibilities [] = new int [9 ];
114
+ while (board .result () == - 1 ) {
115
+ final int possibilities [] = new int [81 ];
116
116
int movesToPlay = 0 ;
117
- for (int position = 0 ; position < 81 ; position ++) {
117
+ for (int position = 0 ; position < possibilities . length ; position ++) {
118
118
if (board .canPlay (position )) {
119
119
possibilities [movesToPlay ] = position ;
120
120
movesToPlay ++;
121
121
final int result = board .result ();
122
- if (result != 0 ) {
123
- return result == player ? 1 : 0 ;
122
+ if (result != - 1 ) {
123
+ return result == player ? 1 : ( result == 0 ? 0.5 : 0 ) ;
124
124
}
125
125
}
126
126
}
@@ -182,8 +182,7 @@ class LargeBoard {
182
182
public static final int FULL = (1 << 10 ) - 1 ;
183
183
int movesPlayed ;
184
184
int largeBoard , largeCaptures , largeOccupied ;
185
- //todo: won't work. Persisted moves are never -1
186
- int moves [] = new int [81 ];
185
+ final int moves [] = new int [81 ];
187
186
final Board boards [] = new Board [9 ];
188
187
189
188
public LargeBoard () {
@@ -196,10 +195,10 @@ public void play(final int player, final int p) {
196
195
moves [movesPlayed ] = p ;
197
196
final int bRow = p / 27 , bCol = (p % 9 ) / 3 ;
198
197
final int row = (p / 9 ) % 3 , col = p % 3 ;
199
- if (movesPlayed > 0 && moves [ movesPlayed - 1 ] != - 1 ) {
198
+ if (movesPlayed > 0 ) {
200
199
final int previousMove = moves [movesPlayed - 1 ];
201
200
final int pRow = previousMove / 27 , pCol = (previousMove % 9 ) / 3 ;
202
- assert bRow == pRow && bRow == pCol ;
201
+ assert ( largeOccupied & ( 1 << ( pRow * 3 + pCol ))) != 0 || ( bRow == pRow && bCol == pCol ) ;
203
202
}
204
203
final int position = bRow * 3 + bCol ;
205
204
assert (largeOccupied & (1 << position )) == 0 ;
@@ -226,17 +225,18 @@ public int result() {
226
225
int firstScore = 0 , secondScore = 0 ;
227
226
for (int i = 0 ; i < 3 ; i ++) {
228
227
for (int j = 0 ; j < 3 ; j ++) {
229
- final int bit = 1 << (i * 3 + j );
230
- if (boards [bit ].result (1 ) == 1 ) {
228
+ final int position = i * 3 + j ;
229
+ final int bit = 1 << position ;
230
+ if (boards [position ].result (1 ) == 1 ) {
231
231
largeBoard = largeBoard | bit ;
232
232
firstScore ++;
233
233
largeCaptures = largeCaptures | bit ;
234
234
largeOccupied = largeOccupied | bit ;
235
- } else if (boards [bit ].result (2 ) == 2 ) {
235
+ } else if (boards [position ].result (2 ) == 2 ) {
236
236
secondScore ++;
237
237
largeCaptures = largeCaptures | bit ;
238
238
largeOccupied = largeOccupied | bit ;
239
- } else if (boards [bit ].occupied == FULL ) {
239
+ } else if (boards [position ].occupied == FULL ) {
240
240
largeOccupied = largeOccupied | bit ;
241
241
}
242
242
}
@@ -252,17 +252,17 @@ public int result() {
252
252
} else if (largeOccupied == FULL ) {
253
253
return firstScore > secondScore ? 1 : (secondScore > firstScore ? 2 : 0 );
254
254
} else {
255
- return 0 ;
255
+ return - 1 ;
256
256
}
257
257
}
258
258
259
259
public boolean canPlay (final int p ) {
260
260
final int bRow = p / 27 , bCol = (p % 9 ) / 3 ;
261
261
final int row = (p / 9 ) % 3 , col = p % 3 ;
262
- if (movesPlayed > 0 && moves [ movesPlayed - 1 ] != - 1 ) {
262
+ if (movesPlayed > 0 ) {
263
263
final int previousMove = moves [movesPlayed - 1 ];
264
264
final int pRow = previousMove / 27 , pCol = (previousMove % 9 ) / 3 ;
265
- if (!(bRow == pRow && bRow == pCol )) {
265
+ if (!(( largeOccupied & ( 1 << ( pRow * 3 + pCol ))) != 0 || ( bRow == pRow && bCol == pCol ) )) {
266
266
return false ;
267
267
}
268
268
}
0 commit comments