use crate::constants::*;
pub type Figure = usize;
pub trait FigureTrait {
fn symbol(self) -> &'static str;
fn base_figure(self) -> Figure;
fn lancer_direction(self) -> usize;
}
impl FigureTrait for Figure {
fn symbol(self) -> &'static str {
FIGURE_FEN_SYMBOLS[self]
}
fn base_figure(self) -> Figure {
if self < LANCER_MIN || self > LANCER_MAX {
return self;
}
LANCER
}
fn lancer_direction(self) -> usize {
self - LANCER_MIN
}
}
pub type Color = usize;
pub trait ColorTrait {
fn turn_fen(self) -> String;
fn inverse(self) -> Color;
}
impl ColorTrait for Color {
fn turn_fen(self) -> String {
(if self == WHITE { "w" } else { "b" }).to_string()
}
fn inverse(self) -> Color {
WHITE - self
}
}
pub type Piece = usize;
pub fn fen_symbol_to_piece(letter: &str) -> Piece {
match letter {
"p" => color_figure(BLACK, PAWN),
"P" => color_figure(WHITE, PAWN),
"n" => color_figure(BLACK, KNIGHT),
"N" => color_figure(WHITE, KNIGHT),
"b" => color_figure(BLACK, BISHOP),
"B" => color_figure(WHITE, BISHOP),
"r" => color_figure(BLACK, ROOK),
"R" => color_figure(WHITE, ROOK),
"q" => color_figure(BLACK, QUEEN),
"Q" => color_figure(WHITE, QUEEN),
"k" => color_figure(BLACK, KING),
"K" => color_figure(WHITE, KING),
"s" => color_figure(BLACK, SENTRY),
"S" => color_figure(WHITE, SENTRY),
"j" => color_figure(BLACK, JAILER),
"J" => color_figure(WHITE, JAILER),
_ => NO_PIECE,
}
}
pub fn color_figure(col: Color, fig: Figure) -> Piece {
(2 * fig) + col
}
pub trait PieceTrait {
fn color(self) -> Color;
fn figure(self) -> Figure;
fn fen_symbol(self) -> &'static str;
fn san_symbol(self) -> &'static str;
fn uci_symbol(self) -> &'static str;
fn san_letter(self) -> &'static str;
}
impl PieceTrait for Piece {
fn color(self) -> Color {
return if self & 1 == 0 { BLACK } else { WHITE };
}
fn figure(self) -> Figure {
self >> 1
}
fn fen_symbol(self) -> &'static str {
PIECE_FEN_SYMBOLS[self]
}
fn san_symbol(self) -> &'static str {
return color_figure(WHITE, self.figure()).fen_symbol();
}
fn uci_symbol(self) -> &'static str {
return self.figure().symbol();
}
fn san_letter(self) -> &'static str {
FIGURE_SAN_LETTERS[self.figure()]
}
}