From f49bcb8505634e090bbae3caa7c9db927fe25277 Mon Sep 17 00:00:00 2001 From: Zino Onomiwo Date: Fri, 22 Mar 2024 17:53:18 +0100 Subject: [PATCH] chore: make games dependant on feature --- Cargo.toml | 42 ++++++++++++++++++++++++++++++++-------- src/ai/mcts.rs | 3 +-- src/bin/bench.rs | 7 +++---- src/games/mod.rs | 8 ++++++++ src/heuristic/mod.rs | 3 +++ src/interface/mod.rs | 3 +++ src/interface/uai/mod.rs | 1 + src/lib.rs | 14 ++++++++++++++ tests/board/mod.rs | 11 +++++++++++ 9 files changed, 78 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3fa8032..0949dac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,7 +10,26 @@ categories = ["algorithms", "games"] repository = "https://github.com/KarelPeeters/board-game-rs" [features] -default = [] +game_chess = ["dep:chess"] +game_arimaa = ["dep:arimaa_engine_step", "dep:once_cell"] +game_sttt = [] +game_ttt = [] +game_ataxx = [] +game_oware = [] +game_connect4 = [] +game_go = ["dep:nohash-hasher", "dep:static_assertions", "dep:lazy_static"] +game_all = [ + "game_chess", + "game_arimaa", + "game_sttt", + "game_ttt", + "game_ataxx", + "game_go", + "game_oware", + "game_connect4", +] + +default = ["game_all"] [dependencies] rand = { version = "0.8.5", features = ["small_rng"] } @@ -25,13 +44,12 @@ num_cpus = "1.15.0" # TODO the chess crate is heavy (and takes a long time to build, see below "temp fix"), maybe replace it with # cozy-chess or shakmaty -chess = "3.2.0" -# TODO make arimaa optional with a feature -arimaa_engine_step = { version = "1.0.1" } # , path = "../arimaa-engine-step" -once_cell = "1.18.0" -lazy_static = "1.4.0" -static_assertions = "1.1.0" -nohash-hasher = "0.2.0" +chess = { version = "3.2.0", optional = true } +arimaa_engine_step = { version = "1.0.1", optional = true } # , path = "../arimaa-engine-step" +once_cell = { version = "1.18.0", optional = true } +lazy_static = { version = "1.4.0", optional = true } +static_assertions = { version = "1.1.0", optional = true } +nohash-hasher = { version = "0.2.0", optional = true } # temporary fix until https://github.com/jordanbray/chess/pull/67 is merged [profile.dev.build-override] @@ -47,3 +65,11 @@ debug = true [profile.release-with-debug] inherits = "release" debug = true + +[[bin]] +name = "bench" +required-features = ["game_ataxx", "game_chess", "game_sttt"] + +[[bin]] +name = "go_split_perft" +required-features = ["game_go"] diff --git a/src/ai/mcts.rs b/src/ai/mcts.rs index b0eca45..faa034a 100644 --- a/src/ai/mcts.rs +++ b/src/ai/mcts.rs @@ -364,8 +364,7 @@ pub fn mcts_build_tree( let mut tree = Tree::new(root_board.clone()); - let root_outcome = root_board.outcome().map(|o| o.pov(root_board.next_player().other())); - tree.nodes.push(Node::new(None, root_outcome)); + tree.nodes.push(Node::new(None, None)); for _ in 0..iterations { //we've solved the root node, so we're done diff --git a/src/bin/bench.rs b/src/bin/bench.rs index 4bb1288..a8266c8 100644 --- a/src/bin/bench.rs +++ b/src/bin/bench.rs @@ -2,14 +2,13 @@ use std::time::Instant; -use itertools::Itertools; -use rand::rngs::SmallRng; -use rand::SeedableRng; - use board_game::ai::mcts::mcts_build_tree; use board_game::games::ataxx::AtaxxBoard; use board_game::games::chess::ChessBoard; use board_game::games::sttt::STTTBoard; +use itertools::Itertools; +use rand::rngs::SmallRng; +use rand::SeedableRng; fn main() { bench("mcts_sttt", || { diff --git a/src/games/mod.rs b/src/games/mod.rs index 4ffb109..603a0a3 100644 --- a/src/games/mod.rs +++ b/src/games/mod.rs @@ -1,10 +1,18 @@ +#[cfg(feature = "game_arimaa")] pub mod arimaa; +#[cfg(feature = "game_ataxx")] pub mod ataxx; +#[cfg(feature = "game_chess")] pub mod chess; +#[cfg(feature = "game_connect4")] pub mod connect4; +#[cfg(feature = "game_go")] pub mod go; +#[cfg(feature = "game_oware")] pub mod oware; +#[cfg(feature = "game_sttt")] pub mod sttt; +#[cfg(feature = "game_ttt")] pub mod ttt; pub mod dummy; diff --git a/src/heuristic/mod.rs b/src/heuristic/mod.rs index af23914..dc5d637 100644 --- a/src/heuristic/mod.rs +++ b/src/heuristic/mod.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "game_ataxx")] pub mod ataxx; +#[cfg(feature = "game_chess")] pub mod chess; +#[cfg(feature = "game_sttt")] pub mod sttt; diff --git a/src/interface/mod.rs b/src/interface/mod.rs index 78ed1bf..4ee0f24 100644 --- a/src/interface/mod.rs +++ b/src/interface/mod.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "game_arimaa")] pub mod aei; +#[cfg(feature = "game_go")] pub mod gtp; +#[cfg(feature = "game_ataxx")] pub mod uai; diff --git a/src/interface/uai/mod.rs b/src/interface/uai/mod.rs index 8f65845..453154c 100644 --- a/src/interface/uai/mod.rs +++ b/src/interface/uai/mod.rs @@ -3,5 +3,6 @@ //! A derivative of the UCI protocol for the game Ataxx. //! Loose specification available at . +#[cfg(feature = "game_ataxx")] pub mod client; pub mod command; diff --git a/src/lib.rs b/src/lib.rs index 63ae28f..16b7552 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -53,10 +53,13 @@ //! ## List the available moves on a board and play a random one. //! //! ``` +//! # #[cfg(feature = "game_ataxx")] //! # use board_game::games::ataxx::AtaxxBoard; //! # use board_game::board::{BoardMoves, Board}; //! # use internal_iterator::InternalIterator; //! # let mut rng = rand::thread_rng(); +//! # #[cfg(feature = "game_ataxx")] +//! # { //! let mut board = AtaxxBoard::default(); //! println!("{}", board); //! @@ -68,24 +71,33 @@ //! println!("Picked move {:?}", mv); //! board.play(mv).unwrap(); //! println!("{}", board); +//! # } //! ``` //! //! ## Get the best move according to MCTS //! //! ``` //! # use board_game::ai::mcts::MCTSBot; +//! # #[cfg(feature = "game_ataxx")] //! # use board_game::games::ataxx::AtaxxBoard; //! # use board_game::ai::Bot; //! # use rand::thread_rng; +//! # #[cfg(feature = "game_ataxx")] +//! # { //! let board = AtaxxBoard::default(); //! println!("{}", board); //! //! let mut bot = MCTSBot::new(1000, 2.0, thread_rng()); //! println!("{:?}", bot.select_move(&board)) +//! # } //! ``` // export used game crates + +#[cfg(feature = "game_arimaa")] pub use arimaa_engine_step; + +#[cfg(feature = "game_chess")] pub use chess; pub mod board; @@ -95,7 +107,9 @@ pub mod pov; pub mod wdl; pub mod ai; + pub mod games; + pub mod heuristic; pub mod util; diff --git a/tests/board/mod.rs b/tests/board/mod.rs index a1a1734..ecf38fd 100644 --- a/tests/board/mod.rs +++ b/tests/board/mod.rs @@ -14,15 +14,26 @@ use board_game::util::tiny::consistent_rng; use crate::util::test_sampler_uniform; +#[cfg(feature = "game_arimaa")] mod arimaa; +#[cfg(feature = "game_ataxx")] mod ataxx; +#[cfg(feature = "game_chess")] mod chess; +#[cfg(feature = "game_connect4")] mod connect4; +#[cfg(feature = "game_go")] mod go; +#[cfg(feature = "game_go")] mod go_chains; + mod max_moves; + +#[cfg(feature = "game_oware")] mod oware; +#[cfg(feature = "game_sttt")] mod sttt; +#[cfg(feature = "game_ttt")] mod ttt; // TODO add test for symmetry after playing moves