Guest User

Untitled

a guest
May 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. $LOAD_PATH << File.expand_path(File.dirname(__FILE__))
  4.  
  5. require 'loader'
  6. require 'world'
  7.  
  8. class Game
  9. include Loader
  10.  
  11. attr_accessor :world
  12.  
  13. def initialize(story_path, options={})
  14. @input = options.fetch(:input) { $stdin }
  15. @output = options.fetch(:output) { $stdout }
  16. @world = World.new load(story_path)
  17. end
  18.  
  19. def play!
  20. start!
  21. execute_one_command! until ended?
  22. end
  23.  
  24. def start!
  25. @output.puts @world.start
  26. @output.print "> "
  27. end
  28.  
  29. def execute_one_command!
  30. input = @input.gets
  31. output = @world.execute!(input)
  32.  
  33. @output.puts output unless output.empty?
  34. @output.print "> " unless ended?
  35. end
  36.  
  37. def ended?
  38. @world.ended?
  39. end
  40. end
  41.  
  42. if $PROGRAM_NAME == __FILE__
  43. story_path = ARGV[0]
  44. unless story_path
  45. warn "Usage: #{$PROGRAM_NAME} STORY_FILE"
  46. exit 1
  47. end
  48. game = Game.new(story_path)
  49. game.play!
  50. end
Add Comment
Please, Sign In to add comment