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

View File

@@ -1,22 +1,17 @@
#[derive(Clone, Copy, PartialEq)] #[derive(Clone, Copy, PartialEq)]
//pub struct Cell(u8);
//pub const Cell::CellEmpty: Cell = Cell(0);
//pub const Cell::CellX: Cell = Cell(1);
//pub const Cell::CellO: Cell = Cell(2);
pub enum Cell { pub enum Cell {
CellEmpty, Empty,
CellX, X,
CellO, O,
} }
impl std::fmt::Display for Cell { impl std::fmt::Display for Cell {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let s: &str; let s: &str;
match *self { match *self {
Cell::CellX => s = "X", Cell::X => s = "X",
Cell::CellO => s = "O", Cell::O => s = "O",
Cell::CellEmpty => s = " ", Cell::Empty => s = " ",
} }
write!(f, "{}", s) write!(f, "{}", s)
@@ -36,18 +31,18 @@ pub struct Board {
impl Board { impl Board {
pub fn new() -> Board { pub fn new() -> Board {
return Board { Board {
cells: vec![Cell::CellEmpty; 9], cells: vec![Cell::Empty; 9],
next: Cell::CellX, next: Cell::X,
}; }
} }
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.cells = vec![Cell::CellEmpty; 9]; self.cells = vec![Cell::Empty; 9];
self.next = Cell::CellX; self.next = Cell::X;
} }
pub fn put(&mut self, m: Move) -> Result<(), &str> { pub fn apply(&mut self, m: Move) -> Result<(), &str> {
let i = m.x * 1 + m.y * 3; let i = m.x * 1 + m.y * 3;
if !self.is_valid_move(&m) { if !self.is_valid_move(&m) {
@@ -57,8 +52,8 @@ impl Board {
self.cells[i] = m.piece; self.cells[i] = m.piece;
match self.next { match self.next {
Cell::CellX => self.next = Cell::CellO, Cell::X => self.next = Cell::O,
Cell::CellO => self.next = Cell::CellX, Cell::O => self.next = Cell::X,
_ => {} _ => {}
} }
@@ -68,41 +63,45 @@ impl Board {
pub fn is_valid_move(&self, m: &Move) -> bool { pub fn is_valid_move(&self, m: &Move) -> bool {
let i = m.x * 1 + m.y * 3; let i = m.x * 1 + m.y * 3;
m.piece == self.next && self.cells[i] == Cell::CellEmpty m.piece == self.next && self.cells[i] == Cell::Empty
}
pub fn valid_moves_available(&self) -> bool {
// check for draw condition
let mut empty: usize = 0;
for i in 0..self.cells.len() {
if self.cells[i] == Cell::Empty {
empty += 1;
}
}
empty > 0
} }
pub fn has_winner(&self) -> Option<Cell> { pub fn has_winner(&self) -> Option<Cell> {
// rows and cols // rows and cols
for i in 0..3 { for i in 0..3 {
if (self.cells[i] == self.cells[i + 3] && self.cells[i] == self.cells[i + 6]) if (self.cells[i] != Cell::Empty)
&& (self.cells[i] != Cell::CellEmpty && (self.cells[i] == self.cells[i + 3] && self.cells[i] == self.cells[i + 6])
&& self.cells[i + 3] != Cell::CellEmpty
&& self.cells[i + 6] != Cell::CellEmpty)
{ {
return Some(self.cells[i]); return Some(self.cells[i]);
} else if (self.cells[i * 3] == self.cells[i * 3 + 1] } else if (self.cells[i * 3] != Cell::Empty)
&& (self.cells[i * 3] == self.cells[i * 3 + 1]
&& self.cells[i * 3] == self.cells[i * 3 + 2]) && self.cells[i * 3] == self.cells[i * 3 + 2])
&& (self.cells[i * 3] != Cell::CellEmpty
&& self.cells[i * 3 + 1] != Cell::CellEmpty
&& self.cells[i * 3 + 2] != Cell::CellEmpty)
{ {
return Some(self.cells[i * 3]); return Some(self.cells[i * 3]);
} }
} }
// diagonals // diagonals
if (self.cells[0] == self.cells[4] && self.cells[0] == self.cells[8]) if (self.cells[0] != Cell::Empty)
&& (self.cells[0] != Cell::CellEmpty && (self.cells[0] == self.cells[4] && self.cells[0] == self.cells[8])
&& self.cells[4] != Cell::CellEmpty
&& self.cells[8] != Cell::CellEmpty)
{ {
return Some(self.cells[0]); return Some(self.cells[0]);
} }
if (self.cells[2] == self.cells[4] && self.cells[2] == self.cells[6]) if (self.cells[2] != Cell::Empty)
&& (self.cells[2] != Cell::CellEmpty && (self.cells[2] == self.cells[4] && self.cells[2] == self.cells[6])
&& self.cells[4] != Cell::CellEmpty
&& self.cells[6] != Cell::CellEmpty)
{ {
return Some(self.cells[2]); return Some(self.cells[2]);
} }
@@ -140,7 +139,10 @@ impl std::fmt::Display for Board {
match self.has_winner() { match self.has_winner() {
Some(w) => s.push_str(&format!("The winner is {w}")), Some(w) => s.push_str(&format!("The winner is {w}")),
None => s.push_str(&format!("Next: {}", self.next)), None => match self.valid_moves_available() {
true => s.push_str(&format!("Next: {}", self.next)),
false => s.push_str("DRAW!"),
},
} }
write!(f, "{s}") write!(f, "{s}")

View File

@@ -11,7 +11,7 @@ impl PlayerConsole {
pub fn new(name: &str) -> impl Player { pub fn new(name: &str) -> impl Player {
PlayerConsole { PlayerConsole {
name: name.to_owned(), name: name.to_owned(),
piece: Cell::CellEmpty, piece: Cell::Empty,
} }
} }