Advertisement
flashgamer001

basicmaze.rb

Mar 4th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.60 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. #This file should be more or less clean now.
  3. class Command
  4. attr_accessor :loc
  5.     def initialize(loc = 0) #initialize the location variable, values are handled in commands.
  6.         @loc = loc
  7.     end
  8.     def go_west             #define west command
  9.         if @loc == 0
  10.             @loc = 1
  11.             puts "West room"
  12.         elsif @loc == 3
  13.             @loc = 0
  14.             puts "Center room"
  15.         else
  16.             puts "You can't go that way."
  17.         end
  18.     end
  19.     def go_east             #define east command
  20.         if @loc == 0
  21.             @loc = 3
  22.             puts "East room"
  23.         elsif @loc == 1
  24.             @loc = 0
  25.             puts "Center room"
  26.         else
  27.             puts "You can't go that way."
  28.         end
  29.     end
  30.     def go_south            #define south command
  31.         if @loc == 0
  32.             @loc = 4
  33.             puts "South room"
  34.         elsif @loc == 2
  35.             @loc = 0
  36.             puts "Center room"
  37.         else
  38.             puts "You can't go that way."
  39.         end
  40.     end
  41.     def go_north            #define north command
  42.         if @loc == 0
  43.             @loc = 2
  44.             puts "North room- there is an exit here, in the north."
  45.         elsif @loc == 4
  46.             @loc = 0
  47.             puts "Center room"
  48.         elsif @loc == 2
  49.             @loc = 5
  50.             puts "End."
  51.         else
  52.             puts "You can't go that way."
  53.         end
  54.     end
  55. end
  56. if __FILE__ == $0   #make sure the file isn't being loaded as a library
  57. cmd = Command.new   #create the new instance
  58.     while cmd.loc != 5  #access the instance's location variable
  59.         puts "Enter direction."
  60.         command = gets.chomp    #get user's input, cutting off the automatic newline
  61.         if command == "west"
  62.             cmd.go_west
  63.         elsif command == "east"
  64.             cmd.go_east
  65.         elsif command == "south"
  66.             cmd.go_south
  67.         elsif command == "north"
  68.             cmd.go_north
  69.         else
  70.             puts "Unrecognized command."    #handle unexpected input
  71.         end
  72.     end
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement