use crate::BitBoard;
use core::fmt;
use core::mem::transmute;
const RANK_BITBOARDS: [BitBoard; 8] = [
BitBoard::RANK_1,
BitBoard::RANK_2,
BitBoard::RANK_3,
BitBoard::RANK_4,
BitBoard::RANK_5,
BitBoard::RANK_6,
BitBoard::RANK_7,
BitBoard::RANK_8,
];
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Debug, Hash)]
#[repr(u8)]
pub enum Rank {
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
}
impl fmt::Display for Rank {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::One => write!(f, "One"),
Self::Two => write!(f, "Two"),
Self::Three => write!(f, "Three"),
Self::Four => write!(f, "Four"),
Self::Five => write!(f, "Five"),
Self::Six => write!(f, "Six"),
Self::Seven => write!(f, "Seven"),
Self::Eight => write!(f, "Eight"),
}
}
}
impl Rank {
pub const NUM_RANKS: usize = 8;
pub const ALL: [Self; 8] = [
Self::One,
Self::Two,
Self::Three,
Self::Four,
Self::Five,
Self::Six,
Self::Seven,
Self::Eight,
];
#[inline(always)]
pub const fn from_index(index: usize) -> Rank {
unsafe { transmute(index as u8 & 7) }
}
#[inline(always)]
pub const fn to_index(self) -> usize {
self as usize
}
#[inline(always)]
pub const fn up(self) -> Self {
unsafe { transmute((self as u8 + 1) & 7) }
}
#[inline(always)]
pub const fn down(self) -> Self {
unsafe { transmute((self as u8).wrapping_sub(1) & 7) }
}
#[inline(always)]
pub const fn to_bitboard(self) -> BitBoard {
RANK_BITBOARDS[self.to_index()]
}
#[inline(always)]
pub const fn to_char(&self) -> char {
match self {
Rank::One => '1',
Rank::Two => '2',
Rank::Three => '3',
Rank::Four => '4',
Rank::Five => '5',
Rank::Six => '6',
Rank::Seven => '7',
Rank::Eight => '8',
}
}
}