Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Turn right when user inputs the right command. Update robots position
- Loading branch information
1 parent
92324ac
commit ce8603d
Showing
5 changed files
with
33 additions
and
46 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,37 +1,22 @@ | ||
# Direction | ||
class Direction | ||
attr_reader :start_direction | ||
|
||
def initialize(start_direction) | ||
@start_direction = start_direction | ||
|
||
@directions_array = %w[WEST NORTH EAST SOUTH] | ||
def initialize | ||
@options_array = %w[WEST NORTH EAST SOUTH] | ||
end | ||
|
||
def turn_right(current_direction) | ||
puts 'turn right' | ||
def turn_right(direction) | ||
# Return if unless the direction exists | ||
return unless @options_array.include?(direction) | ||
|
||
# does the current direction exist? | ||
return unless @directions_array.include?(current_direction) | ||
i = @options_array.index { |e| e == direction } | ||
|
||
i = @directions_array.index { |e| e == current_direction } | ||
|
||
unless current_direction == @directions_array.last | ||
# Find next value in array | ||
unless direction == @options_array.last | ||
i += 1 | ||
|
||
new_direction = @directions_array.fetch(i) | ||
puts "New Direction: #{new_direction}" | ||
|
||
return new_direction | ||
return @options_array.fetch(i) | ||
end | ||
|
||
puts "New Direction: #{@directions_array.first}" | ||
@options_array.first | ||
end | ||
end | ||
|
||
direction = Direction.new('NORTH') | ||
|
||
puts "Starting at #{direction.start_direction}" | ||
current_direction = direction.start_direction | ||
|
||
direction.turn_right(current_direction) |
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,10 +1,10 @@ | ||
# Position class | ||
class Position | ||
attr_accessor :x, :y, :f | ||
attr_accessor :x, :y, :f | ||
|
||
def initialize(x, y, f) | ||
@x = x | ||
@y = y | ||
@f = f | ||
@f = f | ||
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