diff --git a/Cargo.lock b/Cargo.lock index 9995774..e45bbeb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,6 +20,18 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" +[[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]] name = "byteorder" version = "1.5.0" @@ -63,6 +75,12 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + [[package]] name = "getrandom" version = "0.3.1" @@ -165,6 +183,7 @@ name = "othello" version = "0.1.0" dependencies = [ "arrayvec", + "bitvec", "either", "lazy_static", "num", @@ -200,6 +219,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + [[package]] name = "rand" version = "0.9.0" @@ -208,7 +233,7 @@ checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" dependencies = [ "rand_chacha", "rand_core", - "zerocopy 0.8.16", + "zerocopy 0.8.17", ] [[package]] @@ -228,7 +253,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b08f3c9802962f7e1b25113931d94f43ed9725bebc59db9d0c3e9a23b67e15ff" dependencies = [ "getrandom", - "zerocopy 0.8.16", + "zerocopy 0.8.17", ] [[package]] @@ -268,6 +293,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "unicode-ident" version = "1.0.16" @@ -356,6 +387,15 @@ dependencies = [ "bitflags", ] +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + [[package]] name = "zerocopy" version = "0.7.35" @@ -368,11 +408,11 @@ dependencies = [ [[package]] name = "zerocopy" -version = "0.8.16" +version = "0.8.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b8c07a70861ce02bad1607b5753ecb2501f67847b9f9ada7c160fff0ec6300c" +checksum = "aa91407dacce3a68c56de03abe2760159582b846c6a4acd2f456618087f12713" dependencies = [ - "zerocopy-derive 0.8.16", + "zerocopy-derive 0.8.17", ] [[package]] @@ -388,9 +428,9 @@ dependencies = [ [[package]] name = "zerocopy-derive" -version = "0.8.16" +version = "0.8.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5226bc9a9a9836e7428936cde76bb6b22feea1a8bfdbc0d241136e4d13417e25" +checksum = "06718a168365cad3d5ff0bb133aad346959a2074bd4a85c121255a11304a8626" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index edfb972..fe6db56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,10 +8,11 @@ edition = "2021" debug = true [dependencies] -arrayvec = "0.7.6" -either = "1.13.0" +arrayvec = "0.7" +bitvec = "1.0.1" +either = "1.13" lazy_static = "1.5.0" num = "0.4" rand = "0.9" rayon = "1.10" -static_assertions = "1.1.0" +static_assertions = "1.1" diff --git a/src/bitboard.rs b/src/bitboard.rs index 5996ce2..c8932b4 100644 --- a/src/bitboard.rs +++ b/src/bitboard.rs @@ -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::() * 8, - (BOARD_SIZE * BOARD_SIZE) -); +const_assert!(std::mem::size_of::() * 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() } } diff --git a/src/board.rs b/src/board.rs index 1751ab9..60924da 100644 --- a/src/board.rs +++ b/src/board.rs @@ -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 { + pub fn get(&self, i: usize, j: usize) -> Option { 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 { diff --git a/src/main.rs b/src/main.rs index a0b4902..37db162 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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(); } diff --git a/src/misc.rs b/src/misc.rs index 75806e0..a9ca1bd 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -43,7 +43,7 @@ where mod test { use super::*; - pub fn diag_test_helper( + fn diag_test_helper( i: T, j: T, range_i: RangeInclusive,