From f148fcb901b5635e83f466a056400fb53cf4a770 Mon Sep 17 00:00:00 2001 From: Rostyslav Hnatyshyn Date: Tue, 2 Jan 2024 23:32:06 -0700 Subject: [PATCH] clean up imports, turns out world can be mut --- src/lib.rs | 7 +++++++ src/lib/ai.rs | 34 +++++++++++++++------------------- src/lib/entity.rs | 4 ++-- src/lib/screen.rs | 3 +-- src/lib/world.rs | 20 +++++++++----------- src/main.rs | 4 ++-- 6 files changed, 36 insertions(+), 36 deletions(-) create mode 100644 src/lib.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..9c28714 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,7 @@ +pub mod lib { + pub mod point; + pub mod screen; + pub mod world; + pub mod entity; + pub mod ai; +} diff --git a/src/lib/ai.rs b/src/lib/ai.rs index e519938..b7ec3fe 100644 --- a/src/lib/ai.rs +++ b/src/lib/ai.rs @@ -1,12 +1,12 @@ -use crate::lib::screen::{Screen, BoardCommand}; -use crate::World; -use crate::Point; 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::thread_rng; 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; } @@ -15,29 +15,28 @@ impl AI for Ant { self.pos.clone() } // 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 mut rng = thread_rng(); let choice = valid.choose(&mut rng).cloned(); - if choice.is_none() { - return BoardCommand::Noop; - } else { + if !choice.is_none() { let pos = choice.unwrap(); if w.cleared.contains(&pos) { let old_pos = self.pos; self.pos = pos; - return BoardCommand::Move(old_pos, pos); + w.update_occupied(&old_pos, &pos); } else { - return BoardCommand::Dig(pos); + w.clear(pos); } } + BoardCommand::Noop } } 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 { self.counter -= 1; return BoardCommand::Noop; @@ -55,15 +54,13 @@ impl AI for Queen { fn get_position(&self) -> Point { 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 = w.get_valid_movements(&self.pos, b); let mut rng = thread_rng(); let choice = valid.choose(&mut rng).cloned(); - if choice.is_none() { - return BoardCommand::Noop; - } else { + if !choice.is_none() { let pos = choice.unwrap(); if w.cleared.contains(&pos) { // choose between laying an egg and moving @@ -73,11 +70,10 @@ impl AI for Queen { } else { let old_pos = self.pos; self.pos = pos; - return BoardCommand::Move(old_pos, pos); + w.update_occupied(&old_pos, &pos); } - } else { - return BoardCommand::Noop; } } + BoardCommand::Noop } } diff --git a/src/lib/entity.rs b/src/lib/entity.rs index 01edd05..3e538b2 100644 --- a/src/lib/entity.rs +++ b/src/lib/entity.rs @@ -1,6 +1,6 @@ use crate::lib::ai::AI; -use crate::Point; -use crate::Screen; +use crate::lib::point::Point; +use crate::lib::screen::Screen; use std::collections::HashMap; use ncurses::*; diff --git a/src/lib/screen.rs b/src/lib/screen.rs index ecd94c3..f3d104a 100644 --- a/src/lib/screen.rs +++ b/src/lib/screen.rs @@ -1,4 +1,4 @@ -use crate::Point; +use crate::lib::point::Point; pub struct Screen { pub center: Point, @@ -74,7 +74,6 @@ fn test_get_valid_movements_board() { pub enum BoardCommand { Dig(Point), - Move(Point, Point), LayEgg(Point, u32), Hatch(u32, u32), Noop, diff --git a/src/lib/world.rs b/src/lib/world.rs index d603d4b..38f0610 100644 --- a/src/lib/world.rs +++ b/src/lib/world.rs @@ -1,6 +1,6 @@ -use crate::{Screen, BoardCommand}; -use crate::Point; -use crate::{Entities, Queen}; +use crate::lib::screen::{Screen, BoardCommand}; +use crate::lib::point::Point; +use crate::lib::entity::{Entities, Queen}; use std::collections::HashSet; #[derive(Clone)] @@ -29,10 +29,14 @@ impl World { .map(|p| p.clone()) .collect() } + + pub fn update_occupied(&mut self, old: &Point, new: &Point) { + self.occupied.remove(old); + self.occupied.insert(*new); + } } pub fn render(e: &Entities, w: &World, b: &Screen) { - for c in w.cleared.iter() { b.render(c, "x"); } @@ -46,17 +50,11 @@ pub fn simulate(e: &mut Entities, w: &mut World, b: &mut Screen) { let cmds: Vec = e .data .values_mut() - .map(|a| a.step(b, &w)) + .map(|a| a.step(b, w)) .collect(); for cmd in cmds { match cmd { - BoardCommand::Move(old_pos, pos) => { - // still makes no difference - // occupied needs to be updated as soon as the move is made... - w.occupied.remove(&old_pos); - w.occupied.insert(pos); - } BoardCommand::Dig(pos) => { w.clear(pos); } diff --git a/src/main.rs b/src/main.rs index 29c565c..a27a0eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,9 +15,9 @@ mod lib { } use lib::point::Point; -use lib::screen::{init_screen, BoardCommand, Screen}; +use lib::screen::init_screen; use lib::world::{World, simulate, render}; -use lib::entity::{Queen, Entities}; +use lib::entity::Entities; fn main() { let mut board = init_screen();