This commit is contained in:
nora 2022-10-31 14:43:18 +01:00
parent 2fa7531824
commit b666c4a0cb
No known key found for this signature in database
2 changed files with 33 additions and 2 deletions

View file

@ -127,10 +127,10 @@ impl Display for Board {
let index = i * 3 + j; let index = i * 3 + j;
match self.get(index) { match self.get(index) {
Some(player) => { Some(player) => {
write!(f, "\x1B[1m{player}\x1B[0m ")?; write!(f, "\x1B[33m{player}\x1B[0m ")?;
} }
None => { None => {
write!(f, "\x1B[37m{index}\x1B[0m ")?; write!(f, "\x1B[35m{index}\x1B[0m ")?;
} }
} }
} }

View file

@ -1,6 +1,8 @@
mod board; mod board;
mod game; mod game;
use std::io::Write;
use board::Player; use board::Player;
pub use board::Board; pub use board::Board;
@ -17,3 +19,32 @@ impl GamePlayer for GreedyPlayer {
board.set(first_free, Some(this_player)); board.set(first_free, Some(this_player));
} }
} }
pub struct HumanPlayer;
impl GamePlayer for HumanPlayer {
fn next_move(&mut self, board: &mut Board, this_player: Player) {
loop {
print!("{board}where to put the next {this_player}? (0-8): ");
std::io::stdout().flush().unwrap();
let mut buf = String::new();
std::io::stdin().read_line(&mut buf).unwrap();
match buf.trim().parse() {
Ok(number) if number < 9 => match board.get(number) {
None => {
board.set(number, Some(this_player));
return;
}
Some(_) => {
println!("Field is occupied already.")
}
},
Ok(_) | Err(_) => {
println!("Invalid input.")
}
}
}
}
}