a
This commit is contained in:
parent
aab639c04d
commit
2189f00cb3
30
src/board.rs
30
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`]
|
/// Returns a mutable reference to a place on the [`Board`]
|
||||||
/// at (i, j)
|
/// at (i, j)
|
||||||
pub const fn get_mut(&mut self, i: usize, j: usize) -> &mut Option<Piece> {
|
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);
|
*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)
|
/// Propegate piece captures originating from (i, j)
|
||||||
/// DO NOT USE THIS ALONE, this should be called as a part of
|
/// DO NOT USE THIS ALONE, this should be called as a part of
|
||||||
/// [`Board::place`] or [`Board::place_and_prop_unchecked`]
|
/// [`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
|
// returns if that place is empty
|
||||||
let Some(starting_color) = *self.get(i, j) else {
|
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
|
// Create all chains from the piece being propegated from in `i` and `j` coordinates
|
||||||
@ -69,6 +85,7 @@ impl Board {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|range| range.into_iter().map(|i| (i, j)).collect())
|
.map(|range| range.into_iter().map(|i| (i, j)).collect())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
chains.extend(
|
chains.extend(
|
||||||
split_from(0, BOARD_SIZE - 1, j)
|
split_from(0, BOARD_SIZE - 1, j)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@ -80,6 +97,7 @@ impl Board {
|
|||||||
let Some(piece) = self.get(new_i, new_j) else {
|
let Some(piece) = self.get(new_i, new_j) else {
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
|
|
||||||
if piece == &starting_color {
|
if piece == &starting_color {
|
||||||
if chain_length > 0 {
|
if chain_length > 0 {
|
||||||
// fill all opposite colors with this color
|
// fill all opposite colors with this color
|
||||||
@ -91,18 +109,20 @@ impl Board {
|
|||||||
for &(i_o, j_o) in history {
|
for &(i_o, j_o) in history {
|
||||||
self.place_and_prop_unchecked(i_o, j_o, starting_color);
|
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,
|
// either the other pieces were replaced, or this was an invalid chain,
|
||||||
// in both cases, the loop needs to be breaked
|
// 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.place_unchecked(i, j, piece);
|
||||||
self.propegate_from(i, j);
|
self.propegate_from(i, j)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Place a piece on the [`Board`]
|
/// Place a piece on the [`Board`]
|
||||||
|
|||||||
@ -15,17 +15,12 @@ impl Agent for ComplexAgent {
|
|||||||
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
|
fn next_move(&mut self, board: &Board) -> Option<(usize, usize)> {
|
||||||
(0..BOARD_SIZE)
|
(0..BOARD_SIZE)
|
||||||
.zip(0..BOARD_SIZE)
|
.zip(0..BOARD_SIZE)
|
||||||
.filter(|&(i, j)| board.get(i, j) == &Some(!self.color))
|
.filter(|&(i, j)| board.test_prop(i, j, self.color))
|
||||||
.flat_map(|(i, j)| offsets(i, j, 1))
|
// .filter(|&(i, j)| board.get(i, j) == &Some(!self.color))
|
||||||
.filter(|&(i, j)| i < BOARD_SIZE && j < BOARD_SIZE)
|
// .flat_map(|(i, j)| offsets(i, j, 1))
|
||||||
.filter(|&(i, j)| board.get(i, j).is_none())
|
// .filter(|&(i, j)| i < BOARD_SIZE && j < BOARD_SIZE)
|
||||||
|
// .filter(|&(i, j)| board.get(i, j).is_none())
|
||||||
.choose(&mut self.rng)
|
.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 {
|
fn name(&self) -> &'static str {
|
||||||
|
|||||||
10
src/game.rs
10
src/game.rs
@ -25,7 +25,7 @@ impl Game {
|
|||||||
pub const fn new(player1: Box<dyn Agent>, player2: Box<dyn Agent>) -> Self {
|
pub const fn new(player1: Box<dyn Agent>, player2: Box<dyn Agent>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
players: [player1, player2],
|
players: [player1, player2],
|
||||||
board: Board::new(),
|
board: Board::new().starting_pos(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,15 +45,17 @@ impl Game {
|
|||||||
pub fn game_loop(&mut self) {
|
pub fn game_loop(&mut self) {
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
loop {
|
loop {
|
||||||
self.step(i);
|
|
||||||
|
|
||||||
// alternate which player plays
|
// alternate which player plays
|
||||||
i += 1;
|
i += 1;
|
||||||
i %= self.players.len();
|
i %= self.players.len();
|
||||||
|
|
||||||
println!("{}", self);
|
println!("{}", self);
|
||||||
|
|
||||||
std::thread::sleep(Duration::from_millis(100));
|
// return;
|
||||||
|
|
||||||
|
self.step(i);
|
||||||
|
|
||||||
|
std::thread::sleep(Duration::from_millis(200));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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] {
|
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;
|
let new_j = j as i16 + j_o;
|
||||||
if 0 > new_j {
|
if 0 > new_j {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user