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:
parent
fa7ad34dcb
commit
204ba85202
40
Cargo.lock
generated
40
Cargo.lock
generated
@ -41,18 +41,6 @@ version = "2.9.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
|
checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "bitvec"
|
|
||||||
version = "1.0.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c"
|
|
||||||
dependencies = [
|
|
||||||
"funty",
|
|
||||||
"radium",
|
|
||||||
"tap",
|
|
||||||
"wyz",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bumpalo"
|
name = "bumpalo"
|
||||||
version = "3.17.0"
|
version = "3.17.0"
|
||||||
@ -227,12 +215,6 @@ version = "1.0.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0"
|
checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "funty"
|
|
||||||
version = "2.0.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "getrandom"
|
name = "getrandom"
|
||||||
version = "0.3.1"
|
version = "0.3.1"
|
||||||
@ -430,7 +412,6 @@ name = "othello"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"arrayvec",
|
"arrayvec",
|
||||||
"bitvec",
|
|
||||||
"const_fn",
|
"const_fn",
|
||||||
"criterion",
|
"criterion",
|
||||||
"either",
|
"either",
|
||||||
@ -504,12 +485,6 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "radium"
|
|
||||||
version = "0.7.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rand"
|
name = "rand"
|
||||||
version = "0.9.0"
|
version = "0.9.0"
|
||||||
@ -665,12 +640,6 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "tap"
|
|
||||||
version = "1.0.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tinytemplate"
|
name = "tinytemplate"
|
||||||
version = "1.2.1"
|
version = "1.2.1"
|
||||||
@ -881,15 +850,6 @@ dependencies = [
|
|||||||
"bitflags",
|
"bitflags",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wyz"
|
|
||||||
version = "0.5.1"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed"
|
|
||||||
dependencies = [
|
|
||||||
"tap",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "zerocopy"
|
name = "zerocopy"
|
||||||
version = "0.7.35"
|
version = "0.7.35"
|
||||||
|
|||||||
@ -21,12 +21,8 @@ inherits = "release"
|
|||||||
# for profiling
|
# for profiling
|
||||||
debug = true
|
debug = true
|
||||||
|
|
||||||
[features]
|
|
||||||
bitvec = [ "dep:bitvec" ]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
arrayvec = "0.7"
|
arrayvec = "0.7"
|
||||||
bitvec = { version = "1", optional = true }
|
|
||||||
const_fn = "0.4"
|
const_fn = "0.4"
|
||||||
either = "1.13"
|
either = "1.13"
|
||||||
indicatif = "0.17"
|
indicatif = "0.17"
|
||||||
@ -44,10 +40,6 @@ criterion = { version = "0.5", features = [ "html_reports" ] }
|
|||||||
name = "future_children"
|
name = "future_children"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
[[bench]]
|
|
||||||
name = "board"
|
|
||||||
harness = false
|
|
||||||
|
|
||||||
[lints.rust]
|
[lints.rust]
|
||||||
# fix weird warnings about `test` not being expected
|
# fix weird warnings about `test` not being expected
|
||||||
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(test)'] }
|
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(test)'] }
|
||||||
|
|||||||
@ -1,29 +0,0 @@
|
|||||||
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
|
|
||||||
use othello::repr::{Board, Piece};
|
|
||||||
|
|
||||||
fn fill_board(loops: usize) {
|
|
||||||
let mut board = Board::new();
|
|
||||||
board.place_unchecked((0, 1).into(), Piece::White);
|
|
||||||
board.place_unchecked((0, 2).into(), Piece::Black);
|
|
||||||
board.place_unchecked((0, 3).into(), Piece::Black);
|
|
||||||
board.place_unchecked((0, 4).into(), Piece::Black);
|
|
||||||
for _ in 0..loops {
|
|
||||||
let mut board = board;
|
|
||||||
let _ = board.place((0, 5).into(), Piece::White);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn criterion_benchmark(c: &mut Criterion) {
|
|
||||||
let mut group = c.benchmark_group("board");
|
|
||||||
|
|
||||||
const LOOPS: usize = 1000;
|
|
||||||
|
|
||||||
group.throughput(Throughput::Elements(LOOPS as u64));
|
|
||||||
group.bench_function("board_place", |b| {
|
|
||||||
b.iter(|| fill_board(LOOPS));
|
|
||||||
});
|
|
||||||
group.finish();
|
|
||||||
}
|
|
||||||
|
|
||||||
criterion_group!(benches, criterion_benchmark);
|
|
||||||
criterion_main!(benches);
|
|
||||||
@ -135,8 +135,8 @@ impl PlayerArena {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn create_agents(
|
fn create_agents(
|
||||||
player_1_fn: &Box<dyn Fn(Piece) -> Box<dyn Agent>>,
|
player_1_fn: &dyn Fn(Piece) -> Box<dyn Agent>,
|
||||||
player_2_fn: &Box<dyn Fn(Piece) -> Box<dyn Agent>>,
|
player_2_fn: &dyn Fn(Piece) -> Box<dyn Agent>,
|
||||||
) -> (Box<dyn Agent>, Box<dyn Agent>) {
|
) -> (Box<dyn Agent>, Box<dyn Agent>) {
|
||||||
(player_1_fn(Piece::Black), player_2_fn(Piece::White))
|
(player_1_fn(Piece::Black), player_2_fn(Piece::White))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
use elo::run;
|
|
||||||
use game::Game;
|
use game::Game;
|
||||||
use logic::{ChildrenEvalMethod, FutureMoveConfig};
|
use logic::{ChildrenEvalMethod, FutureMoveConfig};
|
||||||
use repr::Piece;
|
use repr::Piece;
|
||||||
@ -13,8 +12,8 @@ pub mod repr;
|
|||||||
|
|
||||||
// TODO! make this agent configuration a config option via `clap-rs`
|
// TODO! make this agent configuration a config option via `clap-rs`
|
||||||
fn main() {
|
fn main() {
|
||||||
run();
|
// run();
|
||||||
return;
|
// return;
|
||||||
let player1 = complexagent::ComplexAgent::new(
|
let player1 = complexagent::ComplexAgent::new(
|
||||||
Piece::Black,
|
Piece::Black,
|
||||||
FutureMoveConfig {
|
FutureMoveConfig {
|
||||||
|
|||||||
@ -1,25 +1,10 @@
|
|||||||
use super::{
|
use super::{
|
||||||
board::Board,
|
board::Board,
|
||||||
coords::{CoordPair, CoordPairInner},
|
coords::{CoordPair, CoordPairInner},
|
||||||
|
CoordAxis,
|
||||||
};
|
};
|
||||||
use const_fn::const_fn;
|
|
||||||
use static_assertions::const_assert;
|
use static_assertions::const_assert;
|
||||||
|
|
||||||
// quick explanation for the dual-nature of [`BitBoard`]
|
|
||||||
// There's both a `bitvec` impl (which is variable length)
|
|
||||||
// and a `native` impl which uses a u64 as the backing type
|
|
||||||
// the `native` impl is ~15-25% faster (in non-BitBoard specific benchmarks)
|
|
||||||
// `bitvec` is only really useful if you're using esoteric board sizes
|
|
||||||
|
|
||||||
#[cfg(feature = "bitvec")]
|
|
||||||
use bitvec::prelude::*;
|
|
||||||
|
|
||||||
#[cfg(feature = "bitvec")]
|
|
||||||
type BBBaseType = u64;
|
|
||||||
|
|
||||||
#[cfg(feature = "bitvec")]
|
|
||||||
pub type BitBoardInner = BitArr!(for Board::BOARD_AREA, in BBBaseType, Lsb0);
|
|
||||||
|
|
||||||
#[cfg(not(feature = "bitvec"))]
|
#[cfg(not(feature = "bitvec"))]
|
||||||
pub type BitBoardInner = u64;
|
pub type BitBoardInner = u64;
|
||||||
|
|
||||||
@ -30,55 +15,133 @@ pub struct BitBoard(BitBoardInner);
|
|||||||
const_assert!(std::mem::size_of::<BitBoard>() * 8 >= Board::BOARD_AREA as usize);
|
const_assert!(std::mem::size_of::<BitBoard>() * 8 >= Board::BOARD_AREA as usize);
|
||||||
|
|
||||||
impl BitBoard {
|
impl BitBoard {
|
||||||
#[cfg(feature = "bitvec")]
|
|
||||||
#[allow(clippy::new_without_default)]
|
|
||||||
pub const fn new() -> Self {
|
|
||||||
Self(bitarr!(BBBaseType, Lsb0; 0; Board::BOARD_AREA))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(feature = "bitvec"))]
|
|
||||||
#[allow(clippy::new_without_default)]
|
#[allow(clippy::new_without_default)]
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Self(0)
|
Self(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[const_fn(cfg(not(feature = "bitvec")))]
|
|
||||||
pub const fn get(&self, coord: CoordPair) -> bool {
|
pub const fn get(&self, coord: CoordPair) -> bool {
|
||||||
self.get_by_index(coord.0)
|
self.get_by_index(coord.0)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[const_fn(cfg(not(feature = "bitvec")))]
|
|
||||||
pub const fn set(&mut self, coord: CoordPair, value: bool) {
|
pub const fn set(&mut self, coord: CoordPair, value: bool) {
|
||||||
self.set_by_index(coord.0, value);
|
self.set_by_index(coord.0, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "bitvec"))]
|
|
||||||
const fn get_by_index(&self, index: CoordPairInner) -> bool {
|
const fn get_by_index(&self, index: CoordPairInner) -> bool {
|
||||||
((self.0 >> index) & 0b1) != 0b0
|
((self.0 >> index) & 0b1) != 0b0
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "bitvec"))]
|
|
||||||
const fn set_by_index(&mut self, index: CoordPairInner, value: bool) {
|
const fn set_by_index(&mut self, index: CoordPairInner, value: bool) {
|
||||||
// PERF! branchless setting of bit (~+3% perf bump)
|
// PERF! branchless setting of bit (~+3% perf bump)
|
||||||
self.0 &= !(0b1 << index); // clear bit
|
self.0 &= !(0b1 << index); // clear bit
|
||||||
self.0 |= (value as BitBoardInner) << index; // set bit (if needed)
|
self.0 |= (value as BitBoardInner) << index; // set bit (if needed)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "bitvec")]
|
|
||||||
pub fn get_by_index(&self, index: CoordPairInner) -> bool {
|
|
||||||
self.0[index as usize]
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "bitvec")]
|
|
||||||
pub fn set_by_index(&mut self, index: CoordPairInner, value: bool) {
|
|
||||||
self.0.set(index as usize, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
// works on both `bitvec` and native (const on native)
|
// works on both `bitvec` and native (const on native)
|
||||||
#[const_fn(cfg(not(feature = "bitvec")))]
|
|
||||||
pub const fn count(&self) -> usize {
|
pub const fn count(&self) -> usize {
|
||||||
self.0.count_ones() as usize
|
self.0.count_ones() as usize
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Directional shifts with edge masking (prevents wrapping)
|
||||||
|
pub const fn east(&self) -> Self {
|
||||||
|
let mask = !Self::col_mask(Board::BOARD_SIZE).0; // Mask to block column 7 bits
|
||||||
|
Self((self.0 & mask) << 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn west(&self) -> Self {
|
||||||
|
let mask = !Self::col_mask(0).0;
|
||||||
|
Self((self.0 & mask) >> 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn north(&self) -> Self {
|
||||||
|
Self(self.0 >> Board::BOARD_SIZE)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn south(&self) -> Self {
|
||||||
|
Self(self.0 << Board::BOARD_SIZE)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn northeast(&self) -> Self {
|
||||||
|
self.north().east()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn northwest(&self) -> Self {
|
||||||
|
self.north().west()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn southeast(&self) -> Self {
|
||||||
|
self.south().east()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn southwest(&self) -> Self {
|
||||||
|
self.south().west()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mask for a specific column (e.g., col_mask(7) = 0x8080808080808080)
|
||||||
|
const fn col_mask(col: CoordAxis) -> Self {
|
||||||
|
let mut mask = 0;
|
||||||
|
let mut i = 0;
|
||||||
|
while i < Board::BOARD_AREA {
|
||||||
|
mask |= 1 << (i + col);
|
||||||
|
i += Board::BOARD_SIZE;
|
||||||
|
}
|
||||||
|
Self(mask)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if a BitBoard contains a coordinate
|
||||||
|
pub const fn contains(&self, coord: CoordPair) -> bool {
|
||||||
|
(self.0 & (1 << coord.0)) != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a BitBoard from a single coordinate
|
||||||
|
pub const fn from_coord(coord: CoordPair) -> Self {
|
||||||
|
Self(1 << coord.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn intersects(self, other: Self) -> bool {
|
||||||
|
(self & other).count() > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn union(self, other: Self) -> Self {
|
||||||
|
self | other
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::Not for BitBoard {
|
||||||
|
type Output = BitBoard;
|
||||||
|
|
||||||
|
fn not(self) -> Self::Output {
|
||||||
|
Self(!self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::BitAnd for BitBoard {
|
||||||
|
type Output = BitBoard;
|
||||||
|
|
||||||
|
fn bitand(self, rhs: Self) -> Self::Output {
|
||||||
|
Self(self.0 & rhs.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::BitOr for BitBoard {
|
||||||
|
type Output = BitBoard;
|
||||||
|
|
||||||
|
fn bitor(self, rhs: Self) -> Self::Output {
|
||||||
|
Self(self.0 | rhs.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::BitAndAssign for BitBoard {
|
||||||
|
fn bitand_assign(&mut self, rhs: Self) {
|
||||||
|
*self = *self & rhs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::ops::BitOrAssign for BitBoard {
|
||||||
|
fn bitor_assign(&mut self, rhs: Self) {
|
||||||
|
*self = *self | rhs;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
@ -1,12 +1,44 @@
|
|||||||
use super::{
|
use super::{bitboard::BitBoard, piece::Piece, CoordAxis, CoordPair};
|
||||||
bitboard::BitBoard,
|
use arrayvec::ArrayVec;
|
||||||
chains::{gen_adj_lookup, ChainCollection, PosMap},
|
|
||||||
piece::Piece,
|
|
||||||
CoordAxis, CoordPair,
|
|
||||||
};
|
|
||||||
use const_fn::const_fn;
|
use const_fn::const_fn;
|
||||||
use rand::seq::IteratorRandom;
|
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)]
|
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
|
||||||
pub enum Winner {
|
pub enum Winner {
|
||||||
@ -15,9 +47,6 @@ pub enum Winner {
|
|||||||
None,
|
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
|
/// Repersents a Othello game board at a certain space
|
||||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
@ -205,7 +234,7 @@ impl Board {
|
|||||||
|
|
||||||
/// Returns a bool which represents whether or not a move would propegate and be valid
|
/// 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 {
|
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> {
|
pub fn place(&mut self, coord: CoordPair, piece: Piece) -> Result<(), &'static str> {
|
||||||
@ -228,19 +257,12 @@ impl Board {
|
|||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// PERF! avoid clones and collections here using raw pointers
|
let flip_mask = self.propegate_from_dry(coord, starting_color);
|
||||||
let iterator = unsafe {
|
let count = flip_mask.count();
|
||||||
// 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 mut count = 0;
|
// Apply the flips
|
||||||
for &coord in iterator {
|
*self.board_mut(starting_color) |= flip_mask;
|
||||||
self.place_unchecked(coord, starting_color);
|
*self.board_mut(starting_color.flip()) &= !flip_mask;
|
||||||
count += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
count
|
count
|
||||||
}
|
}
|
||||||
@ -248,24 +270,46 @@ impl Board {
|
|||||||
/// Propegate piece captures originating from (i, j)
|
/// Propegate piece captures originating from (i, j)
|
||||||
/// DO NOT USE THIS ALONE, this should be called as a part of
|
/// DO NOT USE THIS ALONE, this should be called as a part of
|
||||||
/// [`Board::place`] or [`Board::place_and_prop_unchecked`]
|
/// [`Board::place`] or [`Board::place_and_prop_unchecked`]
|
||||||
fn propegate_from_dry(
|
fn propegate_from_dry(&self, coords: CoordPair, starting_color: Piece) -> BitBoard {
|
||||||
&self,
|
let opponent_color = starting_color.flip();
|
||||||
coords: CoordPair,
|
let player_board = *self.board(starting_color);
|
||||||
starting_color: Piece,
|
let opponent_board = *self.board(opponent_color);
|
||||||
) -> impl Iterator<Item = &CoordPair> + use<'_> {
|
|
||||||
ADJ_LOOKUP
|
let mut flip_mask = BitBoard::new();
|
||||||
.get(coords)
|
let seed = BitBoard::from_coord(coords);
|
||||||
.iter()
|
|
||||||
.flat_map(move |chain| {
|
// Directions to check: east, west, north, south, and diagonals
|
||||||
for (idx, &coord) in chain.into_iter().enumerate() {
|
let directions = [
|
||||||
let piece = self.get(coord)?;
|
BitBoard::east,
|
||||||
if piece == starting_color {
|
BitBoard::west,
|
||||||
return chain.get(..idx);
|
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
|
temp_flips = temp_flips.union(current);
|
||||||
})
|
}
|
||||||
.flatten()
|
|
||||||
|
// 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
|
/// Count the number of a type of [`Piece`] on the board
|
||||||
|
|||||||
@ -11,42 +11,6 @@ type Chain = ArrayVec<CoordPair, { (Board::BOARD_SIZE - 1) as usize }>;
|
|||||||
/// A collection of chains (up vert, down vert, left horiz, right horiz, diagonals....)
|
/// A collection of chains (up vert, down vert, left horiz, right horiz, diagonals....)
|
||||||
pub type ChainCollection = ArrayVec<Chain, 8>;
|
pub type ChainCollection = ArrayVec<Chain, 8>;
|
||||||
|
|
||||||
/// 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a lookup map for adjacencies and chains from each position on the board
|
/// Creates a lookup map for adjacencies and chains from each position on the board
|
||||||
pub fn gen_adj_lookup() -> PosMap<ChainCollection> {
|
pub fn gen_adj_lookup() -> PosMap<ChainCollection> {
|
||||||
PosMap(
|
PosMap(
|
||||||
|
|||||||
@ -1,95 +0,0 @@
|
|||||||
use either::Either;
|
|
||||||
use std::{iter::Rev, ops::RangeInclusive};
|
|
||||||
|
|
||||||
pub fn split_from<T>(range: RangeInclusive<T>, x: T) -> [impl Iterator<Item = T> + Clone; 2]
|
|
||||||
where
|
|
||||||
T: num::Integer + Copy,
|
|
||||||
RangeInclusive<T>: Iterator<Item = T> + DoubleEndedIterator,
|
|
||||||
Rev<RangeInclusive<T>>: Iterator<Item = T>,
|
|
||||||
{
|
|
||||||
let in_range = range.contains(&x);
|
|
||||||
let (start, end) = (*range.start(), *range.end());
|
|
||||||
|
|
||||||
// RangeInclusive (1..=0), has 0 elements
|
|
||||||
let base = Either::Right(T::one()..=T::zero());
|
|
||||||
[
|
|
||||||
if in_range && x > start + T::one() {
|
|
||||||
Either::Left((start..=(x - T::one())).rev())
|
|
||||||
} else {
|
|
||||||
base.clone()
|
|
||||||
},
|
|
||||||
if in_range && x + T::one() < end {
|
|
||||||
Either::Right((x + T::one())..=end)
|
|
||||||
} else {
|
|
||||||
base
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn diag_raw<T>(
|
|
||||||
i_chains: [impl Iterator<Item = T> + Clone; 2],
|
|
||||||
j_chains: [impl Iterator<Item = T> + Clone; 2],
|
|
||||||
) -> [impl Iterator<Item = (T, T)> + Clone; 4]
|
|
||||||
where
|
|
||||||
T: num::Integer + Copy,
|
|
||||||
RangeInclusive<T>: Iterator<Item = T> + DoubleEndedIterator,
|
|
||||||
Rev<RangeInclusive<T>>: Iterator<Item = T>,
|
|
||||||
{
|
|
||||||
[(0, 0), (1, 1), (1, 0), (0, 1)].map(move |(a, b)| i_chains[a].clone().zip(j_chains[b].clone()))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
fn diag_test_helper<T>(
|
|
||||||
i: T,
|
|
||||||
j: T,
|
|
||||||
range_i: RangeInclusive<T>,
|
|
||||||
range_j: RangeInclusive<T>,
|
|
||||||
) -> [impl Iterator<Item = (T, T)> + Clone; 4]
|
|
||||||
where
|
|
||||||
T: num::Integer + Copy,
|
|
||||||
RangeInclusive<T>: Iterator<Item = T> + DoubleEndedIterator,
|
|
||||||
Rev<RangeInclusive<T>>: Iterator<Item = T>,
|
|
||||||
{
|
|
||||||
diag_raw(split_from(range_i, i), split_from(range_j, j))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn split_test() {
|
|
||||||
assert_eq!(
|
|
||||||
split_from(0..=6, 2).map(Iterator::collect::<Vec<usize>>),
|
|
||||||
[vec![1, 0], vec![3, 4, 5, 6]]
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
split_from(0..=6, 0).map(Iterator::collect::<Vec<usize>>),
|
|
||||||
[vec![], vec![1, 2, 3, 4, 5, 6]]
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
split_from(0..=6, 6).map(Iterator::collect::<Vec<usize>>),
|
|
||||||
[vec![5, 4, 3, 2, 1, 0], vec![]]
|
|
||||||
);
|
|
||||||
|
|
||||||
// test out-of-bounds and also generics
|
|
||||||
assert_eq!(
|
|
||||||
split_from::<i16>(-1i16..=4i16, 10i16).map(Iterator::collect::<Vec<i16>>),
|
|
||||||
[const { Vec::<i16>::new() }; 2]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn diag_test() {
|
|
||||||
assert_eq!(
|
|
||||||
diag_test_helper(2, 3, 0..=7, 0..=7).map(Iterator::collect::<Vec<(usize, usize)>>),
|
|
||||||
[
|
|
||||||
vec![(1, 2), (0, 1)],
|
|
||||||
vec![(3, 4), (4, 5), (5, 6), (6, 7)],
|
|
||||||
vec![(3, 2), (4, 1), (5, 0)],
|
|
||||||
vec![(1, 4), (0, 5)]
|
|
||||||
]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,11 +1,8 @@
|
|||||||
mod bitboard;
|
mod bitboard;
|
||||||
mod board;
|
mod board;
|
||||||
mod chains;
|
|
||||||
mod coords;
|
mod coords;
|
||||||
mod misc;
|
|
||||||
mod piece;
|
mod piece;
|
||||||
|
|
||||||
pub use board::{Board, Winner};
|
pub use board::{Board, PosMap, Winner};
|
||||||
pub use chains::PosMap;
|
|
||||||
pub use coords::{CoordAxis, CoordPair};
|
pub use coords::{CoordAxis, CoordPair};
|
||||||
pub use piece::Piece;
|
pub use piece::Piece;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user