use bitvec

This commit is contained in:
2025-02-12 20:07:29 -05:00
parent dbd0a97602
commit 2c241948f7
6 changed files with 74 additions and 45 deletions

View File

@@ -1,48 +1,35 @@
use crate::board::BOARD_SIZE;
use static_assertions::const_assert_eq;
use crate::board::{BOARD_AREA, BOARD_SIZE};
use bitvec::prelude::*;
use static_assertions::const_assert;
// BitBoard should be big enough to fit all points on the board
const_assert_eq!(
std::mem::size_of::<BitBoard>() * 8,
(BOARD_SIZE * BOARD_SIZE)
);
const_assert!(std::mem::size_of::<BitBoard>() * 8 >= BOARD_AREA);
/// Backing Type of BitBoard
type BBBaseType = u64;
/// 8x8
/// TODO! look into variable length bit arrays in rust
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct BitBoard(u64);
pub struct BitBoard(BitArr!(for BOARD_AREA, in BBBaseType, Lsb0));
impl BitBoard {
pub const fn new() -> Self {
BitBoard(0)
Self(bitarr!(BBBaseType, Lsb0; 0; BOARD_AREA))
}
const fn get_index(row: usize, col: usize) -> usize {
row * BOARD_SIZE + col
}
pub const fn get(&self, row: usize, col: usize) -> bool {
((self.0 >> Self::get_index(row, col)) & 1) != 0
pub fn get(&self, row: usize, col: usize) -> bool {
self.0[Self::get_index(row, col)]
}
pub const fn set(&mut self, row: usize, col: usize, value: bool) {
pub fn set(&mut self, row: usize, col: usize, value: bool) {
let index = Self::get_index(row, col);
if value {
self.set_bit(index); // Set the bit at (row, col) to 1
} else {
self.clear_bit(index); // Clear the bit at (row, col)
}
self.0.set(index, value);
}
const fn clear_bit(&mut self, index: usize) {
self.0 &= !(1 << index)
}
const fn set_bit(&mut self, index: usize) {
self.0 |= 1 << index
}
pub const fn count(&self) -> u32 {
pub fn count(&self) -> usize {
self.0.count_ones()
}
}

View File

@@ -8,6 +8,7 @@ use lazy_static::lazy_static;
use std::{cmp::Ordering, fmt};
pub const BOARD_SIZE: usize = 8;
pub const BOARD_AREA: usize = BOARD_SIZE * BOARD_SIZE;
/// A chain of positions across the board
type Chain = ArrayVec<(usize, usize), BOARD_SIZE>;
@@ -115,7 +116,7 @@ impl Board {
}
}
pub const fn starting_pos(mut self) -> Self {
pub 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);
@@ -133,7 +134,7 @@ impl Board {
/// Returns a reference to a place on the [`Board`]
/// at (i, j)
pub const fn get(&self, i: usize, j: usize) -> Option<Piece> {
pub fn get(&self, i: usize, j: usize) -> Option<Piece> {
let white = self.white_board.get(i, j);
let black = self.black_board.get(i, j);
if white {
@@ -145,7 +146,7 @@ impl Board {
}
}
const fn place_unchecked(&mut self, i: usize, j: usize, piece: Piece) {
fn place_unchecked(&mut self, i: usize, j: usize, piece: Piece) {
match piece {
Piece::Black => {
self.black_board.set(i, j, true);
@@ -175,14 +176,14 @@ impl Board {
self.get(i, j).is_none() && !self.propegate_from_dry(i, j, piece).is_empty()
}
pub fn place(&mut self, i: usize, j: usize, piece: Piece) -> Result<(), String> {
pub fn place(&mut self, i: usize, j: usize, piece: Piece) -> Result<(), &'static str> {
if let Some(what_if_result) = self.what_if(i, j, piece) {
if what_if_result.1 > 0 {
*self = what_if_result.0;
return Ok(());
}
}
Err("move would not propegate".to_string())
Err("move would not propegate")
}
fn propegate_from(&mut self, i: usize, j: usize) -> usize {

View File

@@ -12,8 +12,8 @@ mod piece;
fn main() {
let player1 = complexagent::ComplexAgent::new(Piece::Black);
// let player2 = complexagent::ComplexAgent::new(Piece::White);
let player2 = agent::ManualAgent::new(Piece::White);
// let player2 = agent::RandomAgent::new(Piece::White);
// let player2 = agent::ManualAgent::new(Piece::White);
let player2 = agent::RandomAgent::new(Piece::White);
let mut game = Game::new(Box::new(player1), Box::new(player2));
game.game_loop();
}

View File

@@ -43,7 +43,7 @@ where
mod test {
use super::*;
pub fn diag_test_helper<T>(
fn diag_test_helper<T>(
i: T,
j: T,
range_i: RangeInclusive<T>,