Files
rttt/game/entity.rs

149 lines
4.0 KiB
Rust
Raw Normal View History

2025-04-06 18:37:49 +03:00
#[derive(Clone, Copy, PartialEq)]
2025-04-07 02:04:18 +03:00
//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 {
CellEmpty,
CellX,
CellO,
}
2025-04-06 18:37:49 +03:00
impl std::fmt::Display for Cell {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
2025-04-07 02:11:09 +03:00
let s: &str;
2025-04-07 02:04:18 +03:00
match *self {
Cell::CellX => s = "X",
Cell::CellO => s = "O",
Cell::CellEmpty => s = " ",
2025-04-06 18:37:49 +03:00
}
write!(f, "{}", s)
}
}
pub struct Move {
pub x: usize,
pub y: usize,
2025-04-07 02:04:18 +03:00
pub piece: Cell,
2025-04-06 18:37:49 +03:00
}
pub struct Board {
cells: Vec<Cell>,
next: Cell,
}
impl Board {
pub fn new() -> Board {
return Board {
2025-04-07 02:04:18 +03:00
cells: vec![Cell::CellEmpty; 9],
next: Cell::CellX,
2025-04-06 18:37:49 +03:00
};
}
pub fn reset(&mut self) {
2025-04-07 02:04:18 +03:00
self.cells = vec![Cell::CellEmpty; 9];
self.next = Cell::CellX;
2025-04-06 18:37:49 +03:00
}
2025-04-07 02:04:18 +03:00
pub fn put(&mut self, m: Move) -> Result<(), &str> {
2025-04-06 18:37:49 +03:00
let i = m.x * 1 + m.y * 3;
2025-04-07 02:04:18 +03:00
if !self.is_valid_move(&m) {
return Err("invalid move");
}
self.cells[i] = m.piece;
match self.next {
Cell::CellX => self.next = Cell::CellO,
Cell::CellO => self.next = Cell::CellX,
_ => {}
2025-04-06 18:37:49 +03:00
}
2025-04-07 02:04:18 +03:00
Ok(())
}
pub fn is_valid_move(&self, m: &Move) -> bool {
let i = m.x * 1 + m.y * 3;
2025-04-06 18:37:49 +03:00
2025-04-07 02:04:18 +03:00
m.piece == self.next && self.cells[i] == Cell::CellEmpty
2025-04-06 18:37:49 +03:00
}
2025-04-07 02:04:18 +03:00
pub fn has_winner(&self) -> Option<Cell> {
2025-04-06 18:37:49 +03:00
// rows and cols
for i in 0..3 {
2025-04-07 02:04:18 +03:00
if (self.cells[i] == self.cells[i + 3] && self.cells[i] == self.cells[i + 6])
&& (self.cells[i] != Cell::CellEmpty
&& self.cells[i + 3] != Cell::CellEmpty
&& self.cells[i + 6] != Cell::CellEmpty)
{
return Some(self.cells[i]);
} else if (self.cells[i * 3] == self.cells[i * 3 + 1]
&& 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)
2025-04-06 18:37:49 +03:00
{
2025-04-07 02:04:18 +03:00
return Some(self.cells[i * 3]);
2025-04-06 18:37:49 +03:00
}
}
// diagonals
2025-04-07 02:04:18 +03:00
if (self.cells[0] == self.cells[4] && self.cells[0] == self.cells[8])
&& (self.cells[0] != Cell::CellEmpty
&& self.cells[4] != Cell::CellEmpty
&& self.cells[8] != Cell::CellEmpty)
{
return Some(self.cells[0]);
2025-04-06 18:37:49 +03:00
}
2025-04-07 02:04:18 +03:00
if (self.cells[2] == self.cells[4] && self.cells[2] == self.cells[6])
&& (self.cells[2] != Cell::CellEmpty
&& self.cells[4] != Cell::CellEmpty
&& self.cells[6] != Cell::CellEmpty)
{
return Some(self.cells[2]);
2025-04-06 18:37:49 +03:00
}
// no winner
2025-04-07 02:04:18 +03:00
return None;
2025-04-06 18:37:49 +03:00
}
}
impl std::fmt::Display for Board {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut s = String::new();
2025-04-07 02:11:09 +03:00
s.push_str(" 1 2 3 \n");
2025-04-07 02:04:18 +03:00
2025-04-07 02:11:09 +03:00
s.push_str(" ┌───┬───┬───┐\n");
2025-04-06 18:37:49 +03:00
for i in 0..3 {
2025-04-07 02:04:18 +03:00
match i {
0 => s.push_str("a "),
1 => s.push_str("b "),
2 => s.push_str("c "),
_ => {}
}
2025-04-06 18:37:49 +03:00
s.push_str(&format!(
2025-04-07 02:11:09 +03:00
"│ {} │ {} │ {} │\n",
2025-04-06 18:37:49 +03:00
self.cells[i * 3],
self.cells[i * 3 + 1],
self.cells[i * 3 + 2]
));
if i < 2 {
2025-04-07 02:11:09 +03:00
s.push_str(" ├───┼───┼───┤\n");
2025-04-06 18:37:49 +03:00
}
}
2025-04-07 02:11:09 +03:00
s.push_str(" └───┴───┴───┘\n");
2025-04-06 18:37:49 +03:00
2025-04-07 02:04:18 +03:00
match self.has_winner() {
Some(w) => s.push_str(&format!("The winner is {w}")),
None => s.push_str(&format!("Next: {}", self.next)),
2025-04-06 18:37:49 +03:00
}
2025-04-07 02:04:18 +03:00
2025-04-06 18:37:49 +03:00
write!(f, "{s}")
}
}