class MarsRoverWillGem::Rover
Attributes
rover_landing_spot[RW]
Public Class Methods
new()
click to toggle source
# File lib/mars_rover_will_gem.rb, line 9 def initialize @rover_current_pos ||= nil @plateau_information ||= nil @commands ||= nil @crashed ||= false end
Public Instance Methods
check_landing()
click to toggle source
# File lib/mars_rover_will_gem.rb, line 103 def check_landing if (@rover_current_pos[0] < 0 || @rover_current_pos[0] > @plateau_information[0]) || (@rover_current_pos[1] < 0 || @rover_current_pos[1] > @plateau_information[1]) puts "Rover crashed next to the plateau. Dude, take better aim! This gear is expensive. o_O." abort else puts "Rover successfully landed." end end
convert_information(rover_landing_spot, plateau_shape, nasa_instructions)
click to toggle source
# File lib/mars_rover_will_gem.rb, line 19 def convert_information(rover_landing_spot, plateau_shape, nasa_instructions) @commands = nasa_instructions.split("") @plateau_information = plateau_shape.split(" ") @rover_current_pos = rover_landing_spot.split(" ") @rover_current_pos[0] = @rover_current_pos[0].to_i @rover_current_pos[1] = @rover_current_pos[1].to_i @plateau_information[0] = @plateau_information[0].to_i @plateau_information[1] = @plateau_information[1].to_i end
move()
click to toggle source
# File lib/mars_rover_will_gem.rb, line 34 def move @commands.each do |command| case command when "L" case @rover_current_pos[2] when "N" @rover_current_pos[2] = "W" when "E" @rover_current_pos[2] = "N" when "S" @rover_current_pos[2] = "E" when "W" @rover_current_pos[2] = "S" end when "R" case @rover_current_pos[2] when "N" @rover_current_pos[2] = "E" when "E" @rover_current_pos[2] = "S" when "S" @rover_current_pos[2] = "W" when "W" @rover_current_pos[2] = "N" end when "M" case @rover_current_pos[2] when "N" @rover_current_pos[1] += 1 when "E" @rover_current_pos[0] += 1 when "S" @rover_current_pos[1] -= 1 when "W" @rover_current_pos[0] -= 1 end end end end
report()
click to toggle source
# File lib/mars_rover_will_gem.rb, line 95 def report @crashed = true if @rover_current_pos[0] < 1 || @rover_current_pos[1] < 1 puts @crashed == true ? "...but you drove the rover off the plateau. Dude, do you even have a license? o_O" : "Rover's final position is #{@rover_current_pos}." end