overhaul chain system
This made the code so fast, that the `board` benchmark became completely useless. So that benchmark was removed. Overall, we're looking at a futher 20% performance increase in `future_moves`
This commit is contained in:
@@ -1,12 +1,44 @@
|
||||
use super::{
|
||||
bitboard::BitBoard,
|
||||
chains::{gen_adj_lookup, ChainCollection, PosMap},
|
||||
piece::Piece,
|
||||
CoordAxis, CoordPair,
|
||||
};
|
||||
use super::{bitboard::BitBoard, piece::Piece, CoordAxis, CoordPair};
|
||||
use arrayvec::ArrayVec;
|
||||
use const_fn::const_fn;
|
||||
use rand::seq::IteratorRandom;
|
||||
use std::{cmp::Ordering, fmt, sync::LazyLock};
|
||||
use std::{cmp::Ordering, fmt};
|
||||
|
||||
/// 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::BOARD_AREA as usize }>);
|
||||
|
||||
impl<T: Default> PosMap<T> {
|
||||
#[allow(clippy::new_without_default)]
|
||||
pub fn new() -> Self {
|
||||
Self(ArrayVec::from_iter(
|
||||
(0..Board::BOARD_AREA).map(|_| Default::default()),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn get(&self, coords: CoordPair) -> &T {
|
||||
&self.0[coords.0 as usize]
|
||||
}
|
||||
|
||||
pub fn set(&mut self, coords: CoordPair, value: T) {
|
||||
self.0[coords.0 as usize] = value;
|
||||
}
|
||||
}
|
||||
|
||||
type PosMapOrig<T> = [[T; Board::BOARD_SIZE as usize]; Board::BOARD_SIZE as usize];
|
||||
|
||||
impl<T: Default + Copy> From<PosMapOrig<T>> for PosMap<T> {
|
||||
fn from(value: PosMapOrig<T>) -> Self {
|
||||
let mut new = Self::new();
|
||||
for i in 0..Board::BOARD_SIZE {
|
||||
for j in 0..Board::BOARD_SIZE {
|
||||
new.set((i, j).into(), value[i as usize][j as usize]);
|
||||
}
|
||||
}
|
||||
new
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
||||
pub enum Winner {
|
||||
@@ -15,9 +47,6 @@ pub enum Winner {
|
||||
None,
|
||||
}
|
||||
|
||||
/// Precompute all possible chains for each position on the board
|
||||
static ADJ_LOOKUP: LazyLock<PosMap<ChainCollection>> = LazyLock::new(gen_adj_lookup);
|
||||
|
||||
/// Repersents a Othello game board at a certain space
|
||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||
pub struct Board {
|
||||
@@ -205,7 +234,7 @@ impl Board {
|
||||
|
||||
/// Returns a bool which represents whether or not a move would propegate and be valid
|
||||
pub fn would_prop(&self, coord: CoordPair, piece: Piece) -> bool {
|
||||
self.get(coord).is_none() && self.propegate_from_dry(coord, piece).next().is_some()
|
||||
self.get(coord).is_none() && self.propegate_from_dry(coord, piece).count() > 0
|
||||
}
|
||||
|
||||
pub fn place(&mut self, coord: CoordPair, piece: Piece) -> Result<(), &'static str> {
|
||||
@@ -228,19 +257,12 @@ impl Board {
|
||||
return 0;
|
||||
};
|
||||
|
||||
// PERF! avoid clones and collections here using raw pointers
|
||||
let iterator = unsafe {
|
||||
// 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(coord, starting_color)
|
||||
};
|
||||
let flip_mask = self.propegate_from_dry(coord, starting_color);
|
||||
let count = flip_mask.count();
|
||||
|
||||
let mut count = 0;
|
||||
for &coord in iterator {
|
||||
self.place_unchecked(coord, starting_color);
|
||||
count += 1;
|
||||
}
|
||||
// Apply the flips
|
||||
*self.board_mut(starting_color) |= flip_mask;
|
||||
*self.board_mut(starting_color.flip()) &= !flip_mask;
|
||||
|
||||
count
|
||||
}
|
||||
@@ -248,24 +270,46 @@ impl Board {
|
||||
/// Propegate piece captures originating from (i, j)
|
||||
/// DO NOT USE THIS ALONE, this should be called as a part of
|
||||
/// [`Board::place`] or [`Board::place_and_prop_unchecked`]
|
||||
fn propegate_from_dry(
|
||||
&self,
|
||||
coords: CoordPair,
|
||||
starting_color: Piece,
|
||||
) -> impl Iterator<Item = &CoordPair> + use<'_> {
|
||||
ADJ_LOOKUP
|
||||
.get(coords)
|
||||
.iter()
|
||||
.flat_map(move |chain| {
|
||||
for (idx, &coord) in chain.into_iter().enumerate() {
|
||||
let piece = self.get(coord)?;
|
||||
if piece == starting_color {
|
||||
return chain.get(..idx);
|
||||
}
|
||||
fn propegate_from_dry(&self, coords: CoordPair, starting_color: Piece) -> BitBoard {
|
||||
let opponent_color = starting_color.flip();
|
||||
let player_board = *self.board(starting_color);
|
||||
let opponent_board = *self.board(opponent_color);
|
||||
|
||||
let mut flip_mask = BitBoard::new();
|
||||
let seed = BitBoard::from_coord(coords);
|
||||
|
||||
// Directions to check: east, west, north, south, and diagonals
|
||||
let directions = [
|
||||
BitBoard::east,
|
||||
BitBoard::west,
|
||||
BitBoard::north,
|
||||
BitBoard::south,
|
||||
BitBoard::northeast,
|
||||
BitBoard::northwest,
|
||||
BitBoard::southeast,
|
||||
BitBoard::southwest,
|
||||
];
|
||||
|
||||
for dir in directions {
|
||||
let mut current = seed;
|
||||
let mut temp_flips = BitBoard::new();
|
||||
|
||||
// Expand in direction until edge or non-opponent piece
|
||||
loop {
|
||||
current = dir(¤t);
|
||||
if current.count() == 0 || !current.intersects(opponent_board) {
|
||||
break;
|
||||
}
|
||||
None
|
||||
})
|
||||
.flatten()
|
||||
temp_flips = temp_flips.union(current);
|
||||
}
|
||||
|
||||
// If terminated on a player piece, keep the flips
|
||||
if current.intersects(player_board) {
|
||||
flip_mask = flip_mask.union(temp_flips);
|
||||
}
|
||||
}
|
||||
|
||||
flip_mask
|
||||
}
|
||||
|
||||
/// Count the number of a type of [`Piece`] on the board
|
||||
|
||||
Reference in New Issue
Block a user