-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrover.rb
144 lines (127 loc) · 4.18 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class Rover
attr_reader :x, :y, :heading
def initialize(plateau)
puts "What is the rover's position? (0,0) is the lower left (SW corner) of the plateau. Please enter as its x and y coordinates and heading with spaces i.e. 5 5 N"
start_position(plateau)
end
def start_position(plateau)
position = gets.chomp.upcase.split
@x = position[0].to_i
@y = position[1].to_i
@heading = position[2]
if plateau.collide?(@x, @y)
puts "There is another rover at this position. Please provide a new position."
start_position(plateau)
end
if plateau.fall?(@x, @y)
puts "Your chosen starting position is off the plateau grid. Please provide a new position."
start_position(plateau)
end
end
#Rover moves to verified position
def move(requester)
@x = requester.check_x
@y = requester.check_y
end
def turn(direction)
if (direction == "R" && @heading == "W") || (direction =="L" && @heading == "E")
@heading = "N"
elsif (direction == "R" && @heading == "E") || (direction =="L" && @heading == "W")
@heading = "S"
elsif (direction == "R" && @heading == "N") || (direction =="L" && @heading == "S")
@heading = "E"
else
@heading = "W"
end
end
end
#MissionControl is responsible for assigning and ordering rovers around
class MissionControl
attr_reader :check_x, :check_y, :check_heading, :rover_list
def initialize
puts "Bitmaker SpaceLabs Rover Deployment System. Enter your city."
`say "This is the Bitmaker SpaceLabs Rover Deployment System. Which city are you in?"`
greeting = "Welcome to #{gets.chomp.capitalize.strip} Mission Control."
puts greeting + " Enter the names of the plateaux you wish to deploy rovers to, separated by commas:"
`say "#{greeting}"`
create_plateaux
end
def create_plateaux
plateau_list = gets.chomp.upcase.split(",")
plateau_list.each do |plateau_name|
@plateau = Plateau.new(plateau_name.strip.capitalize)
puts "How many rovers would you like to deploy?"
gets.chomp.to_i.times{run_rover_commands}
end
end
def get_instructions(rover)
@check_x, @check_y = rover.x, rover.y
puts "Where would you like the rover to move? Enter L for left, R for right and M for forward in the current heading i.e. LMMMMRMMMRMMMLM"
read_instructions(rover, gets.chomp.upcase.split(""))
end
def read_instructions(rover, set_of_instructions)
set_of_instructions.each do |instruction|
if instruction == 'M'
next_move(rover)
break unless confirm_move(rover)
else
rover.turn(instruction)
end
end
@plateau.rover_list.push(rover)
end
def confirm_move(rover)
if @plateau.fall?(@check_x, @check_y) || @plateau.collide?(@check_x, @check_y)
puts "Execution aborted. The rover has stopped safely at #{rover.x}, #{rover.y} heading #{rover.heading}. Please provide new instructions."
get_instructions(rover)
return false
else
rover.move(self)
end
end
#next_move tracks next location as assignment by increment/decrement based on heading
def next_move(rover)
case rover.heading
when "N"
@check_y += 1
when "S"
@check_y -= 1
when "E"
@check_x += 1
when "W"
@check_x -= 1
end
end
def run_rover_commands
rover = Rover.new(@plateau)
get_instructions(rover)
puts "\nThe rover is now at #{rover.x}, #{rover.y} heading #{rover.heading}."
end
end
class Plateau
attr_accessor :rover_list#, :plateau_name
def initialize(plateau_name)
puts "How big is #{plateau_name}? ___ by ___? (Please enter the numbers like this: 5 5)."
size = gets.chomp.split
@max_x = size[0].to_i
@max_y = size[1].to_i
@rover_list = []
end
def fall?(x, y)
if x < 0 || x > @max_x || y < 0 ||y > @max_y
puts "The next instruction would make the rover fall off the plateau."
return true
end
false
end
def collide?(x, y)
@rover_list.each do |listed_rover|
if x == listed_rover.x && y == listed_rover.y
puts "The next instruction would make the rover crash into another rover."
return true
end
end
false
end
end
MissionControl.new