mirror of
https://github.com/Noratrieb/minmax.git
synced 2026-01-14 15:25:08 +01:00
nice
This commit is contained in:
parent
2fa7531824
commit
b666c4a0cb
2 changed files with 33 additions and 2 deletions
|
|
@ -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 ")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
31
src/lib.rs
31
src/lib.rs
|
|
@ -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.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue