rustenginelib/
piece.rs

1use crate::constants::*;
2
3/// Figure type represents a chess figure as an unsigned int
4pub type Figure = usize;
5
6/// FigureTrait adds methods to Figure
7pub trait FigureTrait {
8    /// returns the fen symbol for the figure ( lower case )
9    fn symbol(self) -> &'static str;
10    /// returns the base figure of the figure, same as figure except for lancers, where it is LANCER
11    fn base_figure(self) -> Figure;
12    /// return the lancer direction of the figure provided that it is a lancer
13    fn lancer_direction(self) -> usize;
14}
15
16impl FigureTrait for Figure {
17    /// returns the fen symbol for the figure ( lower case )
18    fn symbol(self) -> &'static str {
19        FIGURE_FEN_SYMBOLS[self]
20    }
21    /// returns the base figure of the figure, same as figure except for lancers, where it is LANCER
22    fn base_figure(self) -> Figure {
23        if self < LANCER_MIN || self > LANCER_MAX {
24            return self;
25        }
26        LANCER
27    }
28    /// return the lancer direction of the figure provided that it is a lancer
29    fn lancer_direction(self) -> usize {
30        self - LANCER_MIN
31    }
32}
33
34/// Color type represents a chess color
35pub type Color = usize;
36
37/// ColorTrait defines methods for Color
38pub trait ColorTrait {
39    /// returns the color fen string
40    fn turn_fen(self) -> String;
41    /// returns inverse of color
42    fn inverse(self) -> Color;
43}
44
45/// ColorTrait implementation
46impl ColorTrait for Color {
47    fn turn_fen(self) -> String {
48        (if self == WHITE { "w" } else { "b" }).to_string()
49    }
50    /// returns inverse of color
51    fn inverse(self) -> Color {
52        WHITE - self
53    }
54}
55
56/// Piece type represents a chess piece as an unsigned int
57pub type Piece = usize;
58
59/// returns the piece for the fen symbol
60pub fn fen_symbol_to_piece(letter: &str) -> Piece {
61    match letter {
62        "p" => color_figure(BLACK, PAWN),
63        "P" => color_figure(WHITE, PAWN),
64        "n" => color_figure(BLACK, KNIGHT),
65        "N" => color_figure(WHITE, KNIGHT),
66        "b" => color_figure(BLACK, BISHOP),
67        "B" => color_figure(WHITE, BISHOP),
68        "r" => color_figure(BLACK, ROOK),
69        "R" => color_figure(WHITE, ROOK),
70        "q" => color_figure(BLACK, QUEEN),
71        "Q" => color_figure(WHITE, QUEEN),
72        "k" => color_figure(BLACK, KING),
73        "K" => color_figure(WHITE, KING),
74        "s" => color_figure(BLACK, SENTRY),
75        "S" => color_figure(WHITE, SENTRY),
76        "j" => color_figure(BLACK, JAILER),
77        "J" => color_figure(WHITE, JAILER),
78        _ => NO_PIECE,
79    }
80}
81
82/// returns a piece from color and figure
83pub fn color_figure(col: Color, fig: Figure) -> Piece {
84    (2 * fig) + col
85}
86
87/// PieceTrait adds methods to Piece
88pub trait PieceTrait {
89    /// returns the color of the piece
90    fn color(self) -> Color;
91    /// returns the figure of the piece
92    fn figure(self) -> Figure;
93    /// returns the fen symbol for the piece
94    fn fen_symbol(self) -> &'static str;
95    /// returns the san symbol for the piece ( capital piece letter )
96    fn san_symbol(self) -> &'static str;
97    /// returns the uci symbol of the piece ( lower case )
98    fn uci_symbol(self) -> &'static str;
99    /// returns the san letter of the piece ( upper case )
100    fn san_letter(self) -> &'static str;
101}
102
103impl PieceTrait for Piece {
104    /// returns the color of the piece
105    fn color(self) -> Color {
106        return if self & 1 == 0 { BLACK } else { WHITE };
107    }
108    /// returns the figure of the piece
109    fn figure(self) -> Figure {
110        self >> 1
111    }
112    /// returns the fen symbol for the piece
113    fn fen_symbol(self) -> &'static str {
114        PIECE_FEN_SYMBOLS[self]
115    }
116    /// returns the san symbol for the piece ( capital piece letter )
117    fn san_symbol(self) -> &'static str {
118        return color_figure(WHITE, self.figure()).fen_symbol();
119    }
120    /// returns the uci symbol of the piece ( lower case )
121    fn uci_symbol(self) -> &'static str {
122        return self.figure().symbol();
123    }
124    /// returns the san letter of the piece ( upper case )
125    fn san_letter(self) -> &'static str {
126        FIGURE_SAN_LETTERS[self.figure()]
127    }
128}