From 9b92497c3b29655880d2c30484cf3f2ebbad59a4 Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Fri, 23 Jul 2021 15:10:14 +0200 Subject: [PATCH] Fix off-by-ones at world's end --- src/agent.rs | 2 +- src/world.rs | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/agent.rs b/src/agent.rs index 260a922..7cd68f7 100644 --- a/src/agent.rs +++ b/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; } } diff --git a/src/world.rs b/src/world.rs index 581d006..6633052 100644 --- a/src/world.rs +++ b/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); } }