room 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.5.2"
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,16 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.5.2)
6
+ bundler (~> 1.0.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.8.7)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.0.0)
16
+ jeweler (~> 1.5.2)
data/README ADDED
@@ -0,0 +1,5 @@
1
+
2
+ the game is making the game!
3
+ the game is playing the game!
4
+
5
+ type "room"
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "room"
16
+ gem.homepage = "http://github.com/alltom/room"
17
+ gem.summary = "the game is making the game"
18
+ gem.description = "the game is making the game"
19
+ gem.email = "tom@alltom.com"
20
+ gem.authors = ["Tom Lieber"]
21
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
22
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
23
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
24
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/room ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "optparse"
5
+
6
+ # for testing inside gem dir
7
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
8
+
9
+ require "room"
10
+
11
+ opts = OptionParser.new
12
+ opts.on("-h", "--help") do
13
+ puts "usage: room"
14
+ puts " room [room-file.rb]"
15
+ exit
16
+ end
17
+ opts.parse! ARGV
18
+
19
+ FILENAME = ARGV[0] || File.join(File.dirname(__FILE__), '..', 'examples', 'example1.rb')
20
+
21
+ unless File.readable?(FILENAME)
22
+ $stderr.puts "cannot read file #{FILENAME}"
23
+ exit 1
24
+ end
25
+
26
+ reload!
27
+
28
+ 3.times { Printer.puts }
29
+
30
+ Room.do "look"
31
+ while line = Readline.readline('> ', true)
32
+ Room.do line.chomp
33
+ end
34
+
35
+ Printer.puts "\nThe world is your cantaloupe."
@@ -0,0 +1,209 @@
1
+
2
+ class Intro < Room
3
+ def look
4
+ unless @seen_intro
5
+ @seen_intro = true
6
+ "Type 'look' to look around."
7
+ else
8
+ "This is Room," |
9
+ "a virtual world for you to explore." |
10
+ "At any time, you can type 'help', but it won't do anything." |
11
+ "" |
12
+ "Start by entering the room."
13
+ end
14
+ end
15
+
16
+ def enter_room
17
+ "You black out, then awaken in the room." |
18
+ go("bedroom")
19
+ end
20
+ dup :enter
21
+
22
+ def look_XXX feh
23
+ "You *want* to look at #{feh}?"
24
+ end
25
+ end
26
+
27
+ class Bedroom < Room
28
+ def look
29
+ "Your comfortable abode." |
30
+ "There is a closet to your left.#{" Try looking at it." unless @looked_left}" |
31
+ ("There is a pencil sharpener on your right." if have? :pencil) |
32
+ ("Right behind you, there's a chute you could jump into." if @sharpened_pencil) |
33
+ "Everything else is a blur."
34
+ end
35
+
36
+ def look_closet
37
+ @looked_left = true
38
+ "It looks like a normal closet." |
39
+ (@chipped_paint ? "Except you chipped some of the paint off." : "The white paint is a little chipped, though.")
40
+ end
41
+ dup :look_left
42
+
43
+ def look_blur
44
+ "You try to focus your eyes, but the blur... it's too fuzzy." |
45
+ "You strain your eyes. But all you can make out is" |
46
+ look
47
+ end
48
+
49
+ def enter_closet
50
+ "You creep inside the closet." |
51
+ go("closet")
52
+ end
53
+ dup :left, :enter, :go_into_the_closet, :go_into_closet
54
+
55
+ def open_closet
56
+ if @closet_open == 2
57
+ "It ain't getting any more opener."
58
+ elsif @closet_open == 1
59
+ @closet_open += 1
60
+ "The closet was already open, but you found a way to" |
61
+ "make it even MORE open. Its gaping maw beckons for you to enter." |
62
+ "It's hungry."
63
+ else
64
+ @closet_open ||= 0
65
+ @closet_open += 1
66
+ "The closet door is now open, which explains why you" |
67
+ "were celebrating just a moment ago."
68
+ end
69
+ end
70
+ dup :open_the_closet
71
+
72
+ def close_closet
73
+ @closet_open = 0
74
+ "The closet is now closed, for what it's worth."
75
+ end
76
+ dup :closet_the_closet
77
+
78
+ def chip_paint
79
+ @chipped_paint = true
80
+ take :paint_chips
81
+ "Flakes of paint come off into your hand."
82
+ end
83
+ dup :chip_the_paint, :chip_at_the_paint
84
+
85
+ def chip_some_of_the_paint_off_of_the_closet
86
+ "You were way to explicit. Try 'chip the paint' or even 'chip paint'."
87
+ end
88
+
89
+ def look_paint
90
+ "Your job is short. This paint is already dry."
91
+ end
92
+ dup :look_paint_chips
93
+
94
+ def sharpen_pencil
95
+ if have? :pencil
96
+ @sharpened_pencil = true
97
+ lose :pencil
98
+ "You whittle the pencil to nothing."
99
+ else
100
+ unknown_command
101
+ end
102
+ end
103
+ dup :sharpen_the_pencil
104
+
105
+ def look_pencil_sharpener
106
+ if have? :pencil
107
+ "That's a mighty fine pencil sharpener. Red."
108
+ else
109
+ unknown_command
110
+ end
111
+ end
112
+
113
+ def jump_into_chute
114
+ if @sharpened_pencil
115
+ "Throwing caution to the wind, you jump into the chute and die." |
116
+ "Just kidding! You find yourself on skis," |
117
+ "hurtling down a snow-covered slope at break-neck speed!" |
118
+ go("skiing1")
119
+ else
120
+ unknown_command
121
+ end
122
+ end
123
+ dup :jump_into_the_chute
124
+
125
+ def eat_XXX item
126
+ "You eat the #{item}, wondering whether that's a good idea."
127
+ end
128
+ dup :consume_XXX
129
+ end
130
+
131
+ class Closet < Room
132
+ def look
133
+ "In the closet." |
134
+ "It's too dark to see. You might be stuck!" |
135
+ (if @got_pencil
136
+ "No wait, you can still exit the closet."
137
+ else
138
+ "You stub your toe on something sharp. It feels like a pencil."
139
+ end)
140
+ end
141
+
142
+ def open_door
143
+ "The door won't budge."
144
+ end
145
+ dup :open_the_door
146
+
147
+ def look_pencil
148
+ if @got_pencil
149
+ unknown_command
150
+ else
151
+ "It's too dark to see, so you settle for stubbing your" |
152
+ "foot on it again. Definitely a No. 3 pencil." |
153
+ "Maybe you should 'get' it."
154
+ end
155
+ end
156
+
157
+ def get_pencil
158
+ if @got_pencil
159
+ "What pencil?"
160
+ else
161
+ take :pencil
162
+ @got_pencil = true
163
+ "Awesome, a pencil! You place the pencil in your pocket."
164
+ end
165
+ end
166
+
167
+ def exit
168
+ "You somehow manage to leave the closet." |
169
+ go("bedroom")
170
+ end
171
+ dup :exit_closet, :exit_the_closet, :leave
172
+ end
173
+
174
+ class Skiing1 < Room
175
+ def look
176
+ if @looked
177
+ go("skiing2")
178
+ else
179
+ @looked = true
180
+ "The lessons pay off. You've got some snow in your teeth."
181
+ end
182
+ end
183
+
184
+ def eat_snow
185
+ "That was delicious." |
186
+ go("skiing2")
187
+ end
188
+ dup :eat_the_snow
189
+ end
190
+
191
+ class Skiing2 < Room
192
+ def look
193
+ @looked ||= 0
194
+ @looked += 1
195
+
196
+ if @looked == 5
197
+ "Go away."
198
+ elsif @looked >= 2
199
+ "The end."
200
+ else
201
+ "You're at the bottom of the hill." |
202
+ (if have? :paint_chips
203
+ "Your final score is 1 because you still have the paint chips."
204
+ else
205
+ "Your final score is 0 because you don't have any paint chips."
206
+ end)
207
+ end
208
+ end
209
+ end
data/lib/room.rb ADDED
@@ -0,0 +1,156 @@
1
+
2
+ require "thread"
3
+ require "readline"
4
+
5
+ def reload!
6
+ $commands = {}
7
+
8
+ old_count = Room.rooms.keys.count
9
+ load FILENAME
10
+ Room.rooms.keys.count - old_count
11
+ end
12
+
13
+ class String
14
+ def |(o)
15
+ if o.nil?
16
+ self
17
+ else
18
+ self + "\n" + o
19
+ end
20
+ end
21
+
22
+ def commandify
23
+ Regexp.compile("^" + Regexp.escape(self.gsub("_", " ")).gsub("XXX", "(.+)") + "$")
24
+ end
25
+ end
26
+
27
+ class Printer
28
+ class << self
29
+ def puts(str = "")
30
+ lines = str.split("\n", -1)
31
+ lines << "" if lines.length == 0
32
+ lines.each { |line| sleep 0.05; $stdout.puts line }
33
+ end
34
+ end
35
+ end
36
+
37
+ class Room
38
+ DEFAULT_COMMANDS = [["look".commandify, :look], ["l".commandify, :look]]
39
+
40
+ def go key
41
+ Room.go key
42
+ end
43
+
44
+ def inventory
45
+ $inventory ||= []
46
+ end
47
+
48
+ def have? item
49
+ inventory.include? item
50
+ end
51
+
52
+ def take item
53
+ inventory << item
54
+ end
55
+
56
+ def lose item
57
+ inventory.delete item
58
+ end
59
+
60
+ def do action
61
+ Printer.puts
62
+ if action.strip == ""
63
+ elsif (r = self.class.commands.detect { |c, m| c =~ action })
64
+ command, method = r
65
+ args = command.match(action).to_a.drop(1)
66
+ Printer.puts self.send(method, *args).to_s.rstrip
67
+ elsif action == "reload!"
68
+ d = reload!
69
+ Printer.puts "A great wave of relief washes over you."
70
+ Printer.puts "The world seems larger by about #{d}." if d > 0
71
+ else
72
+ Printer.puts unknown_command(action)
73
+ end
74
+ end
75
+
76
+ def look
77
+ "A nondescript room."
78
+ end
79
+
80
+ def unknown_command action = nil
81
+ "I don't understand."
82
+ end
83
+
84
+ def unknown_room key
85
+ "" |
86
+ "The fourth wall falls over and you realize you didn't really" |
87
+ "want to go to '#{key}' anyway. You decide to let the author" |
88
+ "know about this terrible oversight as soon as possible."
89
+ end
90
+
91
+ class << self
92
+ def key
93
+ self.to_s.downcase
94
+ end
95
+
96
+ def commands
97
+ $commands ||= {}
98
+ $commands[key] ||= DEFAULT_COMMANDS.dup
99
+ end
100
+
101
+ def rooms
102
+ $rooms ||= {}
103
+ end
104
+
105
+ def go key
106
+ if rooms[key]
107
+ $here = rooms[key]
108
+ "\n" + $here.look
109
+ else
110
+ $here.unknown_room key
111
+ end
112
+ end
113
+
114
+ def do action
115
+ if $here
116
+ $here.do action
117
+ else
118
+ Printer.puts
119
+ Printer.puts "Where am I?"
120
+ end
121
+ end
122
+
123
+ def dup *cmds
124
+ cmds.each do |cmd|
125
+ alias_method cmd, $last_command
126
+ end
127
+ end
128
+
129
+ def add_command regexp, method
130
+ commands << [regexp, method]
131
+ end
132
+
133
+ def inherited subclass
134
+ rooms[subclass.key] = subclass.new
135
+ $here ||= rooms[subclass.key]
136
+ end
137
+
138
+ def method_added name
139
+ if public_instance_methods.include? name.to_s
140
+ $last_command = name
141
+
142
+ if /^look(.+)/ =~ name.to_s
143
+ %w{ look l look_at look_at_the examine ex }.each do |prefix|
144
+ add_command "#{prefix}#{$1}".commandify, name
145
+ end
146
+ elsif /^enter(.+)/ =~ name.to_s
147
+ %w{ enter enter_the go_into go_into_the }.each do |prefix|
148
+ add_command "#{prefix}#{$1}".commandify, name
149
+ end
150
+ else
151
+ add_command name.to_s.commandify, name
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
data/room.gemspec ADDED
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{room}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tom Lieber"]
12
+ s.date = %q{2011-03-17}
13
+ s.default_executable = %q{room}
14
+ s.description = %q{the game is making the game}
15
+ s.email = %q{tom@alltom.com}
16
+ s.executables = ["room"]
17
+ s.extra_rdoc_files = [
18
+ "README"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "README",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "bin/room",
28
+ "examples/example1.rb",
29
+ "lib/room.rb",
30
+ "room.gemspec"
31
+ ]
32
+ s.homepage = %q{http://github.com/alltom/room}
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.4.2}
35
+ s.summary = %q{the game is making the game}
36
+ s.test_files = [
37
+ "examples/example1.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ s.specification_version = 3
42
+
43
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
45
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
46
+ else
47
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
48
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
49
+ end
50
+ else
51
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
52
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
53
+ end
54
+ end
55
+
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: room
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Tom Lieber
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-17 00:00:00 -07:00
19
+ default_executable: room
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 23
28
+ segments:
29
+ - 1
30
+ - 0
31
+ - 0
32
+ version: 1.0.0
33
+ requirement: *id001
34
+ prerelease: false
35
+ name: bundler
36
+ type: :development
37
+ - !ruby/object:Gem::Dependency
38
+ version_requirements: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 1
46
+ - 5
47
+ - 2
48
+ version: 1.5.2
49
+ requirement: *id002
50
+ prerelease: false
51
+ name: jeweler
52
+ type: :development
53
+ description: the game is making the game
54
+ email: tom@alltom.com
55
+ executables:
56
+ - room
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - README
61
+ files:
62
+ - .document
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - README
66
+ - Rakefile
67
+ - VERSION
68
+ - bin/room
69
+ - examples/example1.rb
70
+ - lib/room.rb
71
+ - room.gemspec
72
+ has_rdoc: true
73
+ homepage: http://github.com/alltom/room
74
+ licenses: []
75
+
76
+ post_install_message:
77
+ rdoc_options: []
78
+
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ requirements: []
100
+
101
+ rubyforge_project:
102
+ rubygems_version: 1.4.2
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: the game is making the game
106
+ test_files:
107
+ - examples/example1.rb