REFACTOR: introduce Position and Plateau value objects
This commit is contained in:
parent
bb868d8dd6
commit
5ee4e1edc0
1 changed files with 48 additions and 28 deletions
|
|
@ -20,21 +20,53 @@ enum Direction {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Position {
|
||||||
|
final int x;
|
||||||
|
final int y;
|
||||||
|
|
||||||
|
Position(this.x, this.y);
|
||||||
|
|
||||||
|
Position moveNorth() => Position(x, y + 1);
|
||||||
|
Position moveEast() => Position(x + 1, y);
|
||||||
|
Position moveSouth() => Position(x, y - 1);
|
||||||
|
Position moveWest() => Position(x - 1, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
class Plateau {
|
||||||
|
final int width;
|
||||||
|
final int height;
|
||||||
|
|
||||||
|
Plateau(this.width, this.height);
|
||||||
|
|
||||||
|
Position wrap(Position position) {
|
||||||
|
final wrappedX = _wrapCoordinate(position.x, width);
|
||||||
|
final wrappedY = _wrapCoordinate(position.y, height);
|
||||||
|
return Position(wrappedX, wrappedY);
|
||||||
|
}
|
||||||
|
|
||||||
|
int _wrapCoordinate(int value, int max) {
|
||||||
|
final wrapped = value % (max + 1);
|
||||||
|
return wrapped < 0 ? wrapped + max + 1 : wrapped;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Rover {
|
class Rover {
|
||||||
int x;
|
Position _position;
|
||||||
int y;
|
|
||||||
Direction _direction;
|
Direction _direction;
|
||||||
final int plateauWidth;
|
final Plateau plateau;
|
||||||
final int plateauHeight;
|
|
||||||
|
|
||||||
Rover({
|
Rover({
|
||||||
required this.x,
|
required int x,
|
||||||
required this.y,
|
required int y,
|
||||||
required String direction,
|
required String direction,
|
||||||
this.plateauWidth = 100,
|
int plateauWidth = 100,
|
||||||
this.plateauHeight = 100,
|
int plateauHeight = 100,
|
||||||
}) : _direction = Direction.fromCode(direction);
|
}) : _position = Position(x, y),
|
||||||
|
_direction = Direction.fromCode(direction),
|
||||||
|
plateau = Plateau(plateauWidth, plateauHeight);
|
||||||
|
|
||||||
|
int get x => _position.x;
|
||||||
|
int get y => _position.y;
|
||||||
String get direction => _direction.code;
|
String get direction => _direction.code;
|
||||||
|
|
||||||
void turnLeft() {
|
void turnLeft() {
|
||||||
|
|
@ -46,25 +78,13 @@ class Rover {
|
||||||
}
|
}
|
||||||
|
|
||||||
void moveForward() {
|
void moveForward() {
|
||||||
switch (_direction) {
|
final newPosition = switch (_direction) {
|
||||||
case Direction.north:
|
Direction.north => _position.moveNorth(),
|
||||||
y = _wrapCoordinate(y + 1, plateauHeight);
|
Direction.east => _position.moveEast(),
|
||||||
break;
|
Direction.south => _position.moveSouth(),
|
||||||
case Direction.east:
|
Direction.west => _position.moveWest(),
|
||||||
x = _wrapCoordinate(x + 1, plateauWidth);
|
};
|
||||||
break;
|
_position = plateau.wrap(newPosition);
|
||||||
case Direction.south:
|
|
||||||
y = _wrapCoordinate(y - 1, plateauHeight);
|
|
||||||
break;
|
|
||||||
case Direction.west:
|
|
||||||
x = _wrapCoordinate(x - 1, plateauWidth);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int _wrapCoordinate(int value, int max) {
|
|
||||||
final wrapped = value % (max + 1);
|
|
||||||
return wrapped < 0 ? wrapped + max + 1 : wrapped;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void execute(String commands) {
|
void execute(String commands) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue