blob: 68f1198c9566ce9ad8c38dbed408105e5309ff8a (
plain)
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
|
require 'json'
class Array
def second; return self[1]; end
def third; return self[2]; end
def rest; return self[1..-1]; end
end
$stuff = JSON.parse(File.read("stuff.json"))
$rooms = JSON.parse(File.read("rooms.json"))
$inventory = []
$room = "start"
$energy = 10
$light = false
def look
if $rooms[$room]["dark"] and !$light then
puts "It's too dark, you can't see anything."
else
puts $rooms[$room]["summary"]
things = $rooms[$room]["inventory"]
if things.length.zero? then
puts "You see nothing."
else
puts "You see:"
things.each do |thing|
puts "\t#{thing}"
end
end
end
end
def handle(line)
a = line.split
case a.first
when "look"
if a.second == "me" || a.second == "self" then
puts "A daring adventurer."
puts "HP: #{$energy}"
handle "inventory"
else
look
end
when "take", "get"
if $rooms[$room]["inventory"].include?(a.second) then
$inventory << a.second
$rooms[$room]["inventory"].delete(a.second)
puts "You take the #{a.second}."
else
puts "You can't see a(n) #{a.second}."
end
when "north", "south", "east", "west"
handle "go #{a.first}"
when "go"
$room = $rooms[$room][a.second]
look
when "inventory"
if $inventory.length.zero? then
puts "You have nothing."
else
puts "You have:"
$inventory.each do |thing|
puts "\t#{thing}"
end
end
when "light"
if $inventory.include?(a.second) then
if $stuff[a.second]["light"] then
puts "You light the #{a.second}."
$light = true
else
puts "You can't light that."
end
else
puts "You don't have a(n) #{a.second} to light."
end
when "snuff"
if $inventory.include?(a.second) then
if $stuff[a.second]["light"] then
puts "You snuff the #{a.second}."
$light = false
else
puts "You can't light that, let alone snuff it."
end
else
puts "You don't have a(n) #{a.second} to snuff."
end
when "eat"
if $inventory.include?(a.second) then
if $stuff[a.second]["edible"] then
$energy += $stuff[a.second]["energy"]
puts "A tasty #{a.second}"
$inventory.delete(a.second)
else
puts "You can't eat that."
end
else
puts "You don't have a(n) #{a.second} to eat."
end
end
end
look
loop do
print ">: "
line = gets.strip.downcase
handle(line)
end
|