Skip to content

Commit

Permalink
feat: full order and chaos game!
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoDog896 committed Sep 26, 2024
1 parent f6e9cf8 commit d8d9080
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
6 changes: 4 additions & 2 deletions crates/game-solver/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ pub fn upper_bound<T: Game>(game: &T) -> isize {
/// Represents an outcome of a game derived by a score and a valid instance of a game.
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum GameScoreOutcome {
/// The inner field represents the amount of moves till a win.
Win(usize),
/// The inner field represents the amount of moves till a loss.
Loss(usize),
Tie
}
Expand All @@ -195,8 +197,8 @@ pub enum GameScoreOutcome {
/// amount of moves to a win or loss, or a tie.
pub fn score_to_outcome<T: Game>(game: &T, score: isize) -> GameScoreOutcome {
match score.cmp(&0) {
Ordering::Greater => GameScoreOutcome::Win((-score + upper_bound(game)) as usize),
Ordering::Greater => GameScoreOutcome::Win((-score + upper_bound(game) - game.move_count() as isize) as usize),
Ordering::Equal => GameScoreOutcome::Tie,
Ordering::Less => GameScoreOutcome::Loss((score + upper_bound(game)) as usize)
Ordering::Less => GameScoreOutcome::Loss((score + upper_bound(game) - game.move_count() as isize) as usize)
}
}
2 changes: 1 addition & 1 deletion crates/games-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() -> Result<()> {
match cli.command {
Games::Reversi(args) => play::<Reversi>(args.try_into().unwrap()),
Games::TicTacToe(args) => play::<TicTacToe>(args.try_into().unwrap()),
Games::OrderAndChaos(args) => play::<OrderAndChaos>(args.try_into().unwrap()),
Games::OrderAndChaos(args) => play::<OrderAndChaos<6, 6, 5, 6>>(args.try_into().unwrap()),
Games::Nim(args) => play::<Nim>(args.try_into().unwrap()),
Games::Domineering(args) => play::<Domineering<5, 5>>(args.try_into().unwrap()),
Games::Chomp(args) => play::<Chomp>(args.try_into().unwrap()),
Expand Down
12 changes: 6 additions & 6 deletions crates/games/src/order_and_chaos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ impl<const WIDTH: usize, const HEIGHT: usize, const MIN_WIN_LENGTH: usize, const
break 'column_check;
}
}
}

return GameState::Win(PartizanPlayer::Left);
return GameState::Win(PartizanPlayer::Left);
}
}
}

Expand All @@ -213,14 +213,14 @@ impl<const WIDTH: usize, const HEIGHT: usize, const MIN_WIN_LENGTH: usize, const
break 'diag_check;
}
}
}

return GameState::Win(PartizanPlayer::Left);
return GameState::Win(PartizanPlayer::Left);
}
}
}

if self.move_count == WIDTH * HEIGHT {
return GameState::Tie;
return GameState::Win(PartizanPlayer::Right);
}

GameState::Playable
Expand Down Expand Up @@ -274,7 +274,7 @@ impl<const WIDTH: usize, const HEIGHT: usize, const MIN_WIN_LENGTH: usize, const
.map(|num| num.parse::<usize>().expect("Not a number!"))
.collect::<Vec<_>>();

let player = match args[2] {
let player = match args[2].to_ascii_lowercase().as_str() {
"x" => Ok(CellType::X),
"o" => Ok(CellType::O),
_ => Err(anyhow!("Invalid player!")),
Expand Down
14 changes: 7 additions & 7 deletions crates/games/src/util/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ where
T::MoveError: Display,
T::Player: Debug,
{
game.make_move(m)
.map_err(|err| anyhow!("Failed to move: {}", err))?;

match game.state() {
GameState::Playable => Ok(()),
GameState::Tie => Err(anyhow!("Can't continue - game is tied.")),
GameState::Win(player) => Err(anyhow!(
GameState::Playable => (),
GameState::Tie => return Err(anyhow!("Can't continue - game is tied.")),
GameState::Win(player) => return Err(anyhow!(
"Can't continue game if player {player:?} already won."
)),
}
};

game.make_move(m)
.map_err(|err| anyhow!("Failed to move: {}", err))
}

0 comments on commit d8d9080

Please sign in to comment.