Skip to content

Commit

Permalink
Merge pull request #9 from BlockCat/optional_games
Browse files Browse the repository at this point in the history
Make games dependent on features
  • Loading branch information
KarelPeeters authored Mar 26, 2024
2 parents 1461db2 + 84ed136 commit 88e30da
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 12 deletions.
42 changes: 34 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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]
Expand All @@ -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"]
1 change: 1 addition & 0 deletions src/ai/mcts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ pub fn mcts_build_tree<B: AltBoard>(
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));

for _ in 0..iterations {
Expand Down
7 changes: 3 additions & 4 deletions src/bin/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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", || {
Expand Down
8 changes: 8 additions & 0 deletions src/games/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/heuristic/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions src/interface/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
//!
Expand All @@ -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;
Expand All @@ -95,7 +107,9 @@ pub mod pov;
pub mod wdl;

pub mod ai;

pub mod games;

pub mod heuristic;

pub mod util;
Expand Down
11 changes: 11 additions & 0 deletions tests/board/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 88e30da

Please sign in to comment.