Skip to content

Commit eed94ba

Browse files
committed
bug fixes
1 parent b1c8bb3 commit eed94ba

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/main/java/main/java/codingame/TicTacToe/TicTacToe.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,16 @@ private double getUtility() {
111111

112112
private double simulate(final LargeBoard board, int player) {
113113
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];
116116
int movesToPlay = 0;
117-
for (int position = 0; position < 81; position++) {
117+
for (int position = 0; position < possibilities.length; position++) {
118118
if (board.canPlay(position)) {
119119
possibilities[movesToPlay] = position;
120120
movesToPlay++;
121121
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);
124124
}
125125
}
126126
}
@@ -182,8 +182,7 @@ class LargeBoard {
182182
public static final int FULL = (1 << 10) - 1;
183183
int movesPlayed;
184184
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];
187186
final Board boards[] = new Board[9];
188187

189188
public LargeBoard() {
@@ -196,10 +195,10 @@ public void play(final int player, final int p) {
196195
moves[movesPlayed] = p;
197196
final int bRow = p / 27, bCol = (p % 9) / 3;
198197
final int row = (p / 9) % 3, col = p % 3;
199-
if (movesPlayed > 0 && moves[movesPlayed - 1] != -1) {
198+
if (movesPlayed > 0) {
200199
final int previousMove = moves[movesPlayed - 1];
201200
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);
203202
}
204203
final int position = bRow * 3 + bCol;
205204
assert (largeOccupied & (1 << position)) == 0;
@@ -226,17 +225,18 @@ public int result() {
226225
int firstScore = 0, secondScore = 0;
227226
for (int i = 0; i < 3; i++) {
228227
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) {
231231
largeBoard = largeBoard | bit;
232232
firstScore++;
233233
largeCaptures = largeCaptures | bit;
234234
largeOccupied = largeOccupied | bit;
235-
} else if (boards[bit].result(2) == 2) {
235+
} else if (boards[position].result(2) == 2) {
236236
secondScore++;
237237
largeCaptures = largeCaptures | bit;
238238
largeOccupied = largeOccupied | bit;
239-
} else if (boards[bit].occupied == FULL) {
239+
} else if (boards[position].occupied == FULL) {
240240
largeOccupied = largeOccupied | bit;
241241
}
242242
}
@@ -252,17 +252,17 @@ public int result() {
252252
} else if (largeOccupied == FULL) {
253253
return firstScore > secondScore ? 1 : (secondScore > firstScore ? 2 : 0);
254254
} else {
255-
return 0;
255+
return -1;
256256
}
257257
}
258258

259259
public boolean canPlay(final int p) {
260260
final int bRow = p / 27, bCol = (p % 9) / 3;
261261
final int row = (p / 9) % 3, col = p % 3;
262-
if (movesPlayed > 0 && moves[movesPlayed - 1] != -1) {
262+
if (movesPlayed > 0) {
263263
final int previousMove = moves[movesPlayed - 1];
264264
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))) {
266266
return false;
267267
}
268268
}

0 commit comments

Comments
 (0)