class MarsRoverKeval::Rover
Your code goes hereā¦
Constants
- GRID_X
- GRID_Y
Public Class Methods
new(x_coordinate, y_coordinate, cardinal_point)
click to toggle source
# File lib/Mars_Rover_keval.rb, line 11 def initialize (x_coordinate, y_coordinate, cardinal_point) @x_coordinate=x_coordinate.to_i @y_coordinate=y_coordinate.to_i @cardinal_point=cardinal_point end
Public Instance Methods
current_postion()
click to toggle source
# File lib/Mars_Rover_keval.rb, line 63 def current_postion puts "Your Rover is at this #{@x_coordinate}, #{@y_coordinate}, #{@cardinal_point} location" end
move()
click to toggle source
'M' is nasa commands
# File lib/Mars_Rover_keval.rb, line 36 def move if @cardinal_point == 'N' then @y_coordinate=@y_coordinate+1 #@y_coordinate+=1 short form elsif @cardinal_point == 'E' then @x_coordinate=@x_coordinate+1 #@x_coordinate+=1 short form elsif @cardinal_point == 'S' then @y_coordinate=@y_coordinate-1 #@y_coordinate-=1 short form elsif @cardinal_point == 'W' then @x_coordinate=@x_coordinate-1 #@x_coordinate-=1 short form end end
parse(instruction)
click to toggle source
# File lib/Mars_Rover_keval.rb, line 44 def parse(instruction) instruction.each_char do |char| if char == 'L' self.turn_left elsif char == 'R' self.turn_right elsif char == 'M' self.move if @x_coordinate > GRID_X || @y_coordinate > GRID_Y puts "Your Rover has reached off the grid" abort elsif @x_coordinate < 0 || @y_coordinate <0 puts "Your Rover has reached off the grid" abort end end end end
turn_left()
click to toggle source
'L' is nasa commands
# File lib/Mars_Rover_keval.rb, line 18 def turn_left if @cardinal_point == 'N' then @cardinal_point ='W' elsif @cardinal_point == 'W' then @cardinal_point ='S' elsif @cardinal_point == 'S' then @cardinal_point ='E' elsif @cardinal_point == 'E' then @cardinal_point ='N' end end
turn_right()
click to toggle source
'R' is nasa commands
# File lib/Mars_Rover_keval.rb, line 27 def turn_right if @cardinal_point == 'N' then @cardinal_point ='E' elsif @cardinal_point == 'E' then @cardinal_point ='S' elsif @cardinal_point == 'S' then @cardinal_point ='W' elsif @cardinal_point == 'W' then @cardinal_point ='N' end end