simple_chess

Enum ChessMoveType

Source
pub enum ChessMoveType {
    Move {
        original_position: (usize, usize),
        new_position: (usize, usize),
        piece: ChessPiece,
        taken_piece: Option<ChessPiece>,
        promotion: Option<ChessPiece>,
    },
    EnPassant {
        original_position: (usize, usize),
        new_position: (usize, usize),
        piece: ChessPiece,
        taken_piece: ChessPiece,
        taken_piece_position: (usize, usize),
        promotion: Option<ChessPiece>,
    },
    Castle {
        rook_original_position: (usize, usize),
        rook_new_position: (usize, usize),
        king_original_position: (usize, usize),
        king_new_position: (usize, usize),
    },
}
Expand description

Represents different types of simple_chess moves.

This enum captures the state and specific details of various types of legal simple_chess moves. It provides functionality to apply and undo these moves on a simple_chess board.

Variants§

§

Move

A regular piece move which might include capturing an opponent’s piece or promoting a pawn.

Fields:

  • original_position: (usize, usize) - The starting position of the simple_chess piece.
  • new_position: (usize, usize) - The destination position of the simple_chess piece.
  • piece: ChessPiece - The simple_chess piece being moved.
  • taken_piece: Option - The opponent’s piece captured during this move, if any.
  • promotion: Option - The piece type a pawn is promoted to, if applicable.

Fields

§original_position: (usize, usize)
§new_position: (usize, usize)
§taken_piece: Option<ChessPiece>
§promotion: Option<ChessPiece>
§

EnPassant

A special pawn capture move where a pawn captures another pawn that has moved two squares forward from its starting position on the previous turn.

Fields:

  • original_position: (usize, usize) - The starting position of the pawn.
  • new_position: (usize, usize) - The destination position of the pawn after capturing.
  • piece: ChessPiece - The pawn performing the en passant capture.
  • taken_piece: ChessPiece - The opponent’s pawn being captured.
  • taken_piece_position: (usize, usize) - The position of the captured pawn.
  • promotion: Option - The piece type if the pawn is promoted, if applicable.

Fields

§original_position: (usize, usize)
§new_position: (usize, usize)
§taken_piece: ChessPiece
§taken_piece_position: (usize, usize)
§promotion: Option<ChessPiece>
§

Castle

A special move involving the king and a rook where both pieces move simultaneously.

Fields:

  • rook_original_position: (usize, usize) - The starting position of the rook.
  • rook_new_position: (usize, usize) - The destination position of the rook.
  • king_original_position: (usize, usize) - The starting position of the king.
  • king_new_position: (usize, usize) - The destination position of the king.

Fields

§rook_original_position: (usize, usize)
§rook_new_position: (usize, usize)
§king_original_position: (usize, usize)
§king_new_position: (usize, usize)

Implementations§

Source§

impl ChessMoveType

Source

pub fn make_move(&self, board: &mut Board<ChessPiece>)

Applies the current move to the simple_chess board.

This method performs the actual move on the board by repositioning the involved pieces according to the move type.

§Arguments
  • board - A mutable reference to the simple_chess board on which the move will be applied.
§Examples
use simple_chess::ChessMoveType;
use simple_chess::Color::White;
use simple_chess::piece::ChessPiece;
use simple_chess::piece::PieceType::Pawn;
use game_board::Board;
let mut board = Board::build(8, 8).unwrap();
let original_position = (0, 1);

board.place_piece(ChessPiece::new(Pawn, White), original_position.0, original_position.1);

let game_move = ChessMoveType::Move {
    original_position,
    new_position: (0, 2),
    piece: *board.get_piece_at_space(original_position.0, original_position.1).unwrap(),
    taken_piece: None,
    promotion: None,
};

assert!(board.get_piece_at_space(0, 2).is_none());
game_move.make_move(&mut board);

assert!(board.get_piece_at_space(original_position.0, original_position.1).is_none());
assert_eq!(Pawn, board.get_piece_at_space(0, 2).unwrap().get_piece_type());
assert_eq!(White, board.get_piece_at_space(0, 2).unwrap().get_color());
Source

pub fn undo_move(&self, board: &mut Board<ChessPiece>)

Trait Implementations§

Source§

impl Clone for ChessMoveType

Source§

fn clone(&self) -> ChessMoveType

Returns a copy 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 ChessMoveType

Source§

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

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

impl Display for ChessMoveType

Source§

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

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

impl PartialEq for ChessMoveType

Source§

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

Source§

impl StructuralPartialEq for ChessMoveType

Auto Trait Implementations§

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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§

default 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.