place weighting
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::repr::board::{BOARD_AREA, BOARD_SIZE};
|
||||
use crate::repr::board::Board;
|
||||
use const_fn::const_fn;
|
||||
use static_assertions::const_assert;
|
||||
|
||||
@@ -15,7 +15,7 @@ use bitvec::prelude::*;
|
||||
type BBBaseType = u64;
|
||||
|
||||
#[cfg(feature = "bitvec")]
|
||||
pub type BitBoardInner = BitArr!(for BOARD_AREA, in BBBaseType, Lsb0);
|
||||
pub type BitBoardInner = BitArr!(for Board::BOARD_AREA, in BBBaseType, Lsb0);
|
||||
|
||||
#[cfg(not(feature = "bitvec"))]
|
||||
pub type BitBoardInner = u64;
|
||||
@@ -24,7 +24,7 @@ pub type BitBoardInner = u64;
|
||||
pub struct BitBoard(BitBoardInner);
|
||||
|
||||
// BitBoard should be big enough to fit all points on the board
|
||||
const_assert!(std::mem::size_of::<BitBoard>() * 8 >= BOARD_AREA);
|
||||
const_assert!(std::mem::size_of::<BitBoard>() * 8 >= Board::BOARD_AREA);
|
||||
|
||||
impl Default for BitBoard {
|
||||
fn default() -> Self {
|
||||
@@ -35,7 +35,7 @@ impl Default for BitBoard {
|
||||
impl BitBoard {
|
||||
#[cfg(feature = "bitvec")]
|
||||
pub const fn new() -> Self {
|
||||
Self(bitarr!(BBBaseType, Lsb0; 0; BOARD_AREA))
|
||||
Self(bitarr!(BBBaseType, Lsb0; 0; Board::BOARD_AREA))
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "bitvec"))]
|
||||
@@ -57,7 +57,7 @@ impl BitBoard {
|
||||
}
|
||||
|
||||
const fn get_index(row: usize, col: usize) -> usize {
|
||||
row * BOARD_SIZE + col
|
||||
row * Board::BOARD_SIZE + col
|
||||
}
|
||||
|
||||
#[cfg(feature = "bitvec")]
|
||||
@@ -84,8 +84,8 @@ mod test {
|
||||
#[test]
|
||||
fn set_and_get() {
|
||||
let mut b = BitBoard::new();
|
||||
for i in 0..BOARD_SIZE {
|
||||
for j in 0..BOARD_SIZE {
|
||||
for i in 0..Board::BOARD_SIZE {
|
||||
for j in 0..Board::BOARD_SIZE {
|
||||
assert!(
|
||||
!b.get(i, j),
|
||||
"A just-initalized BitBoard should be completely empty"
|
||||
|
||||
@@ -8,34 +8,33 @@ use const_fn::const_fn;
|
||||
use lazy_static::lazy_static;
|
||||
use std::{cmp::Ordering, collections::HashSet, fmt};
|
||||
|
||||
/// Size of each dim of the board
|
||||
pub const BOARD_SIZE: usize = 8;
|
||||
|
||||
/// Area of the board
|
||||
#[allow(dead_code)]
|
||||
pub const BOARD_AREA: usize = BOARD_SIZE * BOARD_SIZE;
|
||||
|
||||
const BOARD_SIZE_N1: usize = BOARD_SIZE - 1;
|
||||
|
||||
/// A chain of positions across the board
|
||||
type Chain = ArrayVec<(usize, usize), BOARD_SIZE_N1>;
|
||||
type Chain = ArrayVec<(usize, usize), { Board::BOARD_SIZE - 1 }>;
|
||||
|
||||
/// A collection of chains (up vert, down vert, left horiz, right horiz, diagonals....)
|
||||
type ChainCollection = ArrayVec<Chain, 8>;
|
||||
|
||||
const BOARD_AREA: usize = Board::BOARD_AREA;
|
||||
|
||||
/// Map of all points on the board against some type T
|
||||
/// Used to index like so: example[i][j]
|
||||
/// with each coordinate
|
||||
pub struct PosMap<T: Default>(ArrayVec<T, BOARD_AREA>);
|
||||
|
||||
impl<T: Default> Default for PosMap<T> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Default> PosMap<T> {
|
||||
const fn index(row: usize, col: usize) -> usize {
|
||||
row * BOARD_SIZE + col
|
||||
row * Board::BOARD_SIZE + col
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self(ArrayVec::from_iter(
|
||||
(0..BOARD_AREA).map(|_| Default::default()),
|
||||
(0..Board::BOARD_AREA).map(|_| Default::default()),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -43,7 +42,7 @@ impl<T: Default> PosMap<T> {
|
||||
let index = Self::index(row, col);
|
||||
|
||||
debug_assert!(
|
||||
BOARD_AREA + 1 >= index,
|
||||
Board::BOARD_AREA + 1 >= index,
|
||||
"index out of range, was: {}",
|
||||
index
|
||||
);
|
||||
@@ -54,7 +53,7 @@ impl<T: Default> PosMap<T> {
|
||||
pub fn set(&mut self, row: usize, col: usize, value: T) {
|
||||
let index = Self::index(row, col);
|
||||
debug_assert!(
|
||||
BOARD_AREA + 1 >= index,
|
||||
Board::BOARD_AREA + 1 >= index,
|
||||
"index out of range, was: {}",
|
||||
index
|
||||
);
|
||||
@@ -69,8 +68,8 @@ fn gen_adj_lookup() -> PosMap<ChainCollection> {
|
||||
Board::all_positions()
|
||||
.map(|(i, j)| {
|
||||
let (i_chain, j_chain) = (
|
||||
split_from(0..=BOARD_SIZE - 1, i),
|
||||
split_from(0..=BOARD_SIZE - 1, j),
|
||||
split_from(0..=Board::BOARD_SIZE - 1, i),
|
||||
split_from(0..=Board::BOARD_SIZE - 1, j),
|
||||
);
|
||||
|
||||
let chains: ChainCollection = ArrayVec::from_iter(
|
||||
@@ -95,7 +94,7 @@ fn gen_adj_lookup() -> PosMap<ChainCollection> {
|
||||
.iter()
|
||||
.flatten()
|
||||
.flat_map(|(i, j)| [i, j]) // flatten to just numbers
|
||||
.all(|x| (0..BOARD_SIZE).contains(x)),
|
||||
.all(|x| (0..Board::BOARD_SIZE).contains(x)),
|
||||
"chains go out-of-bounds"
|
||||
);
|
||||
|
||||
@@ -138,25 +137,25 @@ pub struct Board {
|
||||
impl fmt::Display for Board {
|
||||
#[allow(clippy::repeat_once)] // clippy gets mad about when PADDING == 1
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let horiz_sep_line = "-".repeat(BOARD_SIZE * 2 + 1);
|
||||
let horiz_sep_line = "-".repeat(Self::BOARD_SIZE * 2 + 1);
|
||||
|
||||
// basically calculates the # of digits BOARD_SIZE needs
|
||||
const PADDING: usize = (BOARD_SIZE - 1).ilog10() as usize + 1;
|
||||
const PADDING: usize = (Board::BOARD_SIZE - 1).ilog10() as usize + 1;
|
||||
|
||||
let space_padding = " ".repeat(PADDING);
|
||||
|
||||
// Print numbers at top so the board can be read more easier
|
||||
write!(f, "{} ", space_padding)?;
|
||||
for j in (0..BOARD_SIZE).rev() {
|
||||
for j in (0..Self::BOARD_SIZE).rev() {
|
||||
write!(f, "{:0PADDING$} ", j)?;
|
||||
}
|
||||
writeln!(f)?;
|
||||
|
||||
for i in (0..BOARD_SIZE).rev() {
|
||||
for i in (0..Self::BOARD_SIZE).rev() {
|
||||
writeln!(f, "{}{}", space_padding, horiz_sep_line)?;
|
||||
|
||||
write!(f, "{:0PADDING$}|", i)?;
|
||||
for j in (0..BOARD_SIZE).rev() {
|
||||
for j in (0..Self::BOARD_SIZE).rev() {
|
||||
write!(
|
||||
f,
|
||||
"{}|",
|
||||
@@ -194,6 +193,11 @@ impl Default for Board {
|
||||
}
|
||||
|
||||
impl Board {
|
||||
pub const BOARD_SIZE: usize = 8;
|
||||
|
||||
/// Area of the board
|
||||
pub const BOARD_AREA: usize = Self::BOARD_SIZE.pow(2);
|
||||
|
||||
/// Create a new empty board
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
@@ -205,16 +209,28 @@ impl Board {
|
||||
/// Starting position
|
||||
#[const_fn(cfg(not(feature = "bitvec")))]
|
||||
pub const fn starting_pos(mut self) -> Self {
|
||||
self.place_unchecked((BOARD_SIZE / 2) - 1, (BOARD_SIZE / 2) - 1, Piece::White);
|
||||
self.place_unchecked(BOARD_SIZE / 2, (BOARD_SIZE / 2) - 1, Piece::Black);
|
||||
self.place_unchecked((BOARD_SIZE / 2) - 1, BOARD_SIZE / 2, Piece::Black);
|
||||
self.place_unchecked(BOARD_SIZE / 2, BOARD_SIZE / 2, Piece::White);
|
||||
self.place_unchecked(
|
||||
(Self::BOARD_SIZE / 2) - 1,
|
||||
(Self::BOARD_SIZE / 2) - 1,
|
||||
Piece::White,
|
||||
);
|
||||
self.place_unchecked(
|
||||
Self::BOARD_SIZE / 2,
|
||||
(Self::BOARD_SIZE / 2) - 1,
|
||||
Piece::Black,
|
||||
);
|
||||
self.place_unchecked(
|
||||
(Self::BOARD_SIZE / 2) - 1,
|
||||
Self::BOARD_SIZE / 2,
|
||||
Piece::Black,
|
||||
);
|
||||
self.place_unchecked(Self::BOARD_SIZE / 2, Self::BOARD_SIZE / 2, Piece::White);
|
||||
self
|
||||
}
|
||||
|
||||
/// Provides an iterator of all possible positions on the board
|
||||
pub fn all_positions() -> impl Iterator<Item = (usize, usize)> {
|
||||
(0..BOARD_SIZE).flat_map(|i| (0..BOARD_SIZE).map(move |j| (i, j)))
|
||||
(0..Self::BOARD_SIZE).flat_map(|i| (0..Self::BOARD_SIZE).map(move |j| (i, j)))
|
||||
}
|
||||
|
||||
/// Returns an iterator of all possible moves a `color` can make
|
||||
@@ -223,11 +239,17 @@ impl Board {
|
||||
}
|
||||
|
||||
pub fn sides() -> impl Iterator<Item = (usize, usize)> {
|
||||
(0..BOARD_SIZE)
|
||||
.map(|i| (i, BOARD_SIZE - 1))
|
||||
.chain((0..BOARD_SIZE).map(|i| (i, 0)))
|
||||
.chain((0..BOARD_SIZE).map(|j| (BOARD_SIZE - 1, j)))
|
||||
.chain((0..BOARD_SIZE).map(|j| (0, j)))
|
||||
(0..Self::BOARD_SIZE)
|
||||
.map(|i| (i, Self::BOARD_SIZE - 1))
|
||||
.chain((0..Self::BOARD_SIZE).map(|i| (i, 0)))
|
||||
.chain((0..Self::BOARD_SIZE).map(|j| (Self::BOARD_SIZE - 1, j)))
|
||||
.chain((0..Self::BOARD_SIZE).map(|j| (0, j)))
|
||||
}
|
||||
|
||||
pub fn corners() -> impl Iterator<Item = (usize, usize)> {
|
||||
[0, Self::BOARD_SIZE - 1]
|
||||
.into_iter()
|
||||
.flat_map(|i| [0, Self::BOARD_SIZE - 1].into_iter().map(move |j| (i, j)))
|
||||
}
|
||||
|
||||
/// Get a reference to a backing [`BitBoard`]
|
||||
|
||||
Reference in New Issue
Block a user