-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
46 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,27 @@ | ||
class RobotSimulator: | ||
|
||
var _pos: Vector2i | ||
var _position: Vector2i | ||
var _dir: String | ||
|
||
|
||
var position: Vector2i: | ||
get: | ||
return _pos | ||
|
||
|
||
var direction: String: | ||
get: | ||
return _dir | ||
|
||
var allowed_directions = ['north', 'east', 'south', 'west'] | ||
|
||
func _init(position: Vector2i = Vector2i(0, 0), direction: String = 'north'): | ||
_pos = position | ||
_dir = direction | ||
_position = position | ||
_direction = direction | ||
|
||
|
||
func move(instructions: String): | ||
const allowed_directions = ['north', 'east', 'south', 'west'] | ||
for instruction in instructions: | ||
match [instruction, direction]: | ||
['L', _]: | ||
var index = allowed_directions.find(_dir) - 1 | ||
_dir = allowed_directions[index] | ||
_direction = allowed_directions[index] | ||
['R', _]: | ||
var index = allowed_directions.find(_dir) | ||
index = (index + 1) % 4 | ||
_dir = allowed_directions[index] | ||
_direction = allowed_directions[index] | ||
['A', 'north']: | ||
_pos += Vector2i(0, 1) | ||
_position += Vector2i(0, 1) | ||
['A', 'south']: | ||
_pos += Vector2i(0, -1) | ||
_position += Vector2i(0, -1) | ||
['A', 'east']: | ||
_pos += Vector2i(1, 0) | ||
_position += Vector2i(1, 0) | ||
['A', 'west']: | ||
_pos += Vector2i(-1, 0) | ||
_position += Vector2i(-1, 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
class RobotSimulator: | ||
|
||
func _init(position, direction): | ||
pass | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters