From 7632401aa4048ff58e28f52589257e23c6eda36e Mon Sep 17 00:00:00 2001 From: fiatcode Date: Tue, 24 Feb 2026 10:49:58 +0700 Subject: [PATCH] GREEN: implement plateau boundaries with wrapping --- lib/mars_rover.dart | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/mars_rover.dart b/lib/mars_rover.dart index 9032974..b06c6e1 100644 --- a/lib/mars_rover.dart +++ b/lib/mars_rover.dart @@ -2,8 +2,16 @@ class Rover { int x; int y; String direction; + final int plateauWidth; + final int plateauHeight; - Rover({required this.x, required this.y, required this.direction}); + Rover({ + required this.x, + required this.y, + required this.direction, + this.plateauWidth = 100, + this.plateauHeight = 100, + }); void turnLeft() { const leftTurns = { @@ -28,16 +36,18 @@ class Rover { void moveForward() { switch (direction) { case 'N': - y += 1; + y = (y + 1) % (plateauHeight + 1); break; case 'E': - x += 1; + x = (x + 1) % (plateauWidth + 1); break; case 'S': - y -= 1; + y = (y - 1) % (plateauHeight + 1); + if (y < 0) y += plateauHeight + 1; break; case 'W': - x -= 1; + x = (x - 1) % (plateauWidth + 1); + if (x < 0) x += plateauWidth + 1; break; } }