GREEN: implement plateau boundaries with wrapping
This commit is contained in:
parent
1078e1947e
commit
7632401aa4
1 changed files with 15 additions and 5 deletions
|
|
@ -2,8 +2,16 @@ class Rover {
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
String direction;
|
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() {
|
void turnLeft() {
|
||||||
const leftTurns = {
|
const leftTurns = {
|
||||||
|
|
@ -28,16 +36,18 @@ class Rover {
|
||||||
void moveForward() {
|
void moveForward() {
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case 'N':
|
case 'N':
|
||||||
y += 1;
|
y = (y + 1) % (plateauHeight + 1);
|
||||||
break;
|
break;
|
||||||
case 'E':
|
case 'E':
|
||||||
x += 1;
|
x = (x + 1) % (plateauWidth + 1);
|
||||||
break;
|
break;
|
||||||
case 'S':
|
case 'S':
|
||||||
y -= 1;
|
y = (y - 1) % (plateauHeight + 1);
|
||||||
|
if (y < 0) y += plateauHeight + 1;
|
||||||
break;
|
break;
|
||||||
case 'W':
|
case 'W':
|
||||||
x -= 1;
|
x = (x - 1) % (plateauWidth + 1);
|
||||||
|
if (x < 0) x += plateauWidth + 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue