Skip to content

Instantly share code, notes, and snippets.

@vikas17a
Last active August 29, 2015 14:22
Show Gist options
  • Save vikas17a/951815c59f0c2ca32474 to your computer and use it in GitHub Desktop.
Save vikas17a/951815c59f0c2ca32474 to your computer and use it in GitHub Desktop.
Ruby Marsrover problem solution
class Rover
attr_reader :x
attr_reader :y
attr_reader :face
attr_reader :plot_x
attr_reader :plot_y
def initialize(x,y,face, plot_x, plot_y)
@x =x
@y =y
@face =face
@plot_x = plot_x
@plot_y = plot_y
end
def setCordinates(x,y)
if(x <= @plot_x && y <= @plot_y && x >= 0 && y >= 0)
@x = x
@y = y
end
end
def setFace(face)
@face = face
end
end
class Direction
attr_reader :new_direction
def self.changeDirection(rotation, intial_facing)
case rotation
when 'L'
case intial_facing
when 'N'
@new_direction = 'W'
when 'W'
@new_direction = 'S'
when 'S'
@new_direction = 'E'
when 'E'
@new_direction = 'N'
else
puts "Error no initial facing direction added"
end
when 'R'
case intial_facing
when 'N'
@new_direction = 'E'
when 'E'
@new_direction = 'S'
when 'S'
@new_direction = 'W'
when 'W'
@new_direction = 'N'
else
puts "Error no intial facing direction added"
end
else
puts "Error no rotation direction added"
end
@new_direction
end
end
class Move
attr_reader :x
attr_reader :y
def self.move(initial_facing,x,y)
@x=x
@y=y
case initial_facing
when 'N'
@x = x
@y += 1
when 'E'
@x += 1
@y = y
when 'W'
@x -= 1
@y = y
when 'S'
@x = 1
@y -= y
else
puts "Error no initial facing added"
end
[@x, @y]
end
end
class Start
def inputMe
user_in = gets.chomp
dim = user_in.split(" ")
while true
user_in = gets.chomp
init_state = user_in.split(" ")
rover = Rover.new(init_state[0].to_i, init_state[1].to_i, init_state[2], dim[0].to_i, dim[1].to_i)
user_in = gets.chomp
user_in.split("").each do |c|
if ['R', 'L'].include? c
new_direction = Direction.changeDirection(c, rover.face)
rover.setFace(new_direction)
elsif ['M'].include? c
x, y = Move.move(rover.face, rover.x, rover.y)
rover.setCordinates(x,y)
end
end
puts "#{rover.x} #{rover.y} #{rover.face}"
end
end
end
run = Start.new
run.inputMe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment