karnowski-ruby-warrior 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (108) hide show
  1. data/.gitignore +3 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +156 -0
  4. data/Rakefile +36 -0
  5. data/VERSION +1 -0
  6. data/bin/rubywarrior +8 -0
  7. data/features/levels.feature +50 -0
  8. data/features/profiles.feature +46 -0
  9. data/features/step_definitions/common_steps.rb +7 -0
  10. data/features/step_definitions/interaction_steps.rb +53 -0
  11. data/features/support/env.rb +8 -0
  12. data/features/support/mockio.rb +57 -0
  13. data/features/towers.feature +14 -0
  14. data/lib/ruby_warrior.rb +41 -0
  15. data/lib/ruby_warrior/abilities/attack.rb +24 -0
  16. data/lib/ruby_warrior/abilities/base.rb +36 -0
  17. data/lib/ruby_warrior/abilities/bind.rb +19 -0
  18. data/lib/ruby_warrior/abilities/direction_of.rb +13 -0
  19. data/lib/ruby_warrior/abilities/direction_of_stairs.rb +13 -0
  20. data/lib/ruby_warrior/abilities/distance.rb +13 -0
  21. data/lib/ruby_warrior/abilities/explode.rb +16 -0
  22. data/lib/ruby_warrior/abilities/feel.rb +13 -0
  23. data/lib/ruby_warrior/abilities/health.rb +13 -0
  24. data/lib/ruby_warrior/abilities/listen.rb +15 -0
  25. data/lib/ruby_warrior/abilities/look.rb +15 -0
  26. data/lib/ruby_warrior/abilities/pivot.rb +16 -0
  27. data/lib/ruby_warrior/abilities/rescue.rb +23 -0
  28. data/lib/ruby_warrior/abilities/rest.rb +20 -0
  29. data/lib/ruby_warrior/abilities/shoot.rb +23 -0
  30. data/lib/ruby_warrior/abilities/walk.rb +20 -0
  31. data/lib/ruby_warrior/config.rb +11 -0
  32. data/lib/ruby_warrior/core_additions.rb +25 -0
  33. data/lib/ruby_warrior/floor.rb +71 -0
  34. data/lib/ruby_warrior/game.rb +160 -0
  35. data/lib/ruby_warrior/level.rb +93 -0
  36. data/lib/ruby_warrior/level_loader.rb +52 -0
  37. data/lib/ruby_warrior/player_generator.rb +41 -0
  38. data/lib/ruby_warrior/position.rb +80 -0
  39. data/lib/ruby_warrior/profile.rb +86 -0
  40. data/lib/ruby_warrior/space.rb +63 -0
  41. data/lib/ruby_warrior/tower.rb +14 -0
  42. data/lib/ruby_warrior/turn.rb +38 -0
  43. data/lib/ruby_warrior/ui.rb +64 -0
  44. data/lib/ruby_warrior/units/archer.rb +34 -0
  45. data/lib/ruby_warrior/units/base.rb +95 -0
  46. data/lib/ruby_warrior/units/captive.rb +30 -0
  47. data/lib/ruby_warrior/units/sludge.rb +30 -0
  48. data/lib/ruby_warrior/units/thick_sludge.rb +13 -0
  49. data/lib/ruby_warrior/units/warrior.rb +58 -0
  50. data/lib/ruby_warrior/units/wizard.rb +34 -0
  51. data/ruby-warrior.gemspec +191 -0
  52. data/script/console +8 -0
  53. data/spec/fixtures/short-tower/level_001.rb +14 -0
  54. data/spec/fixtures/short-tower/level_002.rb +14 -0
  55. data/spec/fixtures/walking_player.rb +5 -0
  56. data/spec/ruby_warrior/abilities/attack_spec.rb +51 -0
  57. data/spec/ruby_warrior/abilities/base_spec.rb +24 -0
  58. data/spec/ruby_warrior/abilities/bind_spec.rb +19 -0
  59. data/spec/ruby_warrior/abilities/direction_of_spec.rb +13 -0
  60. data/spec/ruby_warrior/abilities/direction_of_stairs_spec.rb +13 -0
  61. data/spec/ruby_warrior/abilities/distance_spec.rb +13 -0
  62. data/spec/ruby_warrior/abilities/explode_spec.rb +21 -0
  63. data/spec/ruby_warrior/abilities/feel_spec.rb +13 -0
  64. data/spec/ruby_warrior/abilities/health_spec.rb +13 -0
  65. data/spec/ruby_warrior/abilities/listen_spec.rb +17 -0
  66. data/spec/ruby_warrior/abilities/look_spec.rb +15 -0
  67. data/spec/ruby_warrior/abilities/pivot_spec.rb +18 -0
  68. data/spec/ruby_warrior/abilities/rescue_spec.rb +40 -0
  69. data/spec/ruby_warrior/abilities/rest_spec.rb +29 -0
  70. data/spec/ruby_warrior/abilities/shoot_spec.rb +22 -0
  71. data/spec/ruby_warrior/abilities/walk_spec.rb +25 -0
  72. data/spec/ruby_warrior/floor_spec.rb +81 -0
  73. data/spec/ruby_warrior/game_spec.rb +96 -0
  74. data/spec/ruby_warrior/level_loader_spec.rb +54 -0
  75. data/spec/ruby_warrior/level_spec.rb +163 -0
  76. data/spec/ruby_warrior/player_generator_spec.rb +12 -0
  77. data/spec/ruby_warrior/position_spec.rb +103 -0
  78. data/spec/ruby_warrior/profile_spec.rb +125 -0
  79. data/spec/ruby_warrior/space_spec.rb +165 -0
  80. data/spec/ruby_warrior/tower_spec.rb +15 -0
  81. data/spec/ruby_warrior/turn_spec.rb +42 -0
  82. data/spec/ruby_warrior/ui_spec.rb +96 -0
  83. data/spec/ruby_warrior/units/archer_spec.rb +23 -0
  84. data/spec/ruby_warrior/units/base_spec.rb +133 -0
  85. data/spec/ruby_warrior/units/captive_spec.rb +34 -0
  86. data/spec/ruby_warrior/units/sludge_spec.rb +27 -0
  87. data/spec/ruby_warrior/units/thick_sludge_spec.rb +19 -0
  88. data/spec/ruby_warrior/units/warrior_spec.rb +60 -0
  89. data/spec/ruby_warrior/units/wizard_spec.rb +23 -0
  90. data/spec/spec_helper.rb +7 -0
  91. data/templates/README.erb +20 -0
  92. data/templates/player.rb +5 -0
  93. data/towers/beginner/level_001.rb +15 -0
  94. data/towers/beginner/level_002.rb +17 -0
  95. data/towers/beginner/level_003.rb +20 -0
  96. data/towers/beginner/level_004.rb +17 -0
  97. data/towers/beginner/level_005.rb +21 -0
  98. data/towers/beginner/level_006.rb +18 -0
  99. data/towers/beginner/level_007.rb +17 -0
  100. data/towers/beginner/level_008.rb +20 -0
  101. data/towers/beginner/level_009.rb +19 -0
  102. data/towers/intermediate/level_001.rb +17 -0
  103. data/towers/intermediate/level_002.rb +19 -0
  104. data/towers/intermediate/level_003.rb +22 -0
  105. data/towers/intermediate/level_004.rb +23 -0
  106. data/towers/intermediate/level_005.rb +18 -0
  107. data/towers/intermediate/level_006.rb +23 -0
  108. metadata +218 -0
@@ -0,0 +1,3 @@
1
+ pkg
2
+ tmp/**/*
3
+ ruby-warrior
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Ryan Bates
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,156 @@
1
+ = Ruby Warrior
2
+
3
+ This is a game designed to teach the Ruby language and artificial intelligence in a fun, interactive way.
4
+
5
+ You play as a warrior climbing a tall tower to reach the precious Ruby at the top level. On each floor you need to write a Ruby script to instruct the warrior to battle enemies, rescue captives, and reach the stairs. You have some idea of what each floor contains, but you never know for certain what will happen. You must give the Warrior enough artificial intelligence up-front to find his own way.
6
+
7
+ NOTE: The player directory structure changed on July 18, 2009. If you have an old profile using the "level-00*" structure then move the contents of the last level into the parent directory.
8
+
9
+
10
+ == Getting Started
11
+
12
+ Run the "rubywarrior" executable (contained in the bin directory) to setup your profile which will create a ruby-warrior directory in your current location. There you will find a player.rb file in your profile's directory which looks like this:
13
+
14
+ class Player
15
+ def play_turn(warrior)
16
+ # your code goes here
17
+ end
18
+ end
19
+
20
+ Your objective is to fill this "play_turn" method with commands to instruct the warrior what to do. With each level your abilities will grow along with the difficulty. See the README in your profile's directory for details on what abilities your warrior has available on the current level.
21
+
22
+ Here is a simple example which will instruct the warrior to attack if he feels an enemy, otherwise he will walk forward.
23
+
24
+ class Player
25
+ def play_turn(warrior)
26
+ if warrior.feel.enemy?
27
+ warrior.attack!
28
+ else
29
+ warrior.walk!
30
+ end
31
+ end
32
+ end
33
+
34
+ Once you are done editing player.rb, save the file and run the "rubywarrior" command again to start playing the level. The play happens through a series of turns. On each one, your "play_turn" method is called along with any enemy's.
35
+
36
+ You cannot change your code in the middle of a level. You must take into account everything that may happen on that level and give your warrior the proper instructions from the start.
37
+
38
+ Losing all of your health will cause you to fail the level. You are not punished by this, you simply need to go back to your player.rb, improve your code, and try again.
39
+
40
+ Once you pass a level (by reaching the stairs), the profile README will be updated for the next level. Ruby rubywarrior again to play the next level.
41
+
42
+
43
+ == Scoring
44
+
45
+ Your objective is to not only reach the stairs, but to get the highest score you can. There are many ways you can earn points on a level.
46
+
47
+ * defeat an enemy to add his max health to your score
48
+ * rescue a captive to earn 20 points
49
+ * pass the level within the bonus time to earn the amount of bonus time remaining
50
+ * defeat all enemies and rescue all captives to receive a 20% overall bonus
51
+
52
+ A total score is kept as you progress through the levels. When you pass a level, that score is added to your total.
53
+
54
+
55
+ == Perspective
56
+
57
+ Even though this is a text-based game, think of it as two-dimensional where you are viewing from overhead. Each level is always rectangular in shape and is made up of a number of squares. Only one unit can be on a given square at a time, and your objective is to find the square with the stairs. Here is an example level map and key.
58
+
59
+ ----
60
+ |C s>|
61
+ | S s|
62
+ |C @ |
63
+ ----
64
+
65
+ > = Stairs
66
+ @ = Warrior (20 HP)
67
+ s = Sludge (12 HP)
68
+ S = Thick Sludge (24 HP)
69
+ C = Captive (1 HP)
70
+
71
+
72
+ == Commanding the Warrior
73
+
74
+ When you first start, your warrior will only have a few abilities, but with each level your abilities will grow. A warrior has two kinds of abilities: actions and senses.
75
+
76
+ An action is something that effects the game in some way. You can easily tell an action because it ends in an exclamation mark. Only one action can be performed per turn, so choose wisely. Here are some examples of actions.
77
+
78
+ warrior.walk!
79
+ Move in given direction (forward by default).
80
+
81
+ warrior.attack!
82
+ Attack the unit in given direction (forward by default).
83
+
84
+ warrior.rest!
85
+ Gain 10% of max health back, but do nothing more.
86
+
87
+ warrior.bind!
88
+ Bind unit in given direction to keep him from moving (forward by default).
89
+
90
+ warrior.rescue!
91
+ Rescue a captive from his chains (earning 50 points) in given direction (forward by default).
92
+
93
+
94
+ A sense is something which gathers information about the floor. You can perform senses as often as you want per turn to gather information about your surroundings and to aid you in choosing the proper action. Senses do NOT end in an exclamation mark.
95
+
96
+ warrior.feel
97
+ Returns a Space for the given direction (forward by default).
98
+
99
+ warrior.health
100
+ Returns an integer representing your health.
101
+
102
+ warrior.distance
103
+ Returns the number of spaces the stairs are away.
104
+
105
+ warrior.listen
106
+ Returns an array of all spaces which have units in them.
107
+
108
+
109
+ Since what you sense will change each turn, you should record what information you gather for use on the next turn. For example, you can determine if you are being attacked if your health has gone down since the last turn.
110
+
111
+
112
+ == Spaces
113
+
114
+ Whenever you sense an area, often one or multiple spaces (in an array) will be returned. A space is an object representing a square in the level. You can call methods on a space to gather information about what is there. Here are the various methods you can call on a space.
115
+
116
+ space.empty?
117
+ If true, this means that nothing (except maybe stairs) is at this location and you can walk here.
118
+
119
+ space.stairs?
120
+ Determine if stairs are at that location
121
+
122
+ space.enemy?
123
+ Determine if an enemy unit is at this location.
124
+
125
+ space.captive?
126
+ Determine if a captive is at this location.
127
+
128
+ space.wall?
129
+ Returns true if this is the edge of the level. You can't walk here.
130
+
131
+ space.ticking?
132
+ Returns true if this space contains a bomb which will explode in time.
133
+
134
+ You will often call these methods directly after a sense. For example, the "feel" sense returns one space. You can call "captive?" on this to determine if a captive is in front of you.
135
+
136
+ warrior.feel.captive?
137
+
138
+
139
+ == Epic Mode
140
+
141
+ Once you reach the top of the tower, you will enter epic mode. When running rubywarrior again it will run your current player.rb through all levels in the tower without stopping.
142
+
143
+ It might take a bit of fine-tuning to get your warrior passing all levels in one sweep, but the real challenge is getting the highest score you can.
144
+
145
+
146
+ == Tips
147
+
148
+ If you ever get stuck on a level, review the README documentation and be sure you're trying each ability out. If you can't keep your health up, be sure to "rest" when no enemy is around (while keeping an eye on your health). Also, try to use far-ranged weapons whenever possible (such as the bow).
149
+
150
+ Remember, you're working in Ruby here. Don't simply fill up the "play_turn" method with a lot of code. Organize it with methods and classes. The player directory is set up as a load path so you can include other ruby files from your player.rb file.
151
+
152
+ Senses are cheap, so use them liberally. Store the sensed information to help you better determine what actions to take in the future.
153
+
154
+ Running "rubywarrior" while you are in your profile directory will auto-select that profile so you don't have to each time.
155
+
156
+ If you're aiming for points, remember to sweep the area. Even if you're close to the stairs, don't go in until you've gotten everything (if you have the health). Use far-ranged senses (such as look and listen) to determine if there are any enemies left.
@@ -0,0 +1,36 @@
1
+ require 'rake'
2
+ require 'spec'
3
+ require 'cucumber'
4
+
5
+ require 'cucumber/rake/task'
6
+ require 'spec/rake/spectask'
7
+
8
+ spec_files = Rake::FileList["spec/**/*_spec.rb"]
9
+
10
+ desc "Run specs"
11
+ Spec::Rake::SpecTask.new do |t|
12
+ t.spec_files = spec_files
13
+ t.spec_opts = ["-c"]
14
+ end
15
+
16
+ Cucumber::Rake::Task.new(:features) do |t|
17
+ t.cucumber_opts = "features --format progress"
18
+ end
19
+
20
+ begin
21
+ require 'jeweler'
22
+ Jeweler::Tasks.new do |gemspec|
23
+ gemspec.name = "ruby-warrior"
24
+ gemspec.summary = "Game written in Ruby for learning Ruby and artificial intelligence."
25
+ gemspec.email = "ryan@railscasts.com"
26
+ gemspec.homepage = "http://github.com/ryanb/ruby-warrior"
27
+ gemspec.authors = ["Ryan Bates"]
28
+
29
+ gemspec.add_development_dependency('rspec')
30
+ gemspec.add_development_dependency('cucumber')
31
+ end
32
+ rescue LoadError
33
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
34
+ end
35
+
36
+ task :default => [:check_dependencies, :spec, :features]
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require File.dirname(__FILE__) + '/../lib/ruby_warrior'
3
+
4
+ RubyWarrior::UI.out_stream = STDOUT
5
+ RubyWarrior::UI.in_stream = STDIN
6
+ RubyWarrior::UI.delay = 0.8 # TODO allow customization
7
+ game = RubyWarrior::Game.new # TODO pass options into game when initializing
8
+ game.start
@@ -0,0 +1,50 @@
1
+ Feature: Play levels
2
+ In order to play ruby warrior
3
+ As a player
4
+ I want to advance through levels or retry them
5
+
6
+ Background:
7
+ Given no profile at "tmp"
8
+
9
+ Scenario: Pass first level, fail second level
10
+ Given a profile named "Joe" on "beginner"
11
+ When I copy fixture "walking_player.rb" to "tmp/ruby-warrior/joe-beginner/player.rb"
12
+ And I run rubywarrior
13
+ And I choose "Joe - beginner - level 1" for "profile"
14
+ And I answer "y" to "next level"
15
+ Then I should see "directory for the next level"
16
+ When I run rubywarrior
17
+ And I choose "Joe - beginner - level 2" for "profile"
18
+ And I answer "y" to "clues"
19
+ Then I should see "warrior.feel.empty?"
20
+
21
+ Scenario: Retry first level
22
+ Given a profile named "Joe" on "beginner"
23
+ When I copy fixture "walking_player.rb" to "tmp/ruby-warrior/joe-beginner/player.rb"
24
+ And I run rubywarrior
25
+ And I choose "Joe - beginner - level 1" for "profile"
26
+ And I answer "n" to "next level"
27
+ Then I should see "current level"
28
+ When I run rubywarrior
29
+ Then I should see "Joe - beginner - level 1"
30
+
31
+ Scenario: Replay levels as epic when finishing last level
32
+ When I copy fixture "short-tower" to "towers/short"
33
+ Given a profile named "Bill" on "short"
34
+ When I copy fixture "walking_player.rb" to "tmp/ruby-warrior/bill-short/player.rb"
35
+ And I run rubywarrior
36
+ And I choose "Bill - short - level 1" for "profile"
37
+ Then I answer "y" to "next level"
38
+ And I should see "next level"
39
+ When I run rubywarrior
40
+ And I choose "Bill - short - level 2" for "profile"
41
+ Then I answer "y" to "epic"
42
+ And I should see "epic mode"
43
+ When I run rubywarrior
44
+ And I choose "Bill - short - first score 34 - epic score 0" for "profile"
45
+ Then I should see "top of the tower"
46
+ When I run rubywarrior
47
+ And I choose "Bill - short - first score 34 - epic score 34" for "profile"
48
+ Then I should see "top of the tower"
49
+ When I run rubywarrior
50
+ Then I should see "Bill - short - first score 34 - epic score 34"
@@ -0,0 +1,46 @@
1
+ Feature: Manage Profiles
2
+ In order to play ruby warrior
3
+ As a player
4
+ I want to create and choose profiles
5
+
6
+ Background:
7
+ Given no profile at "tmp"
8
+
9
+ Scenario: New profile
10
+ When I run rubywarrior
11
+ And I answer "y" to "create one?"
12
+ And I choose "beginner" for "tower"
13
+ And I answer "Joe" to "name"
14
+ Then I should see "generated"
15
+ And I should find file at "tmp/ruby-warrior"
16
+ When I run rubywarrior
17
+ Then I should see "Joe - beginner - level 1 - score 0"
18
+
19
+ Scenario: Another new profile on same tower
20
+ Given a profile named "Joe" on "beginner"
21
+ When I run rubywarrior
22
+ And I choose "New" for "profile"
23
+ And I choose "beginner" for "tower"
24
+ And I answer "Bob" to "name"
25
+ Then I should see "generated"
26
+ When I run rubywarrior
27
+ Then I should see "Bob - beginner - level 1 - score 0"
28
+
29
+ Scenario: Replace profile in same tower with same name
30
+ Given a profile named "Joe" on "beginner"
31
+ When I run rubywarrior
32
+ And I choose "New" for "profile"
33
+ And I choose "beginner" for "tower"
34
+ And I answer "Joe" to "name"
35
+ And I answer "y" to "replace"
36
+ Then I should see "generated"
37
+ When I run rubywarrior
38
+ Then I should see "Joe - beginner - level 1 - score 0"
39
+
40
+ Scenario: Auto select profile at given path
41
+ Given a profile named "Joe" on "beginner"
42
+ And current directory is "tmp/ruby-warrior/joe-beginner"
43
+ When I copy fixture "walking_player.rb" to "tmp/ruby-warrior/joe-beginner/player.rb"
44
+ And I run rubywarrior
45
+ And I answer "y" to "next level"
46
+ Then I should see "directory for the next level"
@@ -0,0 +1,7 @@
1
+ When /^I copy fixture "([^\"]*)" to "([^\"]*)"$/ do |from, to|
2
+ FileUtils.cp_r("spec/fixtures/" + from, to)
3
+ end
4
+
5
+ Then /^I should find file at "([^\"]*)"$/ do |path|
6
+ File.exist?(path).should be_true
7
+ end
@@ -0,0 +1,53 @@
1
+ Given /^a profile named "([^\"]*)" on "([^\"]*)"$/ do |name, tower|
2
+ When 'I run rubywarrior'
3
+ And 'I answer "y" to "create one?"'
4
+ And 'I choose "' + tower + '" for "tower"'
5
+ And 'I answer "' + name + '" to "name"'
6
+ Then 'I should see "generated"'
7
+ end
8
+
9
+ Given /^no profile at "([^\"]*)"$/ do |path|
10
+ RubyWarrior::Config.path_prefix = path
11
+ FileUtils.rm_rf("#{path}/ruby-warrior")
12
+ end
13
+
14
+ Given /^current directory is "([^\"]*)"$/ do |path|
15
+ RubyWarrior::Config.path_prefix = path
16
+ end
17
+
18
+ When /^I run rubywarrior$/ do
19
+ @io = MockIO.new
20
+ @io.start do |io|
21
+ RubyWarrior::UI.out_stream = io
22
+ RubyWarrior::UI.in_stream = io
23
+ RubyWarrior::Game.new.start
24
+ end
25
+ end
26
+
27
+ When /^I answer "([^\"]*)" to "([^\"]*)"$/ do |answer, question|
28
+ @io.gets_until_include(question)
29
+ @io.puts(answer)
30
+ end
31
+
32
+ When /^I choose "([^\"]*)" for "([^\"]*)"$/ do |choice, phrase|
33
+ answer = nil
34
+ content = @io.gets_until_include(phrase)
35
+ content.split("\n").each do |line|
36
+ if line.include?(choice) && line =~ /\[(\d)\]/
37
+ answer = $1
38
+ end
39
+ end
40
+ if answer
41
+ @io.puts(answer)
42
+ else
43
+ raise "Unable to find choice #{choice} in #{content}"
44
+ end
45
+ end
46
+
47
+ When /^I wait until it says "([^\"]*)"$/ do |phrase|
48
+ @io.gets_until_include(phrase)
49
+ end
50
+
51
+ Then /^I should see "([^\"]*)"$/ do |phrase|
52
+ @io.gets_until_include(phrase).should include(phrase)
53
+ end
@@ -0,0 +1,8 @@
1
+ require 'cucumber'
2
+ require 'spec'
3
+
4
+ require File.dirname(__FILE__) + '/../../lib/ruby_warrior'
5
+
6
+ After do
7
+ FileUtils.rm_rf "towers/short"
8
+ end
@@ -0,0 +1,57 @@
1
+ class MockIO
2
+ class Timeout < Exception
3
+ end
4
+
5
+ def initialize(receiver = nil)
6
+ @buffer = ""
7
+ @receiver = receiver || MockIO.new(self)
8
+ end
9
+
10
+ def gets
11
+ 1000.times do |n|
12
+ sleep 0.01 if n > 800 # throttle if it seems to be taking a while
13
+ unless @buffer.empty?
14
+ content = @buffer
15
+ @buffer = ""
16
+ return content
17
+ end
18
+ end
19
+ raise Timeout, "MockIO Timeout: No content was received for gets."
20
+ end
21
+
22
+ def gets_until_include(phrase)
23
+ content = gets
24
+ while !content.include?(phrase)
25
+ begin
26
+ content += gets
27
+ rescue Timeout
28
+ raise "Unable to find #{phrase} in #{content}"
29
+ end
30
+ end
31
+ content
32
+ end
33
+
34
+ # TODO make this thread safe
35
+ def puts(str)
36
+ @receiver << "#{str}\n"
37
+ end
38
+
39
+ def print(str)
40
+ @receiver << str.to_s
41
+ end
42
+
43
+ def <<(str)
44
+ @buffer << str
45
+ end
46
+
47
+ def start
48
+ main_thread = Thread.current
49
+ Thread.new do
50
+ begin
51
+ yield(@receiver)
52
+ rescue Exception => e
53
+ main_thread.raise(e)
54
+ end
55
+ end
56
+ end
57
+ end