-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.rs
64 lines (54 loc) · 1.95 KB
/
game.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Simple command-line application to play chess
use owlchess::{
board::PrettyStyle,
chain::{GameStatusPolicy, NumberPolicy},
moves::Style,
types::OutcomeFilter,
Color, GameStatus, Move, MoveChain,
};
use std::io::{self, BufRead, Write};
fn main() {
let mut stdin = io::stdin().lock();
let mut chain = MoveChain::new_initial();
loop {
if let Some(outcome) = chain.outcome() {
// Indicate that the game is finished and terminate the game loop
println!("Game finished: {}, {}", GameStatus::from(outcome), outcome);
println!("Notation:");
println!(
"{}",
chain.styled(NumberPolicy::FromBoard, Style::San, GameStatusPolicy::Show)
);
break;
}
// Print the current board
println!("{}", chain.last().pretty(PrettyStyle::Ascii));
let side = match chain.last().side() {
Color::White => "White",
Color::Black => "Black",
};
// Prompt for the next move
print!("{} move ({}): ", side, chain.last().raw().move_number);
io::stdout().flush().unwrap();
let mut s = String::new();
stdin.read_line(&mut s).unwrap();
let s = s.trim();
// Parse the move. Note that we could just call `MoveChain::push_san()` here
// directly. But we want to show more features here, so separate parsing a move
// and making it.
let mv = match Move::from_san(s, chain.last()) {
Ok(mv) => mv,
Err(e) => {
println!("Bad move: {}", e);
println!();
continue;
}
};
// Move is definitely legal after `Move::from_san()`, so just `unwrap()` instead of
// error checking.
chain.push(mv).unwrap();
println!();
// Check whether the game must be terminated.
chain.set_auto_outcome(OutcomeFilter::Strict);
}
}