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
- The validity of legal moves
- The validity of legal attacks
- Move generation
- Material and positional value
Variants§
King(Color, Position)
Queen(Color, Position)
Rook(Color, Position)
Bishop(Color, Position)
Knight(Color, Position)
Pawn(Color, Position)
Implementations§
Source§impl Piece
impl Piece
Sourcepub fn get_name(&self) -> &'static str
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?
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 get_material_value(&self) -> i32
pub fn get_material_value(&self) -> i32
Get the material value for a piece.
Name | Value |
---|---|
King | 99999 |
Queen | 9 |
Rook | 5 |
Bishop | 3 |
Knight | 3 |
Pawn | 1 |
Sourcepub fn get_weighted_value(&self) -> f64
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.
Sourcepub fn with_color(&self, color: Color) -> Self
pub fn with_color(&self, color: Color) -> Self
Get the color of a given piece.
Sourcepub fn is_starting_pawn(&self) -> bool
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.
Sourcepub fn is_queenside_rook(&self) -> bool
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.
Sourcepub fn is_kingside_rook(&self) -> bool
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.
Sourcepub fn move_to(&self, new_pos: Position) -> Self
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.