From dafe585f0cbeebfa19cd0a216ccc049461439749 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Thu, 13 Feb 2025 22:33:37 -0500 Subject: [PATCH] some changes --- Cargo.lock | 155 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/bitboard.rs | 3 +- src/board.rs | 52 +++++++-------- src/complexagent.rs | 30 +++++---- src/main.rs | 4 +- 6 files changed, 201 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c28a1b..53e3416 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,12 @@ dependencies = [ "wyz", ] +[[package]] +name = "bumpalo" +version = "3.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" + [[package]] name = "byteorder" version = "1.5.0" @@ -44,12 +50,31 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "console" +version = "0.15.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea3c6ecd8059b57859df5c69830340ed3c41d30e3da0c1cbed90a96ac853041b" +dependencies = [ + "encode_unicode", + "libc", + "once_cell", + "unicode-width", + "windows-sys", +] + [[package]] name = "either" version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +[[package]] +name = "encode_unicode" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" + [[package]] name = "funty" version = "2.0.0" @@ -68,6 +93,29 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "indicatif" +version = "0.17.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "183b3088984b400f4cfac3620d5e076c84da5364016b4f49473de574b2586235" +dependencies = [ + "console", + "number_prefix", + "portable-atomic", + "unicode-width", + "web-time", +] + +[[package]] +name = "js-sys" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -80,6 +128,12 @@ version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" +[[package]] +name = "log" +version = "0.4.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" + [[package]] name = "num" version = "0.4.3" @@ -153,6 +207,18 @@ dependencies = [ "autocfg", ] +[[package]] +name = "number_prefix" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" + +[[package]] +name = "once_cell" +version = "1.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" + [[package]] name = "othello" version = "0.1.0" @@ -160,12 +226,19 @@ dependencies = [ "arrayvec", "bitvec", "either", + "indicatif", "lazy_static", "num", "rand", "static_assertions", ] +[[package]] +name = "portable-atomic" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "280dc24453071f1b63954171985a0b0d30058d287960968b9b2aca264c8d4ee6" + [[package]] name = "ppv-lite86" version = "0.2.20" @@ -259,6 +332,12 @@ version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" +[[package]] +name = "unicode-width" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd" + [[package]] name = "wasi" version = "0.13.3+wasi-0.2.2" @@ -268,6 +347,82 @@ dependencies = [ "wit-bindgen-rt", ] +[[package]] +name = "wasm-bindgen" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" +dependencies = [ + "bumpalo", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "web-time" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.52.6" diff --git a/Cargo.toml b/Cargo.toml index 6671a59..4cc3ae4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ debug = true arrayvec = "0.7" bitvec = "1.0.1" either = "1.13" +indicatif = "0.17.11" lazy_static = "1.5.0" num = "0.4" rand = "0.9" diff --git a/src/bitboard.rs b/src/bitboard.rs index c8932b4..c2a1728 100644 --- a/src/bitboard.rs +++ b/src/bitboard.rs @@ -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 { diff --git a/src/board.rs b/src/board.rs index 5d2233c..b495f61 100644 --- a/src/board.rs +++ b/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 + 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) diff --git a/src/complexagent.rs b/src/complexagent.rs index 899406d..8535089 100644 --- a/src/complexagent.rs +++ b/src/complexagent.rs @@ -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), 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(); }