Skip to content

Commit b1c8bb3

Browse files
committed
bug fixes. Undo still has to be fixed
1 parent f534c9f commit b1c8bb3

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

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

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

112112
private double simulate(final LargeBoard board, int player) {
113113
int numberOfMovesPlayed = board.movesPlayed;
114-
while (!board.isDecided()) {
114+
while (board.result() == 0) {
115115
final int possibilities[] = new int[9];
116116
int movesToPlay = 0;
117117
for (int position = 0; position < 81; position++) {
118118
if (board.canPlay(position)) {
119119
possibilities[movesToPlay] = position;
120120
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;
123124
}
124125
}
125126
}
@@ -178,9 +179,10 @@ public String toString() {
178179
}
179180

180181
class LargeBoard {
182+
public static final int FULL = (1 << 10) - 1;
181183
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
184186
int moves[] = new int[81];
185187
final Board boards[] = new Board[9];
186188

@@ -199,44 +201,59 @@ public void play(final int player, final int p) {
199201
final int pRow = previousMove / 27, pCol = (previousMove % 9) / 3;
200202
assert bRow == pRow && bRow == pCol;
201203
}
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+
}
203210
movesPlayed++;
204211
}
205212

206213
public void undo() {
207214
movesPlayed--;
208215
final int p = moves[movesPlayed];
209-
final int bRow = p / 27, bCol = (p % 9) / 3;
210216
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));
214220
largeBoard = largeBoard & bitFlipped;
215221
largeCaptures = largeCaptures & bitFlipped;
222+
largeOccupied = largeOccupied & bitFlipped;
216223
}
217224

218-
public boolean result(final int player) {
225+
public int result() {
226+
int firstScore = 0, secondScore = 0;
219227
for (int i = 0; i < 3; i++) {
220228
for (int j = 0; j < 3; j++) {
221229
final int bit = 1 << (i * 3 + j);
222230
if (boards[bit].result(1) == 1) {
223231
largeBoard = largeBoard | bit;
232+
firstScore++;
224233
largeCaptures = largeCaptures | bit;
234+
largeOccupied = largeOccupied | bit;
225235
} else if (boards[bit].result(2) == 2) {
236+
secondScore++;
226237
largeCaptures = largeCaptures | bit;
238+
largeOccupied = largeOccupied | bit;
239+
} else if (boards[bit].occupied == FULL) {
240+
largeOccupied = largeOccupied | bit;
227241
}
228242
}
229243
}
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+
}
240257
}
241258

242259
public boolean canPlay(final int p) {
@@ -249,12 +266,17 @@ public boolean canPlay(final int p) {
249266
return false;
250267
}
251268
}
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;
253271
}
254272

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+
'}';
258280
}
259281
}
260282

0 commit comments

Comments
 (0)