refactor, add lobby

This commit is contained in:
2026-01-04 17:45:57 +03:00
parent c3bcf44048
commit 5426da7d07
7 changed files with 153 additions and 137 deletions

View File

@@ -1,7 +1,5 @@
use std::io;
use crate::game::{Board, Cell}; use crate::game::{Board, Cell};
use crate::player::{Player, PlayerConsole}; use crate::player::Player;
pub struct Engine { pub struct Engine {
players: Vec<Box<dyn Player>>, players: Vec<Box<dyn Player>>,
@@ -11,15 +9,10 @@ pub struct Engine {
} }
impl Engine { impl Engine {
pub fn new() -> Engine { pub fn new(p1: Box<dyn Player>, p2: Box<dyn Player>) -> Engine {
// TODO: accept players as args
let p1 = PlayerConsole::new("Gopher");
let p2 = PlayerConsole::new("Rustacean");
let board = Board::new(); let board = Board::new();
Engine { Engine {
players: vec![Box::new(p1), Box::new(p2)], players: vec![p1, p2],
turn: 0, turn: 0,
board: board, board: board,
x: 0, x: 0,
@@ -41,32 +34,16 @@ impl Engine {
// switch sides // switch sides
self.x = (self.x + 1) % 2; self.x = (self.x + 1) % 2;
match request_input("Press Enter to continue or \"q\" to quit.")
.as_str()
.trim()
{
"q" => {
return Ok(());
}
_ => {
continue;
}
}
} }
} }
fn run_single_game(&mut self) -> Result<(), Box<dyn std::error::Error>> { fn run_single_game(&mut self) -> Result<(), Box<dyn std::error::Error>> {
loop { loop {
cls();
home();
println!("{}", self.board);
// request move from player, fail if there is error // 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)?;
let next = (self.turn + 1) % 2;
if !self.board.is_valid_move(&m) { if !self.board.is_valid_move(&m) {
println!("invalid move");
continue; continue;
} }
@@ -75,46 +52,20 @@ impl Engine {
// check if there is a winner // check if there is a winner
if let Some(_winner) = self.board.has_winner() { if let Some(_winner) = self.board.has_winner() {
cls(); self.players[self.turn].message("You win!")?;
home(); self.players[next].message("You loose!")?;
println!("{} wins!", self.players[self.turn].name());
println!("{}", self.board);
request_input("Press Enter to continue...");
break; break;
} }
if !self.board.valid_moves_available() { if !self.board.valid_moves_available() {
cls(); self.players[self.turn].message("It's a draw!")?;
home(); self.players[next].message("It's a draw!")?;
println!("It's a draw!");
println!("{}", self.board);
request_input("press Enter to continue");
break; break;
} }
self.turn = (self.turn + 1) % 2 self.turn = next
} }
Ok(()) Ok(())
} }
} }
fn cls() {
print!("{}[2J", 27 as char);
}
fn home() {
print!("{}[H", 27 as char)
}
fn request_input(prompt: &str) -> String {
println!("{}", prompt);
let mut s = String::new();
match io::stdin().read_line(&mut s) {
Ok(_) => {}
Err(_) => {}
}
s
}

View File

@@ -5,19 +5,6 @@ pub enum Cell {
O, O,
} }
impl std::fmt::Display for Cell {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let s: &str;
match *self {
Cell::X => s = "X",
Cell::O => s = "O",
Cell::Empty => s = " ",
}
write!(f, "{}", s)
}
}
pub struct Move { pub struct Move {
pub x: usize, pub x: usize,
pub y: usize, pub y: usize,
@@ -60,6 +47,10 @@ impl Board {
} }
pub fn is_valid_move(&self, m: &Move) -> bool { pub fn is_valid_move(&self, m: &Move) -> bool {
if m.x > 2 || m.y > 2 {
return false;
}
let i = m.x * 1 + m.y * 3; let i = m.x * 1 + m.y * 3;
m.piece == self.next && self.cells[i] == Cell::Empty m.piece == self.next && self.cells[i] == Cell::Empty
@@ -107,42 +98,12 @@ impl Board {
// no winner // no winner
return None; return None;
} }
pub fn cells(&self) -> Vec<Cell> {
self.cells.clone()
} }
impl std::fmt::Display for Board { pub fn next_move(&self) -> Cell {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { self.next
let mut s = String::new();
s.push_str(" 1 2 3 \n");
s.push_str(" ┌───┬───┬───┐\n");
for i in 0..3 {
match i {
0 => s.push_str("a "),
1 => s.push_str("b "),
2 => s.push_str("c "),
_ => {}
}
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");
match self.has_winner() {
Some(w) => s.push_str(&format!("The winner is {w}")),
None => match self.valid_moves_available() {
true => s.push_str(&format!("Next: {}", self.next)),
false => s.push_str("DRAW!"),
},
}
write!(f, "{s}")
} }
} }

23
lobby/lobby.rs Normal file
View File

@@ -0,0 +1,23 @@
use crate::engine::Engine;
use crate::player::PlayerConsole;
pub struct Lobby {}
impl Lobby {
pub fn new() -> Lobby {
Lobby {}
}
pub fn run(self) -> Result<(), Box<dyn std::error::Error>> {
loop {
let p1 = Box::new(PlayerConsole::new("Gopher"));
let p2 = Box::new(PlayerConsole::new("Rustacean"));
let mut engine = Engine::new(p1, p2);
match engine.run() {
Ok(_) => continue,
Err(e) => return Err(e),
};
}
}
}

2
lobby/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
mod lobby;
pub use lobby::Lobby;

View File

@@ -1,11 +1,12 @@
mod engine; mod engine;
mod game; mod game;
mod lobby;
mod player; mod player;
fn main() { fn main() {
let mut engine = engine::Engine::new(); let lobby = lobby::Lobby::new();
match engine.run() { match lobby.run() {
Ok(_) => println!("game over"), Ok(_) => println!("game over"),
Err(e) => println!("{}", e), Err(e) => println!("{}", e),
}; };

View File

@@ -2,6 +2,59 @@ use crate::game::{Board, Cell, Move};
use crate::player::Player; use crate::player::Player;
use std::io; use std::io;
impl std::fmt::Display for Cell {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let s: &str;
match *self {
Cell::X => s = "X",
Cell::O => s = "O",
Cell::Empty => s = " ",
}
write!(f, "{}", s)
}
}
impl std::fmt::Display for Board {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut s = String::new();
let cells = self.cells();
s.push_str(" 1 2 3 \n");
s.push_str(" ┌───┬───┬───┐\n");
for i in 0..3 {
match i {
0 => s.push_str("a "),
1 => s.push_str("b "),
2 => s.push_str("c "),
_ => {}
}
s.push_str(&format!(
"{}{}{}\n",
cells[i * 3],
cells[i * 3 + 1],
cells[i * 3 + 2]
));
if i < 2 {
s.push_str(" ├───┼───┼───┤\n");
}
}
s.push_str(" └───┴───┴───┘\n");
match self.has_winner() {
Some(w) => s.push_str(&format!("The winner is {w}")),
None => match self.valid_moves_available() {
true => s.push_str(&format!("Next: {}", self.next_move())),
false => s.push_str("DRAW!"),
},
}
write!(f, "{s}")
}
}
pub struct PlayerConsole { pub struct PlayerConsole {
name: String, name: String,
piece: Cell, piece: Cell,
@@ -15,10 +68,6 @@ impl PlayerConsole {
} }
} }
pub fn name(&self) -> &str {
return self.name.as_str();
}
pub fn start_new_game(&mut self, p: Cell) -> Result<(), Box<dyn std::error::Error>> { pub fn start_new_game(&mut self, p: Cell) -> Result<(), Box<dyn std::error::Error>> {
self.piece = p; self.piece = p;
println!("{}, you are now playing {}", self.name, p); println!("{}, you are now playing {}", self.name, p);
@@ -26,33 +75,35 @@ impl PlayerConsole {
Ok(()) Ok(())
} }
pub fn request_move(&self, _: &Board) -> Result<Move, Box<dyn std::error::Error>> { pub fn request_move(&self, board: &Board) -> Result<Move, Box<dyn std::error::Error>> {
let mut x: usize = 0; let mut m: Move = Move {
let mut y: usize = 0; x: 0,
y: 0,
piece: self.piece,
};
loop { loop {
println!("{}, it is your turn", self.name); cls();
home();
println!("{}", board);
let mut s = String::new(); let s: String = request_input("Your turn (or \"q\" to quit):").trim().into();
io::stdin().read_line(&mut s)?;
s = s.trim().into(); if s == "q" {
if s == "" {
return Err("player has surrendered".into()); return Err("player has surrendered".into());
} else if s.len() != 2 { } else if s.len() != 2 {
println!("invalid input. type board coordinates as a1, b2 etc..."); _ = request_input("invalid input. type board coordinates as \"a1\", \"b2\" etc...");
continue; continue;
} }
for c in s.chars() { for c in s.chars() {
match c { match c {
'a' => y = 0, 'a' => m.y = 0,
'b' => y = 1, 'b' => m.y = 1,
'c' => y = 2, 'c' => m.y = 2,
'1' => x = 0, '1' => m.x = 0,
'2' => x = 1, '2' => m.x = 1,
'3' => x = 2, '3' => m.x = 2,
_ => { _ => {
println!("invalid input: {}", c); println!("invalid input: {}", c);
continue; continue;
@@ -60,27 +111,54 @@ impl PlayerConsole {
} }
} }
if !board.is_valid_move(&m) {
_ = request_input("Invalid move. Press Enter and try again...");
continue;
}
break; break;
} }
return Ok(Move { return Ok(m);
x: x,
y: y,
piece: self.piece,
});
} }
pub fn message(&self, msg: &str) -> Result<(), Box<dyn std::error::Error>> {
let s = &format!("Message from game master: \"{}\"", msg);
_ = request_input(s);
Ok(())
}
}
fn cls() {
print!("{}[2J", 27 as char);
}
fn home() {
print!("{}[H", 27 as char)
}
fn request_input(prompt: &str) -> String {
println!("{}", prompt);
let mut s = String::new();
match io::stdin().read_line(&mut s) {
Ok(_) => {}
Err(_) => {}
}
s
} }
impl Player for PlayerConsole { impl Player for PlayerConsole {
fn start_new_game(&mut self, p: Cell) -> Result<(), Box<dyn std::error::Error>> { fn start_new_game(&mut self, p: Cell) -> Result<(), Box<dyn std::error::Error>> {
return self.start_new_game(p); self.start_new_game(p)
} }
fn request_move(&self, b: &Board) -> Result<Move, Box<dyn std::error::Error>> { fn request_move(&self, b: &Board) -> Result<Move, Box<dyn std::error::Error>> {
self.request_move(b) self.request_move(b)
} }
fn name(&self) -> &str { fn message(&self, msg: &str) -> Result<(), Box<dyn std::error::Error>> {
self.name() self.message(msg)
} }
} }

View File

@@ -6,5 +6,5 @@ pub use human::PlayerConsole;
pub trait Player { pub trait Player {
fn start_new_game(&mut self, p: Cell) -> Result<(), Box<dyn std::error::Error>>; fn start_new_game(&mut self, p: Cell) -> Result<(), Box<dyn std::error::Error>>;
fn request_move(&self, b: &Board) -> Result<Move, Box<dyn std::error::Error>>; fn request_move(&self, b: &Board) -> Result<Move, Box<dyn std::error::Error>>;
fn name(&self) -> &str; fn message(&self, msg: &str) -> Result<(), Box<dyn std::error::Error>>;
} }