mirror of
https://github.com/Noratrieb/minmax.git
synced 2026-01-16 16:25:07 +01:00
minmax
This commit is contained in:
parent
5c95aa7536
commit
7f3a0e5ad2
3 changed files with 127 additions and 3 deletions
|
|
@ -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};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue