use crate::game::BoardDimensions;
use crate::game::GameError;
use crate::game::GameState;
use crate::game::Placement;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Game {
state: GameState,
move_history: Vec<usize>,
}
impl Game {
pub fn new() -> Self {
Self::with_dimensions(BoardDimensions::STANDARD)
}
pub fn with_dimensions(dimensions: BoardDimensions) -> Self {
Self {
state: GameState::with_dimensions(dimensions),
move_history: Vec::new(),
}
}
pub fn from_moves<I>(moves: I) -> Result<Self, GameError>
where
I: IntoIterator<Item = usize>,
{
let mut game = Self::new();
game.play_all(moves)?;
Ok(game)
}
pub const fn state(&self) -> &GameState {
&self.state
}
pub fn move_history(&self) -> &[usize] {
&self.move_history
}
pub fn play(&mut self, column: usize) -> Result<Placement, GameError> {
let placement = self.state.apply_move(column)?;
self.move_history.push(column);
Ok(placement)
}
pub fn play_all<I>(&mut self, moves: I) -> Result<(), GameError>
where
I: IntoIterator<Item = usize>,
{
for column in moves {
self.play(column)?;
}
Ok(())
}
pub fn restart(&mut self) {
*self = Self::with_dimensions(self.state.dimensions());
}
}
impl Default for Game {
fn default() -> Self {
Self::new()
}
}