sideways #1
+1
-1
@@ -63,7 +63,7 @@ impl AI for Ant {
|
||||
if w.cleared.contains(&pos) {
|
||||
self.pos = pos;
|
||||
} else {
|
||||
if w.is_safe_to_dig(&self.pos, &pos, &b) {
|
||||
if w.is_safe_to_dig(&pos, &b) {
|
||||
w.clear(pos);
|
||||
}
|
||||
}
|
||||
|
||||
+5
-1
@@ -14,6 +14,10 @@ impl Point {
|
||||
Point(self.0 - 1, self.1)
|
||||
}
|
||||
|
||||
pub fn is_below(&self, other: Point) -> bool {
|
||||
other.down() == *self
|
||||
}
|
||||
|
||||
pub fn right(&self) -> Point {
|
||||
Point(self.0 + 1, self.1)
|
||||
}
|
||||
@@ -99,7 +103,7 @@ pub fn astar(start: &Point, goal: &Point, w: Option<&World>, s: Option<&Screen>)
|
||||
Some(w) => current
|
||||
.get_neighbors()
|
||||
.iter()
|
||||
.filter(|p| w.is_valid_movement(p, s.unwrap()))
|
||||
.filter(|p| w.is_valid_movement(¤t, p, s.unwrap()))
|
||||
.map(|e| e.clone())
|
||||
.collect(),
|
||||
None => current.get_neighbors(),
|
||||
|
||||
+18
-49
@@ -21,44 +21,28 @@ impl World {
|
||||
self.cleared.insert(pos);
|
||||
}
|
||||
|
||||
// make sure that this is as simple & robust as possible
|
||||
// then, use this set of rules when digging
|
||||
// call astar from the suggested dig site to origin (or whatever "home" coordinate")
|
||||
// if route does not exist, do not dig
|
||||
pub fn is_valid_movement(&self, target: &Point, b: &Screen) -> bool {
|
||||
let safe =
|
||||
pub fn create_chamber(&mut self, center: Point, radius: i32) {
|
||||
let cx = center.0;
|
||||
let cy = center.1;
|
||||
|
||||
for i in cx-radius..cx+radius {
|
||||
for j in cy-radius..cy+radius {
|
||||
self.clear(Point(i,j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_valid_movement(&self, current: &Point, target: &Point, b: &Screen) -> bool {
|
||||
// should allow down movements always
|
||||
|
||||
let safe = target.is_below(*current) || (
|
||||
!self.cleared.contains(&target.down())
|
||||
|| !self.cleared.contains(&target.left())
|
||||
|| !self.cleared.contains(&target.right())
|
||||
|| !self.cleared.contains(&target.bldiag())
|
||||
|| !self.cleared.contains(&target.uldiag())
|
||||
|| !self.cleared.contains(&target.brdiag())
|
||||
|| !self.cleared.contains(&target.urdiag());
|
||||
|
||||
// can only go to target if
|
||||
// its above or left or right of a not cleared space
|
||||
|
||||
/*
|
||||
if target.is_above(current_pos) {
|
||||
safe =
|
||||
(!self.cleared.contains(&target.left()) || !self.cleared.contains(&target.right()))
|
||||
|| (!self.cleared.contains(¤t_pos.right()) && self.cleared.contains(&target.right()))
|
||||
|| (!self.cleared.contains(¤t_pos.left()) && self.cleared.contains(&target.left()));
|
||||
}
|
||||
|
||||
if target.is_adjacent(current_pos) {
|
||||
let target_down = target.down();
|
||||
if self.cleared.contains(&target_down) {
|
||||
safe = !self.cleared.contains(&target_down.left())
|
||||
|| !self.cleared.contains(&target_down.right());
|
||||
} else {
|
||||
safe = !self.cleared.contains(&target.down());
|
||||
}
|
||||
}
|
||||
|
||||
if target.is_below(current_pos) {
|
||||
safe = true;
|
||||
}*/
|
||||
|| !self.cleared.contains(&target.urdiag()));
|
||||
|
||||
// of course, its only a valid move if you can walk there!
|
||||
b.is_in_bounds(target) && self.cleared.contains(target) && safe
|
||||
@@ -68,25 +52,10 @@ impl World {
|
||||
self.cleared.contains(pos)
|
||||
}
|
||||
|
||||
pub fn is_safe_to_dig(&self, current_pos: &Point, target: &Point, b: &Screen) -> bool {
|
||||
// create a scenario in which this has been dug out
|
||||
pub fn is_safe_to_dig(&self, target: &Point, b: &Screen) -> bool {
|
||||
let mut hypothetical_world = self.clone();
|
||||
hypothetical_world.clear(*target);
|
||||
// test if we can return from the target to the beginning
|
||||
// if yes, dig
|
||||
let result = astar(target, &Point(0, 0), Some(&hypothetical_world), Some(b));
|
||||
//let to_current_pos = astar(target, current_pos, Some(&hypothetical_world), Some(b));
|
||||
/*if target.is_above(current_pos) {
|
||||
safe = !self.cleared.contains(&target.up());
|
||||
}
|
||||
|
||||
if target.is_adjacent(current_pos) {
|
||||
safe = !self.cleared.contains(&target.up()) && !self.cleared.contains(&target.down());
|
||||
}
|
||||
|
||||
if target.is_below(current_pos) {
|
||||
safe = !self.cleared.contains(&target.down()) && !self.cleared.contains(&target.left()) && !self.cleared.contains(&target.right());
|
||||
}*/
|
||||
b.is_in_bounds(target) && result.is_ok()
|
||||
}
|
||||
|
||||
@@ -108,7 +77,7 @@ impl World {
|
||||
let moves = b.get_valid_movements(pos);
|
||||
let mut ans: Vec<Point> = moves
|
||||
.iter()
|
||||
.filter(|p| self.is_valid_movement(p, b))
|
||||
.filter(|p| self.is_valid_movement(pos, p, b))
|
||||
.map(|p| p.clone())
|
||||
.collect();
|
||||
|
||||
|
||||
+2
-6
@@ -24,14 +24,10 @@ fn main() {
|
||||
let mut world = World::new();
|
||||
|
||||
let mut entities = Entities::new();
|
||||
let q = Queen::new(3,0);
|
||||
let q = Queen::new(board.center.0,board.center.1);
|
||||
entities.add_entity(&q);
|
||||
|
||||
for i in 0..6 {
|
||||
for j in 0..1 {
|
||||
world.clear(Point(i, j));
|
||||
}
|
||||
}
|
||||
world.create_chamber(Point(board.center.0, board.center.1), 3);
|
||||
|
||||
loop {
|
||||
// TODO: add way to break out of the loop by hitting a random key
|
||||
|
||||
Reference in New Issue
Block a user