abstract away more coordinates
This commit is contained in:
@@ -2,15 +2,12 @@ use super::{
|
||||
bitboard::BitBoard,
|
||||
chains::{gen_adj_lookup, ChainCollection, PosMap},
|
||||
piece::Piece,
|
||||
CoordAxis, CoordPair,
|
||||
};
|
||||
use const_fn::const_fn;
|
||||
use lazy_static::lazy_static;
|
||||
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;
|
||||
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
||||
pub enum Winner {
|
||||
Player(Piece),
|
||||
@@ -58,7 +55,10 @@ impl fmt::Display for Board {
|
||||
write!(
|
||||
f,
|
||||
"{}|",
|
||||
self.get(i, j).as_ref().map(Piece::symbol).unwrap_or(' ')
|
||||
self.get((i, j).into())
|
||||
.as_ref()
|
||||
.map(Piece::symbol)
|
||||
.unwrap_or(' ')
|
||||
)?;
|
||||
}
|
||||
writeln!(f)?;
|
||||
@@ -86,10 +86,10 @@ impl fmt::Debug for Board {
|
||||
}
|
||||
|
||||
impl Board {
|
||||
pub const BOARD_SIZE: Coord = 8;
|
||||
pub const BOARD_SIZE: CoordAxis = 8;
|
||||
|
||||
/// Area of the board
|
||||
pub const BOARD_AREA: Coord = Self::BOARD_SIZE.pow(2);
|
||||
pub const BOARD_AREA: CoordAxis = Self::BOARD_SIZE.pow(2);
|
||||
|
||||
/// Create a new empty board
|
||||
#[allow(clippy::new_without_default)]
|
||||
@@ -101,39 +101,39 @@ impl Board {
|
||||
}
|
||||
|
||||
/// Starting position
|
||||
#[const_fn(cfg(not(feature = "bitvec")))]
|
||||
pub const fn starting_pos(mut self) -> Self {
|
||||
pub fn starting_pos(mut self) -> Self {
|
||||
self.place_unchecked(
|
||||
(Self::BOARD_SIZE / 2) - 1,
|
||||
(Self::BOARD_SIZE / 2) - 1,
|
||||
((Self::BOARD_SIZE / 2) - 1, (Self::BOARD_SIZE / 2) - 1).into(),
|
||||
Piece::White,
|
||||
);
|
||||
self.place_unchecked(
|
||||
Self::BOARD_SIZE / 2,
|
||||
(Self::BOARD_SIZE / 2) - 1,
|
||||
(Self::BOARD_SIZE / 2, (Self::BOARD_SIZE / 2) - 1).into(),
|
||||
Piece::Black,
|
||||
);
|
||||
self.place_unchecked(
|
||||
(Self::BOARD_SIZE / 2) - 1,
|
||||
Self::BOARD_SIZE / 2,
|
||||
((Self::BOARD_SIZE / 2) - 1, Self::BOARD_SIZE / 2).into(),
|
||||
Piece::Black,
|
||||
);
|
||||
self.place_unchecked(Self::BOARD_SIZE / 2, Self::BOARD_SIZE / 2, Piece::White);
|
||||
self.place_unchecked(
|
||||
(Self::BOARD_SIZE / 2, Self::BOARD_SIZE / 2).into(),
|
||||
Piece::White,
|
||||
);
|
||||
self
|
||||
}
|
||||
|
||||
/// Provides an iterator of all possible positions on the board
|
||||
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)))
|
||||
pub fn all_positions() -> impl Iterator<Item = CoordPair> {
|
||||
(0..Self::BOARD_SIZE).flat_map(|i| {
|
||||
(0..Self::BOARD_SIZE).map(move |j| (i as CoordAxis, j as CoordAxis).into())
|
||||
})
|
||||
}
|
||||
|
||||
/// Returns an iterator of all possible moves a `color` can make
|
||||
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 possible_moves(&self, color: Piece) -> impl Iterator<Item = CoordPair> + use<'_> {
|
||||
Self::all_positions().filter(move |&coord| self.would_prop(coord, color))
|
||||
}
|
||||
|
||||
pub fn sides() -> impl Iterator<Item = (Coord, Coord)> {
|
||||
pub fn sides() -> impl Iterator<Item = (CoordAxis, CoordAxis)> {
|
||||
(0..Self::BOARD_SIZE)
|
||||
.map(|i| (i, Self::BOARD_SIZE - 1))
|
||||
.chain((0..Self::BOARD_SIZE).map(|i| (i, 0)))
|
||||
@@ -141,7 +141,7 @@ impl Board {
|
||||
.chain((0..Self::BOARD_SIZE).map(|j| (0, j)))
|
||||
}
|
||||
|
||||
pub fn corners() -> impl Iterator<Item = (Coord, Coord)> {
|
||||
pub fn corners() -> impl Iterator<Item = (CoordAxis, CoordAxis)> {
|
||||
[0, Self::BOARD_SIZE - 1]
|
||||
.into_iter()
|
||||
.flat_map(|i| [0, Self::BOARD_SIZE - 1].into_iter().map(move |j| (i, j)))
|
||||
@@ -164,16 +164,16 @@ impl Board {
|
||||
}
|
||||
|
||||
#[const_fn(cfg(not(feature = "bitvec")))]
|
||||
pub const fn get_piece(&self, i: Coord, j: Coord, color: Piece) -> bool {
|
||||
self.board(color).get(i, j)
|
||||
pub const fn get_piece(&self, coord: CoordPair, color: Piece) -> bool {
|
||||
self.board(color).get(coord)
|
||||
}
|
||||
|
||||
/// Returns the color of a place on the [`Board`] at a position
|
||||
#[const_fn(cfg(not(feature = "bitvec")))]
|
||||
pub const fn get(&self, i: Coord, j: Coord) -> Option<Piece> {
|
||||
if self.get_piece(i, j, Piece::White) {
|
||||
pub const fn get(&self, coord: CoordPair) -> Option<Piece> {
|
||||
if self.get_piece(coord, Piece::White) {
|
||||
Some(Piece::White)
|
||||
} else if self.get_piece(i, j, Piece::Black) {
|
||||
} else if self.get_piece(coord, Piece::Black) {
|
||||
Some(Piece::Black)
|
||||
} else {
|
||||
None
|
||||
@@ -182,42 +182,42 @@ impl Board {
|
||||
|
||||
/// Place a piece without checking for propegation of validity
|
||||
#[const_fn(cfg(not(feature = "bitvec")))]
|
||||
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 place_unchecked(&mut self, coord: CoordPair, piece: Piece) {
|
||||
self.board_mut(piece).set(coord, true);
|
||||
self.board_mut(piece.flip()).set(coord, false);
|
||||
}
|
||||
|
||||
#[const_fn(cfg(not(feature = "bitvec")))]
|
||||
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);
|
||||
const fn delete(&mut self, coord: CoordPair) {
|
||||
self.board_mut(Piece::White).set(coord, false);
|
||||
self.board_mut(Piece::Black).set(coord, 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: Coord, j: Coord, piece: Piece) -> Result<Self, &'static str> {
|
||||
pub fn what_if(&self, coord: CoordPair, piece: Piece) -> Result<Self, &'static str> {
|
||||
// extract check here to avoid copy
|
||||
if self.get(i, j).is_some() {
|
||||
if self.get(coord).is_some() {
|
||||
return Err("position is occupied");
|
||||
}
|
||||
|
||||
let mut self_copy = *self;
|
||||
self_copy.place(i, j, piece).map(|_| self_copy)
|
||||
self_copy.place(coord, piece).map(|_| self_copy)
|
||||
}
|
||||
|
||||
/// Returns a bool which represents whether or not a move would propegate and be valid
|
||||
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 would_prop(&self, coord: CoordPair, piece: Piece) -> bool {
|
||||
self.get(coord).is_none() && self.propegate_from_dry(coord, piece).next().is_some()
|
||||
}
|
||||
|
||||
pub fn place(&mut self, i: Coord, j: Coord, piece: Piece) -> Result<(), &'static str> {
|
||||
if self.get(i, j).is_some() {
|
||||
pub fn place(&mut self, coord: CoordPair, piece: Piece) -> Result<(), &'static str> {
|
||||
if self.get(coord).is_some() {
|
||||
return Err("position is occupied");
|
||||
}
|
||||
|
||||
self.place_unchecked(i, j, piece);
|
||||
if self.propegate_from(i, j) == 0 {
|
||||
self.delete(i, j);
|
||||
self.place_unchecked(coord, piece);
|
||||
if self.propegate_from(coord) == 0 {
|
||||
self.delete(coord);
|
||||
Err("move would not propegate")
|
||||
} else {
|
||||
Ok(())
|
||||
@@ -225,8 +225,8 @@ impl Board {
|
||||
}
|
||||
|
||||
/// Propegate the board and captures starting from a specific position
|
||||
fn propegate_from(&mut self, i: Coord, j: Coord) -> usize {
|
||||
let Some(starting_color) = self.get(i, j) else {
|
||||
fn propegate_from(&mut self, coord: CoordPair) -> usize {
|
||||
let Some(starting_color) = self.get(coord) else {
|
||||
return 0;
|
||||
};
|
||||
|
||||
@@ -235,12 +235,12 @@ impl Board {
|
||||
// SAFETY! `propegate_from_dry` should not have overlapping chains
|
||||
// if overlapping chains were to exist, `self.place_unchecked` could collide with `self.get`
|
||||
// I now have a check in `ADJ_LOOKUP` on creation
|
||||
(*(self as *const Self)).propegate_from_dry(i, j, starting_color)
|
||||
(*(self as *const Self)).propegate_from_dry(coord, starting_color)
|
||||
};
|
||||
|
||||
let mut count = 0;
|
||||
for &(i, j) in iterator {
|
||||
self.place_unchecked(i, j, starting_color);
|
||||
for &coord in iterator {
|
||||
self.place_unchecked(coord, starting_color);
|
||||
count += 1;
|
||||
}
|
||||
|
||||
@@ -252,16 +252,15 @@ impl Board {
|
||||
/// [`Board::place`] or [`Board::place_and_prop_unchecked`]
|
||||
fn propegate_from_dry(
|
||||
&self,
|
||||
i: Coord,
|
||||
j: Coord,
|
||||
coords: CoordPair,
|
||||
starting_color: Piece,
|
||||
) -> impl Iterator<Item = &(Coord, Coord)> + use<'_> {
|
||||
) -> impl Iterator<Item = &CoordPair> + use<'_> {
|
||||
ADJ_LOOKUP
|
||||
.get(i, j)
|
||||
.get(coords)
|
||||
.iter()
|
||||
.flat_map(move |chain| {
|
||||
for (idx, &(new_i, new_j)) in chain.into_iter().enumerate() {
|
||||
let piece = self.get(new_i, new_j)?;
|
||||
for (idx, &coord) in chain.into_iter().enumerate() {
|
||||
let piece = self.get(coord)?;
|
||||
if piece == starting_color {
|
||||
return chain.get(..idx);
|
||||
}
|
||||
@@ -310,34 +309,34 @@ mod test {
|
||||
#[test]
|
||||
fn place_and_get() {
|
||||
let mut board = Board::new();
|
||||
assert_eq!(board.get(0, 0), None);
|
||||
board.place_unchecked(0, 0, Piece::Black);
|
||||
assert_eq!(board.get(0, 0), Some(Piece::Black));
|
||||
assert_eq!(board.get((0, 0).into()), None);
|
||||
board.place_unchecked((0, 0).into(), Piece::Black);
|
||||
assert_eq!(board.get((0, 0).into()), Some(Piece::Black));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn place_and_capture_simple() {
|
||||
let mut board = Board::new();
|
||||
board.place_unchecked(0, 0, Piece::Black);
|
||||
board.place_unchecked(0, 1, Piece::White);
|
||||
board.place_unchecked((0, 0).into(), Piece::Black);
|
||||
board.place_unchecked((0, 1).into(), Piece::White);
|
||||
|
||||
assert_eq!(board.place(0, 2, Piece::Black), Ok(()));
|
||||
assert_eq!(board.place((0, 2).into(), Piece::Black), Ok(()));
|
||||
|
||||
assert_eq!(board.get(0, 1), Some(Piece::Black));
|
||||
assert_eq!(board.get((0, 1).into()), Some(Piece::Black));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn failed_capture() {
|
||||
let mut board = Board::new();
|
||||
|
||||
board.place_unchecked(0, 0, Piece::Black);
|
||||
board.place_unchecked(0, 2, Piece::White);
|
||||
board.place_unchecked((0, 0).into(), Piece::Black);
|
||||
board.place_unchecked((0, 2).into(), Piece::White);
|
||||
|
||||
// should fail
|
||||
assert_ne!(board.place(0, 3, Piece::Black), Ok(()));
|
||||
assert_ne!(board.place((0, 3).into(), Piece::Black), Ok(()));
|
||||
|
||||
assert_eq!(
|
||||
board.get(0, 1),
|
||||
board.get((0, 1).into()),
|
||||
None,
|
||||
"(0, 1) was overridden even though it's an empty space"
|
||||
);
|
||||
@@ -347,17 +346,17 @@ mod test {
|
||||
fn long_capture_horiz() {
|
||||
let mut board = Board::new();
|
||||
|
||||
board.place_unchecked(0, 0, Piece::Black);
|
||||
board.place_unchecked((0, 0).into(), Piece::Black);
|
||||
|
||||
for j in 1..=6 {
|
||||
board.place_unchecked(0, j, Piece::White);
|
||||
board.place_unchecked((0, j).into(), Piece::White);
|
||||
}
|
||||
|
||||
assert_eq!(board.place(0, 7, Piece::Black), Ok(()));
|
||||
assert_eq!(board.place((0, 7).into(), Piece::Black), Ok(()));
|
||||
|
||||
for j in 2..=6 {
|
||||
assert_eq!(
|
||||
board.get(0, j),
|
||||
board.get((0, j).into()),
|
||||
Some(Piece::Black),
|
||||
"should be black at: ({}, {})",
|
||||
0,
|
||||
@@ -370,17 +369,17 @@ mod test {
|
||||
fn long_capture_vert() {
|
||||
let mut board = Board::new();
|
||||
|
||||
board.place_unchecked(0, 0, Piece::Black);
|
||||
board.place_unchecked((0, 0).into(), Piece::Black);
|
||||
|
||||
for i in 1..=6 {
|
||||
board.place_unchecked(i, 0, Piece::White);
|
||||
board.place_unchecked((i, 0).into(), Piece::White);
|
||||
}
|
||||
|
||||
assert_eq!(board.place(7, 0, Piece::Black), Ok(()));
|
||||
assert_eq!(board.place((7, 0).into(), Piece::Black), Ok(()));
|
||||
|
||||
for i in 2..=6 {
|
||||
assert_eq!(
|
||||
board.get(i, 0),
|
||||
board.get((i, 0).into()),
|
||||
Some(Piece::Black),
|
||||
"should be black at: ({}, {})",
|
||||
i,
|
||||
@@ -395,12 +394,12 @@ mod test {
|
||||
let mut board = Board::new();
|
||||
|
||||
// Black pieces at (2, 2) and (0, 0)
|
||||
board.place_unchecked(1, 1, Piece::White); // to be captured
|
||||
board.place_unchecked(2, 2, Piece::Black);
|
||||
assert_eq!(board.place(0, 0, Piece::Black), Ok(()));
|
||||
board.place_unchecked((1, 1).into(), Piece::White); // to be captured
|
||||
board.place_unchecked((2, 2).into(), Piece::Black);
|
||||
assert_eq!(board.place((0, 0).into(), Piece::Black), Ok(()));
|
||||
|
||||
// Capture white piece at (1,1)
|
||||
assert_eq!(board.get(1, 1), Some(Piece::Black), "\n{}", board);
|
||||
assert_eq!(board.get((1, 1).into()), Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test corner capture from top-right corner
|
||||
@@ -409,12 +408,12 @@ mod test {
|
||||
let mut board = Board::new();
|
||||
|
||||
// Black pieces at (0, 7) and (2, 5)
|
||||
board.place_unchecked(0, 7, Piece::Black);
|
||||
board.place_unchecked(1, 6, Piece::White); // to be captured
|
||||
assert_eq!(board.place(2, 5, Piece::Black), Ok(()));
|
||||
board.place_unchecked((0, 7).into(), Piece::Black);
|
||||
board.place_unchecked((1, 6).into(), Piece::White); // to be captured
|
||||
assert_eq!(board.place((2, 5).into(), Piece::Black), Ok(()));
|
||||
|
||||
// Capture white piece at (1, 6)
|
||||
assert_eq!(board.get(1, 6), Some(Piece::Black), "\n{}", board);
|
||||
assert_eq!(board.get((1, 6).into()), Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test corner capture from bottom-left corner
|
||||
@@ -423,12 +422,12 @@ mod test {
|
||||
let mut board = Board::new();
|
||||
|
||||
// Black pieces at (7, 0) and (5, 2)
|
||||
board.place_unchecked(7, 0, Piece::Black);
|
||||
board.place_unchecked(6, 1, Piece::White); // to be captured
|
||||
assert_eq!(board.place(5, 2, Piece::Black), Ok(()));
|
||||
board.place_unchecked((7, 0).into(), Piece::Black);
|
||||
board.place_unchecked((6, 1).into(), Piece::White); // to be captured
|
||||
assert_eq!(board.place((5, 2).into(), Piece::Black), Ok(()));
|
||||
|
||||
// Capture white piece at (6, 1)
|
||||
assert_eq!(board.get(6, 1), Some(Piece::Black), "\n{}", board);
|
||||
assert_eq!(board.get((6, 1).into()), Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test corner capture from bottom-right corner
|
||||
@@ -437,12 +436,12 @@ mod test {
|
||||
let mut board = Board::new();
|
||||
|
||||
// Black pieces at (7, 7) and (5, 5)
|
||||
board.place_unchecked(7, 7, Piece::Black);
|
||||
board.place_unchecked(6, 6, Piece::White); // to be captured
|
||||
assert_eq!(board.place(5, 5, Piece::Black), Ok(()));
|
||||
board.place_unchecked((7, 7).into(), Piece::Black);
|
||||
board.place_unchecked((6, 6).into(), Piece::White); // to be captured
|
||||
assert_eq!(board.place((5, 5).into(), Piece::Black), Ok(()));
|
||||
|
||||
// Capture white piece at (6, 6)
|
||||
assert_eq!(board.get(6, 6), Some(Piece::Black), "\n{}", board);
|
||||
assert_eq!(board.get((6, 6).into()), Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test capture from top-left corner (horizontal)
|
||||
@@ -451,11 +450,11 @@ mod test {
|
||||
let mut board = Board::new();
|
||||
|
||||
// Create a scenario where a capture should happen horizontally from (0, 0)
|
||||
board.place_unchecked(0, 0, Piece::Black);
|
||||
board.place_unchecked(0, 1, Piece::White); // to be captured
|
||||
assert_eq!(board.place(0, 2, Piece::Black), Ok(()));
|
||||
board.place_unchecked((0, 0).into(), Piece::Black);
|
||||
board.place_unchecked((0, 1).into(), Piece::White); // to be captured
|
||||
assert_eq!(board.place((0, 2).into(), Piece::Black), Ok(()));
|
||||
|
||||
assert_eq!(board.get(0, 1), Some(Piece::Black), "\n{}", board);
|
||||
assert_eq!(board.get((0, 1).into()), Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test capture from top-right corner (horizontal)
|
||||
@@ -464,11 +463,11 @@ mod test {
|
||||
let mut board = Board::new();
|
||||
|
||||
// Create a scenario where a capture should happen horizontally from (0, 7)
|
||||
board.place_unchecked(0, 7, Piece::Black);
|
||||
board.place_unchecked(0, 6, Piece::White); // to be captured
|
||||
assert_eq!(board.place(0, 5, Piece::Black), Ok(()));
|
||||
board.place_unchecked((0, 7).into(), Piece::Black);
|
||||
board.place_unchecked((0, 6).into(), Piece::White); // to be captured
|
||||
assert_eq!(board.place((0, 5).into(), Piece::Black), Ok(()));
|
||||
|
||||
assert_eq!(board.get(0, 6), Some(Piece::Black), "\n{}", board);
|
||||
assert_eq!(board.get((0, 6).into()), Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test capture from top-left corner (vertical)
|
||||
@@ -477,11 +476,11 @@ mod test {
|
||||
let mut board = Board::new();
|
||||
|
||||
// Create a scenario where a capture should happen vertically from (0, 0)
|
||||
board.place_unchecked(0, 0, Piece::Black);
|
||||
board.place_unchecked(1, 0, Piece::White); // to be captured
|
||||
assert_eq!(board.place(2, 0, Piece::Black), Ok(()));
|
||||
board.place_unchecked((0, 0).into(), Piece::Black);
|
||||
board.place_unchecked((1, 0).into(), Piece::White); // to be captured
|
||||
assert_eq!(board.place((2, 0).into(), Piece::Black), Ok(()));
|
||||
|
||||
assert_eq!(board.get(1, 0), Some(Piece::Black), "\n{}", board);
|
||||
assert_eq!(board.get((1, 0).into()), Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
|
||||
// Test capture from bottom-left corner (vertical)
|
||||
@@ -490,10 +489,10 @@ mod test {
|
||||
let mut board = Board::new();
|
||||
|
||||
// Create a scenario where a capture should happen vertically from (7, 0)
|
||||
board.place_unchecked(7, 0, Piece::Black);
|
||||
board.place_unchecked(6, 0, Piece::White); // to be captured
|
||||
assert_eq!(board.place(5, 0, Piece::Black), Ok(()));
|
||||
board.place_unchecked((7, 0).into(), Piece::Black);
|
||||
board.place_unchecked((6, 0).into(), Piece::White); // to be captured
|
||||
assert_eq!(board.place((5, 0).into(), Piece::Black), Ok(()));
|
||||
|
||||
assert_eq!(board.get(6, 0), Some(Piece::Black), "\n{}", board);
|
||||
assert_eq!(board.get((6, 0).into()), Some(Piece::Black), "\n{}", board);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user