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

@@ -2,6 +2,59 @@ use crate::game::{Board, Cell, Move};
use crate::player::Player;
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 {
name: String,
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>> {
self.piece = p;
println!("{}, you are now playing {}", self.name, p);
@@ -26,33 +75,35 @@ impl PlayerConsole {
Ok(())
}
pub fn request_move(&self, _: &Board) -> Result<Move, Box<dyn std::error::Error>> {
let mut x: usize = 0;
let mut y: usize = 0;
pub fn request_move(&self, board: &Board) -> Result<Move, Box<dyn std::error::Error>> {
let mut m: Move = Move {
x: 0,
y: 0,
piece: self.piece,
};
loop {
println!("{}, it is your turn", self.name);
cls();
home();
println!("{}", board);
let mut s = String::new();
io::stdin().read_line(&mut s)?;
let s: String = request_input("Your turn (or \"q\" to quit):").trim().into();
s = s.trim().into();
if s == "" {
if s == "q" {
return Err("player has surrendered".into());
} 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;
}
for c in s.chars() {
match c {
'a' => y = 0,
'b' => y = 1,
'c' => y = 2,
'1' => x = 0,
'2' => x = 1,
'3' => x = 2,
'a' => m.y = 0,
'b' => m.y = 1,
'c' => m.y = 2,
'1' => m.x = 0,
'2' => m.x = 1,
'3' => m.x = 2,
_ => {
println!("invalid input: {}", c);
continue;
@@ -60,27 +111,54 @@ impl PlayerConsole {
}
}
if !board.is_valid_move(&m) {
_ = request_input("Invalid move. Press Enter and try again...");
continue;
}
break;
}
return Ok(Move {
x: x,
y: y,
piece: self.piece,
});
return Ok(m);
}
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 {
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>> {
self.request_move(b)
}
fn name(&self) -> &str {
self.name()
fn message(&self, msg: &str) -> Result<(), Box<dyn std::error::Error>> {
self.message(msg)
}
}