Trying to fix main state

This commit is contained in:
hodasemi 2023-05-16 09:05:04 +02:00
parent 4ad7a022dc
commit 01deca2c96
2 changed files with 74 additions and 40 deletions

View file

@ -407,14 +407,12 @@ impl MillGame {
*self.last_turn_timing.lock().unwrap() = self.engine.time(); *self.last_turn_timing.lock().unwrap() = self.engine.time();
self.turn_finished.store(true, SeqCst); self.turn_finished.store(true, SeqCst);
*self.selected_field.lock().unwrap() = None;
} }
fn print_game_state(&self, severity: LogSeverity, log_state: bool) -> Result<()> { fn print_game_state(&self, severity: LogSeverity, log_state: bool) -> Result<()> {
let current_turn_state = TurnState::new(self)?; let current_turn_state = TurnState::new(self)?;
// verity that last turn actually something happened
debug_assert!(!current_turn_state.is_empty());
if log_state { if log_state {
current_turn_state.log(severity); current_turn_state.log(severity);
} }
@ -424,6 +422,10 @@ impl MillGame {
if !turn_states.is_empty() { if !turn_states.is_empty() {
let (new_diff, old_diff) = current_turn_state.diff(turn_states.last().unwrap()); let (new_diff, old_diff) = current_turn_state.diff(turn_states.last().unwrap());
// verity that last turn actually something happened
debug_assert!(!new_diff.is_empty());
debug_assert!(!old_diff.is_empty());
Self::log(" ===== OLD DIFF =====", severity); Self::log(" ===== OLD DIFF =====", severity);
old_diff.log(severity); old_diff.log(severity);
Self::log(" ===== NEW DIFF =====", severity); Self::log(" ===== NEW DIFF =====", severity);
@ -520,14 +522,14 @@ impl MillGame {
if player == self.simple_ai.player_color { if player == self.simple_ai.player_color {
Self::log("current player is AI", LogSeverity::Debug); Self::log("current player is AI", LogSeverity::Debug);
if !self.simple_ai.step( self.simple_ai.step(
&mut self.stones(self.simple_ai.player_color), &mut self.stones(self.simple_ai.player_color),
self.board.lock().unwrap().slots(), self.board.lock().unwrap().slots(),
&mut *self.scene.lock().unwrap(), &mut *self.scene.lock().unwrap(),
&mut *self.state.lock().unwrap(), &mut *self.state.lock().unwrap(),
)? { )?;
self.finish_turn();
} self.finish_turn();
} }
Self::log("leave next_game_step", LogSeverity::Debug); Self::log("leave next_game_step", LogSeverity::Debug);
@ -875,7 +877,7 @@ impl MillGame {
pub fn log(s: &str, log_severity: LogSeverity) { pub fn log(s: &str, log_severity: LogSeverity) {
match (LOG_SEVERITY, log_severity) { match (LOG_SEVERITY, log_severity) {
(LogSeverity::Basic, LogSeverity::Basic) => Self::write(s), (LogSeverity::Basic, LogSeverity::Basic) => Self::write(s),
(LogSeverity::Basic, LogSeverity::Debug) => (), (LogSeverity::Basic, LogSeverity::Debug) => Self::write_extra(s),
(LogSeverity::Debug, LogSeverity::Basic) => Self::write(s), (LogSeverity::Debug, LogSeverity::Basic) => Self::write(s),
(LogSeverity::Debug, LogSeverity::Debug) => Self::write(s), (LogSeverity::Debug, LogSeverity::Debug) => Self::write(s),
} }
@ -886,6 +888,16 @@ impl MillGame {
if let Err(_) = file.write_all(format!("{}\n", s.to_string()).as_bytes()) {} if let Err(_) = file.write_all(format!("{}\n", s.to_string()).as_bytes()) {}
} }
} }
fn write_extra(s: &str) {
if let Ok(mut file) = OpenOptions::new()
.append(true)
.create(true)
.open("millgame_extra.log")
{
if let Err(_) = file.write_all(format!("{}\n", s.to_string()).as_bytes()) {}
}
}
} }
impl EngineObject for MillGame { impl EngineObject for MillGame {
@ -1015,6 +1027,8 @@ impl EngineObject for MillGame {
= Self::move_stone((x,y,z), (tx,ty,tz), scene, board, &mut state)? { = Self::move_stone((x,y,z), (tx,ty,tz), scene, board, &mut state)? {
if let MillState::None = millstate { if let MillState::None = millstate {
*selected_slot = None; *selected_slot = None;
drop(selected_slot);
self.finish_turn(); self.finish_turn();
} }
} }

View file

@ -21,7 +21,7 @@ impl SimpleAI {
board: &mut [[[BoardSlot; 3]; 3]; 3], board: &mut [[[BoardSlot; 3]; 3]; 3],
scene: &mut SceneHandle, scene: &mut SceneHandle,
state: &mut GameState, state: &mut GameState,
) -> Result<bool> { ) -> Result<()> {
MillGame::log(&format!("AI at state {:?}", *state), LogSeverity::Debug); MillGame::log(&format!("AI at state {:?}", *state), LogSeverity::Debug);
let mut found_a_mill = false; let mut found_a_mill = false;
@ -93,6 +93,8 @@ impl SimpleAI {
}) })
.unwrap(); .unwrap();
MillGame::log("[AI] remove stone", LogSeverity::Debug);
scene.on_scene(|scene| { scene.on_scene(|scene| {
MillGame::remove_stone(stone, slot, scene)?; MillGame::remove_stone(stone, slot, scene)?;
@ -100,35 +102,39 @@ impl SimpleAI {
})?; })?;
} }
GameState::Main => { GameState::Main => {
let same_color: Vec<&BoardSlot> = board scene.on_scene(|scene| {
.iter() loop {
.flatten() let same_color: Vec<&BoardSlot> = board
.flatten() .iter()
.filter(|slot| match (slot.state(), self.player_color) { .flatten()
(BoardSlotState::White(_), PlayerColor::White) => true, .flatten()
(BoardSlotState::Black(_), PlayerColor::Black) => true, .filter(|slot| match (slot.state(), self.player_color) {
_ => false, (BoardSlotState::White(_), PlayerColor::White) => true,
}) (BoardSlotState::Black(_), PlayerColor::Black) => true,
.collect(); _ => false,
})
.collect();
loop { let len = same_color.len();
let len = same_color.len(); let slot = &same_color[Random::range(0, len as u32) as usize];
let slot = &same_color[Random::range(0, len as u32) as usize]; let n = Board::get_neighbours(slot, board);
let n = Board::get_neighbours(slot, board); let neighbours: Vec<&&BoardSlot> = n
let neighbours: Vec<&&BoardSlot> = n .iter()
.iter() .filter(|n| n.state() == BoardSlotState::Empty)
.filter(|n| n.state() == BoardSlotState::Empty) .collect();
.collect();
if !neighbours.is_empty() { if !neighbours.is_empty() {
let neighbour = let neighbour =
&neighbours[Random::range(0, neighbours.len() as u32) as usize]; &neighbours[Random::range(0, neighbours.len() as u32) as usize];
if neighbour.state() == BoardSlotState::Empty { if neighbour.state() == BoardSlotState::Empty {
let (x, y, z) = (slot.x, slot.y, slot.z); let (x, y, z) = (slot.x, slot.y, slot.z);
let (tx, ty, tz) = (neighbour.x, neighbour.y, neighbour.z); let (tx, ty, tz) = (neighbour.x, neighbour.y, neighbour.z);
let mut successful_move = false;
MillGame::log("[AI] going to make a move", LogSeverity::Debug);
scene.on_scene(|scene| {
if let Some(millstate) = MillGame::move_stone( if let Some(millstate) = MillGame::move_stone(
(x, y, z), (x, y, z),
(tx, ty, tz), (tx, ty, tz),
@ -136,24 +142,38 @@ impl SimpleAI {
board, board,
state, state,
)? { )? {
MillGame::log("[AI] successful move", LogSeverity::Debug);
if MillState::None != millstate { if MillState::None != millstate {
MillGame::log("[AI] found a mill", LogSeverity::Debug);
found_a_mill = true; found_a_mill = true;
} }
successful_move = true;
} else {
MillGame::log("[AI] move failed", LogSeverity::Debug);
} }
Ok(()) if successful_move {
})?; break;
}
break; }
} }
} }
}
Ok(())
})?;
} }
GameState::Waiting => unreachable!(), GameState::Waiting => unreachable!(),
} }
if found_a_mill {
self.step(stones, board, scene, state)?;
}
MillGame::log("finish AI", LogSeverity::Debug); MillGame::log("finish AI", LogSeverity::Debug);
Ok(found_a_mill) Ok(())
} }
} }