Files
rttt/game/entity.rs

110 lines
2.4 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 enum Cell {
Empty,
X,
O,
}
2025-04-06 18:37:49 +03:00
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 {
2025-04-07 02:04:18 +03:00
Board {
cells: vec![Cell::Empty; 9],
next: Cell::X,
}
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::Empty; 9];
self.next = Cell::X;
2025-04-06 18:37:49 +03:00
}
2025-04-07 02:04:18 +03:00
pub fn apply(&mut self, m: Move) -> Result<(), &str> {
if !self.is_valid_move(&m) {
return Err("invalid move");
}
2025-05-03 13:24:44 +03:00
let i = m.x * 1 + m.y * 3;
2025-04-07 02:04:18 +03:00
self.cells[i] = m.piece;
match self.next {
Cell::X => self.next = Cell::O,
Cell::O => self.next = Cell::X,
_ => {}
2025-04-06 18:37:49 +03:00
}
2025-04-07 02:04:18 +03:00
Ok(())
}
2025-04-06 18:37:49 +03:00
2025-04-07 02:04:18 +03:00
pub fn is_valid_move(&self, m: &Move) -> bool {
2026-01-04 17:45:57 +03:00
if m.x > 2 || m.y > 2 {
return false;
}
2025-04-07 02:04:18 +03:00
let i = m.x * 1 + m.y * 3;
m.piece == self.next && self.cells[i] == Cell::Empty
2025-04-06 18:37:49 +03:00
}
2025-04-07 02:04:18 +03:00
pub fn valid_moves_available(&self) -> bool {
// check for draw condition
for i in 0..self.cells.len() {
if self.cells[i] == Cell::Empty {
2025-05-03 13:24:44 +03:00
return true;
2025-04-07 02:04:18 +03:00
}
}
2025-05-03 13:24:44 +03:00
return false;
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] != Cell::Empty)
&& (self.cells[i] == self.cells[i + 3] && self.cells[i] == self.cells[i + 6])
{
return Some(self.cells[i]);
} 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])
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] != Cell::Empty)
&& (self.cells[0] == self.cells[4] && self.cells[0] == self.cells[8])
{
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] != Cell::Empty)
&& (self.cells[2] == self.cells[4] && self.cells[2] == self.cells[6])
{
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
}
2026-01-04 17:45:57 +03:00
pub fn cells(&self) -> Vec<Cell> {
self.cells.clone()
}
2025-04-07 02:04:18 +03:00
2026-01-04 17:45:57 +03:00
pub fn next_move(&self) -> Cell {
self.next
2025-04-06 18:37:49 +03:00
}
}