clean up imports, turns out world can be mut

This commit is contained in:
2024-01-02 23:32:06 -07:00
parent cb020f73d4
commit f148fcb901
6 changed files with 36 additions and 36 deletions
+7
View File
@@ -0,0 +1,7 @@
pub mod lib {
pub mod point;
pub mod screen;
pub mod world;
pub mod entity;
pub mod ai;
}
+15 -19
View File
@@ -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<Point> = 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
}
}
+2 -2
View File
@@ -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::*;
+1 -2
View File
@@ -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,
+9 -11
View File
@@ -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<BoardCommand> = 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);
}
+2 -2
View File
@@ -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();