some changes
This commit is contained in:
@@ -25,8 +25,7 @@ impl BitBoard {
|
||||
}
|
||||
|
||||
pub fn set(&mut self, row: usize, col: usize, value: bool) {
|
||||
let index = Self::get_index(row, col);
|
||||
self.0.set(index, value);
|
||||
self.0.set(Self::get_index(row, col), value);
|
||||
}
|
||||
|
||||
pub fn count(&self) -> usize {
|
||||
|
||||
52
src/board.rs
52
src/board.rs
@@ -190,7 +190,7 @@ impl Board {
|
||||
|
||||
/// Returns a bool which represents whether or not a move would propegate and be valid
|
||||
pub fn would_prop(&self, i: usize, j: usize, piece: Piece) -> bool {
|
||||
self.get(i, j).is_none() && !self.propegate_from_dry(i, j, piece).is_empty()
|
||||
self.get(i, j).is_none() && self.propegate_from_dry(i, j, piece).next().is_some()
|
||||
}
|
||||
|
||||
pub fn place(&mut self, i: usize, j: usize, piece: Piece) -> Result<(), &'static str> {
|
||||
@@ -207,7 +207,7 @@ impl Board {
|
||||
return 0;
|
||||
};
|
||||
|
||||
let pos_s = self.propegate_from_dry(i, j, starting_color);
|
||||
let pos_s: Vec<(usize, usize)> = self.propegate_from_dry(i, j, starting_color).collect();
|
||||
let pos_len = pos_s.len();
|
||||
for (i, j) in pos_s {
|
||||
self.place_unchecked(i, j, starting_color);
|
||||
@@ -219,40 +219,36 @@ 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, i: usize, j: usize, starting_color: Piece) -> Vec<(usize, usize)> {
|
||||
// Longest chain is (BOARD_SIZE - 2) as there needs to be the two pieces containing it
|
||||
let mut fill: Vec<(usize, usize)> = Vec::with_capacity((BOARD_SIZE - 2) * 8);
|
||||
|
||||
for chain in &ADJ_LOOKUP[i][j] {
|
||||
for (chain_length, &(new_i, new_j)) in chain.iter().enumerate() {
|
||||
let Some(piece) = self.get(new_i, new_j) else {
|
||||
// chain interupted by blank space
|
||||
break;
|
||||
};
|
||||
|
||||
if piece == starting_color {
|
||||
// Chain is only ever added to `fill` if it is completed
|
||||
|
||||
if let Some(history) = chain.get(..chain_length) {
|
||||
// fill all opposite colors with this color
|
||||
fill.extend(history);
|
||||
fn propegate_from_dry(
|
||||
&self,
|
||||
i: usize,
|
||||
j: usize,
|
||||
starting_color: Piece,
|
||||
) -> impl Iterator<Item = (usize, usize)> + use<'_> {
|
||||
ADJ_LOOKUP[i][j]
|
||||
.iter()
|
||||
.filter_map(move |chain| {
|
||||
let mut end_idx = None;
|
||||
for (idx, &(new_i, new_j)) in chain.iter().enumerate() {
|
||||
let piece = self.get(new_i, new_j)?;
|
||||
if piece == starting_color {
|
||||
end_idx = Some(idx);
|
||||
break;
|
||||
}
|
||||
|
||||
// either the other pieces were replaced, or this was an invalid chain,
|
||||
// in both cases, the loop needs to be broken
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
fill
|
||||
end_idx.map(|idx| &chain[..idx])
|
||||
})
|
||||
.flatten()
|
||||
.cloned()
|
||||
}
|
||||
|
||||
/// Count the number of a type of [`Piece`] on the board
|
||||
pub fn count(&self, piece: Piece) -> usize {
|
||||
match piece {
|
||||
Piece::Black => self.black_board.count(),
|
||||
Piece::White => self.white_board.count(),
|
||||
Piece::Black => self.black_board,
|
||||
Piece::White => self.white_board,
|
||||
}
|
||||
.count()
|
||||
}
|
||||
|
||||
/// Returns (White score, Black score)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use either::Either;
|
||||
use indicatif::{ProgressBar, ProgressIterator, ProgressStyle};
|
||||
|
||||
use crate::{agent::Agent, board::Board, piece::Piece};
|
||||
|
||||
@@ -92,16 +93,21 @@ impl FutureMoves {
|
||||
if self.arena.len() >= self.max_arena {
|
||||
break;
|
||||
}
|
||||
next_nodes = next_nodes
|
||||
.into_iter()
|
||||
.flat_map(|node_idx| {
|
||||
self.generate_children(
|
||||
Some(node_idx),
|
||||
&self.arena[node_idx].board.clone(),
|
||||
next_color,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let arena_len = self.arena.len();
|
||||
next_nodes =
|
||||
next_nodes
|
||||
.into_iter()
|
||||
.flat_map(|node_idx| {
|
||||
self.generate_children(
|
||||
Some(node_idx),
|
||||
&self.arena[node_idx].board.clone(),
|
||||
next_color,
|
||||
)
|
||||
})
|
||||
.progress_with(ProgressBar::new(arena_len as u64).with_style(
|
||||
ProgressStyle::with_template("({pos}/{len}) {per_sec}").unwrap(),
|
||||
))
|
||||
.collect();
|
||||
next_color = !next_color;
|
||||
}
|
||||
}
|
||||
@@ -277,8 +283,8 @@ pub struct ComplexAgent {
|
||||
|
||||
impl ComplexAgent {
|
||||
pub const fn new(color: Piece) -> Self {
|
||||
const MAX_DEPTH: usize = 8;
|
||||
const MAX_ARENA: usize = 20_000_000;
|
||||
const MAX_DEPTH: usize = 10;
|
||||
const MAX_ARENA: usize = 100_000_000;
|
||||
Self {
|
||||
color,
|
||||
future_moves: FutureMoves::new(color, MAX_DEPTH, MAX_ARENA),
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user