Created
March 17, 2013 12:48
-
-
Save flada-auxv/5181360 to your computer and use it in GitHub Desktop.
usage追加してみた。一文字で打てるようにはしてない!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
require 'hashie' | |
require 'readline' | |
@env = Hashie::Mash.new(YAML.load_file("./world.yml")) | |
@allowed_commands = %w(look walk pickup inventory) | |
@location = :living_room | |
def look | |
res = [] | |
res << describe_location(@location) | |
res << describe_paths(@location) | |
res << describe_objects(@location) | |
res.join("\n") | |
end | |
def describe_location(location) | |
@env.node[location].desc | |
end | |
def describe_paths(location) | |
@env.node[location].edges.map { |edge| | |
"there is a #{edge[:to]} going #{edge[:direction]} from here." | |
}.join("\n") | |
end | |
def describe_objects(location) | |
return unless objs = @env.objects[location] | |
objs.map { |obj| "you see a #{obj} on the floor." }.join("\n") | |
end | |
def pickup(object) | |
if @env.objects[@location].include?(object) | |
@env.objects[@location].delete(object) | |
@env.objects[:body].push(object) | |
"you are now carrying the #{object}." | |
else | |
"you cannot get that." | |
end | |
end | |
def inventory | |
items = @env.objects[:body].join(", ") | |
items = "none" if items.empty? | |
"items: #{items}." | |
end | |
def walk(direction) | |
if next_edge = @env.node[@location].edges.find { |edge| edge[:direction] == direction.to_sym } | |
@location = next_edge[:to] | |
look | |
else | |
"you cannot go that way." | |
end | |
end | |
def usage | |
@env.commands.map { |cmd, args| | |
str = "[#{cmd[0]}]#{cmd[1..-1]}" | |
args = args.map { |a| %Q(\"#{a}\") }.join(" ") | |
str << " #{args}" unless args.empty? | |
str | |
}.join(", ") | |
end | |
loop do | |
unless input = Readline.readline(">> ", true) | |
puts "please enter any commands." | |
puts usage | |
next | |
end | |
cmd, *args = input.chomp.split | |
if cmd == "quit" | |
puts "bye-bye!" | |
break | |
elsif @env.commands.keys.include?(cmd) | |
cmd << args.map { |arg| " \"#{arg}\"" }.join | |
puts eval(cmd) | |
else | |
puts "invalid command." | |
puts usage | |
end | |
puts "" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node: | |
living_room: | |
desc: "you are in the living-room. a wizard is snoring loudly on the couch." | |
edges: | |
- to: :garden | |
direction: :west | |
via: :door | |
- to: :attic | |
direction: :upstairs | |
via: :door | |
garden: | |
desc: "you are ina beautiful garden. there is a well in front of you." | |
edges: | |
- to: :living_room | |
direction: :east | |
via: :door | |
attic: | |
desc: "you are in the attic. there is a giant welding torch in the corner." | |
edges: | |
- to: :living_room | |
direction: :downstairs | |
via: :ladder | |
objects: | |
living_room: ["whiskey", "bucket"] | |
garden: ["frog", "chain"] | |
body: [] | |
commands: | |
look: [] | |
walk: ["direction"] | |
pickup: ["object"] | |
inventory: [] | |
quit: [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
例外処理が無いね 👎