add engine

This commit is contained in:
Dmitry Fedotov
2025-04-06 18:37:49 +03:00
parent aae44cd587
commit e84d6c6054
8 changed files with 246 additions and 33 deletions

44
engine/engine.rs Normal file
View File

@@ -0,0 +1,44 @@
use crate::game;
use crate::player;
pub struct Engine {
players: Vec<player::PlayerConsole>,
turn: usize,
board: game::Board,
}
impl Engine {
pub fn new() -> Engine {
let p1 = player::PlayerConsole::new("P1", game::CELL_X);
let p2 = player::PlayerConsole::new("P2", game::CELL_O);
let board = game::Board::new();
Engine {
players: vec![p1, p2],
turn: 0,
board: board,
}
}
pub fn run(&mut self) -> Result<(), Box<dyn std::error::Error>> {
loop {
let m = self.players[self.turn].request_move(&self.board)?;
let ok = self.board.put(m);
if !ok {
continue;
}
let winner = self.board.has_winner();
if winner != game::CELL_EMPTY {
println!("the winner is {}", self.players[self.turn].name());
break;
}
self.turn = (self.turn + 1) % 2
}
Ok(())
}
}

3
engine/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
mod engine;
pub use engine::Engine;

107
game/entity.rs Normal file
View File

@@ -0,0 +1,107 @@
#[derive(Clone, Copy, PartialEq)]
pub struct Cell(u8);
pub const CELL_EMPTY: Cell = Cell(0);
pub const CELL_X: Cell = Cell(1);
pub const CELL_O: Cell = Cell(2);
impl std::fmt::Display for Cell {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut s: &str = " ";
if *self == CELL_X {
s = "X";
} else if *self == CELL_O {
s = "O";
}
write!(f, "{}", s)
}
}
pub struct Move {
pub x: usize,
pub y: usize,
pub c: Cell,
}
pub struct Board {
cells: Vec<Cell>,
next: Cell,
}
impl Board {
pub fn new() -> Board {
return Board {
cells: vec![CELL_EMPTY; 9],
next: CELL_X,
};
}
pub fn reset(&mut self) {
self.cells = vec![CELL_EMPTY; 9];
self.next = CELL_X;
}
pub fn put(&mut self, m: Move) -> bool {
let i = m.x * 1 + m.y * 3;
if self.cells[i] != CELL_EMPTY {
return false;
}
self.cells[i] = m.c;
return true;
}
pub fn has_winner(&self) -> Cell {
// rows and cols
for i in 0..3 {
if self.cells[i] == self.cells[i + 3] && self.cells[i] == self.cells[i + 6] {
return self.cells[i];
} else if self.cells[i * 3] == self.cells[i * 3 + 1]
&& self.cells[i * 3] == self.cells[i * 3 + 2]
{
return self.cells[i * 3];
}
}
// diagonals
if self.cells[0] == self.cells[4] && self.cells[0] == self.cells[8] {
return self.cells[0];
}
if self.cells[2] == self.cells[4] && self.cells[2] == self.cells[6] {
return self.cells[2];
}
// no winner
return CELL_EMPTY;
}
}
impl std::fmt::Display for Board {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut s = String::new();
s.push_str("┌─┬─┬─┐\n");
for i in 0..3 {
s.push_str(&format!(
"{}{}{}\n",
self.cells[i * 3],
self.cells[i * 3 + 1],
self.cells[i * 3 + 2]
));
if i < 2 {
s.push_str("├─┼─┼─┤\n");
}
}
s.push_str("└─┴─┴─┘\n");
let w = self.has_winner();
if w != CELL_EMPTY {
s.push_str(&format!("The winner is {w}"));
} else {
s.push_str(&format!("Next: {}", self.next));
}
write!(f, "{s}")
}
}

View File

@@ -1,2 +1,2 @@
mod ttt;
pub use ttt::{Board, Cell};
mod entity;
pub use entity::{Board, Cell, Move, CELL_EMPTY, CELL_O, CELL_X};

View File

@@ -1,29 +0,0 @@
use std::io::Empty;
pub enum Cell {
Empty,
X,
O,
}
pub struct Board {
cells: Vec<Cell>,
next: Cell,
}
impl Board {
pub fn new() -> Board {
return Board {
cells: Vec::new(),
next: Cell::X,
};
}
pub fn put(&self, x: usize, y: usize, _: Cell) -> bool {
return false;
}
pub fn winner(self) -> Cell {
return Cell::Empty;
}
}

10
main.rs
View File

@@ -1,6 +1,12 @@
mod engine;
mod game;
mod player;
fn main() {
let b = game::Board::new();
let c = game::Cell::X;
let mut engine = engine::Engine::new();
match engine.run() {
Ok(_) => println!("game over"),
Err(e) => println!("{}", e),
};
}

73
player/human.rs Normal file
View File

@@ -0,0 +1,73 @@
use crate::game::{Board, Cell, Move, CELL_EMPTY, CELL_O, CELL_X};
use crate::player::Player;
use std::io;
pub struct PlayerConsole {
name: String,
piece: Cell,
}
impl PlayerConsole {
pub fn new(name: &str, p: Cell) -> PlayerConsole {
PlayerConsole {
name: name.to_owned(),
piece: p,
}
}
pub fn name(&self) -> &str {
return self.name.as_str();
}
pub fn request_move(&self, b: &Board) -> Result<Move, Box<dyn std::error::Error>> {
let mut x: usize = 0;
let mut y: usize = 0;
loop {
println!("{}", b);
println!("player {}, it is your turn", self.name);
print!("x: ");
let mut s = String::new();
io::stdin().read_line(&mut s).expect("could not read input");
if let Ok(x_) = s.parse::<usize>() {
x = x_;
} else {
continue;
};
print!("y: ");
let mut s = String::new();
io::stdin().read_line(&mut s).expect("could not read input");
if let Ok(y_) = s.parse::<usize>() {
y = y_;
} else {
continue;
};
break;
}
return Ok(Move {
x: x,
y: x,
c: self.piece,
});
}
}
impl Player for PlayerConsole {
fn request_move(&self, b: &Board) -> Result<Move, Box<dyn std::error::Error>> {
self.request_move(b)
}
fn name(&self) -> &str {
self.name()
}
}
impl std::fmt::Display for PlayerConsole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Player: {}, piece: {}", self.name, self.piece)
}
}

9
player/mod.rs Normal file
View File

@@ -0,0 +1,9 @@
mod human;
use crate::game::{Board, Move};
pub use human::PlayerConsole;
pub trait Player {
fn request_move(&self, b: &Board) -> Result<Move, Box<dyn std::error::Error>>;
fn name(&self) -> &str;
}