mcts_lib/board.rs
1/// The central trait of the library, defining the interface for a game state.
2///
3/// To use the MCTS algorithm with a custom game, this trait must be implemented.
4/// It provides the MCTS engine with the necessary methods to understand and interact with the game logic.
5pub trait Board: Clone {
6 /// The type representing a move in the game. This could be a simple `u8` for a board position
7 /// or a more complex struct for games with intricate actions.
8 type Move;
9
10 /// Returns the player whose turn it is to make a move.
11 fn get_current_player(&self) -> Player;
12
13 /// Returns the current outcome of the game.
14 fn get_outcome(&self) -> GameOutcome;
15
16 /// Returns a list of all legal moves available from the current state.
17 fn get_available_moves(&self) -> Vec<Self::Move>;
18
19 /// Applies a given move to the board, modifying its state.
20 fn perform_move(&mut self, b_move: &Self::Move);
21
22 /// Returns a hash value for the current board state.
23 fn get_hash(&self) -> u128;
24}
25
26/// Represents the possible outcomes of a game.
27#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
28pub enum GameOutcome {
29 /// The game is still ongoing.
30 InProgress = 0,
31 /// The current player has won.
32 Win = 1,
33 /// The current player has lost.
34 Lose = 2,
35 /// The game has ended in a draw.
36 Draw = 3,
37}
38
39/// Represents the players in the game from the perspective of the MCTS search.
40#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
41pub enum Player {
42 /// The player for whom the MCTS is currently searching for the best move.
43 Me = 1,
44 /// The opponent.
45 Other = 2,
46}
47
48/// Used for alpha-beta pruning to mark nodes as having a definite outcome.
49#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
50pub enum Bound {
51 /// The outcome of the node is not yet determined.
52 None = 0,
53 /// This node is a guaranteed win for the current player.
54 DefoWin = 1,
55 /// This node is a guaranteed loss for the current player.
56 DefoLose = 2,
57}