This commit is contained in:
Simon Gardling 2025-01-28 15:48:44 -05:00
parent aab639c04d
commit 2189f00cb3
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
4 changed files with 39 additions and 19 deletions

View File

@ -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<Piece> {
@ -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`]

View File

@ -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 {

View File

@ -25,7 +25,7 @@ impl Game {
pub const fn new(player1: Box<dyn Agent>, player2: Box<dyn Agent>) -> 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));
}
}
}

View File

@ -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;