Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mmower/131903 to your computer and use it in GitHub Desktop.
Save mmower/131903 to your computer and use it in GitHub Desktop.
# ruby beginner elevator program
# We might break this stuff up into more classes as the need to separate state and
# behaviour of different entities emerges, for now we keep it simple and imagine
# the state to be that of the building as a whole.
#
class Building
GROUND_FLOOR = 0
attr_reader :elevator_on_floor
attr_accessor :elevator_target_floor
def initialize
@elevator_on_floor = GROUND_FLOOR
@elevator_target_floor = GROUND_FLOOR
end
def call_elevator_to_floor( new_floor )
# Check that the call floor is legal (in a real elevator you wouldn't have buttons for illegal floors but this elevator isn't real!
# If the new floor is different to the current floor, tell the elevator where to go
end
def time_passes
# Is the elevator on the floor it has been called to?
# If not move it one floor towards the target floor
# If the elevator has reached it's target floor, print a message
end
def output_state
# print out where the elevator is
# if the elevator isn't at it's target floor print out whether it's heading up or down
end
end
my_building = Building.new
#
# This part is the "game"
#
# On each move you can either call the elevator to a floor by typing in the floor number
# or simply allow the elevator to carry on its journey. Use ^d to exit (do you know why that
# works?)
#
while line = gets
line.chomp!
if line =~ /d+/
my_building.call_elevator_to_floor( line.to_i )
end
my_building.time_passes
my_building.output_state
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment