### MOCATS Tic Tac Toe Example Source: https://docs.rs/mocats/latest/src/mocats/lib Demonstrates how to use the MOCATS library to implement a Tic Tac Toe game. It includes setting up the game, defining policies, running a search tree, and retrieving the best action. ```rust use std::fmt; use std::fmt::{Display, Formatter}; #[derive(Debug, Clone, Copy, PartialEq)] pub struct TicTacToeMove { pub pos: u16 } impl mocats::GameAction for TicTacToeMove {} impl Display for TicTacToeMove { fn fmt(&self, f: &mut Formatter) -> fmt::Result { todo!() } } #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub enum TicTacToePlayer { X, O } impl TicTacToePlayer {} impl mocats::Player for TicTacToePlayer {} impl Display for TicTacToePlayer { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { todo!() } } #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub struct TicTacToePosition { pub board_x: u16, pub board_o: u16, pub turn: TicTacToePlayer, } // Example usage within a function (commented out in original source) // fn foo() { // let game = tic_tac_toe::TicTacToePosition::new(); // let tree_policy = UctPolicy::new(2.0); // let mut search_tree = mocats::SearchTree::new(game, tree_policy); // search_tree.run(2000); // let best_action = search_tree.get_best_action(); // println!("{}", search_tree); // println!("Best action: {}", best_action.unwrap()); // } ``` -------------------------------- ### mocats Tic-Tac-Toe Example Source: https://docs.rs/mocats/latest/src/mocats/lib This snippet references a tic_tac_toe module, suggesting an example implementation or integration of the mocats library with the game of Tic-Tac-Toe. ```Rust // mocats/tic_tac_toe.rs ``` -------------------------------- ### Run Monte Carlo Tree Search Source: https://docs.rs/mocats/latest/src/mocats/lib Provides a Rust code example for initializing and running a Monte Carlo Tree Search using the mocats library. It includes importing necessary components and setting up the search with a game and tree policy. ```rust use mocats::{tic_tac_toe, UctPolicy}; // Example usage would follow here, demonstrating the creation of a game state // and then running the search. ``` -------------------------------- ### mocats Modules and Features Source: https://docs.rs/mocats/latest/mocats/trait Outlines the module structure of the mocats library, highlighting the 'tic_tac_toe' module as an example. Also mentions the availability of feature flags for customization. ```APIDOC Modules: - tic_tac_toe: Example module demonstrating MCTS for Tic-Tac-Toe. Feature Flags: - Browse available feature flags for customizing mocats behavior. ``` -------------------------------- ### Get Node Count Source: https://docs.rs/mocats/latest/src/mocats/search_node This function calculates the total number of nodes in the search tree, starting from the current node and recursively including all its children. ```rust pub fn get_node_count(&self) -> u32 { let mut count: u32 = 1; for child in &self.children { count += child.get_node_count(); } count } ``` -------------------------------- ### Get Reward for Player in TicTacToe Source: https://docs.rs/mocats/latest/mocats/tic_tac_toe/struct Determines if the game has ended and returns the reward for a specified player. This method is part of the GameState trait. ```rust fn get_reward_for_player(&self, player: TicTacToePlayer) -> f32 ``` -------------------------------- ### mocats Crate Overview Source: https://docs.rs/mocats/latest/mocats/all Provides an overview of the mocats crate, including its version, license, source code links, and dependencies. It also highlights the percentage of documented code. ```rust Project: /websites/rs_mocats Content: [ Docs.rs ](https://docs.rs/) * [ mocats-0.3.0 ](https://docs.rs/mocats/latest/mocats/all.html "A fast, easy-to-use, generalized Monte Carlo Tree Search library. Works for any game, any number of players, and any tree policy \(UCT Policy included as a default\).") * mocats 0.3.0 * [](https://docs.rs/mocats/0.3.0/mocats/all.html "Get a link to this specific version") * [ ](https://docs.rs/crate/mocats/latest "See mocats in docs.rs") * [MIT](https://spdx.org/licenses/MIT) * Links * [ ](https://github.com/goodvibs/mocats) * [ ](https://crates.io/crates/mocats "See mocats in crates.io") * [ ](https://docs.rs/crate/mocats/latest/source/ "Browse source of mocats-0.3.0") * Owners * [ ](https://crates.io/users/goodvibs) * Dependencies * * [ fastrand ^2.1.1 _normal_ ](https://docs.rs/fastrand/^2.1.1) * Versions * [ **59.52%** of the crate is documented ](https://docs.rs/crate/mocats/latest) * [ Platform ](https://docs.rs/mocats/latest/mocats/all.html) * [i686-pc-windows-msvc](https://docs.rs/mocats/latest/mocats/all.html#platform-i686-pc-windows-msvc) * [i686-unknown-linux-gnu](https://docs.rs/mocats/latest/mocats/all.html#platform-i686-unknown-linux-gnu) * [x86_64-apple-darwin](https://docs.rs/mocats/latest/mocats/all.html#platform-x86_64-apple-darwin) * [x86_64-pc-windows-msvc](https://docs.rs/mocats/latest/mocats/all.html#platform-x86_64-pc-windows-msvc) * [x86_64-unknown-linux-gnu](https://docs.rs/mocats/latest/mocats/all.html#platform-x86_64-unknown-linux-gnu) * [ Feature flags ](https://docs.rs/mocats/latest/mocats/features "Browse available feature flags of mocats-0.3.0") ``` -------------------------------- ### Get Current Turn in TicTacToe Source: https://docs.rs/mocats/latest/mocats/tic_tac_toe/struct Retrieves the player whose turn it is in the current TicTacToe game state. This method is part of the GameState trait. ```rust fn get_turn(&self) -> TicTacToePlayer ``` -------------------------------- ### mocats Library Overview Source: https://docs.rs/mocats/latest/src/mocats/game This section provides an overview of the mocats library, its version, license, dependencies, and links to its source code and documentation. ```Rust Project: /websites/rs_mocats Library: mocats-0.3.0 License: MIT Dependencies: fastrand ^2.1.1 Documentation: https://docs.rs/mocats/latest/ Source Code: https://github.com/goodvibs/mocats Crates.io: https://crates.io/crates/mocats ``` -------------------------------- ### Get Best Action Source: https://docs.rs/mocats/latest/src/mocats/search_tree Retrieves the best action determined by the MCTS algorithm after running simulations. It identifies the child node with the most visits from the root. ```rust pub fn get_best_action(&mut self) -> Option { self.root.children.as_slice().into_iter().reduce(|a, b| if a.visits > b.visits { a } else { b }).map(|n| n.action.expect("Expected node to have action")) } ``` -------------------------------- ### New SearchTree Constructor Source: https://docs.rs/mocats/latest/src/mocats/search_tree Creates a new `SearchTree` instance. It initializes the tree with a root node, the provided game state, and the specified tree policy. ```rust pub fn new(game: S, tree_policy: Po) -> SearchTree { SearchTree { root: SearchNode { action: None, children: Vec::new(), root_player: game.get_turn(), state: NodeState::ExpandableLeaf, visits: 0, total_value: 0.0 }, root_game_state: game, policy: tree_policy } } ``` -------------------------------- ### Rust Core Trait: Any Source: https://docs.rs/mocats/latest/mocats/enum The `Any` trait provides a way to get the `TypeId` of a type, enabling dynamic type identification. It requires the type to be `'static` and `Sized`. ```APIDOC trait Any where T: 'static + ?Sized fn type_id(&self) -> TypeId - Gets the TypeId of self. - Returns: TypeId ``` -------------------------------- ### mocats Core Files Source: https://docs.rs/mocats/latest/src/mocats/game Lists the main Rust source files within the mocats library, providing entry points for understanding the library's structure and implementation. ```Rust Core Files: - game.rs - lib.rs - search_node.rs - search_tree.rs - tic_tac_toe.rs - tree_policy.rs ``` -------------------------------- ### mocats Library Overview Source: https://docs.rs/mocats/latest/src/mocats/search_tree This snippet provides an overview of the mocats library, its version, license, and links to its source code and dependencies. It highlights the crate's documentation status and supported platforms. ```Rust Project: /websites/rs_mocats Library: mocats-0.3.0 License: MIT Documentation: 59.52% documented Platforms: i686-pc-windows-msvc, i686-unknown-linux-gnu, x86_64-apple-darwin, x86_64-pc-windows-msvc, x86_64-unknown-linux-gnu Dependencies: fastrand ^2.1.1 Source: https://github.com/goodvibs/mocats Crates.io: https://crates.io/crates/mocats ``` -------------------------------- ### Get Available Moves Mask (Rust) Source: https://docs.rs/mocats/latest/src/mocats/tic_tac_toe Calculates a bitmask representing all currently available (empty) positions on the Tic Tac Toe board. This is used to determine valid moves. ```rust pub fn get_moves_mask(&self) -> u16 { !(self.board_x | self.board_o) & BOARD_MASK } // ... ``` -------------------------------- ### APIDOC: Rust Core Convert Traits Source: https://docs.rs/mocats/latest/mocats/tic_tac_toe/enum API documentation for Rust's core conversion traits, TryFrom and TryInto. This includes details on their associated types and methods, providing a reference for how these traits facilitate type conversions. ```APIDOC trait TryFrom: Sized { type Error; fn try_from(value: T) -> Result; } trait TryInto: Sized { type Error; fn try_into(self) -> Result; } // Example Usage: // let number: u32 = 10; // let bytes: Result<[u8; 4], _> = u32::try_from(number).map(|n| n.to_be_bytes()); // let string_val = 123i32.try_into().unwrap_or_default(); // Converts i32 to String // Associated Types: // Error: The type returned in the event of a conversion error. // Methods: // try_from(value: T) -> Result: Performs the conversion from T to Self. // try_into(self) -> Result: Performs the conversion from Self to T. ``` -------------------------------- ### mocats Crate Overview Source: https://docs.rs/mocats/latest/mocats/trait Provides an overview of the mocats crate, its version, license, dependencies, and documentation status. Links to source code and related resources are included. ```Rust Crate: mocats Version: 0.3.0 License: MIT Dependencies: fastrand ^2.1.1 Documentation Coverage: 59.52% Links: - Source: https://github.com/goodvibs/mocats - Crates.io: https://crates.io/crates/mocats - Docs.rs: https://docs.rs/mocats/latest/mocats/ ``` -------------------------------- ### Get Available Moves List (Rust) Source: https://docs.rs/mocats/latest/src/mocats/tic_tac_toe Returns a vector of `u16` values, where each value represents a bitmask for an available move on the Tic Tac Toe board. This is derived from the `get_moves_mask`. ```rust pub fn get_moves(&self) -> Vec { // ... ``` -------------------------------- ### NodeState Enum (Rust) Source: https://docs.rs/mocats/latest/src/mocats/search_node Defines the possible states for a node within the search tree. These states indicate whether a node is a leaf, can be expanded, or has already been expanded, guiding the search algorithm's behavior. ```rust /// Represents the state of a node in the search tree. #[derive(Copy,Clone,Debug,PartialEq,Eq)] pub enum NodeState { /// No children, but might have available actions ExpandableLeaf, /// No children, no available actions TerminalLeaf, /// Has children (already expanded) Expanded } ``` -------------------------------- ### Rustdoc Keyboard Shortcuts Source: https://docs.rs/mocats/latest/help This section outlines the keyboard shortcuts available within the Rust documentation generated by rustdoc. These shortcuts enhance navigation and interaction with the documentation, such as focusing the search field, moving through search results, and expanding/collapsing sections. ```APIDOC Rustdoc Keyboard Shortcuts: `?`: Show this help dialog `S` / `/`: Focus the search field `↑`: Move up in search results `↓`: Move down in search results `←` / `→`: Switch result tab (when results focused) `⏎`: Go to active search result `+`: Expand all sections `-`: Collapse all sections `_`: Collapse all sections, including impl blocks ``` -------------------------------- ### Run MCTS Search with UctPolicy Source: https://docs.rs/mocats/latest/index Demonstrates how to initialize and run a Monte Carlo Tree Search using the mocats library. It involves creating a game state, a UCT policy, and a search tree, then executing the search and retrieving the best action. ```rust use mocats::{tic_tac_toe, UctPolicy}; fn foo() { let game = tic_tac_toe::TicTacToePosition::new(); let tree_policy = UctPolicy::new(2.0); let mut search_tree = mocats::SearchTree::new(game, tree_policy); search_tree.run(2000); let best_action = search_tree.get_best_action(); println!("{}", search_tree); println!("Best action: {}", best_action.unwrap()); } ``` -------------------------------- ### Run MCTS Search with UctPolicy Source: https://docs.rs/mocats/latest/mocats Demonstrates how to initialize and run a Monte Carlo Tree Search using the mocats library. It involves creating a game state, a UCT policy, and a search tree, then executing the search and retrieving the best action. ```rust use mocats::{tic_tac_toe, UctPolicy}; fn foo() { let game = tic_tac_toe::TicTacToePosition::new(); let tree_policy = UctPolicy::new(2.0); let mut search_tree = mocats::SearchTree::new(game, tree_policy); search_tree.run(2000); let best_action = search_tree.get_best_action(); println!("{}", search_tree); println!("Best action: {}", best_action.unwrap()); } ``` -------------------------------- ### Get Winner in Rust Tic Tac Toe Source: https://docs.rs/mocats/latest/src/mocats/tic_tac_toe Identifies if there is a winner in the current Tic Tac Toe game. It iterates through predefined winning masks and checks if either player 'X' or 'O' has occupied all positions for any winning combination. Returns `Some(TicTacToePlayer)` if a winner is found, otherwise `None`. ```rust pub fn get_winner(&self) -> Option { for mask in WIN_MASKS.iter() { if self.board_x & *mask == *mask { return Some(TicTacToePlayer::X); } if self.board_o & *mask == *mask { return Some(TicTacToePlayer::O); } } None } ``` -------------------------------- ### Tic Tac Toe Game State Management Source: https://docs.rs/mocats/latest/src/mocats/tic_tac_toe Implements the `GameState` trait for `TicTacToePosition` to manage Tic Tac Toe game logic. This includes getting available moves, applying a move, determining the current player's turn, and calculating rewards based on win/loss/draw conditions. ```rust impl GameState for TicTacToePosition { fn get_actions(&self) -> Vec { self.get_moves().iter().map(|&pos| TicTacToeMove { pos }).collect() } fn apply_action(&mut self, action: &TicTacToeMove) { self.make_move(action.pos); } fn get_turn(&self) -> TicTacToePlayer { self.turn } fn get_reward_for_player(&self, player: TicTacToePlayer) -> f32 { match self.get_winner() { Some(winner) => { return if winner == player { 1. } else { -1. } }, None => 0. } } } ``` -------------------------------- ### mocats Library Documentation Source: https://docs.rs/mocats/latest/settings Provides an overview of the mocats Rust library, including its purpose as a generalized Monte Carlo Tree Search (MCTS) implementation, its dependencies (fastrand), supported platforms, and license (MIT). ```rust /// A fast, easy-to-use, generalized Monte Carlo Tree Search library. /// Works for any game, any number of players, and any tree policy (UCT Policy included as a default). /// /// Dependencies: /// * fastrand ^2.1.1 /// /// License: /// MIT /// /// Supported Platforms: /// * i686-pc-windows-msvc /// * i686-unknown-linux-gnu /// * x86_64-apple-darwin /// * x86_64-pc-windows-msvc /// * x86_64-unknown-linux-gnu ``` -------------------------------- ### Get Available Moves in Rust Tic Tac Toe Source: https://docs.rs/mocats/latest/src/mocats/tic_tac_toe Retrieves a list of available moves on the Tic Tac Toe board. It returns an empty vector if the game is over (board full or a winner exists). This function relies on `is_board_full` and `get_winner` to determine game state and `unpack` to convert a bitmask to a list of moves. ```rust pub fn get_moves_mask(&self) -> u16 { if self.is_board_full() | self.get_winner().is_some() { return Vec::new(); } unpack(self.get_moves_mask()) } ``` -------------------------------- ### mocats Tic-Tac-Toe Module Documentation Source: https://docs.rs/mocats/latest/mocats/tic_tac_toe/index Provides an overview of the tic-tac-toe module within the mocats library, detailing its structs, enums, and functions. It also links to the source code and related documentation. ```rust /// Module tic_tac_toe /// Contains a tic-tac-toe game implementation. /// /// Structs: /// - SearchNode /// - SearchTree /// - UctPolicy /// /// Enums: /// - NodeState /// /// Traits: /// - GameAction /// - GameState /// - Player /// - TreePolicy ``` -------------------------------- ### Running MCTS Search with mocats Source: https://docs.rs/mocats/latest/mocats/index Shows how to initialize and run a Monte Carlo Tree Search using the mocats library with a Tic Tac Toe game and UCT policy. ```rust use mocats::{tic_tac_toe, UctPolicy}; fn foo() { let game = tic_tac_toe::TicTacToePosition::new(); let tree_policy = UctPolicy::new(2.0); let mut search_tree = mocats::SearchTree::new(game, tree_policy); search_tree.run(2000); let best_action = search_tree.get_best_action(); println!("{}", search_tree); println!("Best action: {}", best_action.unwrap()); } ``` -------------------------------- ### mocats Core Files Source: https://docs.rs/mocats/latest/src/mocats/tic_tac_toe Lists the main Rust source files for the mocats library, indicating the modular structure for game logic, search nodes, search trees, and tree policies. ```Rust mocats [game.rs](https://docs.rs/mocats/latest/src/mocats/game.rs.html) [lib.rs](https://docs.rs/mocats/latest/src/mocats/lib.rs.html) [search_node.rs](https://docs.rs/mocats/latest/src/mocats/search_node.rs.html) [search_tree.rs](https://docs.rs/mocats/latest/src/mocats/search_tree.rs.html) [tic_tac_toe.rs](https://docs.rs/mocats/latest/src/mocats/tic_tac_toe.rs.html) [tree_policy.rs](https://docs.rs/mocats/latest/src/mocats/tree_policy.rs.html) ``` -------------------------------- ### mocats Supported Platforms Source: https://docs.rs/mocats/latest/src/mocats/game Lists the platforms for which the mocats library has been compiled and tested, indicating cross-platform compatibility. ```Rust Supported Platforms: - i686-pc-windows-msvc - i686-unknown-linux-gnu - x86_64-apple-darwin - x86_64-pc-windows-msvc - x86_64-unknown-linux-gnu ``` -------------------------------- ### mocats Structs Source: https://docs.rs/mocats/latest/mocats/all Lists the available structs in the mocats crate, including SearchNode, SearchTree, UctPolicy, and specific structs for TicTacToe. ```rust ### Structs * [SearchNode](https://docs.rs/mocats/latest/mocats/struct.SearchNode.html) * [SearchTree](https://docs.rs/mocats/latest/mocats/struct.SearchTree.html) * [UctPolicy](https://docs.rs/mocats/latest/mocats/struct.UctPolicy.html) * [tic_tac_toe::TicTacToeMove](https://docs.rs/mocats/latest/mocats/tic_tac_toe/struct.TicTacToeMove.html) * [tic_tac_toe::TicTacToePosition](https://docs.rs/mocats/latest/mocats/tic_tac_toe/struct.TicTacToePosition.html) ``` -------------------------------- ### mocats Library Overview Source: https://docs.rs/mocats/latest/src/mocats/lib This snippet provides an overview of the mocats library, its purpose, and its dependencies. It highlights the library's compatibility with any game, number of players, and tree policy, including UCT as a default. ```Rust /// A fast, easy-to-use, generalized Monte Carlo Tree Search library. /// Works for any game, any number of players, and any tree policy /// (UCT Policy included as a default). // Dependencies: // - fastrand ^2.1.1 ``` -------------------------------- ### mocats Core Files Source: https://docs.rs/mocats/latest/src/mocats/search_tree Lists the core Rust source files within the mocats library, including game logic, search tree implementation, and tree policies. ```Rust Files: - game.rs - lib.rs - search_node.rs - search_tree.rs - tic_tac_toe.rs - tree_policy.rs ``` -------------------------------- ### TicTacToePosition Methods Source: https://docs.rs/mocats/latest/mocats/tic_tac_toe/struct Provides documentation for the methods available on the TicTacToePosition struct, enabling game logic operations. ```APIDOC impl TicTacToePosition { /// Returns a bitmask of available moves. fn get_moves(&self) -> u32; /// Returns a bitmask of available moves, considering specific game rules. fn get_moves_mask(&self) -> u32; /// Determines the winner of the game, if any. /// Returns 0 for no winner, 1 for player X, 2 for player O. fn get_winner(&self) -> u8; /// Checks if the Tic Tac Toe board is full. fn is_board_full(&self) -> bool; /// Makes a move on the board for the current player. /// Takes a move represented as a bitmask. fn make_move(&mut self, move_mask: u32); /// Creates a new TicTacToePosition, initializing an empty board. fn new() -> Self; } ``` -------------------------------- ### mocats Library Overview Source: https://docs.rs/mocats/latest/src/mocats/search_node This snippet provides an overview of the mocats library, its purpose, and links to its source code and documentation. It highlights the library's flexibility for different games and policies. ```Rust /// A fast, easy-to-use, generalized Monte Carlo Tree Search library. /// Works for any game, any number of players, and any tree policy (UCT Policy included as a default). /// /// Links: /// - Source: https://github.com/goodvibs/mocats /// - Crates.io: https://crates.io/crates/mocats /// - Docs.rs: https://docs.rs/mocats/latest/mocats/ /// /// Files: /// - game.rs /// - lib.rs /// - search_node.rs /// - search_tree.rs /// - tic_tac_toe.rs /// - tree_policy.rs ``` -------------------------------- ### Add mocats Dependency Source: https://docs.rs/mocats/latest/src/mocats/lib Demonstrates how to add the mocats library as a dependency to your Rust project using the cargo add command. ```bash cargo add mocats ``` -------------------------------- ### Rustdoc Settings Source: https://docs.rs/mocats/latest/settings Configuration options for customizing the appearance and behavior of Rustdoc generated documentation. ```APIDOC Theme: light dark ayu system preference Preferred light theme: light dark ayu Preferred dark theme: light dark ayu Auto-hide item contents for large items Auto-hide item methods' documentation Auto-hide trait implementation documentation Directly go to item in search if there is only one result Show line numbers on code examples Hide persistent navigation bar Hide table of contents Hide module navigation Disable keyboard shortcuts Use sans serif fonts Word wrap source code ``` -------------------------------- ### New SearchNode Constructor Source: https://docs.rs/mocats/latest/src/mocats/search_node A constructor function to create a new SearchNode. It initializes the node with an optional action, the root player, and default values for state, visits, and total value. ```rust pub fn new(action: Option, root_player: Pl) -> SearchNode { SearchNode:: { action: action, children: Vec::new(), root_player: root_player, state: NodeState::ExpandableLeaf, visits: 0, total_value: 0.0 } } ``` -------------------------------- ### Add mocats Dependency (Cargo.toml) Source: https://docs.rs/mocats/latest/src/mocats/lib Shows how to manually add the mocats dependency to your Cargo.toml file, specifying the version. ```toml [dependencies] mocats = "0.2.1" ``` -------------------------------- ### Initialize Tic Tac Toe Game State (Rust) Source: https://docs.rs/mocats/latest/src/mocats/tic_tac_toe Provides a constructor for the `TicTacToePosition` struct, initializing an empty board with player X to move first. ```rust impl TicTacToePosition { pub fn new() -> TicTacToePosition { TicTacToePosition { board_x: 0, board_o: 0, turn: TicTacToePlayer::X, } } // ... ``` -------------------------------- ### TicTacToePosition Methods Source: https://docs.rs/mocats/latest/mocats/tic_tac_toe/struct Provides essential methods for managing a Tic Tac Toe game. These include initializing a new game, calculating a bitmask of valid moves, retrieving a list of valid moves, executing a move on the board, checking if the board is full, and identifying the winner. ```rust impl TicTacToePosition { pub fn new() -> TicTacToePosition pub fn get_moves_mask(&self) -> u16 pub fn get_moves(&self) -> Vec pub fn make_move(&mut self, pos: u16) pub fn is_board_full(&self) -> bool pub fn get_winner(&self) -> Option } ``` -------------------------------- ### Rust TryFrom and TryInto Traits Source: https://docs.rs/mocats/latest/mocats/tic_tac_toe/struct Documentation for the `TryFrom` and `TryInto` traits, which enable fallible conversions between types. `TryFrom` provides a `try_from` method, and `TryInto` provides a `try_into` method. Both return a `Result` to handle potential conversion errors. ```APIDOC trait TryFrom { type Error; fn try_from(value: U) -> Result; } trait TryInto { type Error; fn try_into(self) -> Result; } // TryFrom::Error: The type returned in the event of a conversion error. // TryFrom::try_from: Performs the conversion. // TryInto::Error: The type returned in the event of a conversion error. // TryInto::try_into: Performs the conversion. // Requires: U: Into for TryFrom, U: TryFrom for TryInto ``` -------------------------------- ### Rustdoc Search Tricks Source: https://docs.rs/mocats/latest/help This section details advanced search functionalities within rustdoc. It explains how to prefix searches with item kinds (e.g., `fn:`, `mod:`), search by type signature, use exact name matching with double quotes, search for slices and arrays using square brackets, and search for items within specific paths. ```APIDOC Rustdoc Search Tricks: Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given item kind. Accepted kinds are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`. Search functions by type signature (e.g., `vec -> usize` or `-> vec` or `String, enum:Cow -> bool`). Look for items with an exact name by putting double quotes around your request: `"string"`. Look for functions that accept or return slices and arrays by writing square brackets (e.g., `-> [u8]` or `[] -> Option`). Look for items inside another one by searching for a path: `vec::Vec`. ``` -------------------------------- ### Add mocats Dependency to Cargo.toml Source: https://docs.rs/mocats/latest/index Instructions on how to add the mocats library as a dependency to your Rust project using Cargo. ```cargo cargo add mocats ``` ```cargo [dependencies] mocats = "0.2.1" ``` -------------------------------- ### mocats Library Overview Source: https://docs.rs/mocats/latest/src/mocats/tree_policy This snippet provides an overview of the mocats Rust library, its purpose, and its key features. It highlights the generalized nature of the MCTS implementation and its flexibility for various game types and policies. ```Rust /// A fast, easy-to-use, generalized Monte Carlo Tree Search library. /// Works for any game, any number of players, and any tree policy (UCT Policy included as a default). ``` -------------------------------- ### mocats Source Code Navigation Source: https://docs.rs/mocats/latest/src/mocats/tree_policy This snippet provides links to navigate the source code of the mocats library on docs.rs. It includes links to specific files within the library. ```Rust game.rs lib.rs search_node.rs search_tree.rs tic_tac_toe.rs tree_policy.rs ``` -------------------------------- ### mocats Library Overview Source: https://docs.rs/mocats/latest/src/mocats/tic_tac_toe This snippet provides an overview of the mocats library, its purpose as a generalized Monte Carlo Tree Search (MCTS) library, and its compatibility with different games and tree policies, including UCT. ```Rust /// A fast, easy-to-use, generalized Monte Carlo Tree Search library. /// Works for any game, any number of players, and any tree policy (UCT Policy included as a default). ``` -------------------------------- ### Add mocats Dependency Source: https://docs.rs/mocats/latest/mocats/index Demonstrates how to add the mocats library as a dependency to your Rust project using Cargo. ```cargo cargo add mocats ``` ```cargo [dependencies] mocats = "0.2.1" ``` -------------------------------- ### mocats Library Overview Source: https://docs.rs/mocats/latest/mocats/index Provides a fast, easy-to-use, generalized Monte Carlo Tree Search library. It supports any game, any number of players, and any tree policy, with UCTPolicy as a default. The library is single-threaded. ```rust /// A fast, easy-to-use, generalized Monte Carlo Tree Search library. /// Works for any game, any number of players, and any tree policy /// (UCT Policy included as a default). /// As of the current version, the search is single-threaded. /// /// # Features /// - Fast and efficient Monte Carlo Tree Search implementation /// - Easy-to-use API /// - Customizable number of players (uses paranoid approach for more than 2 players) /// - Customizable tree policies /// - Nicely formatted display output for debugging ``` -------------------------------- ### TicTacToePosition Hashing and Equality Source: https://docs.rs/mocats/latest/mocats/tic_tac_toe/struct Implements the Hash and PartialEq traits for TicTacToePosition, enabling hashing and equality comparisons for game positions. ```APIDOC impl Hash for TicTacToePosition fn hash<__H: Hasher>(&self, state: &mut __H) Feeds this value into the given Hasher. fn hash_slice(data: &[Self], state: &mut H) where H: Hasher, Self: Sized, Feeds a slice of this type into the given Hasher. impl PartialEq for TicTacToePosition fn eq(&self, other: &TicTacToePosition) -> bool Tests for self and other values to be equal, and is used by ==. fn ne(&self, other: &TicTacToePosition) -> bool Tests for !=. ``` -------------------------------- ### Run MCTS Iterations Source: https://docs.rs/mocats/latest/src/mocats/search_tree Executes the MCTS algorithm for a specified number of iterations. Each iteration involves running a simulation from the root node using the defined policy. ```rust pub fn run(&mut self, iterations: usize) { for _ in 0..iterations { self.root.run_iteration(&mut self.root_game_state.clone(), &self.policy); } } ``` -------------------------------- ### mocats Library Overview Source: https://docs.rs/mocats/latest/index This snippet provides a high-level overview of the mocats Rust library, highlighting its core features and capabilities. It is a generalized Monte Carlo Tree Search library that works for any game, any number of players, and any tree policy. ```Rust /// A fast, easy-to-use, generalized Monte Carlo Tree Search library. /// Works for any game, any number of players, and any tree policy (UCT Policy included as a default). /// As of the current version, the search is single-threaded. /// /// Features: /// * Fast and efficient Monte Carlo Tree Search implementation /// * Easy-to-use API /// * Customizable number of players (uses paranoid approach for more than 2 players) /// * Customizable tree policies /// * Nicely formatted display output for debugging ``` -------------------------------- ### mocats Library Overview Source: https://docs.rs/mocats/latest/mocats This snippet provides a high-level overview of the mocats Rust library, highlighting its core features and capabilities. It is a generalized Monte Carlo Tree Search library that works for any game, any number of players, and any tree policy. ```Rust /// A fast, easy-to-use, generalized Monte Carlo Tree Search library. /// Works for any game, any number of players, and any tree policy (UCT Policy included as a default). /// As of the current version, the search is single-threaded. /// /// Features: /// * Fast and efficient Monte Carlo Tree Search implementation /// * Easy-to-use API /// * Customizable number of players (uses paranoid approach for more than 2 players) /// * Customizable tree policies /// * Nicely formatted display output for debugging ``` -------------------------------- ### Add mocats Dependency to Cargo.toml Source: https://docs.rs/mocats/latest/mocats Instructions on how to add the mocats library as a dependency to your Rust project using Cargo. ```cargo cargo add mocats ``` ```cargo [dependencies] mocats = "0.2.1" ``` -------------------------------- ### mocats Functions Source: https://docs.rs/mocats/latest/mocats/all Lists the available functions in the mocats crate, specifically the print_board function within the tic_tac_toe module. ```rust ### Functions * [tic_tac_toe::print_board](https://docs.rs/mocats/latest/mocats/tic_tac_toe/fn.print_board.html) ``` -------------------------------- ### NodeState PartialEq and Eq Implementations Source: https://docs.rs/mocats/latest/mocats/enum Provides implementations for PartialEq and Eq traits, enabling equality comparisons between NodeState instances. This is fundamental for checking if two nodes are in the same state. ```rust impl PartialEq for NodeState { fn eq(&self, other: &Self) -> bool { // Implementation details for equality comparison // This is a placeholder, actual implementation would be in the source. std::ptr::eq(self, other) } } impl Eq for NodeState {} ``` -------------------------------- ### mocats Traits Overview Source: https://docs.rs/mocats/latest/mocats/index Provides an overview of the key traits available in the mocats Rust library. These traits define the fundamental interfaces for game development within the library. ```APIDOC GameAction: Represents a legal game action that can be applied to some GameState. GameState: Represents a game state. Player: Represents a player in a game. Should be an enum. TreePolicy: A trait that defines a tree policy. ``` -------------------------------- ### Rust TicTacToePosition Clone Methods Source: https://docs.rs/mocats/latest/mocats/tic_tac_toe/struct Provides methods for cloning TicTacToePosition instances. The `clone` method creates a duplicate, while `clone_from` performs copy-assignment. ```rust fn clone(&self) -> TicTacToePosition // Returns a duplicate of the value. ``` ```rust fn clone_from(&mut self, source: &Self) // Performs copy-assignment from `source`. ``` -------------------------------- ### mocats Supported Platforms Source: https://docs.rs/mocats/latest/src/mocats/tree_policy This snippet lists the target platforms for which the mocats library has been compiled and tested. This indicates the cross-platform compatibility of the library. ```Rust i686-pc-windows-msvc i686-unknown-linux-gnu x86_64-apple-darwin x86_64-pc-windows-msvc x86_64-unknown-linux-gnu ``` -------------------------------- ### mocats Dependencies Source: https://docs.rs/mocats/latest/src/mocats/tree_policy This snippet details the external dependencies required by the mocats library. It specifically mentions the 'fastrand' crate with a version constraint. ```Rust fastrand ^2.1.1 ``` -------------------------------- ### mocats Traits Source: https://docs.rs/mocats/latest/mocats/all Lists the available traits in the mocats crate, including GameAction, GameState, Player, and TreePolicy. ```rust ### Traits * [GameAction](https://docs.rs/mocats/latest/mocats/trait.GameAction.html) * [GameState](https://docs.rs/mocats/latest/mocats/trait.GameState.html) * [Player](https://docs.rs/mocats/latest/mocats/trait.Player.html) * [TreePolicy](https://docs.rs/mocats/latest/mocats/trait.TreePolicy.html) ``` -------------------------------- ### mocats Core Structs and Traits Source: https://docs.rs/mocats/latest/mocats/tic_tac_toe/index Details the core data structures and traits provided by the mocats library for implementing Monte Carlo Tree Search algorithms. This includes nodes, trees, policies, and game-specific interfaces. ```APIDOC SearchNode: Represents a node in the MCTS search tree. SearchTree: Manages the MCTS search process, including node expansion and traversal. UctPolicy: Implements the Upper Confidence bounds applied to Trees (UCT) algorithm for selecting child nodes. NodeState: An enum representing the possible states of a node in the search tree (e.g., expanded, unexpanded). GameAction: A trait representing an action that can be taken within a game. GameState: A trait representing the current state of a game. Player: A trait representing a player in the game. TreePolicy: A trait defining the logic for selecting the next node to explore in the search tree. ``` -------------------------------- ### TicTacToePlayer Display Implementation Source: https://docs.rs/mocats/latest/mocats/tic_tac_toe/enum Allows TicTacToePlayer to be formatted as a displayable string, typically for user output. ```rust fn fmt(&self, f: &mut Formatter<'_>) -> Result ``` -------------------------------- ### Rust Core Trait: CloneToUninit Source: https://docs.rs/mocats/latest/mocats/enum The `CloneToUninit` trait, an experimental nightly-only API, allows cloning a value into an uninitialized memory location. It requires the type to implement `Clone`. ```APIDOC trait CloneToUninit where T: Clone unsafe fn clone_to_uninit(&self, dest: *mut u8) - Performs copy-assignment from `self` to `dest`. - Parameters: - dest: A mutable pointer to the destination buffer. ``` -------------------------------- ### Tic Tac Toe Game Implementation for mocats Source: https://docs.rs/mocats/latest/mocats/index Provides a complete implementation of the Tic Tac Toe game, including game state, actions, players, and the necessary traits for use with the mocats library. ```rust use std::fmt; use std::fmt::{Display, Formatter}; #[derive(Debug, Clone, Copy, PartialEq)] pub struct TicTacToeMove { pub pos: u16 } impl mocats::GameAction for TicTacToeMove {} impl Display for TicTacToeMove { fn fmt(&self, f: &mut Formatter) -> fmt::Result { todo!() } } #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub enum TicTacToePlayer { X, O } impl TicTacToePlayer {} impl mocats::Player for TicTacToePlayer {} impl Display for TicTacToePlayer { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { todo!() } } #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub struct TicTacToePosition { pub board_x: u16, pub board_o: u16, pub turn: TicTacToePlayer, } impl TicTacToePosition { pub fn new() -> TicTacToePosition { todo!() } } impl Display for TicTacToePosition { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { todo!() } } impl mocats::GameState for TicTacToePosition { fn get_actions(&self) -> Vec { todo!() } fn apply_action(&mut self, action: &TicTacToeMove) { todo!() } fn get_turn(&self) -> TicTacToePlayer { todo!() } fn get_reward_for_player(&self, player: TicTacToePlayer) -> f32 { todo!() } } ``` -------------------------------- ### mocats Tree Policy Module Source: https://docs.rs/mocats/latest/src/mocats/lib This snippet refers to the tree_policy module, which likely contains implementations for different tree traversal or selection policies used in MCTS, such as UCT. ```Rust // mocats/tree_policy.rs ```