@@ -10,15 +10,8 @@ public class TicTacToe {
10
10
public static void main (String args []) throws IOException {
11
11
final BufferedReader in = new BufferedReader (new InputStreamReader (System .in ));
12
12
final LargeBoard largeBoard = new LargeBoard ();
13
- // largeBoard.play(1, 0);
14
- // largeBoard.play(1, 1);
15
- // largeBoard.play(1, 3);
16
- // largeBoard.play(1, 2);
17
- // largeBoard.play(1, 7);
18
- // largeBoard.play(1, 9);
19
- // System.out.println(largeBoard);
20
13
final MCTS algorithm = new MCTS ();
21
- algorithm .construct (largeBoard , MCTS . TIME_OUT );
14
+ algorithm .construct (largeBoard , 1 );
22
15
while (true ) {
23
16
String line [] = in .readLine ().split (" " );
24
17
final int opponentRow = Integer .parseInt (line [0 ]), opponentCol = Integer .parseInt (line [1 ]);
@@ -29,7 +22,8 @@ public static void main(String args[]) throws IOException {
29
22
}
30
23
largeBoard .play (2 , opponentMove );
31
24
algorithm .root = algorithm .root .getChild (opponentMove );
32
- algorithm .construct (largeBoard , MCTS .TIME_OUT );
25
+ algorithm .root .parent = null ;
26
+ algorithm .construct (largeBoard , 2 );
33
27
System .err .println (largeBoard );
34
28
}
35
29
final int validActionCount = Integer .parseInt (in .readLine ());
@@ -41,7 +35,8 @@ public static void main(String args[]) throws IOException {
41
35
System .out .println (row + " " + col );
42
36
largeBoard .play (1 , bestMove );
43
37
algorithm .root = algorithm .root .getChild (bestMove );
44
- algorithm .construct (largeBoard , MCTS .TIME_OUT );
38
+ algorithm .root .parent = null ;
39
+ algorithm .construct (largeBoard , 1 );
45
40
System .err .println (largeBoard );
46
41
}
47
42
}
@@ -60,12 +55,11 @@ public int suggestMove() {
60
55
.orElseThrow (() -> new RuntimeException ("No moves to play!" ));
61
56
}
62
57
63
- public void construct (final LargeBoard board , final int timeOut ) {
58
+ public void construct (final LargeBoard board , int player ) {
64
59
final long startTime = System .currentTimeMillis ();
65
- while (System .currentTimeMillis () - startTime <= timeOut ) {
60
+ while (System .currentTimeMillis () - startTime <= TIME_OUT ) {
66
61
TreeNode current = root ;
67
62
int position = current .selectChild (board );
68
- int player = 1 ;
69
63
while (current .getChild (position ) != null ) {
70
64
current = current .getChild (position );
71
65
board .play (player , position );
@@ -89,7 +83,7 @@ class TreeNode {
89
83
public final int col ;
90
84
public int plays ;
91
85
public double wins ;
92
- private TreeNode parent ;
86
+ public TreeNode parent ;
93
87
private final int player ;
94
88
private Map <Integer , TreeNode > children = new HashMap <>();
95
89
@@ -130,7 +124,7 @@ private double getUtility() {
130
124
}
131
125
132
126
private double simulate (final LargeBoard board , int player ) {
133
- int numberOfMovesPlayed = board .movesPlayed ;
127
+ final int numberOfMovesPlayed = board .movesPlayed ;
134
128
final int originalPlayer = player ;
135
129
while (board .result () == -1 ) {
136
130
final int currentBoardIndex = board .currentBoard ();
@@ -146,10 +140,6 @@ private double simulate(final LargeBoard board, int player) {
146
140
if (board .canPlay (position )) {
147
141
possibilities [movesToPlay ] = position ;
148
142
movesToPlay ++;
149
- final int result = board .result ();
150
- if (result != -1 ) {
151
- return result == originalPlayer ? 1 : (result == 0 ? 0.5 : 0 );
152
- }
153
143
}
154
144
}
155
145
if (movesToPlay == 0 ) {
@@ -158,10 +148,12 @@ private double simulate(final LargeBoard board, int player) {
158
148
board .play (player , possibilities [random .nextInt (movesToPlay )]);
159
149
player = player == 1 ? 2 : 1 ;
160
150
}
151
+ final int r = board .result ();
152
+ final double result = r == originalPlayer ? 1 : (r == 0 ? 0.5 : 0 );
161
153
while (board .movesPlayed > numberOfMovesPlayed ) {
162
154
board .undo ();
163
155
}
164
- return 0.5 ;
156
+ return result ;
165
157
}
166
158
167
159
public void backPropagate (final TreeNode node ) {
@@ -296,6 +288,9 @@ public int result() {
296
288
}
297
289
298
290
public boolean canPlay (final int p ) {
291
+ if (p < 0 ) {
292
+ return false ;
293
+ }
299
294
final int bRow = p / 27 , bCol = (p % 9 ) / 3 ;
300
295
final int row = (p / 9 ) % 3 , col = p % 3 ;
301
296
if (movesPlayed > 0 ) {
0 commit comments