1use crate::constants::*;
2
3pub type Figure = usize;
5
6pub trait FigureTrait {
8 fn symbol(self) -> &'static str;
10 fn base_figure(self) -> Figure;
12 fn lancer_direction(self) -> usize;
14}
15
16impl FigureTrait for Figure {
17 fn symbol(self) -> &'static str {
19 FIGURE_FEN_SYMBOLS[self]
20 }
21 fn base_figure(self) -> Figure {
23 if self < LANCER_MIN || self > LANCER_MAX {
24 return self;
25 }
26 LANCER
27 }
28 fn lancer_direction(self) -> usize {
30 self - LANCER_MIN
31 }
32}
33
34pub type Color = usize;
36
37pub trait ColorTrait {
39 fn turn_fen(self) -> String;
41 fn inverse(self) -> Color;
43}
44
45impl ColorTrait for Color {
47 fn turn_fen(self) -> String {
48 (if self == WHITE { "w" } else { "b" }).to_string()
49 }
50 fn inverse(self) -> Color {
52 WHITE - self
53 }
54}
55
56pub type Piece = usize;
58
59pub 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
82pub fn color_figure(col: Color, fig: Figure) -> Piece {
84 (2 * fig) + col
85}
86
87pub trait PieceTrait {
89 fn color(self) -> Color;
91 fn figure(self) -> Figure;
93 fn fen_symbol(self) -> &'static str;
95 fn san_symbol(self) -> &'static str;
97 fn uci_symbol(self) -> &'static str;
99 fn san_letter(self) -> &'static str;
101}
102
103impl PieceTrait for Piece {
104 fn color(self) -> Color {
106 return if self & 1 == 0 { BLACK } else { WHITE };
107 }
108 fn figure(self) -> Figure {
110 self >> 1
111 }
112 fn fen_symbol(self) -> &'static str {
114 PIECE_FEN_SYMBOLS[self]
115 }
116 fn san_symbol(self) -> &'static str {
118 return color_figure(WHITE, self.figure()).fen_symbol();
119 }
120 fn uci_symbol(self) -> &'static str {
122 return self.figure().symbol();
123 }
124 fn san_letter(self) -> &'static str {
126 FIGURE_SAN_LETTERS[self.figure()]
127 }
128}