2023-05-09 16:58:07 +00:00
|
|
|
use crate::{
|
|
|
|
board::{BoardSlot, BoardSlotState},
|
2023-05-10 05:47:10 +00:00
|
|
|
game::{GameState, MillGame, PlayerColor, Stone, StoneState},
|
2023-05-09 16:58:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use anyhow::Result;
|
|
|
|
use engine::prelude::*;
|
|
|
|
|
|
|
|
pub struct SimpleAI {
|
|
|
|
pub player_color: PlayerColor,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SimpleAI {
|
|
|
|
pub fn new(player_color: PlayerColor) -> Self {
|
|
|
|
Self { player_color }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn step(
|
|
|
|
&self,
|
|
|
|
stones: &mut [Stone; 9],
|
|
|
|
board: &mut [[[BoardSlot; 3]; 3]; 3],
|
|
|
|
scene: &mut SceneHandle,
|
2023-05-10 05:47:10 +00:00
|
|
|
state: GameState,
|
2023-05-09 16:58:07 +00:00
|
|
|
) -> Result<()> {
|
2023-05-10 05:47:10 +00:00
|
|
|
MillGame::log(&format!("AI at state {:?}", state));
|
|
|
|
|
|
|
|
match state {
|
|
|
|
GameState::Placing => {
|
|
|
|
if let Some(placable) = stones
|
|
|
|
.iter_mut()
|
|
|
|
.find(|stone| stone.state == StoneState::ReadyToBePlaced)
|
|
|
|
{
|
|
|
|
let mut free_slots: Vec<&mut BoardSlot> = board
|
|
|
|
.iter_mut()
|
|
|
|
.flatten()
|
|
|
|
.flatten()
|
|
|
|
.filter(|slot| slot.state == BoardSlotState::Empty)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let len = free_slots.len();
|
|
|
|
|
|
|
|
let slot = &mut free_slots[Random::range(0, len as u32) as usize];
|
|
|
|
|
|
|
|
scene.on_scene(|scene| {
|
|
|
|
MillGame::place_stone(placable, slot, self.player_color, scene)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GameState::Removing => {
|
|
|
|
let mut other_color: Vec<&mut BoardSlot> = board
|
|
|
|
.iter_mut()
|
|
|
|
.flatten()
|
|
|
|
.flatten()
|
|
|
|
.filter(|slot| match (slot.state, self.player_color) {
|
|
|
|
(BoardSlotState::Black(_), PlayerColor::White) => true,
|
|
|
|
(BoardSlotState::White(_), PlayerColor::Black) => true,
|
|
|
|
_ => false,
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let len = other_color.len();
|
|
|
|
|
|
|
|
let slot = &mut other_color[Random::range(0, len as u32) as usize];
|
|
|
|
|
|
|
|
scene.on_scene(|scene| {
|
|
|
|
MillGame::remove_stone(slot, scene)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})?;
|
|
|
|
}
|
|
|
|
GameState::Main => todo!(),
|
2023-05-09 16:58:07 +00:00
|
|
|
}
|
|
|
|
|
2023-05-10 05:47:10 +00:00
|
|
|
MillGame::log("finish AI");
|
|
|
|
|
2023-05-09 16:58:07 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|