This commit is contained in:
nora 2022-11-21 15:46:49 +01:00
parent 5c95aa7536
commit 7f3a0e5ad2
No known key found for this signature in database
3 changed files with 127 additions and 3 deletions

View file

@ -1,6 +1,6 @@
use std::fmt::{Display, Write};
use crate::{Player, State};
use crate::{minmax::GameBoard, Player, State};
#[derive(Clone)]
pub struct Board(u32);
@ -113,6 +113,34 @@ impl Display for Board {
}
}
impl GameBoard for Board {
type Move = usize;
fn possible_moves(&self) -> impl Iterator<Item = Self::Move> {
debug_assert!(
!self.iter().all(|x| x.is_some()),
"the board is full but state is InProgress"
);
self.iter()
.enumerate()
.filter(|(_, position)| position.is_none())
.map(|(pos, _)| pos)
}
fn result(&self) -> State {
Board::result(self)
}
fn make_move(&mut self, position: Self::Move, player: Player) {
self.set(position, Some(player));
}
fn undo_move(&mut self, position: Self::Move) {
self.set(position, None);
}
}
#[cfg(test)]
mod tests {
use super::{Board, Player};