public
Last active

All default tests pass. No extra credit.

  • Download Gist
rpcfn9.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
#!/usr/bin/env ruby
 
class Game
def initialize(story_path, options={})
@input = options.fetch(:input) { $stdin }
@output = options.fetch(:output) { $stdout }
@inventory = {}
raw_data = open story_path
parse raw_data
end
 
def play!
start!
execute_one_command! until ended?
end
 
def start!
set_room '@end_of_road'
end
 
def execute_one_command!
command = @input.gets.chomp
commands = command.split(" ")
known = we_know_this commands[0]
if known
commands[0] = known
execute commands.join " "
else
unknown_command command
end
end
 
def ended?
@current_room == @rooms['@depression']
end
 
private
 
def open file
raw_data = ""
begin
File.open(file, 'r') { |f| raw_data << f.read }
rescue
throw ArgumentError, "File not found or impossible to open."
end
raw_data
end
 
def set_room room
@current_room = @rooms[room]
# I hate edge cases! :)
if @current_room[:visited?] && @current_room[:title] == "at end of road again"
@output.puts "You're at end of road again."
else
@output.puts @current_room[:description]
@current_room[:visited?] = true
end
display_items
end
 
def parse raw
# Necessary first line because the data file has inconsistent
# splits, specifically between action and synonyms.
raw.gsub! /\n\s+\n/, "\n\n"
items = raw.split("\n\n")
rooms = items.find_all { |item| item[0..3] == "Room" }
objects = items.find_all { |item| item[0..5] == "Object"}
commands = items.find_all { |item| item[0..7] == "Synonyms"}
actions = items.find_all { |item| item[0..5] == "Action" }
roomify rooms
objectify objects
commandify commands
actify actions
end
 
def roomify rooms
@rooms = {}
rooms.each do |room|
room.scan /Room (.*):/
identifier = $1
room.scan /Title: (.*)/
title = $1
room.scan /Description:.(.*)Exits/m
description = $1
description = description.strip.squeeze(' ').gsub("\n", "")
 
room.scan /Objects:.(.*)/m
objects = $1
 
# Why yes, I hack for fun and profit. Do you like ugly code?
unless objects.nil?
objects = objects.split
room.scan /Exits:.(.*)Objects/m
else
room.scan /Exits:.(.*)/m
end
exits = {}
exit_string = split_array_with_delimiter_and_clean $1, "\n"
exit_string.each do |string|
# Looks like "east to @building"
# I want ["east", "building"]
temp = split_array_with_delimiter_and_clean string, " to "
exits[temp[0]] = temp[1]
end
@rooms[identifier] = {}
@rooms[identifier][:title] = title
@rooms[identifier][:description] = description
@rooms[identifier][:exits] = exits
@rooms[identifier][:objects] = objects
end
end
 
def objectify objects
@objects = {}
objects.each do |object|
object.scan /Object (.*):/
identifier = $1
object.scan /Terms: (.*)/
terms = split_array_with_delimiter_and_clean $1, ','
description = object.scan /Description: (.*)/
@objects[identifier] = {}
@objects[identifier][:terms] = terms
@objects[identifier][:description] = description.flatten
end
end
 
def commandify commands
commands = commands[0][10..-1]
commands.split "\n"
@commands = {}
commands.each do |command|
command_and_synonyms = split_array_with_delimiter_and_clean command, ':'
main = command_and_synonyms[0]
synonyms = split_array_with_delimiter_and_clean command_and_synonyms[1], ','
@commands[main] = main
synonyms.each do |synonym|
@commands[synonym] = main
end
end
end
 
def actify actions
@actions = {}
actions.each do |action|
action.scan /Action (.*):/
identifier = $1
action.scan /Terms: (.*)/
terms = split_array_with_delimiter_and_clean $1, ','
action.scan /(\{\{\{.*\}\}\})/m
code_to_eval = $1
@actions[identifier] = {}
@actions[identifier][:terms] = terms
@actions[identifier][:code] = code_to_eval
end
end
 
def we_know_this command
# Alright; this is kinda hackish. I'm lazy. Sue me.
# I mean, don't. Jeez. Where's your sense of humor?
if @commands.keys.include? command
return @commands[command]
else
return false
end
end
 
def execute command
# I know, I am joining an array just to resplit it.
# Doing this does make the code a little more readable, though.
split = command.split " "
command = split[0]
arg = split[1..-1].join " "
case command
when "west", "east", "north", "south"
try_to_go command
when "look"
@output.puts @current_room[:description]
display_items
when "drop"
drop arg
when "inventory"
display_inventory
when "take"
pick_up arg
end
end
 
def try_to_go direction
if @current_room[:exits][direction]
set_room @current_room[:exits][direction]
else
@output.puts "There is no way to go in that direction"
end
end
 
def display_items
return unless @current_room[:objects]
@current_room[:objects].each do |item|
@output.puts @objects[item][:description]
end
end
 
def display_inventory
if @inventory.empty?
@output.puts "You're not carrying anything."
else
@inventory.each do |item, properties|
@output.puts properties[:terms]#[0]
end
end
end
 
def pick_up arg
accepted_arguments = []
corresponding_object = @objects.find { |x, i| find_casecmp i[:terms], arg }
unless corresponding_object
@output.puts "This item doesn't exist!!"
return
end
corresponding_object = corresponding_object[0]
unless @current_room[:objects].include? corresponding_object
@output.puts "This item is not there!"
return
end
@inventory[corresponding_object] = @objects[corresponding_object]
@current_room[:objects].delete corresponding_object
@output.puts "OK"
end
 
def drop item
corresponding_object = @inventory.first { |x, i| find_casecmp i[:terms], item }
unless corresponding_object
@output.puts "You don't have this item!"
return
end
@current_room[:objects] ||= []
@current_room[:objects] << corresponding_object[0]
@inventory.delete corresponding_object[0]
end
 
def player_has? item
temp = @inventory.first { |x, i| find_casecmp i[:terms], item }
temp ? true : false
end
 
def player_in? room
@rooms[room] == @current_room
end
 
def unknown_command command
raise ArgumentError, "You entered \"#{command}\", an unknown command."
end
 
def split_array_with_delimiter_and_clean array, delimiter
array.split(delimiter).map { |x| x.strip }
end
 
def find_casecmp array, item
found = false
array.each do |x|
if x.casecmp(item) == 0
found = true
break
end
end
found
end
 
end
 
if $PROGRAM_NAME == __FILE__
story_path = ARGV[0]
unless story_path
warn "Usage: #{$PROGRAM_NAME} STORY_FILE"
exit 1
end
game = Game.new(story_path)
game.play!
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.