Struct Board

Source
pub struct Board { /* private fields */ }

Implementations§

Source§

impl Board

Source

pub fn horde() -> Self

Create the default board for the Horde variant

Source

pub fn empty() -> Self

Source

pub fn rating_bar(&self, len: usize) -> String

Source

pub fn get_turn_color(&self) -> Color

Get the color of the current player

Source

pub fn get_en_passant(&self) -> Option<Position>

Get the position of the En-Passant square

Source

pub fn remove_all(&self, color: Color) -> Self

Remove all of the pieces for a given player

Source

pub fn queen_all(&self, color: Color) -> Self

Convert all of a given players pieces to queens

Source

pub fn set_turn(&self, color: Color) -> Self

Make the game a certain player’s turn

Source

pub fn get_material_advantage(&self, color: Color) -> i32

Get the value of the material advantage of a certain player

Source

pub fn get_piece(&self, pos: Position) -> Option<Piece>

Does a square have any piece?

Examples found in repository?
examples/terminal.rs (line 27)
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}
Source

pub fn has_ally_piece(&self, pos: Position, ally_color: Color) -> bool

Does a square have an ally piece?

Source

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.

Source

pub fn has_piece(&self, pos: Position) -> bool

If a square at a given position has any piece, return true. Otherwise, return false.

Source

pub fn has_no_piece(&self, pos: Position) -> bool

If a square at a given position has no piece, return true. Otherwise, return false.

Source

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.

Source

pub fn is_threatened(&self, pos: Position, ally_color: Color) -> bool

Is a square threatened by an enemy piece?

Source

pub fn is_in_check(&self, color: Color) -> bool

Get whether or not the king of a given color is in check.

Source

pub fn can_kingside_castle(&self, color: Color) -> bool

Can a given player castle kingside?

Source

pub fn can_queenside_castle(&self, color: Color) -> bool

Can a given player castle queenside?

Source

pub fn has_sufficient_material(&self, color: Color) -> bool

Does the respective player have sufficient material?

Source

pub fn has_insufficient_material(&self, color: Color) -> bool

Does the respective player have insufficient material?

Source

pub fn is_stalemate(&self) -> bool

Is the current player in stalemate?

Source

pub fn is_checkmate(&self) -> bool

Is the current player in checkmate?

Source

pub fn change_turn(self) -> Self

Change the current turn to the next player.

Examples found in repository?
examples/terminal.rs (line 71)
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}
Source

pub fn play_move(&self, m: Move) -> GameResult

Play a move and confirm it is legal.

Examples found in repository?
examples/terminal.rs (line 92)
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 Clone for Board

Source§

fn clone(&self) -> Board

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Board

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Board

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Board

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Evaluate for Board

Source§

fn value_for(&self, ally_color: Color) -> f64

Get the value of the board for a given color. This subtracts the opponents value, and accounts for piece positions and material value.
Source§

fn get_current_player_color(&self) -> Color

Get the current player’s color.
Source§

fn apply_eval_move(&self, m: Move) -> Self

Apply a move to the board for evaluation.
Get the legal moves for the current player.
Source§

fn get_best_next_move(&self, depth: i32) -> (Move, u64, f64)

Get the best move for the current player with depth number of moves of lookahead. Read more
Source§

fn get_worst_next_move(&self, depth: i32) -> (Move, u64, f64)

Get the best move for the current player with depth number of moves of lookahead. Read more
Source§

fn minimax( &self, depth: i32, alpha: f64, beta: f64, is_maximizing: bool, getting_move_for: Color, board_count: &mut u64, ) -> f64

Perform minimax on a certain position, and get the minimum or maximum value for a board. To get the best move, you minimize the values of the possible outcomes from your own position, and maximize the values of the replies made by the other player. Read more
Source§

impl From<Board> for BoardBuilder

Source§

fn from(board: Board) -> Self

Converts to this type from the input type.
Source§

impl Ord for Board

Source§

fn cmp(&self, other: &Board) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Board

Source§

fn eq(&self, other: &Board) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Board

Source§

fn partial_cmp(&self, other: &Board) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for Board

Source§

impl Eq for Board

Source§

impl StructuralPartialEq for Board

Auto Trait Implementations§

§

impl Freeze for Board

§

impl RefUnwindSafe for Board

§

impl Send for Board

§

impl Sync for Board

§

impl Unpin for Board

§

impl UnwindSafe for Board

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.