Enum Piece

Source
pub enum Piece {
    King(Color, Position),
    Queen(Color, Position),
    Rook(Color, Position),
    Bishop(Color, Position),
    Knight(Color, Position),
    Pawn(Color, Position),
}
Expand description

A piece on a board.

Every piece has both a color and a position. These, combined with the type of piece it is, determine things like

  1. The validity of legal moves
  2. The validity of legal attacks
  3. Move generation
  4. Material and positional value

Variants§

Implementations§

Source§

impl Piece

Source

pub fn get_name(&self) -> &'static str

Get the name of the piece such as "pawn" or "king". All names are lowercase.

Examples found in repository?
examples/terminal.rs (line 30)
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 get_material_value(&self) -> i32

Get the material value for a piece.

NameValue
King99999
Queen9
Rook5
Bishop3
Knight3
Pawn1
Source

pub fn get_weighted_value(&self) -> f64

Get the weighted value of a piece. This simply factors in position to the pieces value. For example, a knight that is in the center is more favorable than a knight on the side of the board. Similarly, a king in the center of the board is highly unfavorable compared to a king its respective side.

Additionally, the weighted value of the piece is 10 times greater than its material value, plus or minus a weight ranging between 5.0 and -5.0.

Source

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

Get the color of a given piece.

Source

pub fn get_color(&self) -> Color

Get the color of a given piece.

Source

pub fn get_pos(&self) -> Position

Get the position of a piece.

Source

pub fn is_king(&self) -> bool

Is this piece a king?

Source

pub fn is_queen(&self) -> bool

Is this piece a queen?

Source

pub fn is_rook(&self) -> bool

Is this piece a rook?

Source

pub fn is_bishop(&self) -> bool

Is this piece a bishop?

Source

pub fn is_knight(&self) -> bool

Is this piece a knight?

Source

pub fn is_pawn(&self) -> bool

Is this piece a pawn?

Source

pub fn is_starting_pawn(&self) -> bool

Is this piece a starting pawn?

A starting pawn is a pawn that has not been pushed yet whatsoever.

Source

pub fn is_queenside_rook(&self) -> bool

Is this piece in the starting position for the queenside rook?

This method will only return true for rooks that are in the position of the queenside rook, not for any particular rook.

Source

pub fn is_kingside_rook(&self) -> bool

Is this piece in the starting position for the kingside rook?

This method will only return true for rooks that are in the position of the kingside rook, not for any particular rook.

Source

pub fn move_to(&self, new_pos: Position) -> Self

Change the position of this piece to a new position.

For example, Pawn(Color::White, E4).move_to(E5) will result in Pawn(Color::White, E5). This does not check for move legality, it merely creates a new piece with the same color and type, but with a new position.

Trait Implementations§

Source§

impl Clone for Piece

Source§

fn clone(&self) -> Piece

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 Piece

Source§

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

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

impl Display for Piece

Source§

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

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

impl From<Piece> for Square

Source§

fn from(piece: Piece) -> Self

Converts to this type from the input type.
Source§

impl Ord for Piece

Source§

fn cmp(&self, other: &Piece) -> 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 Piece

Source§

fn eq(&self, other: &Piece) -> 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 Piece

Source§

fn partial_cmp(&self, other: &Piece) -> 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 Piece

Source§

impl Eq for Piece

Source§

impl StructuralPartialEq for Piece

Auto Trait Implementations§

§

impl Freeze for Piece

§

impl RefUnwindSafe for Piece

§

impl Send for Piece

§

impl Sync for Piece

§

impl Unpin for Piece

§

impl UnwindSafe for Piece

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.