add check for draw

This commit is contained in:
Dmitry Fedotov
2025-04-07 03:25:32 +03:00
parent b42659a604
commit 5b0119b5b6
3 changed files with 53 additions and 43 deletions

View File

@@ -27,8 +27,8 @@ impl Engine {
pub fn run(&mut self) -> Result<(), Box<dyn std::error::Error>> {
loop {
// setup new game for players
self.players[self.x].start_new_game(Cell::CellX)?;
self.players[(self.x + 1) % 2].start_new_game(Cell::CellO)?;
self.players[self.x].start_new_game(Cell::X)?;
self.players[(self.x + 1) % 2].start_new_game(Cell::O)?;
// reset board
self.board.reset();
@@ -46,6 +46,7 @@ impl Engine {
loop {
println!("{}", self.board);
// request move from player, fail if there is error
let m = self.players[self.turn].request_move(&self.board)?;
if !self.board.is_valid_move(&m) {
@@ -53,14 +54,21 @@ impl Engine {
continue;
}
self.board.put(m)?;
// apply move
self.board.apply(m)?;
// check if there is a winner
if let Some(_winner) = self.board.has_winner() {
println!("{}", self.board);
println!("The winner is: {}", self.players[self.turn].name());
break;
}
if !self.board.valid_moves_available() {
println!("{}", self.board);
break;
}
self.turn = (self.turn + 1) % 2
}