add engine
This commit is contained in:
73
player/human.rs
Normal file
73
player/human.rs
Normal file
@@ -0,0 +1,73 @@
|
||||
use crate::game::{Board, Cell, Move, CELL_EMPTY, CELL_O, CELL_X};
|
||||
use crate::player::Player;
|
||||
use std::io;
|
||||
|
||||
pub struct PlayerConsole {
|
||||
name: String,
|
||||
piece: Cell,
|
||||
}
|
||||
|
||||
impl PlayerConsole {
|
||||
pub fn new(name: &str, p: Cell) -> PlayerConsole {
|
||||
PlayerConsole {
|
||||
name: name.to_owned(),
|
||||
piece: p,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(&self) -> &str {
|
||||
return self.name.as_str();
|
||||
}
|
||||
|
||||
pub fn request_move(&self, b: &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);
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
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 {
|
||||
continue;
|
||||
};
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return Ok(Move {
|
||||
x: x,
|
||||
y: x,
|
||||
c: self.piece,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Player for PlayerConsole {
|
||||
fn request_move(&self, b: &Board) -> Result<Move, Box<dyn std::error::Error>> {
|
||||
self.request_move(b)
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
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