switch coordinates to global 'Coord' type

This commit is contained in:
2025-02-26 23:45:58 -05:00
parent a10b762b39
commit 7ec8a66a4e
10 changed files with 63 additions and 50 deletions

View File

@@ -5,8 +5,15 @@ use super::{
};
use const_fn::const_fn;
use lazy_static::lazy_static;
use static_assertions::const_assert;
use std::{cmp::Ordering, fmt};
// PERF! having `Coord` be of type `u8` (instead of usize) increases
// performance by about 1-2% overall
pub type Coord = u8;
const_assert!(Board::BOARD_SIZE_RAW <= Coord::MAX as usize);
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum Winner {
Player(Piece),
@@ -32,7 +39,7 @@ 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(Self::BOARD_SIZE * 2 + 1);
let horiz_sep_line = "-".repeat((Self::BOARD_SIZE * 2 + 1) as usize);
// basically calculates the # of digits BOARD_SIZE needs
const PADDING: usize = (Board::BOARD_SIZE - 1).ilog10() as usize + 1;
@@ -82,10 +89,11 @@ impl fmt::Debug for Board {
}
impl Board {
pub const BOARD_SIZE: usize = 8;
const BOARD_SIZE_RAW: usize = 8;
pub const BOARD_SIZE: Coord = Self::BOARD_SIZE_RAW as Coord;
/// Area of the board
pub const BOARD_AREA: usize = Self::BOARD_SIZE.pow(2);
pub const BOARD_AREA: Coord = Self::BOARD_SIZE.pow(2);
/// Create a new empty board
#[allow(clippy::new_without_default)]
@@ -119,16 +127,17 @@ impl Board {
}
/// Provides an iterator of all possible positions on the board
pub fn all_positions() -> impl Iterator<Item = (usize, usize)> {
(0..Self::BOARD_SIZE).flat_map(|i| (0..Self::BOARD_SIZE).map(move |j| (i, j)))
pub fn all_positions() -> impl Iterator<Item = (Coord, Coord)> {
(0..Self::BOARD_SIZE)
.flat_map(|i| (0..Self::BOARD_SIZE).map(move |j| (i as Coord, j as Coord)))
}
/// Returns an iterator of all possible moves a `color` can make
pub fn possible_moves(&self, color: Piece) -> impl Iterator<Item = (usize, usize)> + use<'_> {
pub fn possible_moves(&self, color: Piece) -> impl Iterator<Item = (Coord, Coord)> + use<'_> {
Self::all_positions().filter(move |&(i, j)| self.would_prop(i, j, color))
}
pub fn sides() -> impl Iterator<Item = (usize, usize)> {
pub fn sides() -> impl Iterator<Item = (Coord, Coord)> {
(0..Self::BOARD_SIZE)
.map(|i| (i, Self::BOARD_SIZE - 1))
.chain((0..Self::BOARD_SIZE).map(|i| (i, 0)))
@@ -136,7 +145,7 @@ impl Board {
.chain((0..Self::BOARD_SIZE).map(|j| (0, j)))
}
pub fn corners() -> impl Iterator<Item = (usize, usize)> {
pub fn corners() -> impl Iterator<Item = (Coord, Coord)> {
[0, Self::BOARD_SIZE - 1]
.into_iter()
.flat_map(|i| [0, Self::BOARD_SIZE - 1].into_iter().map(move |j| (i, j)))
@@ -159,13 +168,13 @@ impl Board {
}
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn get_piece(&self, i: usize, j: usize, color: Piece) -> bool {
pub const fn get_piece(&self, i: Coord, j: Coord, color: Piece) -> bool {
self.board(color).get(i, j)
}
/// Returns the color of a place on the [`Board`] at a position
#[const_fn(cfg(not(feature = "bitvec")))]
pub const fn get(&self, i: usize, j: usize) -> Option<Piece> {
pub const fn get(&self, i: Coord, j: Coord) -> Option<Piece> {
if self.get_piece(i, j, Piece::White) {
Some(Piece::White)
} else if self.get_piece(i, j, Piece::Black) {
@@ -177,20 +186,20 @@ impl Board {
/// Place a piece without checking for propegation of validity
#[const_fn(cfg(not(feature = "bitvec")))]
const fn place_unchecked(&mut self, i: usize, j: usize, piece: Piece) {
const fn place_unchecked(&mut self, i: Coord, j: Coord, piece: Piece) {
self.board_mut(piece).set(i, j, true);
self.board_mut(piece.flip()).set(i, j, false);
}
#[const_fn(cfg(not(feature = "bitvec")))]
const fn delete(&mut self, i: usize, j: usize) {
const fn delete(&mut self, i: Coord, j: Coord) {
self.board_mut(Piece::White).set(i, j, false);
self.board_mut(Piece::Black).set(i, j, false);
}
/// Return a modified [`Board`] with the piece placed at a position
/// Returns None if the move was invalid
pub fn what_if(&self, i: usize, j: usize, piece: Piece) -> Result<Self, &'static str> {
pub fn what_if(&self, i: Coord, j: Coord, piece: Piece) -> Result<Self, &'static str> {
// extract check here to avoid copy
if self.get(i, j).is_some() {
return Err("position is occupied");
@@ -201,11 +210,11 @@ impl Board {
}
/// Returns a bool which represents whether or not a move would propegate and be valid
pub fn would_prop(&self, i: usize, j: usize, piece: Piece) -> bool {
pub fn would_prop(&self, i: Coord, j: Coord, piece: Piece) -> bool {
self.get(i, j).is_none() && self.propegate_from_dry(i, j, piece).next().is_some()
}
pub fn place(&mut self, i: usize, j: usize, piece: Piece) -> Result<(), &'static str> {
pub fn place(&mut self, i: Coord, j: Coord, piece: Piece) -> Result<(), &'static str> {
if self.get(i, j).is_some() {
return Err("position is occupied");
}
@@ -220,7 +229,7 @@ impl Board {
}
/// Propegate the board and captures starting from a specific position
fn propegate_from(&mut self, i: usize, j: usize) -> usize {
fn propegate_from(&mut self, i: Coord, j: Coord) -> usize {
let Some(starting_color) = self.get(i, j) else {
return 0;
};
@@ -247,10 +256,10 @@ impl Board {
/// [`Board::place`] or [`Board::place_and_prop_unchecked`]
fn propegate_from_dry(
&self,
i: usize,
j: usize,
i: Coord,
j: Coord,
starting_color: Piece,
) -> impl Iterator<Item = &(usize, usize)> + use<'_> {
) -> impl Iterator<Item = &(Coord, Coord)> + use<'_> {
ADJ_LOOKUP
.get(i, j)
.iter()