49 lines
1.2 KiB
Rust
49 lines
1.2 KiB
Rust
|
use crate::{
|
||
|
board::{BoardSlot, BoardSlotState},
|
||
|
game::{MillGame, PlayerColor, Stone, StoneState},
|
||
|
};
|
||
|
|
||
|
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,
|
||
|
) -> Result<()> {
|
||
|
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(())
|
||
|
})?;
|
||
|
}
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|