tic_tac_toe/
tic_tac_toe.rs

1extern crate mcts_lib;
2
3use mcts_lib::boards::tic_tac_toe::TicTacToeBoard;
4use mcts_lib::mcts::MonteCarloTreeSearch;
5use mcts_lib::random::CustomNumberGenerator;
6
7fn main() {
8    // Create a new Tic-Tac-Toe board
9    let board = TicTacToeBoard::default();
10
11    // Create a new MCTS search instance
12    let mut mcts = MonteCarloTreeSearch::builder(board)
13        .with_alpha_beta_pruning(false)
14        .with_random_generator(CustomNumberGenerator::default())
15        .build();
16
17    // Run the search for 20,000 iterations
18    mcts.iterate_n_times(20000);
19
20    // Print the chances
21    let root = mcts.get_root();
22    for node in root.children() {
23        println!(
24            "Move: {:?} = {:.2?}%",
25            node.value().prev_move,
26            node.value().wins_rate() * 100.0
27        );
28    }
29
30    // Get the most promising move
31    let best_move_node = root.get_best_child().unwrap();
32    let best_move = best_move_node.value().prev_move;
33
34    println!("The best move is: {:?}", best_move);
35    assert_eq!(best_move, Some(4));
36}