Browse Source

Fix off-by-ones at world's end

main
Adrian Heine 3 years ago
parent
commit
9b92497c3b
  1. 2
      src/agent.rs
  2. 11
      src/world.rs

2
src/agent.rs

@ -110,7 +110,7 @@ fn random_move_within(
loop {
let mv = random_move();
if let Move::TryMove(Direction { x, y }) = mv {
if top + y > 0 && bottom - y > 0 && left + x > 0 && right - x > 0 {
if top + y >= 0 && bottom - y >= 0 && left + x >= 0 && right - x >= 0 {
return mv;
}
}

11
src/world.rs

@ -57,7 +57,12 @@ impl ActualWorld {
self_id: *id,
tagged_by: self.state.tagged_by,
tagged: self.state.tagged,
bounds_distance: (pos.y, self.size.x - pos.x, self.size.y - pos.y, pos.x),
bounds_distance: (
pos.y,
self.size.x - pos.x - 1,
self.size.y - pos.y - 1,
pos.x,
),
other_agents: &mut self
.state
.agent_positions
@ -123,9 +128,9 @@ impl ActualWorld {
assert!(dir.y.abs() <= 1);
let pos = self.state.agent_positions.get(&id).unwrap();
let size = &self.size;
assert!(pos.x + dir.x > 0);
assert!(pos.x + dir.x >= 0);
assert!(pos.x + dir.x < size.x);
assert!(pos.y + dir.y > 0);
assert!(pos.y + dir.y >= 0);
assert!(pos.y + dir.y < size.y);
}
}

Loading…
Cancel
Save