improvements
This commit is contained in:
parent
2189f00cb3
commit
6ed4208534
54
src/board.rs
54
src/board.rs
@ -63,21 +63,38 @@ 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 {
|
pub fn move_would_propegate(&self, i: usize, j: usize, piece: Piece) -> i16 {
|
||||||
|
self.what_if(i, j, piece).map(|x| x.1 as i16).unwrap_or(-1)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn what_if(&self, i: usize, j: usize, piece: Piece) -> Option<(Self, usize)> {
|
||||||
let mut self_copy = self.clone();
|
let mut self_copy = self.clone();
|
||||||
if self_copy.get(i, j).is_some() {
|
if self_copy.get(i, j).is_some() {
|
||||||
return false;
|
return None;
|
||||||
}
|
}
|
||||||
self_copy.place_and_prop_unchecked(i, j, piece)
|
self_copy.place_unchecked(i, j, piece);
|
||||||
|
let how_many_prop = self_copy.propegate_from(i, j);
|
||||||
|
Some((self_copy, how_many_prop))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn place(&mut self, i: usize, j: usize, piece: Piece) -> Result<(), String> {
|
||||||
|
let what_if_result = self.what_if(i, j, piece);
|
||||||
|
if let Some(what_if_result) = what_if_result {
|
||||||
|
if what_if_result.1 > 0 {
|
||||||
|
*self = what_if_result.0;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(format!("move would not propegate"))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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) -> bool {
|
fn propegate_from(&mut self, i: usize, j: usize) -> usize {
|
||||||
// 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 false;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 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
|
||||||
@ -92,6 +109,8 @@ impl Board {
|
|||||||
.map(|range| range.into_iter().map(|j| (i, j)).collect()),
|
.map(|range| range.into_iter().map(|j| (i, j)).collect()),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let mut captured: usize = 0;
|
||||||
|
|
||||||
for chain in chains {
|
for chain in chains {
|
||||||
for (chain_length, &(new_i, new_j)) in chain.iter().enumerate() {
|
for (chain_length, &(new_i, new_j)) in chain.iter().enumerate() {
|
||||||
let Some(piece) = self.get(new_i, new_j) else {
|
let Some(piece) = self.get(new_i, new_j) else {
|
||||||
@ -107,34 +126,17 @@ impl Board {
|
|||||||
|
|
||||||
// fill all opposite colors with this color
|
// fill all opposite colors with this color
|
||||||
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_unchecked(i_o, j_o, starting_color);
|
||||||
|
captured += self.propegate_from(i_o, j_o) + 1;
|
||||||
}
|
}
|
||||||
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
|
||||||
return false;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
false
|
captured
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Place a piece on the [`Board`]
|
|
||||||
/// Returns an error if there already exists a piece there
|
|
||||||
pub fn place(&mut self, i: usize, j: usize, piece: Piece) -> Result<(), String> {
|
|
||||||
if self.get(i, j).is_some() {
|
|
||||||
return Err(format!("Cannot place on ({},{}), piece exists", i, j));
|
|
||||||
} else {
|
|
||||||
self.place_and_prop_unchecked(i, j, piece);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -14,13 +14,15 @@ pub struct ComplexAgent {
|
|||||||
impl Agent for ComplexAgent {
|
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)
|
.flat_map(|i| {
|
||||||
.filter(|&(i, j)| board.test_prop(i, j, self.color))
|
(0..BOARD_SIZE)
|
||||||
// .filter(|&(i, j)| board.get(i, j) == &Some(!self.color))
|
.map(|j| (i, j))
|
||||||
// .flat_map(|(i, j)| offsets(i, j, 1))
|
.collect::<Vec<(usize, usize)>>()
|
||||||
// .filter(|&(i, j)| i < BOARD_SIZE && j < BOARD_SIZE)
|
})
|
||||||
// .filter(|&(i, j)| board.get(i, j).is_none())
|
.map(|(i, j)| (i, j, board.move_would_propegate(i, j, self.color)))
|
||||||
.choose(&mut self.rng)
|
.filter(|&(_, _, c)| c >= 0)
|
||||||
|
.max_by_key(|&(_, _, c)| c)
|
||||||
|
.map(|(i, j, _)| (i, j))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name(&self) -> &'static str {
|
fn name(&self) -> &'static str {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user