-
Notifications
You must be signed in to change notification settings - Fork 0
/
rover.rb
36 lines (32 loc) · 1.16 KB
/
rover.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -----------------------------------------------------------------------------
# Handles moving of Rover on Mars
class Rover
attr_reader :coordinates
attr_reader :orientation
attr_reader :path_pattern
attr_reader :mars
# ---------------------------------------------------------------------------
def initialize(coordinates, orientation, path_pattern, mars)
@coordinates = coordinates
@orientation = orientation
@path_pattern = path_pattern
@mars = mars
end
# ---------------------------------------------------------------------------
def print_current_state
puts "Coordinates: #{@coordinates}"
puts "Orientation: #{@orientation}"
end
# ---------------------------------------------------------------------------
def traverse_path
@path_pattern.split('').each do |direction|
@coordinates, @orientation = @mars.obtain_new_position(@coordinates,
@orientation,
direction)
if @mars.not_valid?(*@coordinates)
puts 'Not a valid move. Out of Mars :\'('
exit
end
end
end
end