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

@ -1,6 +1,8 @@
mod board;
mod game;
use std::io::Write;
use board::Player;
pub use board::Board;
@ -17,3 +19,32 @@ impl GamePlayer for GreedyPlayer {
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.")
}
}
}
}
}