#!/usr/bin/env ruby #This file should be more or less clean now. class Command attr_accessor :loc def initialize(loc = 0) #initialize the location variable, values are handled in commands. @loc = loc end def go_west #define west command if @loc == 0 @loc = 1 puts "West room" elsif @loc == 3 @loc = 0 puts "Center room" else puts "You can't go that way." end end def go_east #define east command if @loc == 0 @loc = 3 puts "East room" elsif @loc == 1 @loc = 0 puts "Center room" else puts "You can't go that way." end end def go_south #define south command if @loc == 0 @loc = 4 puts "South room" elsif @loc == 2 @loc = 0 puts "Center room" else puts "You can't go that way." end end def go_north #define north command if @loc == 0 @loc = 2 puts "North room- there is an exit here, in the north." elsif @loc == 4 @loc = 0 puts "Center room" elsif @loc == 2 @loc = 5 puts "End." else puts "You can't go that way." end end end if __FILE__ == $0 #make sure the file isn't being loaded as a library cmd = Command.new #create the new instance while cmd.loc != 5 #access the instance's location variable puts "Enter direction." command = gets.chomp #get user's input, cutting off the automatic newline if command == "west" cmd.go_west elsif command == "east" cmd.go_east elsif command == "south" cmd.go_south elsif command == "north" cmd.go_north else puts "Unrecognized command." #handle unexpected input end end end