basically working version
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::game::{Board, Cell, Move, CELL_EMPTY, CELL_O, CELL_X};
|
||||
use crate::game::{Board, Cell, Move};
|
||||
use crate::player::Player;
|
||||
use std::io;
|
||||
|
||||
@@ -8,10 +8,10 @@ pub struct PlayerConsole {
|
||||
}
|
||||
|
||||
impl PlayerConsole {
|
||||
pub fn new(name: &str, p: Cell) -> PlayerConsole {
|
||||
pub fn new(name: &str) -> impl Player {
|
||||
PlayerConsole {
|
||||
name: name.to_owned(),
|
||||
piece: p,
|
||||
piece: Cell::Empty,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,44 +19,63 @@ impl PlayerConsole {
|
||||
return self.name.as_str();
|
||||
}
|
||||
|
||||
pub fn request_move(&self, b: &Board) -> Result<Move, Box<dyn std::error::Error>> {
|
||||
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(())
|
||||
}
|
||||
|
||||
pub fn request_move(&self, _: &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);
|
||||
println!("{}, 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;
|
||||
};
|
||||
io::stdin().read_line(&mut s)?;
|
||||
|
||||
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 {
|
||||
s = s.trim().into();
|
||||
|
||||
if s == "" {
|
||||
return Err("player has surrendered".into());
|
||||
} else if s.len() != 2 {
|
||||
println!("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,
|
||||
_ => {
|
||||
println!("invalid input: {}", c);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return Ok(Move {
|
||||
x: x,
|
||||
y: x,
|
||||
c: self.piece,
|
||||
y: y,
|
||||
piece: self.piece,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Player for PlayerConsole {
|
||||
fn start_new_game(&mut self, p: Cell) -> Result<(), Box<dyn std::error::Error>> {
|
||||
return self.start_new_game(p);
|
||||
}
|
||||
|
||||
fn request_move(&self, b: &Board) -> Result<Move, Box<dyn std::error::Error>> {
|
||||
self.request_move(b)
|
||||
}
|
||||
@@ -65,9 +84,3 @@ impl Player for PlayerConsole {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user