Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Turn left when user inputs the left command. Update robots position.
- Loading branch information
Showing
with
53 additions
and 12 deletions.
- +23 −9 lib/direction.rb
- +12 −2 lib/handle_input.rb
- +18 −1 spec/direction_spec.rb
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
@@ -1,22 +1,36 @@ | ||
# Direction | ||
class Direction | ||
OPTIONS = %w[WEST NORTH EAST SOUTH].freeze | ||
|
||
def turn_right(direction) | ||
# Return unless the direction exists | ||
return unless OPTIONS.include?(direction) | ||
|
||
i = OPTIONS.index { |e| e == direction } | ||
|
||
# Find next value in array | ||
unless direction == OPTIONS.last | ||
i += 1 | ||
|
||
return OPTIONS.fetch(i) | ||
end | ||
|
||
OPTIONS.first | ||
end | ||
|
||
def turn_left(direction) | ||
# Return if unless the direction exists | ||
return unless OPTIONS.include?(direction) | ||
|
||
i = OPTIONS.index { |e| e == direction } | ||
|
||
# Find next value in array | ||
unless direction == OPTIONS.first | ||
i -= 1 | ||
|
||
return OPTIONS.fetch(i) | ||
end | ||
|
||
OPTIONS.last | ||
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