simple_chess/piece/
mod.rs

1use crate::chess_move::ChessMoveType;
2use crate::Color;
3use game_board::Board;
4use std::fmt::{Display, Formatter};
5
6mod bishop;
7mod king;
8mod knight;
9mod pawn;
10mod queen;
11mod rook;
12
13#[derive(Debug, PartialEq, Clone, Copy, Eq)]
14pub enum PieceType {
15    Pawn,
16    Rook,
17    Knight,
18    Bishop,
19    Queen,
20    King,
21}
22
23/// ChessPiece represents a single simple_chess piece on the board.
24///
25/// Each ChessPiece has a specific type (Pawn, Rook, Knight, Bishop, Queen, King)
26/// and a color (White or Black).
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub struct ChessPiece {
29    piece_type: PieceType,
30    color: Color,
31}
32
33impl ChessPiece {
34    /// Creates a new `ChessPiece` with the specified type and color.
35    ///
36    /// # Arguments
37    ///
38    /// * `piece_type` - The type of the simple_chess piece (e.g., Pawn, Rook, Knight, Bishop, Queen, King).
39    /// * `color` - The color of the simple_chess piece (e.g., White or Black).
40    ///
41    /// # Examples
42    ///
43    /// ```
44    /// use simple_chess::piece::{ChessPiece, PieceType};
45    /// use simple_chess::Color;
46    ///
47    /// let white_king = ChessPiece::new(PieceType::King, Color::White);
48    /// let black_pawn = ChessPiece::new(PieceType::Pawn, Color::Black);
49    /// ```
50    pub fn new(piece_type: PieceType, color: Color) -> Self {
51        Self { piece_type, color }
52    }
53
54    /// Returns the color of the simple_chess piece.
55    ///
56    /// # Examples
57    ///
58    /// ```
59    /// use simple_chess::piece::{ChessPiece, PieceType};
60    /// use simple_chess::Color;
61    ///
62    /// let white_pawn = ChessPiece::new(PieceType::Pawn, Color::White);
63    /// assert_eq!(white_pawn.get_color(), Color::White);
64    ///
65    /// let black_queen = ChessPiece::new(PieceType::Queen, Color::Black);
66    /// assert_eq!(black_queen.get_color(), Color::Black);
67    /// ```
68    pub fn get_color(&self) -> Color {
69        self.color
70    }
71
72    /// Returns the type of the simple_chess piece.
73    ///
74    /// # Examples
75    ///
76    /// ```
77    /// use simple_chess::piece::{ChessPiece, PieceType};
78    /// use simple_chess::Color;
79    ///
80    /// let white_pawn = ChessPiece::new(PieceType::Pawn, Color::White);
81    /// assert_eq!(white_pawn.get_piece_type(), PieceType::Pawn);
82    ///
83    /// let black_queen = ChessPiece::new(PieceType::Queen, Color::Black);
84    /// assert_eq!(black_queen.get_piece_type(), PieceType::Queen);
85    /// ```
86    pub fn get_piece_type(&self) -> PieceType {
87        self.piece_type
88    }
89
90    /// Returns the UTF-8 string representation of the simple_chess piece based on its type and color.
91    ///
92    /// # Examples
93    ///
94    /// ```
95    /// use simple_chess::piece::{ChessPiece, PieceType};
96    /// use simple_chess::Color;
97    ///
98    /// let white_king = ChessPiece::new(PieceType::King, Color::White);
99    /// assert_eq!(white_king.as_utf_str(), "♔");
100    ///
101    /// let black_pawn = ChessPiece::new(PieceType::Pawn, Color::Black);
102    /// assert_eq!(black_pawn.as_utf_str(), "♟");
103    /// ```
104    pub fn as_utf_str(&self) -> &str {
105        match self.piece_type {
106            PieceType::King => king::as_utf_str(self.color),
107            PieceType::Queen => queen::as_utf_str(self.color),
108            PieceType::Rook => rook::as_utf_str(self.color),
109            PieceType::Bishop => bishop::as_utf_str(self.color),
110            PieceType::Knight => knight::as_utf_str(self.color),
111            PieceType::Pawn => pawn::as_utf_str(self.color),
112        }
113    }
114
115    /// Returns a vector of possible moves for the simple_chess piece from a given position on the board.
116    ///
117    /// This function computes the possible moves for the simple_chess piece, based on its type, current
118    /// position on the board, and the state of the board. The rules for valid moves for each
119    /// type of simple_chess piece are applied.
120    ///
121    /// Pins are not taken into account in this function, you will need to filter the returned
122    /// vector of moves based off if they leave the king in check or not.
123    ///
124    /// # Arguments
125    ///
126    /// * `position` - A tuple `(usize, usize)` representing the current position of the simple_chess piece
127    ///                on the board (row, column).
128    /// * `board` - A reference to the `Board<ChessPiece>` which represents the current state of the
129    ///             simple_chess board, including all pieces and their positions.
130    ///
131    /// # Returns
132    ///
133    /// A `Vec<ChessMoveType>` containing all possible moves for the simple_chess piece from its current
134    /// position, based on the rules for the specific type of simple_chess piece.
135    ///
136    /// # Examples
137    ///
138    /// ```
139    /// use simple_chess::piece::{ChessPiece, PieceType};
140    /// use simple_chess::Color;
141    /// use game_board::Board;
142    ///
143    /// let white_king = ChessPiece::new(PieceType::King, Color::White);
144    /// let board = Board::build(8, 8).unwrap();
145    /// let moves = white_king.possible_moves((0, 4), &board, None);
146    /// assert_eq!(5, moves.len());
147    /// ```
148    pub fn possible_moves(
149        &self,
150        position: (usize, usize),
151        board: &Board<ChessPiece>,
152        last_move: Option<&ChessMoveType>,
153    ) -> Vec<ChessMoveType> {
154        match self.piece_type {
155            PieceType::King => king::possible_moves(self.color, position, board),
156            PieceType::Queen => queen::possible_moves(self.color, position, board),
157            PieceType::Rook => rook::possible_moves(self.color, position, board),
158            PieceType::Bishop => bishop::possible_moves(self.color, position, board),
159            PieceType::Knight => knight::possible_moves(self.color, position, board),
160            PieceType::Pawn => pawn::possible_moves(self.color, position, board, last_move),
161        }
162    }
163}
164
165impl Display for ChessPiece {
166    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
167        write!(f, "{}", self.as_utf_str())
168    }
169}