move around

This commit is contained in:
nora 2022-11-21 15:20:46 +01:00
parent 6c3bf8dddb
commit 5c95aa7536
No known key found for this signature in database
7 changed files with 144 additions and 110 deletions

33
src/tic_tac_toe/game.rs Normal file
View file

@ -0,0 +1,33 @@
use crate::{GamePlayer, Player, State};
use super::{board::Board, TicTacToe};
impl Board {
pub fn default_play<X: GamePlayer<TicTacToe>, O: GamePlayer<TicTacToe>>() -> Option<Player> {
Self::empty().play(&mut X::default(), &mut O::default())
}
pub fn play<A: GamePlayer<TicTacToe>, B: GamePlayer<TicTacToe>>(&mut self, x: &mut A, o: &mut B) -> Option<Player> {
let mut current_player = Player::X;
for _ in 0..9 {
if current_player == Player::X {
x.next_move(self, current_player);
} else {
o.next_move(self, current_player);
}
match self.result() {
State::Winner(player) => return Some(player),
State::Draw => {
return None;
}
State::InProgress => {}
}
current_player = current_player.opponent();
}
None
}
}