rename board variables
This commit is contained in:
parent
f84706ac40
commit
37292dd0f1
@ -11,7 +11,7 @@ pub type BitBoardInner = u64;
|
|||||||
pub struct BitBoard(BitBoardInner);
|
pub struct BitBoard(BitBoardInner);
|
||||||
|
|
||||||
// BitBoard should be big enough to fit all points on the board
|
// BitBoard should be big enough to fit all points on the board
|
||||||
const_assert!(std::mem::size_of::<BitBoard>() * 8 >= Board::BOARD_AREA.0 as usize);
|
const_assert!(std::mem::size_of::<BitBoard>() * 8 >= Board::AREA.0 as usize);
|
||||||
|
|
||||||
impl BitBoard {
|
impl BitBoard {
|
||||||
#[allow(clippy::new_without_default)]
|
#[allow(clippy::new_without_default)]
|
||||||
@ -46,7 +46,7 @@ impl BitBoard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub const fn east(&self, n: usize) -> Self {
|
pub const fn east(&self, n: usize) -> Self {
|
||||||
let mask = !Self::col_mask(Board::BOARD_SIZE - 1).0; // Mask to block column BOARD_SIZE-1 bits
|
let mask = !Self::col_mask(Board::SIZE - 1).0; // Mask to block column BOARD_SIZE-1 bits
|
||||||
Self((self.0 & mask) << n)
|
Self((self.0 & mask) << n)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,11 +56,11 @@ impl BitBoard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub const fn north(&self, n: usize) -> Self {
|
pub const fn north(&self, n: usize) -> Self {
|
||||||
Self(self.0 >> (Board::BOARD_SIZE as usize * n))
|
Self(self.0 >> (Board::SIZE as usize * n))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn south(&self, n: usize) -> Self {
|
pub const fn south(&self, n: usize) -> Self {
|
||||||
Self(self.0 << (Board::BOARD_SIZE as usize * n))
|
Self(self.0 << (Board::SIZE as usize * n))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn northeast(&self, n: usize) -> Self {
|
pub const fn northeast(&self, n: usize) -> Self {
|
||||||
@ -95,9 +95,9 @@ impl BitBoard {
|
|||||||
const fn col_mask(col: CoordAxis) -> Self {
|
const fn col_mask(col: CoordAxis) -> Self {
|
||||||
let mut mask = 0;
|
let mut mask = 0;
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < Board::BOARD_AREA.0 {
|
while i < Board::AREA.0 {
|
||||||
mask |= 1 << (i + col);
|
mask |= 1 << (i + col);
|
||||||
i += Board::BOARD_SIZE;
|
i += Board::SIZE;
|
||||||
}
|
}
|
||||||
Self(mask)
|
Self(mask)
|
||||||
}
|
}
|
||||||
@ -155,7 +155,7 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn set_and_get() {
|
fn set_and_get() {
|
||||||
let mut b = BitBoard::new();
|
let mut b = BitBoard::new();
|
||||||
for c in 0..Board::BOARD_AREA.0 {
|
for c in 0..Board::AREA.0 {
|
||||||
assert!(
|
assert!(
|
||||||
!b.get(CoordPair(c)),
|
!b.get(CoordPair(c)),
|
||||||
"A just-initalized BitBoard should be completely empty"
|
"A just-initalized BitBoard should be completely empty"
|
||||||
|
|||||||
@ -10,13 +10,13 @@ use std::{
|
|||||||
/// Map of all points on the board against some type T
|
/// Map of all points on the board against some type T
|
||||||
/// Used to index like so: example[i][j]
|
/// Used to index like so: example[i][j]
|
||||||
/// with each coordinate
|
/// with each coordinate
|
||||||
pub struct PosMap<T: Default>(ArrayVec<T, { Board::BOARD_AREA.0 as usize }>);
|
pub struct PosMap<T: Default>(ArrayVec<T, { Board::AREA.0 as usize }>);
|
||||||
|
|
||||||
impl<T: Default> PosMap<T> {
|
impl<T: Default> PosMap<T> {
|
||||||
#[allow(clippy::new_without_default)]
|
#[allow(clippy::new_without_default)]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self(ArrayVec::from_iter(
|
Self(ArrayVec::from_iter(
|
||||||
(0..Board::BOARD_AREA.0).map(|_| Default::default()),
|
(0..Board::AREA.0).map(|_| Default::default()),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,13 +29,13 @@ impl<T: Default> PosMap<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type PosMapOrig<T> = [[T; Board::BOARD_SIZE as usize]; Board::BOARD_SIZE as usize];
|
type PosMapOrig<T> = [[T; Board::SIZE as usize]; Board::SIZE as usize];
|
||||||
|
|
||||||
impl<T: Default + Copy> From<PosMapOrig<T>> for PosMap<T> {
|
impl<T: Default + Copy> From<PosMapOrig<T>> for PosMap<T> {
|
||||||
fn from(value: PosMapOrig<T>) -> Self {
|
fn from(value: PosMapOrig<T>) -> Self {
|
||||||
let mut new = Self::new();
|
let mut new = Self::new();
|
||||||
for i in 0..Board::BOARD_SIZE {
|
for i in 0..Board::SIZE {
|
||||||
for j in 0..Board::BOARD_SIZE {
|
for j in 0..Board::SIZE {
|
||||||
new.set((i, j).into(), value[i as usize][j as usize]);
|
new.set((i, j).into(), value[i as usize][j as usize]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -97,25 +97,25 @@ pub struct Board {
|
|||||||
impl fmt::Display for Board {
|
impl fmt::Display for Board {
|
||||||
#[allow(clippy::repeat_once)] // clippy gets mad about when PADDING == 1
|
#[allow(clippy::repeat_once)] // clippy gets mad about when PADDING == 1
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let horiz_sep_line = "-".repeat((Self::BOARD_SIZE * 2 + 1) as usize);
|
let horiz_sep_line = "-".repeat((Self::SIZE * 2 + 1) as usize);
|
||||||
|
|
||||||
// basically calculates the # of digits BOARD_SIZE needs
|
// basically calculates the # of digits BOARD_SIZE needs
|
||||||
const PADDING: usize = (Board::BOARD_SIZE - 1).ilog10() as usize + 1;
|
const PADDING: usize = (Board::SIZE - 1).ilog10() as usize + 1;
|
||||||
|
|
||||||
let space_padding = " ".repeat(PADDING);
|
let space_padding = " ".repeat(PADDING);
|
||||||
|
|
||||||
// Print numbers at top so the board can be read more easier
|
// Print numbers at top so the board can be read more easier
|
||||||
write!(f, "{} ", space_padding)?;
|
write!(f, "{} ", space_padding)?;
|
||||||
for j in (0..Self::BOARD_SIZE).rev() {
|
for j in (0..Self::SIZE).rev() {
|
||||||
write!(f, "{:0PADDING$} ", j)?;
|
write!(f, "{:0PADDING$} ", j)?;
|
||||||
}
|
}
|
||||||
writeln!(f)?;
|
writeln!(f)?;
|
||||||
|
|
||||||
for i in (0..Self::BOARD_SIZE).rev() {
|
for i in (0..Self::SIZE).rev() {
|
||||||
writeln!(f, "{}{}", space_padding, horiz_sep_line)?;
|
writeln!(f, "{}{}", space_padding, horiz_sep_line)?;
|
||||||
|
|
||||||
write!(f, "{:0PADDING$}|", i)?;
|
write!(f, "{:0PADDING$}|", i)?;
|
||||||
for j in (0..Self::BOARD_SIZE).rev() {
|
for j in (0..Self::SIZE).rev() {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"{}|",
|
"{}|",
|
||||||
@ -152,10 +152,10 @@ impl fmt::Debug for Board {
|
|||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
/// Width of the board
|
/// Width of the board
|
||||||
pub const BOARD_SIZE: CoordAxis = 8;
|
pub const SIZE: CoordAxis = 8;
|
||||||
|
|
||||||
/// Area of the board
|
/// Area of the board
|
||||||
pub const BOARD_AREA: CoordPair = CoordPair::area(Self::BOARD_SIZE);
|
pub const AREA: CoordPair = CoordPair::area(Self::SIZE);
|
||||||
|
|
||||||
/// Create a new empty board
|
/// Create a new empty board
|
||||||
#[allow(clippy::new_without_default)]
|
#[allow(clippy::new_without_default)]
|
||||||
@ -183,7 +183,7 @@ impl Board {
|
|||||||
|
|
||||||
/// Starting position
|
/// Starting position
|
||||||
pub const fn starting_pos(mut self) -> Self {
|
pub const fn starting_pos(mut self) -> Self {
|
||||||
let hf = Self::BOARD_SIZE / 2;
|
let hf = Self::SIZE / 2;
|
||||||
self.place_unchecked(CoordPair::from_axes(hf - 1, hf - 1), Piece::White);
|
self.place_unchecked(CoordPair::from_axes(hf - 1, hf - 1), Piece::White);
|
||||||
self.place_unchecked(CoordPair::from_axes(hf, hf - 1), Piece::Black);
|
self.place_unchecked(CoordPair::from_axes(hf, hf - 1), Piece::Black);
|
||||||
|
|
||||||
@ -194,7 +194,7 @@ impl Board {
|
|||||||
|
|
||||||
/// Provides an iterator of all possible positions on the board
|
/// Provides an iterator of all possible positions on the board
|
||||||
pub fn all_positions() -> impl Iterator<Item = CoordPair> {
|
pub fn all_positions() -> impl Iterator<Item = CoordPair> {
|
||||||
(0..Self::BOARD_AREA.0).map(CoordPair)
|
(0..Self::AREA.0).map(CoordPair)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an iterator of all possible moves a `color` can make
|
/// Returns an iterator of all possible moves a `color` can make
|
||||||
|
|||||||
@ -9,7 +9,7 @@ pub type CoordAxis = u8;
|
|||||||
/// using u8 for this results in a ~3-6% perf increase
|
/// using u8 for this results in a ~3-6% perf increase
|
||||||
pub type CoordPairInner = u8;
|
pub type CoordPairInner = u8;
|
||||||
|
|
||||||
const_assert!(CoordPairInner::MAX as usize >= Board::BOARD_AREA.0 as usize);
|
const_assert!(CoordPairInner::MAX as usize >= Board::AREA.0 as usize);
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
|
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
|
||||||
pub struct CoordPair(pub CoordPairInner);
|
pub struct CoordPair(pub CoordPairInner);
|
||||||
@ -17,12 +17,12 @@ pub struct CoordPair(pub CoordPairInner);
|
|||||||
impl CoordPair {
|
impl CoordPair {
|
||||||
/// Convert a row and column to an index in the board
|
/// Convert a row and column to an index in the board
|
||||||
pub const fn from_axes(row: CoordAxis, col: CoordAxis) -> Self {
|
pub const fn from_axes(row: CoordAxis, col: CoordAxis) -> Self {
|
||||||
Self(row * Board::BOARD_SIZE + col)
|
Self(row * Board::SIZE + col)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::wrong_self_convention)]
|
#[allow(clippy::wrong_self_convention)]
|
||||||
fn into_indexes(&self) -> (CoordAxis, CoordAxis) {
|
fn into_indexes(&self) -> (CoordAxis, CoordAxis) {
|
||||||
self.0.div_mod_floor(&Board::BOARD_SIZE)
|
self.0.div_mod_floor(&Board::SIZE)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn area(pos: CoordAxis) -> Self {
|
pub const fn area(pos: CoordAxis) -> Self {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user