From 2189f00cb3cf5519346a75904ff92a3e6581792a Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Tue, 28 Jan 2025 15:48:44 -0500 Subject: [PATCH] a --- src/board.rs | 30 +++++++++++++++++++++++++----- src/complexagent.rs | 15 +++++---------- src/game.rs | 10 ++++++---- src/misc.rs | 3 +++ 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/board.rs b/src/board.rs index 95b7fa1..8dee53a 100644 --- a/src/board.rs +++ b/src/board.rs @@ -39,6 +39,14 @@ impl Board { } } + pub const fn starting_pos(mut self) -> Self { + self.place_unchecked(3, 3, Piece::White); + self.place_unchecked(4, 3, Piece::Black); + self.place_unchecked(3, 4, Piece::Black); + self.place_unchecked(4, 4, Piece::White); + self + } + /// 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 { @@ -55,13 +63,21 @@ impl Board { *self.get_mut(i, j) = Some(piece); } + pub fn test_prop(&self, i: usize, j: usize, piece: Piece) -> bool { + let mut self_copy = self.clone(); + if self_copy.get(i, j).is_some() { + return false; + } + self_copy.place_and_prop_unchecked(i, j, piece) + } + /// 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(&mut self, i: usize, j: usize) { + fn propegate_from(&mut self, i: usize, j: usize) -> bool { // returns if that place is empty let Some(starting_color) = *self.get(i, j) else { - return; + return false; }; // Create all chains from the piece being propegated from in `i` and `j` coordinates @@ -69,6 +85,7 @@ impl Board { .into_iter() .map(|range| range.into_iter().map(|i| (i, j)).collect()) .collect(); + chains.extend( split_from(0, BOARD_SIZE - 1, j) .into_iter() @@ -80,6 +97,7 @@ impl Board { let Some(piece) = self.get(new_i, new_j) else { break; }; + if piece == &starting_color { if chain_length > 0 { // fill all opposite colors with this color @@ -91,18 +109,20 @@ impl Board { for &(i_o, j_o) in history { self.place_and_prop_unchecked(i_o, j_o, starting_color); } + return true; } // either the other pieces were replaced, or this was an invalid chain, // in both cases, the loop needs to be breaked - break; + return false; } } } + false } - fn place_and_prop_unchecked(&mut self, i: usize, j: usize, piece: Piece) { + fn place_and_prop_unchecked(&mut self, i: usize, j: usize, piece: Piece) -> bool { self.place_unchecked(i, j, piece); - self.propegate_from(i, j); + self.propegate_from(i, j) } /// Place a piece on the [`Board`] diff --git a/src/complexagent.rs b/src/complexagent.rs index c32f295..ae36b77 100644 --- a/src/complexagent.rs +++ b/src/complexagent.rs @@ -15,17 +15,12 @@ impl Agent for ComplexAgent { fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> { (0..BOARD_SIZE) .zip(0..BOARD_SIZE) - .filter(|&(i, j)| board.get(i, j) == &Some(!self.color)) - .flat_map(|(i, j)| offsets(i, j, 1)) - .filter(|&(i, j)| i < BOARD_SIZE && j < BOARD_SIZE) - .filter(|&(i, j)| board.get(i, j).is_none()) + .filter(|&(i, j)| board.test_prop(i, j, self.color)) + // .filter(|&(i, j)| board.get(i, j) == &Some(!self.color)) + // .flat_map(|(i, j)| offsets(i, j, 1)) + // .filter(|&(i, j)| i < BOARD_SIZE && j < BOARD_SIZE) + // .filter(|&(i, j)| board.get(i, j).is_none()) .choose(&mut self.rng) - .or_else(|| { - (0..BOARD_SIZE) - .zip(0..BOARD_SIZE) - .filter(|&(i, j)| board.get(i, j).is_none()) - .choose(&mut self.rng) - }) } fn name(&self) -> &'static str { diff --git a/src/game.rs b/src/game.rs index 7520f1f..ab9f1db 100644 --- a/src/game.rs +++ b/src/game.rs @@ -25,7 +25,7 @@ impl Game { pub const fn new(player1: Box, player2: Box) -> Self { Self { players: [player1, player2], - board: Board::new(), + board: Board::new().starting_pos(), } } @@ -45,15 +45,17 @@ impl Game { pub fn game_loop(&mut self) { let mut i = 0; loop { - self.step(i); - // alternate which player plays i += 1; i %= self.players.len(); println!("{}", self); - std::thread::sleep(Duration::from_millis(100)); + // return; + + self.step(i); + + std::thread::sleep(Duration::from_millis(200)); } } } diff --git a/src/misc.rs b/src/misc.rs index c74f2d8..1b1d63c 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -31,6 +31,9 @@ pub fn offsets(i: usize, j: usize, range: usize) -> Vec<(usize, usize)> { } for j_o in [-(range as i16), 0, range as i16] { + if (i_o, j_o) == (0, 0) { + continue; + } let new_j = j as i16 + j_o; if 0 > new_j { continue;