Files
rttt/player/human.rs

165 lines
4.1 KiB
Rust
Raw Normal View History

2025-04-07 02:04:18 +03:00
use crate::game::{Board, Cell, Move};
2025-04-06 18:37:49 +03:00
use crate::player::Player;
use std::io;
2026-01-04 17:45:57 +03:00
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}")
}
}
2025-04-06 18:37:49 +03:00
pub struct PlayerConsole {
name: String,
piece: Cell,
}
impl PlayerConsole {
2025-04-07 02:04:18 +03:00
pub fn new(name: &str) -> impl Player {
2025-04-06 18:37:49 +03:00
PlayerConsole {
name: name.to_owned(),
2025-04-07 02:04:18 +03:00
piece: Cell::Empty,
2025-04-06 18:37:49 +03:00
}
}
2025-04-07 02:04:18 +03:00
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);
Ok(())
}
2026-01-04 17:45:57 +03:00
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,
};
2025-04-06 18:37:49 +03:00
loop {
2026-01-04 17:45:57 +03:00
cls();
home();
println!("{}", board);
2025-04-06 18:37:49 +03:00
2026-01-04 17:45:57 +03:00
let s: String = request_input("Your turn (or \"q\" to quit):").trim().into();
2025-04-07 02:04:18 +03:00
2026-01-04 17:45:57 +03:00
if s == "q" {
2025-04-07 02:04:18 +03:00
return Err("player has surrendered".into());
} else if s.len() != 2 {
2026-01-04 17:45:57 +03:00
_ = request_input("invalid input. type board coordinates as \"a1\", \"b2\" etc...");
2025-04-06 18:37:49 +03:00
continue;
2025-04-07 02:04:18 +03:00
}
for c in s.chars() {
match c {
2026-01-04 17:45:57 +03:00
'a' => m.y = 0,
'b' => m.y = 1,
'c' => m.y = 2,
'1' => m.x = 0,
'2' => m.x = 1,
'3' => m.x = 2,
2025-04-07 02:04:18 +03:00
_ => {
println!("invalid input: {}", c);
continue;
}
}
}
2025-04-06 18:37:49 +03:00
2026-01-04 17:45:57 +03:00
if !board.is_valid_move(&m) {
_ = request_input("Invalid move. Press Enter and try again...");
continue;
}
2025-04-06 18:37:49 +03:00
break;
}
2026-01-04 17:45:57 +03:00
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(())
2025-04-06 18:37:49 +03:00
}
}
2026-01-04 17:45:57 +03:00
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
}
2025-04-06 18:37:49 +03:00
impl Player for PlayerConsole {
2025-04-07 02:04:18 +03:00
fn start_new_game(&mut self, p: Cell) -> Result<(), Box<dyn std::error::Error>> {
2026-01-04 17:45:57 +03:00
self.start_new_game(p)
2025-04-07 02:04:18 +03:00
}
2025-04-06 18:37:49 +03:00
fn request_move(&self, b: &Board) -> Result<Move, Box<dyn std::error::Error>> {
self.request_move(b)
}
2026-01-04 17:45:57 +03:00
fn message(&self, msg: &str) -> Result<(), Box<dyn std::error::Error>> {
self.message(msg)
2025-04-06 18:37:49 +03:00
}
}