-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tidy and reorganise code to make more sense. Update specs.
- Loading branch information
1 parent
ceed3ff
commit 5047899
Showing
16 changed files
with
226 additions
and
240 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,4 +1,4 @@ | ||
# Position class | ||
# Position | ||
class Position | ||
attr_accessor :x, :y, :f | ||
|
||
|
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,20 +1,76 @@ | ||
require_relative 'position' | ||
|
||
# Robot Class | ||
# Robot | ||
class Robot | ||
attr_reader :position | ||
|
||
def intialize | ||
@position = nil | ||
end | ||
|
||
# Update the robot's position | ||
def place(command) | ||
_command, x, y, f = command.tr(',', ' ').split | ||
|
||
Position.new(x.to_i, y.to_i, f) | ||
end | ||
|
||
def move(position) | ||
y = position.y | ||
x = position.x | ||
f = position.f | ||
|
||
y += 1 if f.match?('NORTH') | ||
x += 1 if f.match?('EAST') | ||
y -= 1 if f.match?('SOUTH') | ||
x -= 1 if f.match?('WEST') | ||
|
||
Position.new(x, y, f) | ||
end | ||
|
||
def left(position) | ||
Position.new(position.x, position.y, prev_option(position.f)) | ||
end | ||
|
||
def right(position) | ||
Position.new(position.x, position.y, next_option(position.f)) | ||
end | ||
|
||
def report(position) | ||
message = "Output: #{position.x},#{position.y},#{position.f} \n" | ||
$stdout.print message | ||
end | ||
|
||
def update_robot(new_position) | ||
@position = new_position | ||
end | ||
|
||
# Check the robot has been placed | ||
def not_in_place? | ||
@position.nil? | ||
end | ||
|
||
private | ||
|
||
OPTIONS = %w[WEST NORTH EAST SOUTH].freeze | ||
|
||
def prev_option(direction) | ||
return unless OPTIONS.include?(direction) | ||
|
||
return OPTIONS.last if direction == OPTIONS.first | ||
|
||
i = OPTIONS.index { |e| e == direction } | ||
i -= 1 | ||
|
||
OPTIONS.fetch(i) | ||
end | ||
|
||
def next_option(direction) | ||
return unless OPTIONS.include?(direction) | ||
|
||
return OPTIONS.first if direction == OPTIONS.last | ||
|
||
i = OPTIONS.index { |e| e == direction } | ||
i += 1 | ||
|
||
OPTIONS.fetch(i) | ||
end | ||
end |
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.