Monte Carlo Tree Search - Problem
Implement a Monte Carlo Tree Search (MCTS) algorithm for a simple Tic-Tac-Toe game. MCTS is a heuristic search algorithm that combines the precision of tree search with the generality of random sampling.
Your implementation should include all four phases:
- Selection: Start from root, select child nodes using UCB1 formula until reaching a leaf
- Expansion: Add one or more child nodes to expand the tree
- Simulation: Run a random playout from the new node to get a game result
- Backpropagation: Update all nodes on the path with the simulation result
Given a board state and the number of simulations to run, return the best move coordinates [row, col] for the current player.
Board representation: 0 = empty, 1 = player 1 (X), 2 = player 2 (O)
Input & Output
Example 1 — Early Game Position
$
Input:
board = [[1,0,2],[0,0,0],[0,0,0]], simulations = 1000, current_player = 1
›
Output:
[1,1]
💡 Note:
Player X has corner, O took edge. MCTS simulates many random games and finds center position [1,1] wins most often, giving strong strategic control.
Example 2 — Blocking Move Required
$
Input:
board = [[1,1,0],[2,0,0],[0,0,0]], simulations = 500, current_player = 2
›
Output:
[0,2]
💡 Note:
Player O must block X's winning threat. MCTS quickly identifies that [0,2] prevents immediate loss, as failing to block results in X winning on next move.
Example 3 — Winning Move Available
$
Input:
board = [[1,2,1],[0,2,0],[0,0,0]], simulations = 100, current_player = 2
›
Output:
[2,1]
💡 Note:
O can win immediately by completing the middle column. Even with few simulations, MCTS finds [2,1] has 100% win rate in all random playouts.
Constraints
- board is 3×3 matrix with values 0 (empty), 1 (player 1), 2 (player 2)
- 1 ≤ simulations ≤ 10000
- current_player is 1 or 2
- Board contains at least one empty position
- Game is not already finished
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code