From 7992aca8030364cf52f8bc8a6f3ebefc222b91c4 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 11 Feb 2025 23:53:17 -0500 Subject: [PATCH] logic fixes and nits --- src/board.rs | 79 +++++++++++++++++++++------------------------ src/complexagent.rs | 18 +++++------ src/main.rs | 4 +-- 3 files changed, 46 insertions(+), 55 deletions(-) diff --git a/src/board.rs b/src/board.rs index 37253bc..6912ec0 100644 --- a/src/board.rs +++ b/src/board.rs @@ -4,7 +4,7 @@ use crate::{ }; use arrayvec::ArrayVec; use lazy_static::lazy_static; -use std::{cmp::Ordering, fmt}; +use std::{cmp::Ordering, fmt, mem::MaybeUninit}; pub const BOARD_SIZE: usize = 8; @@ -19,50 +19,43 @@ type ChainCollection = ArrayVec; /// with each coordinate type PosMap = ArrayVec, BOARD_SIZE>; +fn gen_adj_lookup() -> PosMap { + (0..BOARD_SIZE) + .map(|i| { + (0..BOARD_SIZE) + .map(|j| { + let (i_chain, j_chain) = ( + split_from(0..=BOARD_SIZE - 1, i), + split_from(0..=BOARD_SIZE - 1, j), + ); + + let mut chains: ChainCollection = ArrayVec::new_const(); + + chains.extend( + i_chain + .clone() + .map(|range| range.map(move |i| (i, j))) + .map(Iterator::collect), + ); + chains.extend( + j_chain + .clone() + .map(|range| range.map(move |j| (i, j))) + .map(Iterator::collect), + ); + + // handle diagonals + chains.extend(diag_raw(i_chain, j_chain).map(Iterator::collect)); + chains + }) + .collect() + }) + .collect() +} + lazy_static! { /// Precompute all possible chains for each position on the board - pub static ref ADJ_LOOKUP: PosMap = { - let mut output = ArrayVec::new(); - - // populate arrayvec as we index it [i][j] - for _ in 0..BOARD_SIZE { - let mut a = ArrayVec::new(); - // [j] - for _ in 0..BOARD_SIZE { - a.push(ArrayVec::new()); - } - output.push(a); - } - - for (i, j) in Board::all_positions() { - let (i_chain, j_chain) = ( - split_from(0..=BOARD_SIZE - 1, i), - split_from(0..=BOARD_SIZE - 1, j), - ); - - let mut chains: ChainCollection = ArrayVec::new_const(); - - chains.extend( - i_chain - .clone() - .map(|range| range.map(move |i| (i, j))) - .map(Iterator::collect), - ); - chains.extend( - j_chain - .clone() - .map(|range| range.map(move |j| (i, j))) - .map(Iterator::collect), - ); - - // handle diagonals - chains.extend(diag_raw(i_chain, j_chain).map(Iterator::collect)); - - output[i][j] = chains; - } - - output - }; + pub static ref ADJ_LOOKUP: PosMap = gen_adj_lookup(); } #[derive(Copy, Clone, PartialEq, Eq)] diff --git a/src/complexagent.rs b/src/complexagent.rs index 74205e8..2e3ad0b 100644 --- a/src/complexagent.rs +++ b/src/complexagent.rs @@ -142,11 +142,8 @@ impl FutureMoves { fn compute_values(&mut self) { for depth in (0..=self.current_depth).rev() { - let nodes_at_depth: Vec = self - .arena - .iter() - .enumerate() - .filter_map(|(idx, node)| (self.depth_of(node.parent) == depth).then_some(idx)) + let nodes_at_depth: Vec = (0..self.arena.len()) + .filter(|&idx| self.depth_of(idx) == depth) .collect(); for idx in nodes_at_depth { @@ -166,9 +163,10 @@ impl FutureMoves { } } - fn depth_of(&self, node_parent: Option) -> usize { + /// Given an index from `self.arena`, what depth is it at? 0-indexed + fn depth_of(&self, node_parent: usize) -> usize { let mut depth = 0; - let mut current = node_parent; + let mut current = Some(node_parent); while let Some(parent_idx) = current { depth += 1; current = self.arena[parent_idx].parent; @@ -203,7 +201,7 @@ impl FutureMoves { }) .inspect(|&root| { self.current_root = Some(root); - self.current_depth = self.max_depth - self.depth_of(Some(root)); + self.current_depth = self.max_depth - self.depth_of(root); self.prune_unrelated(); }) .is_some() @@ -254,8 +252,8 @@ pub struct ComplexAgent { } impl ComplexAgent { - pub fn new(color: Piece) -> Self { - const MAX_DEPTH: usize = 6; + pub const fn new(color: Piece) -> Self { + const MAX_DEPTH: usize = 8; Self { color, future_moves: FutureMoves::new(color, MAX_DEPTH), diff --git a/src/main.rs b/src/main.rs index 6506e54..5c9bb14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,8 +11,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(); }