Fix logic error regarding mills

This commit is contained in:
hodasemi 2023-05-12 09:04:48 +02:00
parent 27eb709c02
commit 9b0b9d5680
2 changed files with 18 additions and 12 deletions

View file

@ -54,7 +54,7 @@ pub enum LogSeverity {
Debug, Debug,
} }
const LOG_SEVERITY: LogSeverity = LogSeverity::Basic; const LOG_SEVERITY: LogSeverity = LogSeverity::Debug;
impl PlayerColor { impl PlayerColor {
pub fn swap(&mut self) { pub fn swap(&mut self) {
@ -483,7 +483,7 @@ impl MillGame {
scene: &mut Scene, scene: &mut Scene,
board: &mut [[[BoardSlot; 3]; 3]; 3], board: &mut [[[BoardSlot; 3]; 3]; 3],
state: &mut GameState, state: &mut GameState,
) -> Result<MillState> { ) -> Result<Option<MillState>> {
Self::log( Self::log(
&format!("move stone ({:?}) to ({:?})", (x, y, z), (tx, ty, tz)), &format!("move stone ({:?}) to ({:?})", (x, y, z), (tx, ty, tz)),
LogSeverity::Debug, LogSeverity::Debug,
@ -492,9 +492,11 @@ impl MillGame {
let slot = &board[x][y][z]; let slot = &board[x][y][z];
let neighbour = &board[tx][ty][tz]; let neighbour = &board[tx][ty][tz];
if neighbour.state() != BoardSlotState::Empty { if neighbour.state() != BoardSlotState::Empty
|| !Self::is_neighbour((x, y, z), (tx, ty, tz))
{
Self::log("neighbour not empty", LogSeverity::Debug); Self::log("neighbour not empty", LogSeverity::Debug);
return Ok(MillState::None); return Ok(None);
} }
neighbour.set_state(slot.state()); neighbour.set_state(slot.state());
@ -544,7 +546,7 @@ impl MillGame {
LogSeverity::Debug, LogSeverity::Debug,
); );
Ok(millstate) Ok(Some(millstate))
} }
pub fn remove_stone(stone: &mut Stone, slot: &mut BoardSlot, scene: &mut Scene) -> Result<()> { pub fn remove_stone(stone: &mut Stone, slot: &mut BoardSlot, scene: &mut Scene) -> Result<()> {
@ -822,9 +824,12 @@ impl EngineObject for MillGame {
match *selected_slot { match *selected_slot {
Some((x, y, z)) => { Some((x, y, z)) => {
if Self::is_neighbour((x, y, z), (tx, ty, tz)) { if Self::is_neighbour((x, y, z), (tx, ty, tz)) {
if Self::move_stone((x,y,z), (tx,ty,tz), scene, board, &mut state)? == MillState::None { if let Some(millstate)
*selected_slot = None; = Self::move_stone((x,y,z), (tx,ty,tz), scene, board, &mut state)? {
self.finish_turn(); if let MillState::None = millstate {
*selected_slot = None;
self.finish_turn();
}
} }
} }
} }

View file

@ -129,15 +129,16 @@ impl SimpleAI {
let (tx, ty, tz) = (neighbour.x, neighbour.y, neighbour.z); let (tx, ty, tz) = (neighbour.x, neighbour.y, neighbour.z);
scene.on_scene(|scene| { scene.on_scene(|scene| {
if MillGame::move_stone( if let Some(millstate) = MillGame::move_stone(
(x, y, z), (x, y, z),
(tx, ty, tz), (tx, ty, tz),
scene, scene,
board, board,
state, state,
)? != MillState::None )? {
{ if MillState::None != millstate {
found_a_mill = true; found_a_mill = true;
}
} }
Ok(()) Ok(())