|
|
@ -1,12 +1,12 @@ |
|
|
|
use crate::lib::screen::{Screen, BoardCommand}; |
|
|
|
|
|
|
|
use crate::World; |
|
|
|
|
|
|
|
use crate::Point; |
|
|
|
|
|
|
|
use crate::lib::entity::{Ant, Egg, Queen}; |
|
|
|
use crate::lib::entity::{Ant, Egg, Queen}; |
|
|
|
use rand::thread_rng; |
|
|
|
use crate::lib::point::Point; |
|
|
|
|
|
|
|
use crate::lib::screen::{BoardCommand, Screen}; |
|
|
|
|
|
|
|
use crate::lib::world::World; |
|
|
|
use rand::prelude::SliceRandom; |
|
|
|
use rand::prelude::SliceRandom; |
|
|
|
|
|
|
|
use rand::thread_rng; |
|
|
|
|
|
|
|
|
|
|
|
pub trait AI { |
|
|
|
pub trait AI { |
|
|
|
fn step(&mut self, b: &Screen, w: &World) -> BoardCommand; |
|
|
|
fn step(&mut self, b: &Screen, w: &mut World) -> BoardCommand; |
|
|
|
fn get_position(&self) -> Point; |
|
|
|
fn get_position(&self) -> Point; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -15,29 +15,28 @@ impl AI for Ant { |
|
|
|
self.pos.clone() |
|
|
|
self.pos.clone() |
|
|
|
} |
|
|
|
} |
|
|
|
// return the next move for this ant
|
|
|
|
// return the next move for this ant
|
|
|
|
fn step(&mut self, b: &Screen, w: &World) -> BoardCommand { |
|
|
|
fn step(&mut self, b: &Screen, w: &mut World) -> BoardCommand { |
|
|
|
let valid = w.get_valid_movements(&self.pos, b); |
|
|
|
let valid = w.get_valid_movements(&self.pos, b); |
|
|
|
let mut rng = thread_rng(); |
|
|
|
let mut rng = thread_rng(); |
|
|
|
|
|
|
|
|
|
|
|
let choice = valid.choose(&mut rng).cloned(); |
|
|
|
let choice = valid.choose(&mut rng).cloned(); |
|
|
|
|
|
|
|
|
|
|
|
if choice.is_none() { |
|
|
|
if !choice.is_none() { |
|
|
|
return BoardCommand::Noop; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
let pos = choice.unwrap(); |
|
|
|
let pos = choice.unwrap(); |
|
|
|
if w.cleared.contains(&pos) { |
|
|
|
if w.cleared.contains(&pos) { |
|
|
|
let old_pos = self.pos; |
|
|
|
let old_pos = self.pos; |
|
|
|
self.pos = pos; |
|
|
|
self.pos = pos; |
|
|
|
return BoardCommand::Move(old_pos, pos); |
|
|
|
w.update_occupied(&old_pos, &pos); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return BoardCommand::Dig(pos); |
|
|
|
w.clear(pos); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
BoardCommand::Noop |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl AI for Egg { |
|
|
|
impl AI for Egg { |
|
|
|
fn step(&mut self, b: &Screen, w: &World) -> BoardCommand { |
|
|
|
fn step(&mut self, b: &Screen, w: &mut World) -> BoardCommand { |
|
|
|
if self.counter > 0 { |
|
|
|
if self.counter > 0 { |
|
|
|
self.counter -= 1; |
|
|
|
self.counter -= 1; |
|
|
|
return BoardCommand::Noop; |
|
|
|
return BoardCommand::Noop; |
|
|
@ -55,15 +54,13 @@ impl AI for Queen { |
|
|
|
fn get_position(&self) -> Point { |
|
|
|
fn get_position(&self) -> Point { |
|
|
|
self.pos.clone() |
|
|
|
self.pos.clone() |
|
|
|
} |
|
|
|
} |
|
|
|
fn step(&mut self, b: &Screen, w: &World) -> BoardCommand { |
|
|
|
fn step(&mut self, b: &Screen, w: &mut World) -> BoardCommand { |
|
|
|
let valid: Vec<Point> = w.get_valid_movements(&self.pos, b); |
|
|
|
let valid: Vec<Point> = w.get_valid_movements(&self.pos, b); |
|
|
|
let mut rng = thread_rng(); |
|
|
|
let mut rng = thread_rng(); |
|
|
|
|
|
|
|
|
|
|
|
let choice = valid.choose(&mut rng).cloned(); |
|
|
|
let choice = valid.choose(&mut rng).cloned(); |
|
|
|
|
|
|
|
|
|
|
|
if choice.is_none() { |
|
|
|
if !choice.is_none() { |
|
|
|
return BoardCommand::Noop; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
let pos = choice.unwrap(); |
|
|
|
let pos = choice.unwrap(); |
|
|
|
if w.cleared.contains(&pos) { |
|
|
|
if w.cleared.contains(&pos) { |
|
|
|
// choose between laying an egg and moving
|
|
|
|
// choose between laying an egg and moving
|
|
|
@ -73,11 +70,10 @@ impl AI for Queen { |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
let old_pos = self.pos; |
|
|
|
let old_pos = self.pos; |
|
|
|
self.pos = pos; |
|
|
|
self.pos = pos; |
|
|
|
return BoardCommand::Move(old_pos, pos); |
|
|
|
w.update_occupied(&old_pos, &pos); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
return BoardCommand::Noop; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
BoardCommand::Noop |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|