@@ -10,15 +10,8 @@ public class TicTacToe {
1010 public static void main (String args []) throws IOException {
1111 final BufferedReader in = new BufferedReader (new InputStreamReader (System .in ));
1212 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);
2013 final MCTS algorithm = new MCTS ();
21- algorithm .construct (largeBoard , MCTS . TIME_OUT );
14+ algorithm .construct (largeBoard , 1 );
2215 while (true ) {
2316 String line [] = in .readLine ().split (" " );
2417 final int opponentRow = Integer .parseInt (line [0 ]), opponentCol = Integer .parseInt (line [1 ]);
@@ -29,7 +22,8 @@ public static void main(String args[]) throws IOException {
2922 }
3023 largeBoard .play (2 , opponentMove );
3124 algorithm .root = algorithm .root .getChild (opponentMove );
32- algorithm .construct (largeBoard , MCTS .TIME_OUT );
25+ algorithm .root .parent = null ;
26+ algorithm .construct (largeBoard , 2 );
3327 System .err .println (largeBoard );
3428 }
3529 final int validActionCount = Integer .parseInt (in .readLine ());
@@ -41,7 +35,8 @@ public static void main(String args[]) throws IOException {
4135 System .out .println (row + " " + col );
4236 largeBoard .play (1 , bestMove );
4337 algorithm .root = algorithm .root .getChild (bestMove );
44- algorithm .construct (largeBoard , MCTS .TIME_OUT );
38+ algorithm .root .parent = null ;
39+ algorithm .construct (largeBoard , 1 );
4540 System .err .println (largeBoard );
4641 }
4742 }
@@ -60,12 +55,11 @@ public int suggestMove() {
6055 .orElseThrow (() -> new RuntimeException ("No moves to play!" ));
6156 }
6257
63- public void construct (final LargeBoard board , final int timeOut ) {
58+ public void construct (final LargeBoard board , int player ) {
6459 final long startTime = System .currentTimeMillis ();
65- while (System .currentTimeMillis () - startTime <= timeOut ) {
60+ while (System .currentTimeMillis () - startTime <= TIME_OUT ) {
6661 TreeNode current = root ;
6762 int position = current .selectChild (board );
68- int player = 1 ;
6963 while (current .getChild (position ) != null ) {
7064 current = current .getChild (position );
7165 board .play (player , position );
@@ -89,7 +83,7 @@ class TreeNode {
8983 public final int col ;
9084 public int plays ;
9185 public double wins ;
92- private TreeNode parent ;
86+ public TreeNode parent ;
9387 private final int player ;
9488 private Map <Integer , TreeNode > children = new HashMap <>();
9589
@@ -130,7 +124,7 @@ private double getUtility() {
130124 }
131125
132126 private double simulate (final LargeBoard board , int player ) {
133- int numberOfMovesPlayed = board .movesPlayed ;
127+ final int numberOfMovesPlayed = board .movesPlayed ;
134128 final int originalPlayer = player ;
135129 while (board .result () == -1 ) {
136130 final int currentBoardIndex = board .currentBoard ();
@@ -146,10 +140,6 @@ private double simulate(final LargeBoard board, int player) {
146140 if (board .canPlay (position )) {
147141 possibilities [movesToPlay ] = position ;
148142 movesToPlay ++;
149- final int result = board .result ();
150- if (result != -1 ) {
151- return result == originalPlayer ? 1 : (result == 0 ? 0.5 : 0 );
152- }
153143 }
154144 }
155145 if (movesToPlay == 0 ) {
@@ -158,10 +148,12 @@ private double simulate(final LargeBoard board, int player) {
158148 board .play (player , possibilities [random .nextInt (movesToPlay )]);
159149 player = player == 1 ? 2 : 1 ;
160150 }
151+ final int r = board .result ();
152+ final double result = r == originalPlayer ? 1 : (r == 0 ? 0.5 : 0 );
161153 while (board .movesPlayed > numberOfMovesPlayed ) {
162154 board .undo ();
163155 }
164- return 0.5 ;
156+ return result ;
165157 }
166158
167159 public void backPropagate (final TreeNode node ) {
@@ -296,6 +288,9 @@ public int result() {
296288 }
297289
298290 public boolean canPlay (final int p ) {
291+ if (p < 0 ) {
292+ return false ;
293+ }
299294 final int bRow = p / 27 , bCol = (p % 9 ) / 3 ;
300295 final int row = (p / 9 ) % 3 , col = p % 3 ;
301296 if (movesPlayed > 0 ) {
0 commit comments