pub struct Board { /* private fields */ }
Implementations§
Source§impl Board
impl Board
pub fn empty() -> Self
pub fn rating_bar(&self, len: usize) -> String
Sourcepub fn get_turn_color(&self) -> Color
pub fn get_turn_color(&self) -> Color
Get the color of the current player
Sourcepub fn get_en_passant(&self) -> Option<Position>
pub fn get_en_passant(&self) -> Option<Position>
Get the position of the En-Passant square
Sourcepub fn remove_all(&self, color: Color) -> Self
pub fn remove_all(&self, color: Color) -> Self
Remove all of the pieces for a given player
Sourcepub fn get_material_advantage(&self, color: Color) -> i32
pub fn get_material_advantage(&self, color: Color) -> i32
Get the value of the material advantage of a certain player
Sourcepub fn get_piece(&self, pos: Position) -> Option<Piece>
pub fn get_piece(&self, pos: Position) -> Option<Piece>
Does a square have any piece?
Examples found in repository?
18fn get_cpu_move(b: &Board, best: bool) -> Move {
19 let (m, count, _) = if best {
20 b.get_best_next_move(4)
21 } else {
22 b.get_worst_next_move(4)
23 };
24
25 print!("CPU evaluated {} moves before choosing to ", count);
26 match m {
27 Move::Piece(from, to) => match (b.get_piece(from), b.get_piece(to)) {
28 (Some(piece), Some(takes)) => println!(
29 "take {}({}) with {}({})",
30 takes.get_name(),
31 to,
32 piece.get_name(),
33 from
34 ),
35 (Some(piece), None) => {
36 println!("move {}({}) to {}", piece.get_name(), from, to)
37 }
38 _ => println!("move {} to {}", from, to),
39 },
40 Move::KingSideCastle => {
41 println!("castle kingside")
42 }
43 Move::QueenSideCastle => {
44 println!("castle queenside")
45 }
46 Move::Resign => println!("resign"),
47 }
48
49 m
50}
Sourcepub fn has_ally_piece(&self, pos: Position, ally_color: Color) -> bool
pub fn has_ally_piece(&self, pos: Position, ally_color: Color) -> bool
Does a square have an ally piece?
Sourcepub fn has_enemy_piece(&self, pos: Position, ally_color: Color) -> bool
pub fn has_enemy_piece(&self, pos: Position, ally_color: Color) -> bool
If a square at a given position has an enemy piece from a given ally color, return true. Otherwise, return false.
For example, if a square has a black piece, and this method is called
upon it with an ally_color
of Color::White
, then it will return true.
If called with Color::Black
upon the same square, however, it will return false.
Sourcepub fn has_piece(&self, pos: Position) -> bool
pub fn has_piece(&self, pos: Position) -> bool
If a square at a given position has any piece, return true. Otherwise, return false.
Sourcepub fn has_no_piece(&self, pos: Position) -> bool
pub fn has_no_piece(&self, pos: Position) -> bool
If a square at a given position has no piece, return true. Otherwise, return false.
Sourcepub fn get_king_pos(&self, color: Color) -> Option<Position>
pub fn get_king_pos(&self, color: Color) -> Option<Position>
If there is a king on the board, return the position that it sits on.
Sourcepub fn is_threatened(&self, pos: Position, ally_color: Color) -> bool
pub fn is_threatened(&self, pos: Position, ally_color: Color) -> bool
Is a square threatened by an enemy piece?
Sourcepub fn is_in_check(&self, color: Color) -> bool
pub fn is_in_check(&self, color: Color) -> bool
Get whether or not the king of a given color is in check.
Sourcepub fn can_kingside_castle(&self, color: Color) -> bool
pub fn can_kingside_castle(&self, color: Color) -> bool
Can a given player castle kingside?
Sourcepub fn can_queenside_castle(&self, color: Color) -> bool
pub fn can_queenside_castle(&self, color: Color) -> bool
Can a given player castle queenside?
Sourcepub fn has_sufficient_material(&self, color: Color) -> bool
pub fn has_sufficient_material(&self, color: Color) -> bool
Does the respective player have sufficient material?
Sourcepub fn has_insufficient_material(&self, color: Color) -> bool
pub fn has_insufficient_material(&self, color: Color) -> bool
Does the respective player have insufficient material?
Sourcepub fn is_stalemate(&self) -> bool
pub fn is_stalemate(&self) -> bool
Is the current player in stalemate?
Sourcepub fn is_checkmate(&self) -> bool
pub fn is_checkmate(&self) -> bool
Is the current player in checkmate?
Sourcepub fn change_turn(self) -> Self
pub fn change_turn(self) -> Self
Change the current turn to the next player.
Examples found in repository?
52fn main() -> Result<(), String> {
53 let mut b = Board::default();
54
55 println!("{}", b);
56 let mut history = vec![];
57
58 loop {
59 let mut s = input(">>> ");
60 s = s.trim().to_string();
61
62 let m = if s.is_empty() {
63 println!("Waiting for CPU to choose best move...");
64 get_cpu_move(&b, true)
65 } else if s == "worst" {
66 println!("Waiting for CPU to choose worst move...");
67 get_cpu_move(&b, false)
68 } else if s == "rate" {
69 continue;
70 } else if s == "pass" {
71 b = b.change_turn();
72 continue;
73 } else if s == "history" {
74 for i in 0..history.len() {
75 if i < history.len() - 1 {
76 println!("{} {}", history[i], history[i + 1]);
77 } else {
78 println!("{}", history[i]);
79 }
80 }
81 continue;
82 } else {
83 match Move::try_from(s) {
84 Ok(m) => m,
85 Err(e) => {
86 eprintln!("{}", e);
87 continue;
88 }
89 }
90 };
91
92 match b.play_move(m) {
93 GameResult::Continuing(next_board) => {
94 b = next_board;
95 println!("{}", b);
96 history.push(m);
97 }
98
99 GameResult::Victory(winner) => {
100 println!("{}", b);
101 println!("{} loses. {} is victorious.", !winner, winner);
102 break;
103 }
104
105 GameResult::IllegalMove(x) => {
106 eprintln!("{} is an illegal move.", x);
107 }
108
109 GameResult::Stalemate => {
110 println!("Drawn game.");
111 break;
112 }
113 }
114 }
115
116 for m in history {
117 println!("{}", m);
118 }
119 Ok(())
120}
Sourcepub fn play_move(&self, m: Move) -> GameResult
pub fn play_move(&self, m: Move) -> GameResult
Play a move and confirm it is legal.
Examples found in repository?
52fn main() -> Result<(), String> {
53 let mut b = Board::default();
54
55 println!("{}", b);
56 let mut history = vec![];
57
58 loop {
59 let mut s = input(">>> ");
60 s = s.trim().to_string();
61
62 let m = if s.is_empty() {
63 println!("Waiting for CPU to choose best move...");
64 get_cpu_move(&b, true)
65 } else if s == "worst" {
66 println!("Waiting for CPU to choose worst move...");
67 get_cpu_move(&b, false)
68 } else if s == "rate" {
69 continue;
70 } else if s == "pass" {
71 b = b.change_turn();
72 continue;
73 } else if s == "history" {
74 for i in 0..history.len() {
75 if i < history.len() - 1 {
76 println!("{} {}", history[i], history[i + 1]);
77 } else {
78 println!("{}", history[i]);
79 }
80 }
81 continue;
82 } else {
83 match Move::try_from(s) {
84 Ok(m) => m,
85 Err(e) => {
86 eprintln!("{}", e);
87 continue;
88 }
89 }
90 };
91
92 match b.play_move(m) {
93 GameResult::Continuing(next_board) => {
94 b = next_board;
95 println!("{}", b);
96 history.push(m);
97 }
98
99 GameResult::Victory(winner) => {
100 println!("{}", b);
101 println!("{} loses. {} is victorious.", !winner, winner);
102 break;
103 }
104
105 GameResult::IllegalMove(x) => {
106 eprintln!("{} is an illegal move.", x);
107 }
108
109 GameResult::Stalemate => {
110 println!("Drawn game.");
111 break;
112 }
113 }
114 }
115
116 for m in history {
117 println!("{}", m);
118 }
119 Ok(())
120}
Trait Implementations§
Source§impl Evaluate for Board
impl Evaluate for Board
Source§fn value_for(&self, ally_color: Color) -> f64
fn value_for(&self, ally_color: Color) -> f64
Source§fn get_current_player_color(&self) -> Color
fn get_current_player_color(&self) -> Color
Source§fn apply_eval_move(&self, m: Move) -> Self
fn apply_eval_move(&self, m: Move) -> Self
Source§fn get_legal_moves(&self) -> Vec<Move>
fn get_legal_moves(&self) -> Vec<Move>
Source§fn get_best_next_move(&self, depth: i32) -> (Move, u64, f64)
fn get_best_next_move(&self, depth: i32) -> (Move, u64, f64)
depth
number of moves
of lookahead. Read moreSource§fn get_worst_next_move(&self, depth: i32) -> (Move, u64, f64)
fn get_worst_next_move(&self, depth: i32) -> (Move, u64, f64)
depth
number of moves
of lookahead. Read more