Skip to content

Instantly share code, notes, and snippets.

@ndeto
Last active February 4, 2022 07:02
Show Gist options
  • Save ndeto/36750c85320ccc4c8a204f78e10be51a to your computer and use it in GitHub Desktop.
Save ndeto/36750c85320ccc4c8a204f78e10be51a to your computer and use it in GitHub Desktop.
A robot actions code
class Robot
MOVEMENTS = ['N', 'E', 'W', 'S']
ACTIONS = ['G', 'D']
CRATES_POSITIONS = []
@length = 10
@width = 10
@is_lifting = false
@current_position = [0,0]
def execute(movements:)
movements.each do |i|
if MOVEMENTS.include?(i)
send("move_#{i}")
end
if ACTIONS.include?(i)
send("action_#{i}")
end
end
end
def move_N
if @current_position[1] == @length
return
end
@current_position[1] += 1
end
def move_E
if @current_position[0] == @width
return
end
@current_position[0] += 1
end
def move_W
if @current_position[0] == 0
return
end
@current_position[0] -= 1
end
def move_S
if @current_position[1] == 0
return
end
@current_position[1] -= 1
end
# Action Grab
def action_G
if !@is_lifting && crate_present?(@current_position)
puts ' Robot is already lifting a crate'
return
end
CRATES_POSITIONS.delete(@current_position)
end
# Position are the coordinates
def crate_present?(position)
return false if CRATES_POSITIONS.include?(position)
true
end
def action_D
crate_present?(@current_position)
CRATES_POSITIONS << @current_position
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment