Skip to content

Commit

Permalink
begin better cli
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoDog896 committed Sep 23, 2024
1 parent d3a54eb commit aa58a33
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/game-solver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn negamax<T: Game<Player = impl TwoPlayer> + Eq + Hash>(

// check if this is a winning configuration
if let Ok(Some(board)) = game.find_immediately_resolvable_game() {
return Ok(upper_bound(&board) - board.move_count() as isize - 1);
return Ok(upper_bound(&board) - board.move_count() as isize);
}

// fetch values from the transposition table
Expand Down
12 changes: 11 additions & 1 deletion crates/games/src/nim/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Game for Nim {
impl Display for Nim {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (i, heap) in self.heaps.iter().enumerate() {
write!(f, "heap {i}: {heap}")?;
writeln!(f, "Heap {i}: {heap}")?;
}

Ok(())
Expand Down Expand Up @@ -168,3 +168,13 @@ impl TryFrom<NimArgs> for Nim {
Ok(game)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn max_moves_is_heap_sum() {
assert_eq!(Nim::new(vec![3, 5, 7]).max_moves(), Some(3 + 5 + 7));
}
}
5 changes: 5 additions & 0 deletions crates/games/src/sprouts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#![doc = include_str!("./README.md")]

use game_solver::game::Game;
use petgraph::matrix_graph::MatrixGraph;

#[derive(Clone)]
pub struct Sprouts(MatrixGraph<(), ()>);

// impl Game for Sprouts {

// }
13 changes: 9 additions & 4 deletions crates/games/src/util/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use core::hash::Hash;
use game_solver::{
game::{Game, GameState},
par_move_scores,
player::TwoPlayer,
player::{ImpartialPlayer, TwoPlayer},
};
use std::fmt::{Debug, Display};
use std::{any::TypeId, fmt::{Debug, Display}};

pub fn play<
T: Game<Player = impl TwoPlayer + Debug> + Eq + Hash + Sync + Send + Display + 'static,
T: Game<Player = impl TwoPlayer + Debug + 'static> + Eq + Hash + Sync + Send + Display + 'static,
>(
game: T,
) where
Expand All @@ -20,7 +20,12 @@ pub fn play<

match game.state() {
GameState::Playable => {
println!("Player {:?} to move", game.player());
if TypeId::of::<T::Player>() != TypeId::of::<ImpartialPlayer>() {
println!("Player {:?} to move", game.player());
} else {
// TODO: can we assert that game.player() is the next player?
println!("Impartial game; Next player is moving.");
}

let move_scores = par_move_scores(&game);
let mut move_scores = move_scores
Expand Down

0 comments on commit aa58a33

Please sign in to comment.