initial bit board storage
This commit is contained in:
44
src/bitboard.rs
Normal file
44
src/bitboard.rs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
use crate::board::BOARD_SIZE;
|
||||||
|
|
||||||
|
/// 8x8
|
||||||
|
/// TODO! look into variable length bit arrays in rust
|
||||||
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
|
pub struct BitBoard(u64);
|
||||||
|
|
||||||
|
impl BitBoard {
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
BitBoard(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn get(&self, row: usize, col: usize) -> bool {
|
||||||
|
((self.0 >> (row * BOARD_SIZE + col)) & 1) != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn set(&mut self, row: usize, col: usize, value: bool) {
|
||||||
|
let index = row * BOARD_SIZE + col;
|
||||||
|
if value {
|
||||||
|
self.0 |= 1 << index; // Set the bit at (row, col) to 1
|
||||||
|
} else {
|
||||||
|
self.0 &= !(1 << index); // Clear the bit at (row, col)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const fn count(&self) -> u32 {
|
||||||
|
self.0.count_ones()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn set_and_get() {
|
||||||
|
let mut b = BitBoard::new();
|
||||||
|
assert!(!b.get(2, 4));
|
||||||
|
b.set(2, 4, true);
|
||||||
|
assert!(b.get(2, 4));
|
||||||
|
b.set(2, 4, false);
|
||||||
|
assert!(!b.get(2, 4));
|
||||||
|
}
|
||||||
|
}
|
||||||
83
src/board.rs
83
src/board.rs
@@ -1,10 +1,11 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
|
bitboard::BitBoard,
|
||||||
misc::{diag_raw, split_from},
|
misc::{diag_raw, split_from},
|
||||||
piece::Piece,
|
piece::Piece,
|
||||||
};
|
};
|
||||||
use arrayvec::ArrayVec;
|
use arrayvec::ArrayVec;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use std::{cmp::Ordering, fmt, mem::MaybeUninit};
|
use std::{cmp::Ordering, fmt};
|
||||||
|
|
||||||
pub const BOARD_SIZE: usize = 8;
|
pub const BOARD_SIZE: usize = 8;
|
||||||
|
|
||||||
@@ -60,7 +61,8 @@ lazy_static! {
|
|||||||
|
|
||||||
#[derive(Copy, Clone, PartialEq, Eq)]
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
board: [[Option<Piece>; BOARD_SIZE]; BOARD_SIZE],
|
white_board: BitBoard,
|
||||||
|
black_board: BitBoard,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Board {
|
impl fmt::Display for Board {
|
||||||
@@ -108,7 +110,8 @@ impl fmt::Display for Board {
|
|||||||
impl Board {
|
impl Board {
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
board: [[None; BOARD_SIZE]; BOARD_SIZE],
|
white_board: BitBoard::new(),
|
||||||
|
black_board: BitBoard::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,20 +131,31 @@ impl Board {
|
|||||||
Self::all_positions().filter(move |(i, j)| self.would_prop(*i, *j, color))
|
Self::all_positions().filter(move |(i, j)| self.would_prop(*i, *j, color))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a mutable reference to a place on the [`Board`]
|
|
||||||
/// at (i, j)
|
|
||||||
pub const fn get_mut(&mut self, i: usize, j: usize) -> &mut Option<Piece> {
|
|
||||||
&mut self.board[i][j]
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a reference to a place on the [`Board`]
|
/// Returns a reference to a place on the [`Board`]
|
||||||
/// at (i, j)
|
/// at (i, j)
|
||||||
pub const fn get(&self, i: usize, j: usize) -> &Option<Piece> {
|
pub const fn get(&self, i: usize, j: usize) -> Option<Piece> {
|
||||||
&self.board[i][j]
|
let white = self.white_board.get(i, j);
|
||||||
|
let black = self.black_board.get(i, j);
|
||||||
|
if white {
|
||||||
|
Some(Piece::White)
|
||||||
|
} else if black {
|
||||||
|
Some(Piece::Black)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const fn place_unchecked(&mut self, i: usize, j: usize, piece: Piece) {
|
const fn place_unchecked(&mut self, i: usize, j: usize, piece: Piece) {
|
||||||
*self.get_mut(i, j) = Some(piece);
|
match piece {
|
||||||
|
Piece::Black => {
|
||||||
|
self.black_board.set(i, j, true);
|
||||||
|
self.white_board.set(i, j, false);
|
||||||
|
}
|
||||||
|
Piece::White => {
|
||||||
|
self.black_board.set(i, j, false);
|
||||||
|
self.white_board.set(i, j, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn what_if(&self, i: usize, j: usize, piece: Piece) -> Option<(Self, usize)> {
|
pub fn what_if(&self, i: usize, j: usize, piece: Piece) -> Option<(Self, usize)> {
|
||||||
@@ -172,7 +186,7 @@ impl Board {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn propegate_from(&mut self, i: usize, j: usize) -> usize {
|
fn propegate_from(&mut self, i: usize, j: usize) -> usize {
|
||||||
let Some(starting_color) = *self.get(i, j) else {
|
let Some(starting_color) = self.get(i, j) else {
|
||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -202,7 +216,7 @@ impl Board {
|
|||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
|
|
||||||
if piece == &starting_color {
|
if piece == starting_color {
|
||||||
// get history of this chain
|
// get history of this chain
|
||||||
if let Some(history) = chain.get(..chain_length) {
|
if let Some(history) = chain.get(..chain_length) {
|
||||||
// fill all opposite colors with this color
|
// fill all opposite colors with this color
|
||||||
@@ -220,15 +234,10 @@ impl Board {
|
|||||||
|
|
||||||
/// Returns (White score, Black score)
|
/// Returns (White score, Black score)
|
||||||
pub fn get_score(&self) -> (usize, usize) {
|
pub fn get_score(&self) -> (usize, usize) {
|
||||||
self.board
|
(
|
||||||
.iter()
|
self.white_board.count() as usize,
|
||||||
.flatten()
|
self.black_board.count() as usize,
|
||||||
.flatten()
|
)
|
||||||
.map(|cell| match cell {
|
|
||||||
Piece::White => (1, 0),
|
|
||||||
Piece::Black => (0, 1),
|
|
||||||
})
|
|
||||||
.fold((0_usize, 0usize), |(a, b), (c, d)| (a + c, b + d))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn game_winner(&self, turn: Piece) -> Option<Piece> {
|
pub fn game_winner(&self, turn: Piece) -> Option<Piece> {
|
||||||
@@ -258,9 +267,9 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn place_and_get() {
|
fn place_and_get() {
|
||||||
let mut board = Board::new();
|
let mut board = Board::new();
|
||||||
assert_eq!(board.get(0, 0), &None);
|
assert_eq!(board.get(0, 0), None);
|
||||||
board.place_unchecked(0, 0, Piece::Black);
|
board.place_unchecked(0, 0, Piece::Black);
|
||||||
assert_eq!(board.get(0, 0), &Some(Piece::Black));
|
assert_eq!(board.get(0, 0), Some(Piece::Black));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -271,7 +280,7 @@ mod test {
|
|||||||
board.place_unchecked(0, 2, Piece::Black);
|
board.place_unchecked(0, 2, Piece::Black);
|
||||||
|
|
||||||
board.propegate_from(0, 2);
|
board.propegate_from(0, 2);
|
||||||
assert_eq!(board.get(0, 1), &Some(Piece::Black));
|
assert_eq!(board.get(0, 1), Some(Piece::Black));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -287,7 +296,7 @@ mod test {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
board.get(0, 1),
|
board.get(0, 1),
|
||||||
&None,
|
None,
|
||||||
"(0, 1) was overridden even though it's an empty space"
|
"(0, 1) was overridden even though it's an empty space"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -308,7 +317,7 @@ mod test {
|
|||||||
for j in 2..=6 {
|
for j in 2..=6 {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
board.get(0, j),
|
board.get(0, j),
|
||||||
&Some(Piece::Black),
|
Some(Piece::Black),
|
||||||
"should be black at: ({}, {})",
|
"should be black at: ({}, {})",
|
||||||
0,
|
0,
|
||||||
j
|
j
|
||||||
@@ -332,7 +341,7 @@ mod test {
|
|||||||
for i in 2..=6 {
|
for i in 2..=6 {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
board.get(i, 0),
|
board.get(i, 0),
|
||||||
&Some(Piece::Black),
|
Some(Piece::Black),
|
||||||
"should be black at: ({}, {})",
|
"should be black at: ({}, {})",
|
||||||
i,
|
i,
|
||||||
0
|
0
|
||||||
@@ -365,7 +374,7 @@ mod test {
|
|||||||
board.propegate_from(0, 0);
|
board.propegate_from(0, 0);
|
||||||
|
|
||||||
// Capture white piece at (1,1)
|
// Capture white piece at (1,1)
|
||||||
assert_eq!(board.get(1, 1), &Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(1, 1), Some(Piece::Black), "\n{}", board);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test corner capture from top-right corner
|
// Test corner capture from top-right corner
|
||||||
@@ -381,7 +390,7 @@ mod test {
|
|||||||
board.propegate_from(2, 5);
|
board.propegate_from(2, 5);
|
||||||
|
|
||||||
// Capture white piece at (1, 6)
|
// Capture white piece at (1, 6)
|
||||||
assert_eq!(board.get(1, 6), &Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(1, 6), Some(Piece::Black), "\n{}", board);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test corner capture from bottom-left corner
|
// Test corner capture from bottom-left corner
|
||||||
@@ -397,7 +406,7 @@ mod test {
|
|||||||
board.propegate_from(5, 2);
|
board.propegate_from(5, 2);
|
||||||
|
|
||||||
// Capture white piece at (6, 1)
|
// Capture white piece at (6, 1)
|
||||||
assert_eq!(board.get(6, 1), &Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(6, 1), Some(Piece::Black), "\n{}", board);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test corner capture from bottom-right corner
|
// Test corner capture from bottom-right corner
|
||||||
@@ -413,7 +422,7 @@ mod test {
|
|||||||
board.propegate_from(5, 5);
|
board.propegate_from(5, 5);
|
||||||
|
|
||||||
// Capture white piece at (6, 6)
|
// Capture white piece at (6, 6)
|
||||||
assert_eq!(board.get(6, 6), &Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(6, 6), Some(Piece::Black), "\n{}", board);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test capture from top-left corner (horizontal)
|
// Test capture from top-left corner (horizontal)
|
||||||
@@ -428,7 +437,7 @@ mod test {
|
|||||||
|
|
||||||
board.propegate_from(0, 2);
|
board.propegate_from(0, 2);
|
||||||
|
|
||||||
assert_eq!(board.get(0, 1), &Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(0, 1), Some(Piece::Black), "\n{}", board);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test capture from top-right corner (horizontal)
|
// Test capture from top-right corner (horizontal)
|
||||||
@@ -443,7 +452,7 @@ mod test {
|
|||||||
|
|
||||||
board.propegate_from(0, 5);
|
board.propegate_from(0, 5);
|
||||||
|
|
||||||
assert_eq!(board.get(0, 6), &Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(0, 6), Some(Piece::Black), "\n{}", board);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test capture from top-left corner (vertical)
|
// Test capture from top-left corner (vertical)
|
||||||
@@ -458,7 +467,7 @@ mod test {
|
|||||||
|
|
||||||
board.propegate_from(2, 0);
|
board.propegate_from(2, 0);
|
||||||
|
|
||||||
assert_eq!(board.get(1, 0), &Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(1, 0), Some(Piece::Black), "\n{}", board);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test capture from bottom-left corner (vertical)
|
// Test capture from bottom-left corner (vertical)
|
||||||
@@ -473,6 +482,6 @@ mod test {
|
|||||||
|
|
||||||
board.propegate_from(5, 0);
|
board.propegate_from(5, 0);
|
||||||
|
|
||||||
assert_eq!(board.get(6, 0), &Some(Piece::Black), "\n{}", board);
|
assert_eq!(board.get(6, 0), Some(Piece::Black), "\n{}", board);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -253,7 +253,7 @@ pub struct ComplexAgent {
|
|||||||
|
|
||||||
impl ComplexAgent {
|
impl ComplexAgent {
|
||||||
pub const fn new(color: Piece) -> Self {
|
pub const fn new(color: Piece) -> Self {
|
||||||
const MAX_DEPTH: usize = 8;
|
const MAX_DEPTH: usize = 7;
|
||||||
Self {
|
Self {
|
||||||
color,
|
color,
|
||||||
future_moves: FutureMoves::new(color, MAX_DEPTH),
|
future_moves: FutureMoves::new(color, MAX_DEPTH),
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ use game::Game;
|
|||||||
use piece::Piece;
|
use piece::Piece;
|
||||||
|
|
||||||
mod agent;
|
mod agent;
|
||||||
|
mod bitboard;
|
||||||
mod board;
|
mod board;
|
||||||
mod complexagent;
|
mod complexagent;
|
||||||
mod game;
|
mod game;
|
||||||
@@ -11,8 +12,8 @@ mod piece;
|
|||||||
fn main() {
|
fn main() {
|
||||||
let player1 = complexagent::ComplexAgent::new(Piece::Black);
|
let player1 = complexagent::ComplexAgent::new(Piece::Black);
|
||||||
// let player2 = complexagent::ComplexAgent::new(Piece::White);
|
// let player2 = complexagent::ComplexAgent::new(Piece::White);
|
||||||
let player2 = agent::ManualAgent::new(Piece::White);
|
// let player2 = agent::ManualAgent::new(Piece::White);
|
||||||
// let player2 = agent::RandomAgent::new(Piece::White);
|
let player2 = agent::RandomAgent::new(Piece::White);
|
||||||
let mut game = Game::new(Box::new(player1), Box::new(player2));
|
let mut game = Game::new(Box::new(player1), Box::new(player2));
|
||||||
game.game_loop();
|
game.game_loop();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user