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, } impl PlayerConsole { pub fn new(name: &str) -> impl Player { PlayerConsole { name: name.to_owned(), piece: Cell::Empty, } } pub fn start_new_game(&mut self, p: Cell) -> Result<(), Box> { self.piece = p; println!("{}, you are now playing {}", self.name, p); Ok(()) } pub fn request_move(&self, board: &Board) -> Result> { let mut m: Move = Move { x: 0, y: 0, piece: self.piece, }; loop { cls(); home(); println!("{}", board); let s: String = request_input("Your turn (or \"q\" to quit):").trim().into(); if s == "q" { return Err("player has surrendered".into()); } else if s.len() != 2 { _ = request_input("invalid input. type board coordinates as \"a1\", \"b2\" etc..."); continue; } for c in s.chars() { match c { '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; } } } if !board.is_valid_move(&m) { _ = request_input("Invalid move. Press Enter and try again..."); continue; } break; } return Ok(m); } pub fn message(&self, msg: &str) -> Result<(), Box> { 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> { self.start_new_game(p) } fn request_move(&self, b: &Board) -> Result> { self.request_move(b) } fn message(&self, msg: &str) -> Result<(), Box> { self.message(msg) } }