From d75c1bab720eff1ca8a971de46a46a7dc5a9f9a3 Mon Sep 17 00:00:00 2001 From: Robert Qualls Date: Wed, 5 Jun 2013 00:00:11 -0400 Subject: [PATCH 01/32] Fix points earned on rescue --- README.rdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 63ff9f3c..46f5f50a 100644 --- a/README.rdoc +++ b/README.rdoc @@ -94,7 +94,7 @@ An action is something that effects the game in some way. You can easily tell an Bind unit in given direction to keep him from moving (forward by default). warrior.rescue! - Rescue a captive from his chains (earning 50 points) in given direction (forward by default). + Rescue a captive from his chains (earning 20 points) in given direction (forward by default). 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. From 9b1b723c69a43020c117dec8faa1e030f70556eb Mon Sep 17 00:00:00 2001 From: kenny-evitt Date: Thu, 19 Nov 2015 14:17:10 -0500 Subject: [PATCH 02/32] Format inline command as code I didn't initially catch that the m-dash was 'really' two regular dashes/hyphens. --- README.rdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 63ff9f3c..52346018 100644 --- a/README.rdoc +++ b/README.rdoc @@ -181,4 +181,4 @@ Running "rubywarrior" while you are in your profile directory will auto-select t 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. -Make sure to try the different options you can pass to the rubywarrior command. Run "rubywarrior --help" to see them all. +Make sure to try the different options you can pass to the rubywarrior command. Run rubywarrior --help to see them all. From af4c984ca558c9a2b7f171ddf186639605ba0579 Mon Sep 17 00:00:00 2001 From: David Winiecki Date: Mon, 4 Jan 2016 21:26:02 -0800 Subject: [PATCH 03/32] Make success output more intuitive when using --skip option. --- lib/ruby_warrior/game.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/ruby_warrior/game.rb b/lib/ruby_warrior/game.rb index 59f00c47..582cc42e 100644 --- a/lib/ruby_warrior/game.rb +++ b/lib/ruby_warrior/game.rb @@ -94,7 +94,9 @@ def play_current_level end def request_next_level - if !Config.skip_input? && (next_level.exists? ? UI.ask("Would you like to continue on to the next level?") : UI.ask("Would you like to continue on to epic mode?")) + if Config.skip_input? + UI.puts "Staying on current level. Remove --skip option to continue on to the next level." + elsif (next_level.exists? ? UI.ask("Would you like to continue on to the next level?") : UI.ask("Would you like to continue on to epic mode?")) if next_level.exists? prepare_next_level UI.puts "See the updated README in the rubywarrior/#{profile.directory_name} directory." @@ -103,7 +105,7 @@ def request_next_level UI.puts "Run rubywarrior again to play epic mode." end else - UI.puts "Staying on current level. Try to earn more points next time." + UI.puts "Staying on current level." end end From 3c00ffcd104fa905584b73398f56662475ce07cc Mon Sep 17 00:00:00 2001 From: kathanshukla <70348137+kathanshukla@users.noreply.github.com> Date: Thu, 1 Oct 2020 02:18:19 +0530 Subject: [PATCH 04/32] Update README.rdoc --- README.rdoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rdoc b/README.rdoc index 63ff9f3c..46fb9623 100644 --- a/README.rdoc +++ b/README.rdoc @@ -1,6 +1,6 @@ = Ruby Warrior -This is a game designed to teach the Ruby language and artificial intelligence in a fun, interactive way. +This is a game designed to teach the Ruby language and artificial intelligence in a fun and interactive way. 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. From 6dd5667da0d4fd8ae90e99f5087794e231aba3fd Mon Sep 17 00:00:00 2001 From: Felipe Vogel Date: Mon, 25 Mar 2024 15:31:37 -0400 Subject: [PATCH 05/32] Use require_relative in executable --- bin/rubywarrior | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/rubywarrior b/bin/rubywarrior index 07bccd0c..062ee7a6 100755 --- a/bin/rubywarrior +++ b/bin/rubywarrior @@ -1,5 +1,5 @@ #!/usr/bin/env ruby -require File.dirname(__FILE__) + '/../lib/ruby_warrior' +require_relative '../lib/ruby_warrior' runner = RubyWarrior::Runner.new(ARGV, STDIN, STDOUT) runner.run From 94d73b55ccbf3d90e57f172974d1f915f8b58436 Mon Sep 17 00:00:00 2001 From: Felipe Vogel Date: Mon, 25 Mar 2024 15:34:34 -0400 Subject: [PATCH 06/32] Use File.exist? instead of File.exists? --- lib/ruby_warrior/player_generator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby_warrior/player_generator.rb b/lib/ruby_warrior/player_generator.rb index f7ddc8ee..18ed16c7 100644 --- a/lib/ruby_warrior/player_generator.rb +++ b/lib/ruby_warrior/player_generator.rb @@ -19,7 +19,7 @@ def previous_level # TODO refactor and test this method def generate if level.number == 1 - FileUtils.mkdir_p(level.player_path) unless File.exists? level.player_path + FileUtils.mkdir_p(level.player_path) unless File.exist? level.player_path FileUtils.cp(templates_path + '/player.rb', level.player_path) end From 5f6499ef3976795b26ce245c82db82b471a4b81b Mon Sep 17 00:00:00 2001 From: Felipe Vogel Date: Mon, 25 Mar 2024 15:35:19 -0400 Subject: [PATCH 07/32] Add rubywarrior directory to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index d8a14f21..66c46dce 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ tmp/**/* +rubywarrior/* *.gem .project .buildpath From a5ae961ae5b4f4771e93ba9eb42dceadd4da6d0c Mon Sep 17 00:00:00 2001 From: Felipe Vogel Date: Mon, 25 Mar 2024 15:37:37 -0400 Subject: [PATCH 08/32] Use kwarg in ERB.new to avoid deprecation warning --- lib/ruby_warrior/player_generator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ruby_warrior/player_generator.rb b/lib/ruby_warrior/player_generator.rb index 18ed16c7..3c482853 100644 --- a/lib/ruby_warrior/player_generator.rb +++ b/lib/ruby_warrior/player_generator.rb @@ -35,7 +35,7 @@ def templates_path private def read_template(path) - ERB.new(File.read(path), nil, '-').result(binding) + ERB.new(File.read(path), trim_mode: '-').result(binding) end end end From 0350172b6e13db94816cde2b3c4efc3767887764 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 11:06:58 -0700 Subject: [PATCH 09/32] Add .tool-versions file and set to Ruby 3.3 --- .tool-versions | 1 + 1 file changed, 1 insertion(+) create mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 00000000..3294aeda --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +ruby 3.3.0 From 323c60119480418980d607534b6eb13b42aa7933 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 11:08:00 -0700 Subject: [PATCH 10/32] Update rubygems source in Gemfile --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index aa453fa1..1300321d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -source :rubygems +source "https://rubygems.org/" group :test do gem 'rake' From 70ca824cf1a981ee18087298327db6941fa995bb Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 11:18:47 -0700 Subject: [PATCH 11/32] Upgrade RSpec to 3.13 --- Gemfile | 2 +- features/step_definitions/common_steps.rb | 4 +- spec/ruby_warrior/abilities/listen_spec.rb | 4 +- spec/ruby_warrior/level_spec.rb | 60 +++++++++++----------- spec/ruby_warrior/ui_spec.rb | 34 ++++++------ 5 files changed, 52 insertions(+), 52 deletions(-) diff --git a/Gemfile b/Gemfile index 1300321d..08874b09 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,7 @@ source "https://rubygems.org/" group :test do gem 'rake' - gem 'rspec', '~> 2.8.0' + gem 'rspec', '~> 3.13.0' gem 'cucumber' gem 'mocha' end diff --git a/features/step_definitions/common_steps.rb b/features/step_definitions/common_steps.rb index 8351b765..14f9de41 100644 --- a/features/step_definitions/common_steps.rb +++ b/features/step_definitions/common_steps.rb @@ -11,9 +11,9 @@ end Then /^I should find file at "([^\"]*)"$/ do |path| - File.exist?(path).should be_true + File.exist?(path).should eq(true) end Then /^I should find no file at "([^\"]*)"$/ do |path| - File.exist?(path).should be_false + File.exist?(path).should eq(false) end diff --git a/spec/ruby_warrior/abilities/listen_spec.rb b/spec/ruby_warrior/abilities/listen_spec.rb index 39ad2283..e9fc1212 100644 --- a/spec/ruby_warrior/abilities/listen_spec.rb +++ b/spec/ruby_warrior/abilities/listen_spec.rb @@ -9,9 +9,9 @@ @floor.add(@warrior, 0, 0) @listen = RubyWarrior::Abilities::Listen.new(@warrior) end - + it "should return an array of spaces which have units on them besides main unit" do @floor.add(RubyWarrior::Units::Base.new, 0, 1) - @listen.perform.should have(1).record + @listen.perform.size.should == 1 end end diff --git a/spec/ruby_warrior/level_spec.rb b/spec/ruby_warrior/level_spec.rb index 36ff6dbb..f057036b 100644 --- a/spec/ruby_warrior/level_spec.rb +++ b/spec/ruby_warrior/level_spec.rb @@ -9,18 +9,18 @@ @level.floor = @floor @level.stubs(:failed?).returns(false) end - + it "should consider passed when warrior is on stairs" do @level.warrior = RubyWarrior::Units::Warrior.new @floor.add(@level.warrior, 0, 0, :north) @floor.place_stairs(0, 0) @level.should be_passed end - + it "should default time bonus to zero" do @level.time_bonus.should be_zero end - + it "should have a grade relative to ace score" do @level.ace_score = 100 @level.grade_for(110).should == "S" @@ -31,42 +31,42 @@ @level.grade_for(69).should == "D" @level.grade_for(59).should == "F" end - + it "should have no grade if there is no ace score" do @level.ace_score.should be_nil @level.grade_for(100).should be_nil end - + it "should load file contents into level" do @level.stubs(:load_path).returns('path/to/level.rb') File.expects(:read).with('path/to/level.rb').returns("description 'foo'") @level.load_level @level.description.should == 'foo' end - + it "should have a player path from profile" do @profile.stubs(:player_path).returns('path/to/player') @level.player_path.should == 'path/to/player' end - + it "should have a load path from profile tower with level number in it" do @profile.stubs(:tower_path).returns('path/to/tower') @level.load_path.should == File.expand_path('towers/tower/level_001.rb') end - + it "should exist if file exists" do @level.stubs(:load_path).returns('/foo/bar') File.expects(:exist?).with('/foo/bar').returns(true) - @level.exists?.should be_true + @level.exists?.should eq(true) end - + it "should load player and player path" do @level.stubs(:player_path).returns('player/path') $:.expects(:<<).with('player/path') @level.expects(:load).with('player.rb') @level.load_player end - + it "should generate player files" do @level.expects(:load_level) generator = stub @@ -74,31 +74,31 @@ RubyWarrior::PlayerGenerator.expects(:new).with(@level).returns(generator) @level.generate_player_files end - + it "should setup warrior with profile abilities" do @profile.abilities = [:foo, :bar] warrior = stub_everything warrior.expects(:add_abilities).with(:foo, :bar) @level.setup_warrior(warrior) end - + it "should setup warrior with profile name" do @profile.warrior_name = "Joe" warrior = stub_everything warrior.expects(:name=).with("Joe") @level.setup_warrior(warrior) end - + describe "playing" do before(:each) do @level.stubs(:load_level) end - + it "should load level once when playing multiple turns" do @level.expects(:load_level) @level.play(2) end - + it "should call prepare_turn and play_turn on each object specified number of times" do object = RubyWarrior::Units::Base.new object.expects(:prepare_turn).times(2) @@ -106,7 +106,7 @@ @floor.add(object, 0, 0, :north) @level.play(2) end - + it "should return immediately when passed" do object = RubyWarrior::Units::Base.new object.expects(:turn).times(0) @@ -114,7 +114,7 @@ @level.stubs(:passed?).returns(true) @level.play(2) end - + it "should yield to block in play method for each turn" do int = 0 @level.play(2) do @@ -122,48 +122,48 @@ end int.should == 2 end - + it "should count down time_bonus once each turn" do @level.time_bonus = 10 @level.play(3) @level.time_bonus.should == 7 end - + it "should count down time_bonus below 0" do @level.time_bonus = 2 @level.play(5) @level.time_bonus.should be_zero end - + it "should have a pretty score calculation" do @level.score_calculation(123, 45).should == "123 + 45 = 168" end - + it "should not have a score calculation when starting score is zero" do @level.score_calculation(0, 45).should == "45" end end - + describe "tallying points" do before(:each) do @warrior = stub(:score => 0, :abilities => {}) @level.stubs(:warrior).returns(@warrior) @level.floor.stubs(:other_units).returns([stub]) end - + it "should add warrior score to profile" do @warrior.stubs(:score).returns(30) @level.tally_points @profile.score.should == 30 end - + it "should add warrior score to profile for epic mode" do @profile.enable_epic_mode @warrior.stubs(:score).returns(30) @level.tally_points @profile.current_epic_score.should == 30 end - + it "should add level grade percent to profile for epic mode" do @level.ace_score = 100 @profile.enable_epic_mode @@ -171,7 +171,7 @@ @level.tally_points @profile.current_epic_grades.should == { 1 => 0.3 } end - + it "should not add level grade if ace score is not set" do @level.ace_score = nil @profile.enable_epic_mode @@ -179,19 +179,19 @@ @level.tally_points @profile.current_epic_grades.should == {} end - + it "should apply warrior abilities to profile" do @warrior.stubs(:abilities).returns({:foo => nil, :bar => nil}) @level.tally_points @profile.abilities.to_set.should == [:foo, :bar].to_set end - + it "should apply time bonus to profile score" do @level.time_bonus = 20 @level.tally_points @profile.score.should == 20 end - + it "should give 20% bonus when no other units left" do @level.floor.stubs(:other_units).returns([]) @warrior.stubs(:score).returns(10) diff --git a/spec/ruby_warrior/ui_spec.rb b/spec/ruby_warrior/ui_spec.rb index 52f8f817..b3b84cfb 100644 --- a/spec/ruby_warrior/ui_spec.rb +++ b/spec/ruby_warrior/ui_spec.rb @@ -9,50 +9,50 @@ @config.out_stream = @out @config.in_stream = @in end - + it "should add puts to out stream" do @ui.puts "hello" @out.string.should == "hello\n" end - + it "should add print to out stream without newline" do @ui.print "hello" @out.string.should == "hello" end - + it "should fetch gets from in stream" do @in.puts "bar" @in.rewind @ui.gets.should == "bar\n" end - + it "should gets should return empty string if no input" do @config.in_stream = nil @ui.gets.should == "" end - + it "should request text input" do @in.puts "bar" @in.rewind @ui.request("foo").should == "bar" @out.string.should == "foo" end - + it "should ask for yes/no and return true when yes" do @ui.expects(:request).with('foo? [yn] ').returns('y') - @ui.ask("foo?").should be_true + @ui.ask("foo?").should eq(true) end - + it "should ask for yes/no and return false when no" do @ui.stubs(:request).returns('n') - @ui.ask("foo?").should be_false + @ui.ask("foo?").should eq(false) end - + it "should ask for yes/no and return false for any input" do @ui.stubs(:request).returns('aklhasdf') - @ui.ask("foo?").should be_false + @ui.ask("foo?").should eq(false) end - + it "should present multiple options and return selected one" do @ui.expects(:request).with(includes('item')).returns('2') @ui.choose('item', [:foo, :bar, :test]).should == :bar @@ -60,31 +60,31 @@ @out.string.should include('[2] bar') @out.string.should include('[3] test') end - + it "choose should accept array as option" do @ui.stubs(:request).returns('3') @ui.choose('item', [:foo, :bar, [:tower, 'easy']]).should == :tower @out.string.should include('[3] easy') end - + it "choose should return option without prompt if only one item" do @ui.expects(:puts).never @ui.expects(:gets).never @ui.stubs(:request).returns('3') @ui.choose('item', [:foo]).should == :foo end - + it "choose should return first value in array of option if only on item" do @ui.choose('item', [[:foo, :bar]]).should == :foo end - + it "should delay after puts when specified" do @config.delay = 1.3 @ui.expects(:puts).with("foo") @ui.expects(:sleep).with(1.3) @ui.puts_with_delay("foo") end - + it "should not delay puts when delay isn't specified" do @ui.expects(:puts).with("foo") @ui.expects(:sleep).never From 1f9034f1f0f5c3895d00ecbcea01ae15de47b404 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 11:22:54 -0700 Subject: [PATCH 12/32] Enable rspec should syntax for now --- spec/spec_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 89c5d9d4..fb834c68 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,4 +7,5 @@ config.before(:each) do RubyWarrior::Config.reset end + config.expect_with(:rspec) { |c| c.syntax = :should } end From 11b50d95bee951b58b6d863bb318fa8357c6384b Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 11:23:59 -0700 Subject: [PATCH 13/32] Fix mocha deprecation warning on hash argument --- spec/ruby_warrior/units/base_spec.rb | 44 ++++++++++++++-------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/spec/ruby_warrior/units/base_spec.rb b/spec/ruby_warrior/units/base_spec.rb index ec5c2c76..4c10d379 100644 --- a/spec/ruby_warrior/units/base_spec.rb +++ b/spec/ruby_warrior/units/base_spec.rb @@ -4,67 +4,67 @@ before(:each) do @unit = RubyWarrior::Units::Base.new end - + it "should have an attack power which defaults to zero" do @unit.attack_power.should be_zero end - + it "should consider itself dead when no position" do @unit.position.should be_nil @unit.should_not be_alive end - + it "should consider itself alive with position" do @unit.position = stub @unit.should be_alive end - + it "should default max health to 10" do @unit.max_health.should be_zero end - + it "should do nothing when earning points" do lambda { @unit.earn_points(10) }.should_not raise_error end - + it "should default health to max health" do @unit.stubs(:max_health).returns(10) @unit.health.should == 10 end - + it "should subtract health when taking damage" do @unit.stubs(:max_health).returns(10) @unit.take_damage(3) @unit.health.should == 7 end - + it "should do nothing when taking damage if health isn't set" do lambda { @unit.take_damage(3) }.should_not raise_error end - + it "should set position to nil when running out of health" do @unit.position = stub @unit.stubs(:max_health).returns(10) @unit.take_damage(10) @unit.position.should be_nil end - + it "should print out line with name when speaking" do RubyWarrior::UI.expects(:puts_with_delay).with("Base foo") @unit.say "foo" end - + it "should return name in to_s" do @unit.name.should == 'Base' @unit.to_s.should == 'Base' end - + it "should prepare turn by calling play_turn with next turn object" do @unit.stubs(:next_turn).returns('next_turn') @unit.expects(:play_turn).with('next_turn') @unit.prepare_turn end - + it "should perform action when calling perform on turn" do @unit.position = stub RubyWarrior::Abilities::Walk.any_instance.expects(:perform).with(:backward) @@ -74,7 +74,7 @@ @unit.prepare_turn @unit.perform_turn end - + it "should not perform action when dead (no position)" do @unit.position = nil RubyWarrior::Abilities::Walk.any_instance.stubs(:perform).raises("action should not be called") @@ -84,28 +84,28 @@ @unit.prepare_turn @unit.perform_turn end - + it "should not raise an exception when calling perform_turn when there's no action" do @unit.prepare_turn lambda { @unit.perform_turn }.should_not raise_error end - + it "should pass abilities to new turn when calling next_turn" do - RubyWarrior::Turn.expects(:new).with(:walk! => nil, :attack! => nil, :feel => nil).returns('turn') + RubyWarrior::Turn.expects(:new).with({:walk! => nil, :attack! => nil, :feel => nil}).returns('turn') @unit.stubs(:abilities).returns(:walk! => nil, :attack! => nil, :feel => nil) @unit.next_turn.should == 'turn' end - + it "should add ability" do RubyWarrior::Abilities::Walk.expects(:new).with(@unit).returns('walk') @unit.add_abilities(:walk!) @unit.abilities.should == { :walk! => 'walk' } end - + it "should appear as question mark on map" do @unit.character.should == "?" end - + it "should be released from bonds when taking damage" do @unit.stubs(:max_health).returns(10) @unit.bind @@ -113,13 +113,13 @@ @unit.take_damage(2) @unit.should_not be_bound end - + it "should be released from bonds when calling release" do @unit.bind @unit.unbind @unit.should_not be_bound end - + it "should not perform action when bound" do @unit.position = stub @unit.bind From 5f17c148e924a6c84c40692c12fdbf67126a30b9 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 11:26:43 -0700 Subject: [PATCH 14/32] Fix warning with rspec raise_error call by specifying error message --- spec/ruby_warrior/turn_spec.rb | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/spec/ruby_warrior/turn_spec.rb b/spec/ruby_warrior/turn_spec.rb index c45f11de..c4d3d0bf 100644 --- a/spec/ruby_warrior/turn_spec.rb +++ b/spec/ruby_warrior/turn_spec.rb @@ -5,27 +5,27 @@ before(:each) do @turn = RubyWarrior::Turn.new({:walk! => nil, :attack! => nil}) end - + it "should have no action performed at first" do @turn.action.should be_nil end - + it "should be able to perform action and recall it" do @turn.walk! @turn.action.should == [:walk!] end - + it "should include arguments passed to action" do @turn.walk! :forward @turn.action.should == [:walk!, :forward] end - + it "should not be able to call multiple actions per turn" do @turn.walk! :forward - lambda { @turn.attack! }.should raise_error + lambda { @turn.attack! }.should raise_error("Only one action can be performed per turn.") end end - + describe "with senses" do before(:each) do @feel = RubyWarrior::Abilities::Feel.new(Object.new) @@ -33,7 +33,7 @@ @feel.stubs(:space).with(:backward).returns(Object.new) @turn = RubyWarrior::Turn.new({:feel => @feel}) end - + it "should be able to call sense with any argument and return expected results" do @turn.feel.should == @feel.perform @turn.feel(:backward).should == @feel.perform(:backward) From 53e25dde60730fe14f6211e2d23b3b8eee75b94b Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 11:27:40 -0700 Subject: [PATCH 15/32] Add base64 to Gemfile to fix deprecation warning --- Gemfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Gemfile b/Gemfile index 08874b09..f13e686d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,5 +1,7 @@ source "https://rubygems.org/" +gem 'base64' + group :test do gem 'rake' gem 'rspec', '~> 3.13.0' From fbdc3b67504378068747574a96d98eefdbf8d7d3 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 11:45:08 -0700 Subject: [PATCH 16/32] Replace should with expect in specs --- features/step_definitions/common_steps.rb | 4 +- .../step_definitions/interaction_steps.rb | 4 +- spec/ruby_warrior/abilities/attack_spec.rb | 18 +-- spec/ruby_warrior/abilities/base_spec.rb | 36 ++--- spec/ruby_warrior/abilities/bind_spec.rb | 8 +- .../abilities/direction_of_spec.rb | 4 +- .../abilities/direction_of_stairs_spec.rb | 4 +- .../abilities/distance_of_spec.rb | 4 +- spec/ruby_warrior/abilities/explode_spec.rb | 12 +- spec/ruby_warrior/abilities/form_spec.rb | 26 ++-- spec/ruby_warrior/abilities/health_spec.rb | 4 +- spec/ruby_warrior/abilities/listen_spec.rb | 2 +- spec/ruby_warrior/abilities/look_spec.rb | 4 +- spec/ruby_warrior/abilities/rescue_spec.rb | 14 +- spec/ruby_warrior/abilities/rest_spec.rb | 12 +- spec/ruby_warrior/abilities/shoot_spec.rb | 6 +- spec/ruby_warrior/abilities/throw_spec.rb | 18 +-- spec/ruby_warrior/abilities/walk_spec.rb | 8 +- spec/ruby_warrior/core_additions_spec.rb | 2 +- spec/ruby_warrior/floor_spec.rb | 52 +++---- spec/ruby_warrior/game_spec.rb | 68 ++++----- spec/ruby_warrior/level_loader_spec.rb | 36 ++--- spec/ruby_warrior/level_spec.rb | 54 +++---- spec/ruby_warrior/player_generator_spec.rb | 4 +- spec/ruby_warrior/position_spec.rb | 84 +++++------ spec/ruby_warrior/profile_spec.rb | 114 +++++++------- spec/ruby_warrior/space_spec.rb | 140 +++++++++--------- spec/ruby_warrior/tower_spec.rb | 8 +- spec/ruby_warrior/turn_spec.rb | 12 +- spec/ruby_warrior/ui_spec.rb | 34 ++--- spec/ruby_warrior/units/archer_spec.rb | 16 +- spec/ruby_warrior/units/base_spec.rb | 38 ++--- spec/ruby_warrior/units/captive_spec.rb | 16 +- spec/ruby_warrior/units/golem_spec.rb | 16 +- spec/ruby_warrior/units/sludge_spec.rb | 20 +-- spec/ruby_warrior/units/thick_sludge_spec.rb | 12 +- spec/ruby_warrior/units/warrior_spec.rb | 44 +++--- spec/ruby_warrior/units/wizard_spec.rb | 16 +- spec/spec_helper.rb | 2 +- 39 files changed, 488 insertions(+), 488 deletions(-) diff --git a/features/step_definitions/common_steps.rb b/features/step_definitions/common_steps.rb index 14f9de41..a624cfad 100644 --- a/features/step_definitions/common_steps.rb +++ b/features/step_definitions/common_steps.rb @@ -11,9 +11,9 @@ end Then /^I should find file at "([^\"]*)"$/ do |path| - File.exist?(path).should eq(true) + expect(File.exist?(path)).to eq(true) end Then /^I should find no file at "([^\"]*)"$/ do |path| - File.exist?(path).should eq(false) + expect(File.exist?(path)).to eq(false) end diff --git a/features/step_definitions/interaction_steps.rb b/features/step_definitions/interaction_steps.rb index be2b1077..784c0831 100644 --- a/features/step_definitions/interaction_steps.rb +++ b/features/step_definitions/interaction_steps.rb @@ -57,9 +57,9 @@ end Then /^I should see "([^\"]*)"$/ do |phrase| - @io.gets_until_include(phrase).should include(phrase) + expect(@io.gets_until_include(phrase)).to include(phrase) end Then /^I should not see "([^\"]*)" before "([^\"]*)"$/ do |first_phrase, second_phrase| - @io.gets_until_include(second_phrase).should_not include(first_phrase) + expect(@io.gets_until_include(second_phrase)).to_not include(first_phrase) end diff --git a/spec/ruby_warrior/abilities/attack_spec.rb b/spec/ruby_warrior/abilities/attack_spec.rb index d684253d..3aa14481 100644 --- a/spec/ruby_warrior/abilities/attack_spec.rb +++ b/spec/ruby_warrior/abilities/attack_spec.rb @@ -5,33 +5,33 @@ @attacker = stub(:position => stub, :attack_power => 3, :say => nil) @attack = RubyWarrior::Abilities::Attack.new(@attacker) end - + it "should subtract attack power amount from health" do receiver = RubyWarrior::Units::Base.new receiver.stubs(:alive?).returns(true) receiver.health = 5 @attack.stubs(:unit).returns(receiver) @attack.perform - receiver.health.should == 2 + expect(receiver.health).to eq(2) end - + it "should do nothing if recipient is nil" do @attack.stubs(:unit).returns(nil) - lambda { @attack.perform }.should_not raise_error + expect { @attack.perform }.to_not raise_error end - + it "should get object at position from offset" do @attacker.position.expects(:relative_space).with(1, 0) @attack.space(:forward) end - + it "should award points when killing unit" do receiver = stub(:take_damage => nil, :max_health => 8, :alive? => false) @attack.stubs(:unit).returns(receiver) @attacker.expects(:earn_points).with(8) @attack.perform end - + it "should not award points when not killing unit" do receiver = stub(:max_health => 8, :alive? => true) receiver.expects(:take_damage) @@ -39,13 +39,13 @@ @attacker.expects(:earn_points).never @attack.perform end - + it "should reduce attack power when attacking backward" do receiver = RubyWarrior::Units::Base.new receiver.stubs(:alive?).returns(true) receiver.health = 5 @attack.stubs(:unit).returns(receiver) @attack.perform(:backward) - receiver.health.should == 3 + expect(receiver.health).to eq(3) end end diff --git a/spec/ruby_warrior/abilities/base_spec.rb b/spec/ruby_warrior/abilities/base_spec.rb index d833c22f..0cb9f895 100644 --- a/spec/ruby_warrior/abilities/base_spec.rb +++ b/spec/ruby_warrior/abilities/base_spec.rb @@ -5,34 +5,34 @@ @unit = stub @ability = RubyWarrior::Abilities::Base.new(@unit) end - + it "should have offset for directions" do - @ability.offset(:forward).should == [1, 0] - @ability.offset(:right).should == [0, 1] - @ability.offset(:backward).should == [-1, 0] - @ability.offset(:left).should == [0, -1] + expect(@ability.offset(:forward)).to eq([1, 0]) + expect(@ability.offset(:right)).to eq([0, 1]) + expect(@ability.offset(:backward)).to eq([-1, 0]) + expect(@ability.offset(:left)).to eq([0, -1]) end - + it "should have offset for relative forward/right amounts" do - @ability.offset(:forward, 2).should == [2, 0] - @ability.offset(:forward, 2, 1).should == [2, -1] - @ability.offset(:right, 2, 1).should == [1, 2] - @ability.offset(:backward, 2, 1).should == [-2, 1] - @ability.offset(:left, 2, 1).should == [-1, -2] + expect(@ability.offset(:forward, 2)).to eq([2, 0]) + expect(@ability.offset(:forward, 2, 1)).to eq([2, -1]) + expect(@ability.offset(:right, 2, 1)).to eq([1, 2]) + expect(@ability.offset(:backward, 2, 1)).to eq([-2, 1]) + expect(@ability.offset(:left, 2, 1)).to eq([-1, -2]) end - + it "should fetch unit at given direction with distance" do @ability.expects(:space).with(:right, 3, 1).returns(stub(:unit => 'unit')) - @ability.unit(:right, 3, 1).should == 'unit' + expect(@ability.unit(:right, 3, 1)).to eq('unit') end - + it "should have no description" do - @ability.description.should be_nil + expect(@ability.description).to be_nil end - + it "should raise an exception if direction isn't recognized" do - lambda { + expect { @ability.verify_direction(:foo) - }.should raise_error("Unknown direction :foo. Should be :forward, :backward, :left or :right.") + }.to raise_error("Unknown direction :foo. Should be :forward, :backward, :left or :right.") end end diff --git a/spec/ruby_warrior/abilities/bind_spec.rb b/spec/ruby_warrior/abilities/bind_spec.rb index f30a23d6..164af8cd 100644 --- a/spec/ruby_warrior/abilities/bind_spec.rb +++ b/spec/ruby_warrior/abilities/bind_spec.rb @@ -4,16 +4,16 @@ before(:each) do @bind = RubyWarrior::Abilities::Bind.new(stub(:say => nil)) end - + it "should bind recipient" do receiver = RubyWarrior::Units::Base.new @bind.stubs(:unit).returns(receiver) @bind.perform - receiver.should be_bound + expect(receiver).to be_bound end - + it "should do nothing if no recipient" do @bind.stubs(:unit).returns(nil) - lambda { @bind.perform }.should_not raise_error + expect { @bind.perform }.to_not raise_error end end diff --git a/spec/ruby_warrior/abilities/direction_of_spec.rb b/spec/ruby_warrior/abilities/direction_of_spec.rb index 8a6d9d4e..843d34c1 100644 --- a/spec/ruby_warrior/abilities/direction_of_spec.rb +++ b/spec/ruby_warrior/abilities/direction_of_spec.rb @@ -5,9 +5,9 @@ @position = stub @distance = RubyWarrior::Abilities::DirectionOf.new(stub(:position => @position, :say => nil)) end - + it "should return relative direction of given space" do @position.stubs(:relative_direction_of).with(:space).returns(:left) - @distance.perform(:space).should == :left + expect(@distance.perform(:space)).to eq(:left) end end diff --git a/spec/ruby_warrior/abilities/direction_of_stairs_spec.rb b/spec/ruby_warrior/abilities/direction_of_stairs_spec.rb index 76b2b6c1..0cff28a3 100644 --- a/spec/ruby_warrior/abilities/direction_of_stairs_spec.rb +++ b/spec/ruby_warrior/abilities/direction_of_stairs_spec.rb @@ -5,9 +5,9 @@ @position = stub @distance = RubyWarrior::Abilities::DirectionOfStairs.new(stub(:position => @position, :say => nil)) end - + it "should return relative direction of stairs" do @position.stubs(:relative_direction_of_stairs).returns(:left) - @distance.perform.should == :left + expect(@distance.perform).to eq(:left) end end diff --git a/spec/ruby_warrior/abilities/distance_of_spec.rb b/spec/ruby_warrior/abilities/distance_of_spec.rb index 0faddf97..39fc138c 100644 --- a/spec/ruby_warrior/abilities/distance_of_spec.rb +++ b/spec/ruby_warrior/abilities/distance_of_spec.rb @@ -5,9 +5,9 @@ @position = stub @distance = RubyWarrior::Abilities::DistanceOf.new(stub(:position => @position, :say => nil)) end - + it "should return distance from stairs" do @position.stubs(:distance_of).with(:space).returns(5) - @distance.perform(:space).should == 5 + expect(@distance.perform(:space)).to eq(5) end end diff --git a/spec/ruby_warrior/abilities/explode_spec.rb b/spec/ruby_warrior/abilities/explode_spec.rb index bf4d7e6c..e066635f 100644 --- a/spec/ruby_warrior/abilities/explode_spec.rb +++ b/spec/ruby_warrior/abilities/explode_spec.rb @@ -9,24 +9,24 @@ @floor.add(@captive, 0, 0) @explode = RubyWarrior::Abilities::Explode.new(@captive) end - + it "should subtract 100 health from each unit on the floor" do unit = RubyWarrior::Units::Base.new unit.health = 20 @floor.add(unit, 0, 1) @captive.health = 10 @explode.perform - @captive.health.should == -90 - unit.health.should == -80 + expect(@captive.health).to eq(-90) + expect(unit.health).to eq(-80) end - + it "should explode when bomb time reaches zero" do @captive.health = 10 @explode.time = 3 @explode.pass_turn @explode.pass_turn - @captive.health.should == 10 + expect(@captive.health).to eq(10) @explode.pass_turn - @captive.health.should == -90 + expect(@captive.health).to eq(-90) end end diff --git a/spec/ruby_warrior/abilities/form_spec.rb b/spec/ruby_warrior/abilities/form_spec.rb index 9407dca3..e62cfc59 100644 --- a/spec/ruby_warrior/abilities/form_spec.rb +++ b/spec/ruby_warrior/abilities/form_spec.rb @@ -9,40 +9,40 @@ @floor.add(@warrior, 0, 0, :east) @form = RubyWarrior::Abilities::Form.new(@warrior) end - + it "should form a golem in front of warrior" do @form.perform - @floor.get(1, 0).should be_kind_of(RubyWarrior::Units::Golem) + expect(@floor.get(1, 0)).to be_kind_of(RubyWarrior::Units::Golem) end - + it "should form a golem in given direction" do @form.perform(:right) - @floor.get(0, 1).should be_kind_of(RubyWarrior::Units::Golem) + expect(@floor.get(0, 1)).to be_kind_of(RubyWarrior::Units::Golem) end - + it "should not form golem if space isn't empty" do @floor.add(RubyWarrior::Units::Base.new, 1, 0) @form.perform(:forward) @form.perform(:left) - @floor.units.size.should == 2 + expect(@floor.units.size).to eq(2) end - + it "should reduce warrior's health by half (rounding down) and give it to the golem" do @warrior.health = 19 @form.perform - @warrior.health.should == 10 - @floor.get(1, 0).max_health.should == 9 + expect(@warrior.health).to eq(10) + expect(@floor.get(1, 0).max_health).to eq(9) end - + it "should add passed block as golem's turn block" do @passed = nil @form.perform(:forward) { |turn| @passed = turn } @floor.get(1, 0).play_turn(:turn) - @passed.should == :turn + expect(@passed).to eq(:turn) end - + it "should start in same direction as warrior" do @form.perform(:right) - @floor.get(0, 1).position.direction.should == :east + expect(@floor.get(0, 1).position.direction).to eq(:east) end end diff --git a/spec/ruby_warrior/abilities/health_spec.rb b/spec/ruby_warrior/abilities/health_spec.rb index 95a8f7b5..6091992f 100644 --- a/spec/ruby_warrior/abilities/health_spec.rb +++ b/spec/ruby_warrior/abilities/health_spec.rb @@ -5,9 +5,9 @@ @warrior = RubyWarrior::Units::Warrior.new @health = RubyWarrior::Abilities::Health.new(@warrior) end - + it "should return the amount of health" do @warrior.health = 10 - @health.perform.should == 10 + expect(@health.perform).to eq(10) end end diff --git a/spec/ruby_warrior/abilities/listen_spec.rb b/spec/ruby_warrior/abilities/listen_spec.rb index e9fc1212..c771e0ec 100644 --- a/spec/ruby_warrior/abilities/listen_spec.rb +++ b/spec/ruby_warrior/abilities/listen_spec.rb @@ -12,6 +12,6 @@ it "should return an array of spaces which have units on them besides main unit" do @floor.add(RubyWarrior::Units::Base.new, 0, 1) - @listen.perform.size.should == 1 + expect(@listen.perform.size).to eq(1) end end diff --git a/spec/ruby_warrior/abilities/look_spec.rb b/spec/ruby_warrior/abilities/look_spec.rb index e94ba387..acfb6926 100644 --- a/spec/ruby_warrior/abilities/look_spec.rb +++ b/spec/ruby_warrior/abilities/look_spec.rb @@ -5,11 +5,11 @@ @unit = stub(:position => stub, :say => nil) @feel = RubyWarrior::Abilities::Look.new(@unit) end - + it "should get 3 objects at position from offset" do @unit.position.expects(:relative_space).with(1, 0).returns(1) @unit.position.expects(:relative_space).with(2, 0).returns(2) @unit.position.expects(:relative_space).with(3, 0).returns(3) - @feel.perform(:forward).should == [1, 2, 3] + expect(@feel.perform(:forward)).to eq([1, 2, 3]) end end diff --git a/spec/ruby_warrior/abilities/rescue_spec.rb b/spec/ruby_warrior/abilities/rescue_spec.rb index fe7f032b..0e57bdc1 100644 --- a/spec/ruby_warrior/abilities/rescue_spec.rb +++ b/spec/ruby_warrior/abilities/rescue_spec.rb @@ -5,7 +5,7 @@ @warrior = RubyWarrior::Units::Warrior.new @rescue = RubyWarrior::Abilities::Rescue.new(@warrior) end - + it "should rescue captive" do captive = RubyWarrior::Units::Captive.new captive.position = stub @@ -13,9 +13,9 @@ @rescue.expects(:unit).with(:forward).returns(captive) @warrior.expects(:earn_points).with(20) @rescue.perform - captive.position.should be_nil + expect(captive.position).to be_nil end - + it "should do nothing to other unit if not bound" do unit = RubyWarrior::Units::Base.new unit.position = stub @@ -23,9 +23,9 @@ @rescue.expects(:unit).with(:forward).never @warrior.expects(:earn_points).never @rescue.perform - unit.position.should_not be_nil + expect(unit.position).to_not be_nil end - + it "should release other unit when bound" do unit = RubyWarrior::Units::Base.new unit.bind @@ -34,7 +34,7 @@ @rescue.expects(:unit).with(:forward).returns(unit) @warrior.expects(:earn_points).never @rescue.perform - unit.should_not be_bound - unit.position.should_not be_nil + expect(unit).to_not be_bound + expect(unit.position).to_not be_nil end end diff --git a/spec/ruby_warrior/abilities/rest_spec.rb b/spec/ruby_warrior/abilities/rest_spec.rb index 33f64ac1..88a341ae 100644 --- a/spec/ruby_warrior/abilities/rest_spec.rb +++ b/spec/ruby_warrior/abilities/rest_spec.rb @@ -5,25 +5,25 @@ @warrior = RubyWarrior::Units::Warrior.new @rest = RubyWarrior::Abilities::Rest.new(@warrior) end - + it "should give 10% health back" do @warrior.stubs(:max_health).returns(20) @warrior.health = 10 @rest.perform - @warrior.health.should == 12 + expect(@warrior.health).to eq(12) end - + it "should add health when at max" do @warrior.stubs(:max_health).returns(20) @warrior.health = 20 @rest.perform - @warrior.health.should == 20 + expect(@warrior.health).to eq(20) end - + it "should not go over max health" do @warrior.stubs(:max_health).returns(20) @warrior.health = 19 @rest.perform - @warrior.health.should == 20 + expect(@warrior.health).to eq(20) end end diff --git a/spec/ruby_warrior/abilities/shoot_spec.rb b/spec/ruby_warrior/abilities/shoot_spec.rb index b5bb8c2e..2674f5f0 100644 --- a/spec/ruby_warrior/abilities/shoot_spec.rb +++ b/spec/ruby_warrior/abilities/shoot_spec.rb @@ -5,7 +5,7 @@ @shooter = stub(:position => stub, :shoot_power => 2, :say => nil) @shoot = RubyWarrior::Abilities::Shoot.new(@shooter) end - + it "should shoot only first unit" do receiver = stub(:alive? => true) receiver.expects(:take_damage).with(2) @@ -14,9 +14,9 @@ @shoot.expects(:multi_unit).with(:forward, anything).returns([nil, receiver, other, nil]) @shoot.perform end - + it "should shoot and do nothing if no units in the way" do @shoot.expects(:multi_unit).with(:forward, anything).returns([nil, nil]) - lambda { @shoot.perform }.should_not raise_error + expect { @shoot.perform }.to_not raise_error end end diff --git a/spec/ruby_warrior/abilities/throw_spec.rb b/spec/ruby_warrior/abilities/throw_spec.rb index 3dffdd35..e928dea2 100644 --- a/spec/ruby_warrior/abilities/throw_spec.rb +++ b/spec/ruby_warrior/abilities/throw_spec.rb @@ -9,7 +9,7 @@ @floor.add(@warrior, 0, 0, :south) @detonate = RubyWarrior::Abilities::Detonate.new(@warrior) end - + it "should subtract 8 from forward unit and 4 from surrounding units" do target_unit = RubyWarrior::Units::Base.new target_unit.health = 15 @@ -18,10 +18,10 @@ @floor.add(target_unit, 0, 1) @floor.add(second_unit, 1, 1) @detonate.perform - target_unit.health.should == 7 - second_unit.health.should == 11 + expect(target_unit.health).to eq(7) + expect(second_unit.health).to eq(11) end - + it "should subtract 8 from left unit and 4 from surrounding units" do target_unit = RubyWarrior::Units::Base.new target_unit.health = 15 @@ -30,17 +30,17 @@ @floor.add(target_unit, 1, 0) @floor.add(second_unit, 1, 1) @detonate.perform(:left) - target_unit.health.should == 7 - second_unit.health.should == 11 + expect(target_unit.health).to eq(7) + expect(second_unit.health).to eq(11) end - + it "should detonate an explosive if any unit has one" do captive = RubyWarrior::Units::Captive.new captive.health = 1 captive.add_abilities :explode! @floor.add(captive, 1, 1) @detonate.perform - captive.health.should == -99 - @warrior.health.should == -80 + expect(captive.health).to eq(-99) + expect(@warrior.health).to eq(-80) end end diff --git a/spec/ruby_warrior/abilities/walk_spec.rb b/spec/ruby_warrior/abilities/walk_spec.rb index 921d64de..fb6892cc 100644 --- a/spec/ruby_warrior/abilities/walk_spec.rb +++ b/spec/ruby_warrior/abilities/walk_spec.rb @@ -6,20 +6,20 @@ @position = stub(:relative_space => @space, :move => nil) @walk = RubyWarrior::Abilities::Walk.new(stub(:position => @position, :say => nil)) end - + it "should move position forward when calling perform" do @position.expects(:move).with(1, 0) @walk.perform end - + it "should move position right if that is direction" do @position.expects(:move).with(0, 1) @walk.perform(:right) end - + it "should keep position if something is in the way" do @position.stubs(:move).raises("shouldn't be called") @space.stubs(:empty?).returns(false) - lambda { @walk.perform(:right) }.should_not raise_error + expect { @walk.perform(:right) }.to_not raise_error end end diff --git a/spec/ruby_warrior/core_additions_spec.rb b/spec/ruby_warrior/core_additions_spec.rb index 21bc697a..6a368d17 100644 --- a/spec/ruby_warrior/core_additions_spec.rb +++ b/spec/ruby_warrior/core_additions_spec.rb @@ -2,6 +2,6 @@ describe String do it "should wrap text at white space when over a specific character length" do - "foo bar blah".hard_wrap(10).should == "foo bar\nblah" + expect("foo bar blah".hard_wrap(10)).to eq("foo bar\nblah") end end diff --git a/spec/ruby_warrior/floor_spec.rb b/spec/ruby_warrior/floor_spec.rb index ad0435df..77b60f42 100644 --- a/spec/ruby_warrior/floor_spec.rb +++ b/spec/ruby_warrior/floor_spec.rb @@ -7,75 +7,75 @@ @floor.width = 2 @floor.height = 3 end - + it "should be able to add a unit and fetch it at that position" do unit = RubyWarrior::Units::Base.new @floor.add(unit, 0, 1, :north) - @floor.get(0, 1).should == unit + expect(@floor.get(0, 1)).to eq(unit) end - + it "should not consider unit on floor if no position" do unit = RubyWarrior::Units::Base.new @floor.add(unit, 0, 1, :north) unit.position = nil - @floor.units.should_not include(unit) + expect(@floor.units).to_not include(unit) end - + it "should fetch other units not warrior" do unit = RubyWarrior::Units::Base.new warrior = RubyWarrior::Units::Warrior.new @floor.add(unit, 0, 0, :north) @floor.add(warrior, 1, 0, :north) - @floor.other_units.should_not include(warrior) - @floor.other_units.should include(unit) + expect(@floor.other_units).to_not include(warrior) + expect(@floor.other_units).to include(unit) end - + it "should not consider corners out of bounds" do - @floor.should_not be_out_of_bounds(0, 0) - @floor.should_not be_out_of_bounds(1, 0) - @floor.should_not be_out_of_bounds(1, 2) - @floor.should_not be_out_of_bounds(0, 2) + expect(@floor).to_not be_out_of_bounds(0, 0) + expect(@floor).to_not be_out_of_bounds(1, 0) + expect(@floor).to_not be_out_of_bounds(1, 2) + expect(@floor).to_not be_out_of_bounds(0, 2) end - + it "should consider out of bounds when going beyond sides" do - @floor.should be_out_of_bounds(-1, 0) - @floor.should be_out_of_bounds(0, -1) - @floor.should be_out_of_bounds(0, 3) - @floor.should be_out_of_bounds(2, 0) + expect(@floor).to be_out_of_bounds(-1, 0) + expect(@floor).to be_out_of_bounds(0, -1) + expect(@floor).to be_out_of_bounds(0, 3) + expect(@floor).to be_out_of_bounds(2, 0) end - + it "should return space at the specified location" do - @floor.space(0, 0).should be_kind_of(RubyWarrior::Space) + expect(@floor.space(0, 0)).to be_kind_of(RubyWarrior::Space) end - + it "should place stairs and be able to fetch the location" do @floor.place_stairs(1, 2) - @floor.stairs_location.should == [1, 2] + expect(@floor.stairs_location).to eq([1, 2]) end end - + describe "3x1" do before(:each) do @floor = RubyWarrior::Floor.new @floor.width = 3 @floor.height = 1 end - + it "should print map with stairs and unit" do @floor.add(RubyWarrior::Units::Warrior.new, 0, 0) @floor.place_stairs(2, 0) - @floor.character.should == <<-MAP + expect(@floor.character).to eq(<<-MAP) --- |@ >| --- MAP end - + it "should return unique units" do u1 = RubyWarrior::Units::Base.new @floor.add(u1, 0, 0) @floor.add(RubyWarrior::Units::Base.new, 1, 0) - @floor.unique_units.should == [u1] + expect(@floor.unique_units).to eq([u1]) end end end diff --git a/spec/ruby_warrior/game_spec.rb b/spec/ruby_warrior/game_spec.rb index a5f40853..9cef7587 100644 --- a/spec/ruby_warrior/game_spec.rb +++ b/spec/ruby_warrior/game_spec.rb @@ -4,55 +4,55 @@ before(:each) do @game = RubyWarrior::Game.new end - + # GAME DIR - + it "should make game directory if player says so" do RubyWarrior::UI.stubs(:ask).returns(true) Dir.expects(:mkdir).with('./rubywarrior') @game.make_game_directory end - + it "should not make game and exit if player says no" do RubyWarrior::UI.stubs(:ask).returns(false) Dir.stubs(:mkdir).raises('should not be called') - lambda { @game.make_game_directory }.should raise_error(SystemExit) + expect { @game.make_game_directory }.to raise_error(SystemExit) end - - + + # PROFILES - + it "should load profiles for each profile path" do RubyWarrior::Profile.expects(:load).with('foo/.profile').returns(1) RubyWarrior::Profile.expects(:load).with('bar/.profile').returns(2) @game.stubs(:profile_paths).returns(['foo/.profile', 'bar/.profile']) - @game.profiles.should == [1, 2] + expect(@game.profiles).to eq([1, 2]) end - + it "should find profile paths using Dir[] search" do Dir.expects(:[]).with("./rubywarrior/**/.profile") @game.profile_paths end - + it "should try to create profile when no profile paths are specified" do @game.stubs(:profiles).returns([]) @game.expects(:new_profile).returns('profile') - @game.profile.should == 'profile' + expect(@game.profile).to eq('profile') end - + it "should ask a player to choose a profile if multiple profiles are available, but only once" do RubyWarrior::UI.expects(:choose).with('profile', [:profile1, [:new, 'New Profile']]).returns(:profile1) @game.stubs(:profiles).returns([:profile1]) - 2.times { @game.profile.should == :profile1 } + 2.times { expect(@game.profile).to eq(:profile1) } end - + it "should ask user to choose a tower when creating a new profile" do RubyWarrior::UI.stubs(:gets).returns('') @game.stubs(:towers).returns([:tower1, :tower2]) RubyWarrior::UI.expects(:choose).with('tower', [:tower1, :tower2]).returns(stub(:path => '/foo/bar')) @game.new_profile end - + it "should pass name and selected tower to profile" do profile = stub RubyWarrior::UI.stubs(:choose).returns(stub(:path => 'tower_path')) @@ -60,60 +60,60 @@ RubyWarrior::Profile.expects(:new).returns(profile) profile.expects(:tower_path=).with('tower_path') profile.expects(:warrior_name=).with('name') - @game.new_profile.should == profile + expect(@game.new_profile).to eq(profile) end - - + + # TOWERS - + it "load towers for each tower path" do RubyWarrior::Tower.expects(:new).with('towers/foo').returns(1) RubyWarrior::Tower.expects(:new).with('towers/bar').returns(2) @game.stubs(:tower_paths).returns(['towers/foo', 'towers/bar']) - @game.towers.should == [1, 2] + expect(@game.towers).to eq([1, 2]) end - + it "should find tower paths using Dir[] search" do Dir.expects(:[]).with(File.expand_path('../../../towers/*', __FILE__)) @game.tower_paths end - - + + # LEVEL - + it "should fetch current level from profile and cache it" do @game.stubs(:profile).returns(stub) @game.profile.expects(:current_level).returns('foo') - 2.times { @game.current_level.should == 'foo' } + 2.times { expect(@game.current_level).to eq('foo') } end - + it "should fetch next level from profile and cache it" do @game.stubs(:profile).returns(stub) @game.profile.expects(:next_level).returns('bar') - 2.times { @game.next_level.should == 'bar' } + 2.times { expect(@game.next_level).to eq('bar') } end - + it "should report final grade" do profile = RubyWarrior::Profile.new profile.current_epic_grades = { 1 => 0.7, 2 => 0.9 } @game.stubs(:profile).returns(profile) report = @game.final_report - report.should include("Your average grade for this tower is: B") - report.should include("Level 1: C\n Level 2: A") + expect(report).to include("Your average grade for this tower is: B") + expect(report).to include("Level 1: C\n Level 2: A") end - + it "should have an empty final report if no epic grades are available" do profile = RubyWarrior::Profile.new profile.current_epic_grades = {} @game.stubs(:profile).returns(profile) - @game.final_report.should be_nil + expect(@game.final_report).to be_nil end - + it "should have an empty final report if practice level" do RubyWarrior::Config.practice_level = 2 profile = RubyWarrior::Profile.new profile.current_epic_grades = { 1 => 0.7, 2 => 0.9 } @game.stubs(:profile).returns(profile) - @game.final_report.should be_nil + expect(@game.final_report).to be_nil end end diff --git a/spec/ruby_warrior/level_loader_spec.rb b/spec/ruby_warrior/level_loader_spec.rb index f02b3c96..bcc6449e 100644 --- a/spec/ruby_warrior/level_loader_spec.rb +++ b/spec/ruby_warrior/level_loader_spec.rb @@ -7,48 +7,48 @@ @level = RubyWarrior::Level.new(@profile, 1) @loader = RubyWarrior::LevelLoader.new(@level) end - + it "should be able to add description, tip and clue" do @loader.description "foo" @loader.tip "bar" @loader.clue "clue" - @level.description.should == "foo" - @level.tip.should == "bar" - @level.clue.should == "clue" + expect(@level.description).to eq("foo") + expect(@level.tip).to eq("bar") + expect(@level.clue).to eq("clue") end - + it "should be able to set size" do @loader.size 5, 3 - @level.floor.width.should == 5 - @level.floor.height.should == 3 + expect(@level.floor.width).to eq(5) + expect(@level.floor.height).to eq(3) end - + it "should be able to add stairs" do @level.floor.expects(:place_stairs).with(1, 2) @loader.stairs 1, 2 end - + it "should yield new unit when building" do @loader.unit :base, 1, 2 do |unit| - unit.should be_kind_of(RubyWarrior::Units::Base) - unit.position.should be_at(1, 2) + expect(unit).to be_kind_of(RubyWarrior::Units::Base) + expect(unit.position).to be_at(1, 2) end end - + it "should be able to add multi-word units" do - lambda { @loader.unit :thick_sludge, 1, 2 }.should_not raise_error + expect { @loader.unit :thick_sludge, 1, 2 }.to_not raise_error end - + it "should build warrior from profile" do @loader.warrior 1, 2 do |unit| - unit.should be_kind_of(RubyWarrior::Units::Warrior) - unit.position.should be_at(1, 2) + expect(unit).to be_kind_of(RubyWarrior::Units::Warrior) + expect(unit.position).to be_at(1, 2) end end - + it "should be able to set time bonus" do @loader.time_bonus 100 - @level.time_bonus.should == 100 + expect(@level.time_bonus).to eq(100) end end end diff --git a/spec/ruby_warrior/level_spec.rb b/spec/ruby_warrior/level_spec.rb index f057036b..00c08750 100644 --- a/spec/ruby_warrior/level_spec.rb +++ b/spec/ruby_warrior/level_spec.rb @@ -14,50 +14,50 @@ @level.warrior = RubyWarrior::Units::Warrior.new @floor.add(@level.warrior, 0, 0, :north) @floor.place_stairs(0, 0) - @level.should be_passed + expect(@level).to be_passed end it "should default time bonus to zero" do - @level.time_bonus.should be_zero + expect(@level.time_bonus).to be_zero end it "should have a grade relative to ace score" do @level.ace_score = 100 - @level.grade_for(110).should == "S" - @level.grade_for(100).should == "S" - @level.grade_for(99).should == "A" - @level.grade_for(89).should == "B" - @level.grade_for(79).should == "C" - @level.grade_for(69).should == "D" - @level.grade_for(59).should == "F" + expect(@level.grade_for(110)).to eq("S") + expect(@level.grade_for(100)).to eq("S") + expect(@level.grade_for(99)).to eq("A") + expect(@level.grade_for(89)).to eq("B") + expect(@level.grade_for(79)).to eq("C") + expect(@level.grade_for(69)).to eq("D") + expect(@level.grade_for(59)).to eq("F") end it "should have no grade if there is no ace score" do - @level.ace_score.should be_nil - @level.grade_for(100).should be_nil + expect(@level.ace_score).to be_nil + expect(@level.grade_for(100)).to be_nil end it "should load file contents into level" do @level.stubs(:load_path).returns('path/to/level.rb') File.expects(:read).with('path/to/level.rb').returns("description 'foo'") @level.load_level - @level.description.should == 'foo' + expect(@level.description).to eq('foo') end it "should have a player path from profile" do @profile.stubs(:player_path).returns('path/to/player') - @level.player_path.should == 'path/to/player' + expect(@level.player_path).to eq('path/to/player') end it "should have a load path from profile tower with level number in it" do @profile.stubs(:tower_path).returns('path/to/tower') - @level.load_path.should == File.expand_path('towers/tower/level_001.rb') + expect(@level.load_path).to eq(File.expand_path('towers/tower/level_001.rb')) end it "should exist if file exists" do @level.stubs(:load_path).returns('/foo/bar') File.expects(:exist?).with('/foo/bar').returns(true) - @level.exists?.should eq(true) + expect(@level.exists?).to eq(true) end it "should load player and player path" do @@ -120,27 +120,27 @@ @level.play(2) do int += 1 end - int.should == 2 + expect(int).to eq(2) end it "should count down time_bonus once each turn" do @level.time_bonus = 10 @level.play(3) - @level.time_bonus.should == 7 + expect(@level.time_bonus).to eq(7) end it "should count down time_bonus below 0" do @level.time_bonus = 2 @level.play(5) - @level.time_bonus.should be_zero + expect(@level.time_bonus).to be_zero end it "should have a pretty score calculation" do - @level.score_calculation(123, 45).should == "123 + 45 = 168" + expect(@level.score_calculation(123, 45)).to eq("123 + 45 = 168") end it "should not have a score calculation when starting score is zero" do - @level.score_calculation(0, 45).should == "45" + expect(@level.score_calculation(0, 45)).to eq("45") end end @@ -154,14 +154,14 @@ it "should add warrior score to profile" do @warrior.stubs(:score).returns(30) @level.tally_points - @profile.score.should == 30 + expect(@profile.score).to eq(30) end it "should add warrior score to profile for epic mode" do @profile.enable_epic_mode @warrior.stubs(:score).returns(30) @level.tally_points - @profile.current_epic_score.should == 30 + expect(@profile.current_epic_score).to eq(30) end it "should add level grade percent to profile for epic mode" do @@ -169,7 +169,7 @@ @profile.enable_epic_mode @warrior.stubs(:score).returns(30) @level.tally_points - @profile.current_epic_grades.should == { 1 => 0.3 } + expect(@profile.current_epic_grades).to eq({ 1 => 0.3 }) end it "should not add level grade if ace score is not set" do @@ -177,19 +177,19 @@ @profile.enable_epic_mode @warrior.stubs(:score).returns(30) @level.tally_points - @profile.current_epic_grades.should == {} + expect(@profile.current_epic_grades).to eq({}) end it "should apply warrior abilities to profile" do @warrior.stubs(:abilities).returns({:foo => nil, :bar => nil}) @level.tally_points - @profile.abilities.to_set.should == [:foo, :bar].to_set + expect(@profile.abilities.to_set).to eq([:foo, :bar].to_set) end it "should apply time bonus to profile score" do @level.time_bonus = 20 @level.tally_points - @profile.score.should == 20 + expect(@profile.score).to eq(20) end it "should give 20% bonus when no other units left" do @@ -197,7 +197,7 @@ @warrior.stubs(:score).returns(10) @level.time_bonus = 10 @level.tally_points - @profile.score.should == 24 + expect(@profile.score).to eq(24) end end end diff --git a/spec/ruby_warrior/player_generator_spec.rb b/spec/ruby_warrior/player_generator_spec.rb index e2431318..99d05ef9 100644 --- a/spec/ruby_warrior/player_generator_spec.rb +++ b/spec/ruby_warrior/player_generator_spec.rb @@ -5,8 +5,8 @@ @level = RubyWarrior::Level.new(RubyWarrior::Profile.new, 15) @generator = RubyWarrior::PlayerGenerator.new(@level) end - + it "should know templates path" do - @generator.templates_path.should == File.expand_path("../../../templates", __FILE__) + expect(@generator.templates_path).to eq(File.expand_path("../../../templates", __FILE__)) end end diff --git a/spec/ruby_warrior/position_spec.rb b/spec/ruby_warrior/position_spec.rb index d155f7c5..16497802 100644 --- a/spec/ruby_warrior/position_spec.rb +++ b/spec/ruby_warrior/position_spec.rb @@ -9,100 +9,100 @@ @floor.add(@unit, 1, 2, :north) @position = @unit.position end - + it "should rotate clockwise" do - @position.direction.should == :north + expect(@position.direction).to eq(:north) [:east, :south, :west, :north, :east].each do |dir| @position.rotate(1) - @position.direction.should == dir + expect(@position.direction).to eq(dir) end end - + it "should rotate counterclockwise" do - @position.direction.should == :north + expect(@position.direction).to eq(:north) [:west, :south, :east, :north, :west].each do |dir| @position.rotate(-1) - @position.direction.should == dir + expect(@position.direction).to eq(dir) end end - + it "should get relative space in front" do unit = RubyWarrior::Units::Base.new @floor.add(unit, 1, 1) - @position.relative_space(1).should_not be_empty + expect(@position.relative_space(1)).to_not be_empty end - + it "should get relative object in front when rotated" do unit = RubyWarrior::Units::Base.new @floor.add(unit, 2, 2) @position.rotate(1) - @position.relative_space(1).should_not be_empty + expect(@position.relative_space(1)).to_not be_empty end - + it "should get relative object diagonally" do unit = RubyWarrior::Units::Base.new @floor.add(unit, 0, 1) - @position.relative_space(1, -1).should_not be_empty + expect(@position.relative_space(1, -1)).to_not be_empty end - + it "should get relative object diagonally when rotating" do unit = RubyWarrior::Units::Base.new @floor.add(unit, 0, 1) @position.rotate(2) - @position.relative_space(-1, 1).should_not be_empty + expect(@position.relative_space(-1, 1)).to_not be_empty end - + it "should move object on floor relatively" do - @floor.get(1, 2).should == @unit + expect(@floor.get(1, 2)).to eq(@unit) @position.move(-1, 2) - @floor.get(1, 2).should be_nil - @floor.get(3, 3).should == @unit + expect(@floor.get(1, 2)).to be_nil + expect(@floor.get(3, 3)).to eq(@unit) @position.rotate(1) @position.move(-1) - @floor.get(3, 3).should be_nil - @floor.get(2, 3).should == @unit + expect(@floor.get(3, 3)).to be_nil + expect(@floor.get(2, 3)).to eq(@unit) end - + it "should return distance from stairs as 0 when on stairs" do @floor.place_stairs(1, 2) - @position.distance_from_stairs.should == 0 + expect(@position.distance_from_stairs).to eq(0) end - + it "should return distance from stairs in both directions" do @floor.place_stairs(0, 3) - @position.distance_from_stairs.should == 2 + expect(@position.distance_from_stairs).to eq(2) end - + it "should return relative direction of stairs" do @floor.place_stairs(0, 0) - @position.relative_direction_of_stairs.should == :forward + expect(@position.relative_direction_of_stairs).to eq(:forward) end - + it "should return relative direction of given space" do - @position.relative_direction_of(@floor.space(5, 3)).should == :right + expect(@position.relative_direction_of(@floor.space(5, 3))).to eq(:right) @position.rotate 1 - @position.relative_direction_of(@floor.space(1, 4)).should == :right + expect(@position.relative_direction_of(@floor.space(1, 4))).to eq(:right) end - + it "should be able to determine relative direction" do - @position.relative_direction(:north).should == :forward - @position.relative_direction(:south).should == :backward - @position.relative_direction(:west).should == :left - @position.relative_direction(:east).should == :right + expect(@position.relative_direction(:north)).to eq(:forward) + expect(@position.relative_direction(:south)).to eq(:backward) + expect(@position.relative_direction(:west)).to eq(:left) + expect(@position.relative_direction(:east)).to eq(:right) @position.rotate 1 - @position.relative_direction(:north).should == :left + expect(@position.relative_direction(:north)).to eq(:left) @position.rotate 1 - @position.relative_direction(:north).should == :backward + expect(@position.relative_direction(:north)).to eq(:backward) @position.rotate 1 - @position.relative_direction(:north).should == :right + expect(@position.relative_direction(:north)).to eq(:right) end - + it "should return a space at the same location as position" do - @position.space.location.should == [1, 2] + expect(@position.space.location).to eq([1, 2]) end - + it "should return distance of given space" do - @position.distance_of(@floor.space(5, 3)).should == 5 - @position.distance_of(@floor.space(4, 2)).should == 3 + expect(@position.distance_of(@floor.space(5, 3))).to eq(5) + expect(@position.distance_of(@floor.space(4, 2))).to eq(3) end end diff --git a/spec/ruby_warrior/profile_spec.rb b/spec/ruby_warrior/profile_spec.rb index c346ccb9..b3ef1e75 100644 --- a/spec/ruby_warrior/profile_spec.rb +++ b/spec/ruby_warrior/profile_spec.rb @@ -4,80 +4,80 @@ before(:each) do @profile = RubyWarrior::Profile.new end - + it "should have warrior name" do @profile.warrior_name = 'Joe' - @profile.warrior_name.should == 'Joe' + expect(@profile.warrior_name).to eq('Joe') end - + it "should start level number at 0" do - @profile.level_number.should be_zero + expect(@profile.level_number).to be_zero end - + it "should start score at 0 and allow it to increment" do - @profile.score.should be_zero + expect(@profile.score).to be_zero @profile.score += 5 - @profile.score.should == 5 + expect(@profile.score).to eq(5) end - + it "should have no abilities and allow adding" do - @profile.abilities.should == [] + expect(@profile.abilities).to eq([]) @profile.abilities += [:foo, :bar] - @profile.abilities.should == [:foo, :bar] + expect(@profile.abilities).to eq([:foo, :bar]) end - + it "should encode with marshal + base64" do - @profile.encode.should == Base64.encode64(Marshal.dump(@profile)) + expect(@profile.encode).to eq(Base64.encode64(Marshal.dump(@profile))) end - + it "should decode with marshal + base64" do - RubyWarrior::Profile.decode(@profile.encode).warrior_name.should == @profile.warrior_name + expect(RubyWarrior::Profile.decode(@profile.encode).warrior_name).to eq(@profile.warrior_name) end - + it "load should read file, decode and set player path" do profile = 'profile' profile.expects(:player_path=).with('path/to') File.expects(:read).with('path/to/.profile').returns('encoded_profile') RubyWarrior::Profile.expects(:decode).with('encoded_profile').returns(profile) - RubyWarrior::Profile.load('path/to/.profile').should == profile + expect(RubyWarrior::Profile.load('path/to/.profile')).to eq(profile) end - + it "should add abilities and remove duplicates" do @profile.add_abilities(:foo, :bar, :blah, :bar) - @profile.abilities.should == [:foo, :bar, :blah] + expect(@profile.abilities).to eq([:foo, :bar, :blah]) end - + it "should fetch new level with current number" do @profile.level_number = 1 - @profile.current_level.number.should == 1 + expect(@profile.current_level.number).to eq(1) end it "should fetch next level" do @profile.level_number = 1 - @profile.next_level.number.should == 2 + expect(@profile.next_level.number).to eq(2) end it "should enable epic mode and reset scores if nil" do @profile.epic_score = nil @profile.current_epic_score = nil @profile.enable_epic_mode - @profile.should be_epic - @profile.epic_score.should be_zero - @profile.current_epic_score.should be_zero + expect(@profile).to be_epic + expect(@profile.epic_score).to be_zero + expect(@profile.current_epic_score).to be_zero end - + it "should override epic score with current one if it is higher" do @profile.enable_epic_mode - @profile.epic_score.should be_zero - @profile.average_grade.should be_nil + expect(@profile.epic_score).to be_zero + expect(@profile.average_grade).to be_nil @profile.current_epic_score = 123 @profile.current_epic_grades = { 1 => 0.7, 2 => 0.9 } @profile.update_epic_score - @profile.epic_score.should == 123 - @profile.average_grade.should == 0.8 + expect(@profile.epic_score).to eq(123) + expect(@profile.average_grade).to eq(0.8) end - + it "should not override epic score with current one if it is lower" do @profile.enable_epic_mode @profile.epic_score = 124 @@ -85,20 +85,20 @@ @profile.current_epic_score = 123 @profile.current_epic_grades = { 1 => 0.7, 2 => 0.9 } @profile.update_epic_score - @profile.epic_score.should == 124 - @profile.average_grade.should == 0.9 + expect(@profile.epic_score).to eq(124) + expect(@profile.average_grade).to eq(0.9) end - + it "should not calculate average grade if no grades are present" do @profile.enable_epic_mode @profile.current_epic_grades = {} - @profile.calculate_average_grade.should be_nil + expect(@profile.calculate_average_grade).to be_nil end it "should remember current level number as last_level_number" do @profile.level_number = 7 @profile.enable_epic_mode - @profile.last_level_number.should == 7 + expect(@profile.last_level_number).to eq(7) end it "should enable normal mode by clearing epic scores and resetting last level number" do @@ -108,26 +108,26 @@ @profile.current_epic_grades = { 1 => 100 } @profile.average_grade = "C" @profile.enable_normal_mode - @profile.should_not be_epic - @profile.epic_score.should be_zero - @profile.current_epic_score.should be_zero - @profile.last_level_number.should be_nil - @profile.average_grade.should be_nil - @profile.current_epic_grades.should == {} - @profile.level_number.should == 7 + expect(@profile).to_not be_epic + expect(@profile.epic_score).to be_zero + expect(@profile.current_epic_score).to be_zero + expect(@profile.last_level_number).to be_nil + expect(@profile.average_grade).to be_nil + expect(@profile.current_epic_grades).to eq({}) + expect(@profile.level_number).to eq(7) end it "should be no level after epic if last level isn't specified" do @profile.last_level_number = nil - @profile.should_not be_level_after_epic + expect(@profile).to_not be_level_after_epic end - + describe "with tower path" do before(:each) do @profile.warrior_name = "John Smith" @profile.tower_path = 'path/to/tower' end - + it "save should write file with encoded profile" do file = stub file.expects(:write).with('encoded_profile') @@ -135,37 +135,37 @@ @profile.expects(:encode).returns('encoded_profile') @profile.save end - + it "should have a nice string representation" do @profile.warrior_name = 'Joe' - @profile.to_s.should == "Joe - tower - level 0 - score 0" + expect(@profile.to_s).to eq("Joe - tower - level 0 - score 0") end - + it "should include epic score in string representation" do @profile.warrior_name = 'Joe' @profile.enable_epic_mode - @profile.to_s.should == "Joe - tower - first score 0 - epic score 0" + expect(@profile.to_s).to eq("Joe - tower - first score 0 - epic score 0") end - + it "should include epic score with grade in string representation" do @profile.warrior_name = 'Joe' @profile.enable_epic_mode @profile.average_grade = 0.7 - @profile.to_s.should == "Joe - tower - first score 0 - epic score 0 (C)" + expect(@profile.to_s).to eq("Joe - tower - first score 0 - epic score 0 (C)") end - + it "should guess at the player path" do - @profile.player_path.should == './rubywarrior/john-smith-tower' + expect(@profile.player_path).to eq('./rubywarrior/john-smith-tower') end - + it "should use specified player path" do @profile.player_path = "path/to/player" - @profile.player_path.should == "path/to/player" + expect(@profile.player_path).to eq("path/to/player") end - + it "should load tower from path" do RubyWarrior::Tower.expects(:new).with('tower').returns('tower') - @profile.tower.should == 'tower' + expect(@profile.tower).to eq('tower') end end end diff --git a/spec/ruby_warrior/space_spec.rb b/spec/ruby_warrior/space_spec.rb index 869a995b..3c92c2bb 100644 --- a/spec/ruby_warrior/space_spec.rb +++ b/spec/ruby_warrior/space_spec.rb @@ -6,185 +6,185 @@ @floor.width = 2 @floor.height = 3 end - + describe "with empty space" do before(:each) do @space = @floor.space(0, 0) end - + it "should not be enemy" do - @space.should_not be_enemy + expect(@space).to_not be_enemy end - + it "should not be warrior" do - @space.should_not be_warrior + expect(@space).to_not be_warrior end - + it "should be empty" do - @space.should be_empty + expect(@space).to be_empty end - + it "should not be wall" do - @space.should_not be_wall + expect(@space).to_not be_wall end - + it "should not be stairs" do - @space.should_not be_stairs + expect(@space).to_not be_stairs end - + it "should not be captive" do - @space.should_not be_captive + expect(@space).to_not be_captive end - + it "should say 'nothing' as name" do - @space.to_s.should == 'nothing' + expect(@space.to_s).to eq('nothing') end - + it "should not be ticking" do - @space.should_not be_ticking + expect(@space).to_not be_ticking end end - + describe "out of bounds" do before(:each) do @space = @floor.space(-1, 1) end - + it "should be wall" do - @space.should be_wall + expect(@space).to be_wall end - + it "should not be empty" do - @space.should_not be_empty + expect(@space).to_not be_empty end - + it "should have name of 'wall'" do - @space.to_s.should == 'wall' + expect(@space.to_s).to eq('wall') end end - + describe "with warrior" do before(:each) do warrior = RubyWarrior::Units::Warrior.new @floor.add(warrior, 0, 0) @space = @floor.space(0, 0) end - + it "should be warrior" do - @space.should be_warrior + expect(@space).to be_warrior end - + it "should be player" do - @space.should be_warrior + expect(@space).to be_warrior end - + it "should not be enemy" do - @space.should_not be_enemy + expect(@space).to_not be_enemy end - + it "should not be empty" do - @space.should_not be_enemy + expect(@space).to_not be_enemy end - + it "should know what unit is on that space" do - @space.unit.should be_kind_of(RubyWarrior::Units::Warrior) + expect(@space.unit).to be_kind_of(RubyWarrior::Units::Warrior) end end - + describe "with enemy" do before(:each) do @floor.add(RubyWarrior::Units::Sludge.new, 0, 0) @space = @floor.space(0, 0) end - + it "should be enemy" do - @space.should be_enemy + expect(@space).to be_enemy end - + it "should not be warrior" do - @space.should_not be_warrior + expect(@space).to_not be_warrior end - + it "should not be empty" do - @space.should_not be_empty + expect(@space).to_not be_empty end - + it "should have name of unit" do - @space.to_s.should == "Sludge" + expect(@space.to_s).to eq("Sludge") end - + describe "bound" do before(:each) do @space.unit.bind end - + it "should be captive" do - @space.should be_captive + expect(@space).to be_captive end - + it "should not look like enemy" do - @space.should_not be_enemy + expect(@space).to_not be_enemy end end end - + describe "with captive" do before(:each) do @captive = RubyWarrior::Units::Captive.new @floor.add(@captive, 0, 0) @space = @floor.space(0, 0) end - + it "should be captive" do - @space.should be_captive + expect(@space).to be_captive end - + it "should not be enemy" do - @space.should_not be_enemy + expect(@space).to_not be_enemy end - + it "should be ticking if captive has time bomb" do @captive.add_abilities :explode! - @space.should be_ticking + expect(@space).to be_ticking end - + it "should not be ticking if captive does not have time bomb" do - @space.should_not be_ticking + expect(@space).to_not be_ticking end end - + describe "with golem" do before(:each) do @golem = RubyWarrior::Units::Golem.new @floor.add(@golem, 0, 0) @space = @floor.space(0, 0) end - + it "should be golem" do - @space.should be_golem + expect(@space).to be_golem end - + it "should not be enemy" do - @space.should_not be_enemy + expect(@space).to_not be_enemy end - + it "should be player" do - @space.should be_player + expect(@space).to be_player end end - + describe "at stairs" do before(:each) do @floor.place_stairs(0, 0) @space = @floor.space(0, 0) end - + it "should be empty" do - @space.should be_empty + expect(@space).to be_empty end - + it "should be stairs" do - @space.should be_stairs + expect(@space).to be_stairs end end end diff --git a/spec/ruby_warrior/tower_spec.rb b/spec/ruby_warrior/tower_spec.rb index f42840c0..d4f1d6bd 100644 --- a/spec/ruby_warrior/tower_spec.rb +++ b/spec/ruby_warrior/tower_spec.rb @@ -4,12 +4,12 @@ before(:each) do @tower = RubyWarrior::Tower.new('path/to/tower') end - + it "should consider last part of path as name" do - @tower.name.should == 'tower' + expect(@tower.name).to eq('tower') end - + it "should use name when converting to string" do - @tower.to_s.should == @tower.name + expect(@tower.to_s).to eq(@tower.name) end end diff --git a/spec/ruby_warrior/turn_spec.rb b/spec/ruby_warrior/turn_spec.rb index c4d3d0bf..3b441a78 100644 --- a/spec/ruby_warrior/turn_spec.rb +++ b/spec/ruby_warrior/turn_spec.rb @@ -7,22 +7,22 @@ end it "should have no action performed at first" do - @turn.action.should be_nil + expect(@turn.action).to be_nil end it "should be able to perform action and recall it" do @turn.walk! - @turn.action.should == [:walk!] + expect(@turn.action).to eq([:walk!]) end it "should include arguments passed to action" do @turn.walk! :forward - @turn.action.should == [:walk!, :forward] + expect(@turn.action).to eq([:walk!, :forward]) end it "should not be able to call multiple actions per turn" do @turn.walk! :forward - lambda { @turn.attack! }.should raise_error("Only one action can be performed per turn.") + expect { @turn.attack! }.to raise_error("Only one action can be performed per turn.") end end @@ -35,8 +35,8 @@ end it "should be able to call sense with any argument and return expected results" do - @turn.feel.should == @feel.perform - @turn.feel(:backward).should == @feel.perform(:backward) + expect(@turn.feel).to eq(@feel.perform) + expect(@turn.feel(:backward)).to eq(@feel.perform(:backward)) end end end diff --git a/spec/ruby_warrior/ui_spec.rb b/spec/ruby_warrior/ui_spec.rb index b3b84cfb..d5379b38 100644 --- a/spec/ruby_warrior/ui_spec.rb +++ b/spec/ruby_warrior/ui_spec.rb @@ -12,70 +12,70 @@ it "should add puts to out stream" do @ui.puts "hello" - @out.string.should == "hello\n" + expect(@out.string).to eq("hello\n") end it "should add print to out stream without newline" do @ui.print "hello" - @out.string.should == "hello" + expect(@out.string).to eq("hello") end it "should fetch gets from in stream" do @in.puts "bar" @in.rewind - @ui.gets.should == "bar\n" + expect(@ui.gets).to eq("bar\n") end it "should gets should return empty string if no input" do @config.in_stream = nil - @ui.gets.should == "" + expect(@ui.gets).to eq("") end it "should request text input" do @in.puts "bar" @in.rewind - @ui.request("foo").should == "bar" - @out.string.should == "foo" + expect(@ui.request("foo")).to eq("bar") + expect(@out.string).to eq("foo") end it "should ask for yes/no and return true when yes" do @ui.expects(:request).with('foo? [yn] ').returns('y') - @ui.ask("foo?").should eq(true) + expect(@ui.ask("foo?")).to eq(true) end it "should ask for yes/no and return false when no" do @ui.stubs(:request).returns('n') - @ui.ask("foo?").should eq(false) + expect(@ui.ask("foo?")).to eq(false) end it "should ask for yes/no and return false for any input" do @ui.stubs(:request).returns('aklhasdf') - @ui.ask("foo?").should eq(false) + expect(@ui.ask("foo?")).to eq(false) end it "should present multiple options and return selected one" do @ui.expects(:request).with(includes('item')).returns('2') - @ui.choose('item', [:foo, :bar, :test]).should == :bar - @out.string.should include('[1] foo') - @out.string.should include('[2] bar') - @out.string.should include('[3] test') + expect(@ui.choose('item', [:foo, :bar, :test])).to eq(:bar) + expect(@out.string).to include('[1] foo') + expect(@out.string).to include('[2] bar') + expect(@out.string).to include('[3] test') end it "choose should accept array as option" do @ui.stubs(:request).returns('3') - @ui.choose('item', [:foo, :bar, [:tower, 'easy']]).should == :tower - @out.string.should include('[3] easy') + expect(@ui.choose('item', [:foo, :bar, [:tower, 'easy']])).to eq(:tower) + expect(@out.string).to include('[3] easy') end it "choose should return option without prompt if only one item" do @ui.expects(:puts).never @ui.expects(:gets).never @ui.stubs(:request).returns('3') - @ui.choose('item', [:foo]).should == :foo + expect(@ui.choose('item', [:foo])).to eq(:foo) end it "choose should return first value in array of option if only on item" do - @ui.choose('item', [[:foo, :bar]]).should == :foo + expect(@ui.choose('item', [[:foo, :bar]])).to eq(:foo) end it "should delay after puts when specified" do diff --git a/spec/ruby_warrior/units/archer_spec.rb b/spec/ruby_warrior/units/archer_spec.rb index 2f5820d7..3202e2a5 100644 --- a/spec/ruby_warrior/units/archer_spec.rb +++ b/spec/ruby_warrior/units/archer_spec.rb @@ -4,20 +4,20 @@ before(:each) do @archer = RubyWarrior::Units::Archer.new end - + it "should have look and shoot abilities" do - @archer.abilities.keys.to_set.should == [:shoot!, :look].to_set + expect(@archer.abilities.keys.to_set).to eq([:shoot!, :look].to_set) end - + it "should have shoot power of 3" do - @archer.shoot_power.should == 3 + expect(@archer.shoot_power).to eq(3) end - + it "should have 7 max health" do - @archer.max_health.should == 7 + expect(@archer.max_health).to eq(7) end - + it "should appear as a on map" do - @archer.character.should == "a" + expect(@archer.character).to eq("a") end end diff --git a/spec/ruby_warrior/units/base_spec.rb b/spec/ruby_warrior/units/base_spec.rb index 4c10d379..09affe32 100644 --- a/spec/ruby_warrior/units/base_spec.rb +++ b/spec/ruby_warrior/units/base_spec.rb @@ -6,47 +6,47 @@ end it "should have an attack power which defaults to zero" do - @unit.attack_power.should be_zero + expect(@unit.attack_power).to be_zero end it "should consider itself dead when no position" do - @unit.position.should be_nil - @unit.should_not be_alive + expect(@unit.position).to be_nil + expect(@unit).to_not be_alive end it "should consider itself alive with position" do @unit.position = stub - @unit.should be_alive + expect(@unit).to be_alive end it "should default max health to 10" do - @unit.max_health.should be_zero + expect(@unit.max_health).to be_zero end it "should do nothing when earning points" do - lambda { @unit.earn_points(10) }.should_not raise_error + expect { @unit.earn_points(10) }.to_not raise_error end it "should default health to max health" do @unit.stubs(:max_health).returns(10) - @unit.health.should == 10 + expect(@unit.health).to eq(10) end it "should subtract health when taking damage" do @unit.stubs(:max_health).returns(10) @unit.take_damage(3) - @unit.health.should == 7 + expect(@unit.health).to eq(7) end it "should do nothing when taking damage if health isn't set" do - lambda { @unit.take_damage(3) }.should_not raise_error + expect { @unit.take_damage(3) }.to_not raise_error end it "should set position to nil when running out of health" do @unit.position = stub @unit.stubs(:max_health).returns(10) @unit.take_damage(10) - @unit.position.should be_nil + expect(@unit.position).to be_nil end it "should print out line with name when speaking" do @@ -55,8 +55,8 @@ end it "should return name in to_s" do - @unit.name.should == 'Base' - @unit.to_s.should == 'Base' + expect(@unit.name).to eq('Base') + expect(@unit.to_s).to eq('Base') end it "should prepare turn by calling play_turn with next turn object" do @@ -87,37 +87,37 @@ it "should not raise an exception when calling perform_turn when there's no action" do @unit.prepare_turn - lambda { @unit.perform_turn }.should_not raise_error + expect { @unit.perform_turn }.to_not raise_error end it "should pass abilities to new turn when calling next_turn" do RubyWarrior::Turn.expects(:new).with({:walk! => nil, :attack! => nil, :feel => nil}).returns('turn') @unit.stubs(:abilities).returns(:walk! => nil, :attack! => nil, :feel => nil) - @unit.next_turn.should == 'turn' + expect(@unit.next_turn).to eq('turn') end it "should add ability" do RubyWarrior::Abilities::Walk.expects(:new).with(@unit).returns('walk') @unit.add_abilities(:walk!) - @unit.abilities.should == { :walk! => 'walk' } + expect(@unit.abilities).to eq({ :walk! => 'walk' }) end it "should appear as question mark on map" do - @unit.character.should == "?" + expect(@unit.character).to eq("?") end it "should be released from bonds when taking damage" do @unit.stubs(:max_health).returns(10) @unit.bind - @unit.should be_bound + expect(@unit).to be_bound @unit.take_damage(2) - @unit.should_not be_bound + expect(@unit).to_not be_bound end it "should be released from bonds when calling release" do @unit.bind @unit.unbind - @unit.should_not be_bound + expect(@unit).to_not be_bound end it "should not perform action when bound" do diff --git a/spec/ruby_warrior/units/captive_spec.rb b/spec/ruby_warrior/units/captive_spec.rb index cda46c22..034e6eb7 100644 --- a/spec/ruby_warrior/units/captive_spec.rb +++ b/spec/ruby_warrior/units/captive_spec.rb @@ -4,20 +4,20 @@ before(:each) do @captive = RubyWarrior::Units::Captive.new end - + it "should have 1 max health" do - @captive.max_health.should == 1 + expect(@captive.max_health).to eq(1) end - + it "should appear as C on map" do - @captive.character.should == "C" + expect(@captive.character).to eq("C") end - + it "should be bound by default" do - @captive.should be_bound + expect(@captive).to be_bound end - + it "should not have explode ability by default (this should be added when needed)" do - @captive.abilities.should_not include(:explode!) + expect(@captive.abilities).to_not include(:explode!) end end diff --git a/spec/ruby_warrior/units/golem_spec.rb b/spec/ruby_warrior/units/golem_spec.rb index bd69d40a..54c53dd1 100644 --- a/spec/ruby_warrior/units/golem_spec.rb +++ b/spec/ruby_warrior/units/golem_spec.rb @@ -4,25 +4,25 @@ before(:each) do @golem = RubyWarrior::Units::Golem.new end - + it "should execute turn proc when playing turn" do proc = Object.new proc.expects(:call).with(:turn) @golem.turn = proc @golem.play_turn(:turn) end - + it "should set max health and update current health" do @golem.max_health = 10 - @golem.max_health.should == 10 - @golem.health.should == 10 + expect(@golem.max_health).to eq(10) + expect(@golem.health).to eq(10) end - + it "should have attack power of 3" do - @golem.attack_power.should == 3 + expect(@golem.attack_power).to eq(3) end - + it "should appear as G on map" do - @golem.character.should == "G" + expect(@golem.character).to eq("G") end end diff --git a/spec/ruby_warrior/units/sludge_spec.rb b/spec/ruby_warrior/units/sludge_spec.rb index 898fd7b0..a73753c7 100644 --- a/spec/ruby_warrior/units/sludge_spec.rb +++ b/spec/ruby_warrior/units/sludge_spec.rb @@ -4,24 +4,24 @@ before(:each) do @sludge = RubyWarrior::Units::Sludge.new end - + it "should have attack action" do - @sludge.abilities.keys.should include(:attack!) + expect(@sludge.abilities.keys).to include(:attack!) end - + it "should have feel sense" do - @sludge.abilities.keys.should include(:feel) + expect(@sludge.abilities.keys).to include(:feel) end - + it "should have attack power of 3" do - @sludge.attack_power.should == 3 + expect(@sludge.attack_power).to eq(3) end - + it "should have 12 max health" do - @sludge.max_health.should == 12 + expect(@sludge.max_health).to eq(12) end - + it "should appear as s on map" do - @sludge.character.should == "s" + expect(@sludge.character).to eq("s") end end diff --git a/spec/ruby_warrior/units/thick_sludge_spec.rb b/spec/ruby_warrior/units/thick_sludge_spec.rb index 8d790fa5..9ef57a2d 100644 --- a/spec/ruby_warrior/units/thick_sludge_spec.rb +++ b/spec/ruby_warrior/units/thick_sludge_spec.rb @@ -4,16 +4,16 @@ before(:each) do @sludge = RubyWarrior::Units::ThickSludge.new end - + it "should have 24 max health" do - @sludge.max_health.should == 24 + expect(@sludge.max_health).to eq(24) end - + it "should appear as S on map" do - @sludge.character.should == "S" + expect(@sludge.character).to eq("S") end - + it "should have the name of 'Thick Sludge'" do - @sludge.name.should == "Thick Sludge" + expect(@sludge.name).to eq("Thick Sludge") end end diff --git a/spec/ruby_warrior/units/warrior_spec.rb b/spec/ruby_warrior/units/warrior_spec.rb index 276897b4..9691fe73 100644 --- a/spec/ruby_warrior/units/warrior_spec.rb +++ b/spec/ruby_warrior/units/warrior_spec.rb @@ -9,57 +9,57 @@ def turn(warrior) before(:each) do @warrior = RubyWarrior::Units::Warrior.new end - + it "should default name to warrior" do - @warrior.name.should == "Warrior" + expect(@warrior.name).to eq("Warrior") @warrior.name = '' - @warrior.name.should == "Warrior" + expect(@warrior.name).to eq("Warrior") end - + it "should be able to set name" do @warrior.name = "Joe" - @warrior.name.should == "Joe" - @warrior.to_s.should == "Joe" + expect(@warrior.name).to eq("Joe") + expect(@warrior.to_s).to eq("Joe") end - + it "should have 20 max health" do - @warrior.max_health.should == 20 + expect(@warrior.max_health).to eq(20) end - + it "should have 0 score at beginning and be able to earn points" do - @warrior.score.should be_zero + expect(@warrior.score).to be_zero @warrior.earn_points(5) - @warrior.score.should == 5 + expect(@warrior.score).to eq(5) end - + it "should call player.play_turn and pass turn to player" do player = stub player.expects(:play_turn).with('turn') @warrior.stubs(:player).returns(player) @warrior.play_turn('turn') end - + it "should call Player.new the first time loading player, and return same object next time" do Player.expects(:new).returns('player').times(1) 2.times do - @warrior.player.should == 'player' + expect(@warrior.player).to eq('player') end end - + it "should have an attack power of 5" do - @warrior.attack_power.should == 5 + expect(@warrior.attack_power).to eq(5) end - + it "should have an shoot power of 3" do - @warrior.shoot_power.should == 3 + expect(@warrior.shoot_power).to eq(3) end - + it "should appear as @ on map" do - @warrior.character.should == "@" + expect(@warrior.character).to eq("@") end - + it "should be able to add golem abilities which are used on base golem" do @warrior.add_golem_abilities :walk! - @warrior.base_golem.abilities.keys.should == [:walk!] + expect(@warrior.base_golem.abilities.keys).to eq([:walk!]) end end diff --git a/spec/ruby_warrior/units/wizard_spec.rb b/spec/ruby_warrior/units/wizard_spec.rb index b4a582d0..686a9dd2 100644 --- a/spec/ruby_warrior/units/wizard_spec.rb +++ b/spec/ruby_warrior/units/wizard_spec.rb @@ -4,20 +4,20 @@ before(:each) do @wizard = RubyWarrior::Units::Wizard.new end - + it "should have look and shoot abilities" do - @wizard.abilities.keys.to_set.should == [:shoot!, :look].to_set + expect(@wizard.abilities.keys.to_set).to eq([:shoot!, :look].to_set) end - + it "should have shoot power of 11" do - @wizard.shoot_power.should == 11 + expect(@wizard.shoot_power).to eq(11) end - + it "should have 3 max health" do - @wizard.max_health.should == 3 + expect(@wizard.max_health).to eq(3) end - + it "should appear as w on map" do - @wizard.character.should == "w" + expect(@wizard.character).to eq("w") end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fb834c68..6c9171b5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -7,5 +7,5 @@ config.before(:each) do RubyWarrior::Config.reset end - config.expect_with(:rspec) { |c| c.syntax = :should } + config.raise_errors_for_deprecations! end From 8bfcc41d9f2fbc6778d26cf3922d2b35330cba11 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 13:02:09 -0700 Subject: [PATCH 17/32] Update license year --- LICENSE | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/LICENSE b/LICENSE index 3a4f4da2..35cb3c21 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ -Copyright (c) 2010 Ryan Bates - +Copyright (c) 2024 Ryan Bates + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including @@ -7,10 +7,10 @@ without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND From af068c6833560c601b3dc274622e0e938c9f1dfb Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 13:09:47 -0700 Subject: [PATCH 18/32] Migrate README to Markdown --- README.rdoc => README.md | 208 ++++++++++++++++++++++----------------- 1 file changed, 115 insertions(+), 93 deletions(-) rename README.rdoc => README.md (62%) diff --git a/README.rdoc b/README.md similarity index 62% rename from README.rdoc rename to README.md index 9f50633b..342ba1f6 100644 --- a/README.rdoc +++ b/README.md @@ -1,50 +1,58 @@ -= Ruby Warrior +# Ruby Warrior -This is a game designed to teach the Ruby language and artificial intelligence in a fun and interactive way. +This is a game designed to teach the Ruby language in a fun and interactive way. 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. -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. +> 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. -== Getting Started +## Getting Started First install the gem. - gem install rubywarrior +```sh +gem install rubywarrior +``` -Then run the "rubywarrior" command to setup your profile. This will create a rubywarrior directory in your current location where you will find a player.rb file in your profile's directory containing this: +> As of version 0.2.0, this gem has been updated to work with Ruby 3.3. If you have difficulty running it in an older version of Ruby, try using an older version of the gem. - class Player - def play_turn(warrior) - # your code goes here - end +Then run the `rubywarrior` command to setup your profile. This will create a rubywarrior directory in your current location where you will find a player.rb file in your profile's directory containing this: + +```rb +class Player + def play_turn(warrior) + # your code goes here end +end +``` -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. +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. Here is a simple example which will instruct the warrior to attack if he feels an enemy, otherwise he will walk forward. - class Player - def play_turn(warrior) - if warrior.feel.enemy? - warrior.attack! - else - warrior.walk! - end +```rb +class Player + def play_turn(warrior) + if warrior.feel.enemy? + warrior.attack! + else + warrior.walk! end end +end +``` -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. +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. 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. -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. +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. -Once you pass a level (by reaching the stairs), the profile README will be updated for the next level. Alter the player.rb file and run rubywarrior again to play the next level. +Once you pass a level (by reaching the stairs), the profile README will be updated for the next level. Alter the `player.rb` file and run `rubywarrior` again to play the next level. -== Scoring +## Scoring 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. @@ -58,127 +66,141 @@ A total score is kept as you progress through the levels. When you pass a level, Don't be too concerned about scoring perfectly in the beginning. After you reach the top of the tower you will be able to re-run the tower and fine-tune your warrior to get the highest score. See the Epic Mode below for details. -== Perspective +## Perspective 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. - ---- - |C s>| - | S s| - |C @ | - ---- - - > = Stairs - @ = Warrior (20 HP) - s = Sludge (12 HP) - S = Thick Sludge (24 HP) - C = Captive (1 HP) +``` + ---- +|C s>| +| S s| +|C @ | + ---- +> = Stairs +@ = Warrior (20 HP) +s = Sludge (12 HP) +S = Thick Sludge (24 HP) +C = Captive (1 HP) +``` -== Commanding the Warrior + +## Commanding the Warrior 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. 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. - warrior.walk! - Move in given direction (forward by default). +``` +warrior.walk! + Move in given direction (forward by default). - warrior.attack! - Attack the unit in given direction (forward by default). +warrior.attack! + Attack the unit in given direction (forward by default). - warrior.rest! - Gain 10% of max health back, but do nothing more. +warrior.rest! + Gain 10% of max health back, but do nothing more. - warrior.bind! - Bind unit in given direction to keep him from moving (forward by default). +warrior.bind! + Bind unit in given direction to keep him from moving (forward by default). - warrior.rescue! - Rescue a captive from his chains (earning 20 points) in given direction (forward by default). +warrior.rescue! + Rescue a captive from his chains (earning 20 points) in given direction (forward by default). +``` 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. - warrior.feel - Returns a Space for the given direction (forward by default). +``` +warrior.feel + Returns a Space for the given direction (forward by default). - warrior.health - Returns an integer representing your health. +warrior.health + Returns an integer representing your health. - warrior.distance - Returns the number of spaces the stairs are away. +warrior.distance + Returns the number of spaces the stairs are away. - warrior.listen - Returns an array of all spaces which have units in them. +warrior.listen + Returns an array of all spaces which have units in them. +``` 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. -== Spaces +## Spaces 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. - space.empty? - If true, this means that nothing (except maybe stairs) is at this location and you can walk here. - - space.stairs? - Determine if stairs are at that location - - space.enemy? - Determine if an enemy unit is at this location. - - space.captive? - Determine if a captive is at this location. - - space.wall? - Returns true if this is the edge of the level. You can't walk here. - - space.ticking? - Returns true if this space contains a bomb which will explode in time. - - space.golem? - Returns true if a golem is occupying this space. - -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. - - warrior.feel.captive? - - -== Golem +``` +space.empty? + If true, this means that nothing (except maybe stairs) is at this location and you can walk here. + +space.stairs? + Determine if stairs are at that location + +space.enemy? + Determine if an enemy unit is at this location. + +space.captive? + Determine if a captive is at this location. + +space.wall? + Returns true if this is the edge of the level. You can't walk here. + +space.ticking? + Returns true if this space contains a bomb which will explode in time. + +space.golem? + Returns true if a golem is occupying this space. +``` + +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. + +``` +warrior.feel.captive? +``` + + +## Golem Along your journey you may discover the ability to create a golem. This is a separate unit which you also control. The turn handling is done through a block. Here is an example. - warrior.form! do |golem| - golem.attack! if golem.feel.enemy? - end +```rb +warrior.form! do |golem| + golem.attack! if golem.feel.enemy? +end +``` Complex logic can be placed in this block just like in the player turn method. You may want to move the logic into its own class or create multiple classes for different types of golems. You can create multiple golems in a level, but each one will take half of the warrior's health. -== Epic Mode - +## Epic Mode + 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. Your warrior will most likely not succeed the first time around, so use the -l option on levels you are having difficulty or want to fine-tune the scoring. - rubywarrior -l 3 +```sh +rubywarrior -l 3 +``` Once your warrior reaches the top again you will receive an average grade, along with a grade for each level. The grades from best to worst are S, A, B, C, D and F. Try to get S on each level for the ultimate score. -Note: I'm in the process of fine-tuning the grading system. If you find the "S" grade to be too easy or too difficult to achieve on a given level, please add an issue for this on GitHub. +Note: I'm in the process of fine-tuning the grading system. If you find the `S` grade to be too easy or too difficult to achieve on a given level, please add an issue for this on GitHub. -== Tips +## Tips -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). +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). -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. +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. Senses are cheap, so use them liberally. Store the sensed information to help you better determine what actions to take in the future. -Running "rubywarrior" while you are in your profile directory will auto-select that profile so you don't have to each time. +Running `rubywarrior` while you are in your profile directory will auto-select that profile so you don't have to each time. 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. -Make sure to try the different options you can pass to the rubywarrior command. Run rubywarrior --help to see them all. +Make sure to try the different options you can pass to the rubywarrior command. Run `rubywarrior --help` to see them all. From 4561ffc3b479f4f19fa1bdc967b57c9cf263cc5c Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 13:13:48 -0700 Subject: [PATCH 19/32] Migrate changelog to Markdown --- CHANGELOG.rdoc => CHANGELOG.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename CHANGELOG.rdoc => CHANGELOG.md (88%) diff --git a/CHANGELOG.rdoc b/CHANGELOG.md similarity index 88% rename from CHANGELOG.rdoc rename to CHANGELOG.md index f7d0b20a..652c24d3 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -0.1.3 (April 28, 2012) +## 0.1.3 (April 28, 2012) * Making specs pass with latest RSpec. @@ -9,7 +9,7 @@ * Other minor bug fixes (see commit history for details) -0.1.2 (September 23, 2010) +## 0.1.2 (September 23, 2010) * Adding intermediate level 9 with distance_of ability @@ -22,7 +22,7 @@ * Mention direction when ability is performed -0.1.1 (Jan 3, 2010) +## 0.1.1 (Jan 3, 2010) * Speeding up play speed a little @@ -33,6 +33,6 @@ * Keep track of last level played in profile and go back to normal mode when level is added -0.1.0 (Jan 2, 2010) +## 0.1.0 (Jan 2, 2010) * initial release From ec251eb5ebf54a7380a234f3a9678431aa8e6f58 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 13:16:32 -0700 Subject: [PATCH 20/32] Add changelog entry for v0.2.0 --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 652c24d3..5a63f283 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.2.0 (March 26, 2024) + +* Support Ruby 3.3 (thanks fpsvogel) + +* Improve message when using --skip option (thanks dcki) + + ## 0.1.3 (April 28, 2012) * Making specs pass with latest RSpec. From 98e3945195116baef7bc1fbd1fd29c2afaa5b157 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 13:18:00 -0700 Subject: [PATCH 21/32] Release 0.2.0 --- rubywarrior.gemspec | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rubywarrior.gemspec b/rubywarrior.gemspec index 0650924e..18ddc179 100644 --- a/rubywarrior.gemspec +++ b/rubywarrior.gemspec @@ -1,16 +1,16 @@ Gem::Specification.new do |s| s.name = "rubywarrior" - s.version = "0.1.3" + s.version = "0.2.0" s.author = "Ryan Bates" s.email = "ryan@railscasts.com" s.homepage = "https://github.com/ryanb/ruby-warrior" - s.summary = "Game written in Ruby for learning Ruby and artificial intelligence." + s.summary = "Game written in Ruby for learning Ruby." s.description = "You play as a warrior climbing a tall tower. On each floor you need to write a Ruby script to instruct the warrior to battle enemies, rescue captives, and reach the stairs." + s.license = "MIT" s.files = Dir["{lib,spec,features,towers,templates,bin}/**/*", "[A-Z]*", "init.rb"] s.require_path = "lib" s.executables = ["rubywarrior"] - s.rubyforge_project = s.name s.required_rubygems_version = ">= 1.3.4" end From 4fb028814b84f3e353e044897af103aa55a407ac Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 13:52:06 -0700 Subject: [PATCH 22/32] Fix example level spacing in README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 342ba1f6..6ce5c95b 100644 --- a/README.md +++ b/README.md @@ -71,11 +71,11 @@ Don't be too concerned about scoring perfectly in the beginning. After you reach 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. ``` - ---- + ---- |C s>| | S s| |C @ | - ---- + ---- > = Stairs @ = Warrior (20 HP) From 5035be20f0c90922eea233f4d4dc94527bd86b74 Mon Sep 17 00:00:00 2001 From: Felipe Vogel Date: Tue, 12 Mar 2024 00:37:34 -0400 Subject: [PATCH 23/32] Replace Mocha with RSpec mocks --- Gemfile | 1 - lib/ruby_warrior/level.rb | 2 +- spec/ruby_warrior/abilities/attack_spec.rb | 28 ++++---- spec/ruby_warrior/abilities/base_spec.rb | 4 +- spec/ruby_warrior/abilities/bind_spec.rb | 6 +- .../abilities/direction_of_spec.rb | 6 +- .../abilities/direction_of_stairs_spec.rb | 6 +- .../abilities/distance_of_spec.rb | 6 +- spec/ruby_warrior/abilities/feel_spec.rb | 4 +- spec/ruby_warrior/abilities/look_spec.rb | 8 +-- spec/ruby_warrior/abilities/pivot_spec.rb | 8 +-- spec/ruby_warrior/abilities/rescue_spec.rb | 24 +++---- spec/ruby_warrior/abilities/rest_spec.rb | 6 +- spec/ruby_warrior/abilities/shoot_spec.rb | 14 ++-- spec/ruby_warrior/abilities/walk_spec.rb | 14 ++-- spec/ruby_warrior/game_spec.rb | 64 ++++++++--------- spec/ruby_warrior/level_loader_spec.rb | 2 +- spec/ruby_warrior/level_spec.rb | 68 +++++++++---------- spec/ruby_warrior/profile_spec.rb | 16 ++--- spec/ruby_warrior/turn_spec.rb | 4 +- spec/ruby_warrior/ui_spec.rb | 24 +++---- spec/ruby_warrior/units/base_spec.rb | 46 ++++++------- spec/ruby_warrior/units/golem_spec.rb | 2 +- spec/ruby_warrior/units/warrior_spec.rb | 8 +-- spec/spec_helper.rb | 1 - 25 files changed, 185 insertions(+), 187 deletions(-) diff --git a/Gemfile b/Gemfile index f13e686d..d1fc2fca 100644 --- a/Gemfile +++ b/Gemfile @@ -6,5 +6,4 @@ group :test do gem 'rake' gem 'rspec', '~> 3.13.0' gem 'cucumber' - gem 'mocha' end diff --git a/lib/ruby_warrior/level.rb b/lib/ruby_warrior/level.rb index c34d27fb..174b070e 100644 --- a/lib/ruby_warrior/level.rb +++ b/lib/ruby_warrior/level.rb @@ -113,7 +113,7 @@ def failed? def exists? File.exist? load_path end - + def setup_warrior(warrior) @warrior = warrior @warrior.add_abilities(*profile.abilities) diff --git a/spec/ruby_warrior/abilities/attack_spec.rb b/spec/ruby_warrior/abilities/attack_spec.rb index 3aa14481..e207c02a 100644 --- a/spec/ruby_warrior/abilities/attack_spec.rb +++ b/spec/ruby_warrior/abilities/attack_spec.rb @@ -2,49 +2,49 @@ describe RubyWarrior::Abilities::Attack do before(:each) do - @attacker = stub(:position => stub, :attack_power => 3, :say => nil) + @attacker = double(:position => double, :attack_power => 3, :say => nil) @attack = RubyWarrior::Abilities::Attack.new(@attacker) end it "should subtract attack power amount from health" do receiver = RubyWarrior::Units::Base.new - receiver.stubs(:alive?).returns(true) + allow(receiver).to receive(:alive?).and_return(true) receiver.health = 5 - @attack.stubs(:unit).returns(receiver) + allow(@attack).to receive(:unit).and_return(receiver) @attack.perform expect(receiver.health).to eq(2) end it "should do nothing if recipient is nil" do - @attack.stubs(:unit).returns(nil) + allow(@attack).to receive(:unit).and_return(nil) expect { @attack.perform }.to_not raise_error end it "should get object at position from offset" do - @attacker.position.expects(:relative_space).with(1, 0) + expect(@attacker.position).to receive(:relative_space).with(1, 0) @attack.space(:forward) end it "should award points when killing unit" do - receiver = stub(:take_damage => nil, :max_health => 8, :alive? => false) - @attack.stubs(:unit).returns(receiver) - @attacker.expects(:earn_points).with(8) + receiver = double(:take_damage => nil, :max_health => 8, :alive? => false) + allow(@attack).to receive(:unit).and_return(receiver) + expect(@attacker).to receive(:earn_points).with(8) @attack.perform end it "should not award points when not killing unit" do - receiver = stub(:max_health => 8, :alive? => true) - receiver.expects(:take_damage) - @attack.stubs(:unit).returns(receiver) - @attacker.expects(:earn_points).never + receiver = double(:max_health => 8, :alive? => true) + expect(receiver).to receive(:take_damage) + allow(@attack).to receive(:unit).and_return(receiver) + expect(@attacker).to receive(:earn_points).never @attack.perform end it "should reduce attack power when attacking backward" do receiver = RubyWarrior::Units::Base.new - receiver.stubs(:alive?).returns(true) + allow(receiver).to receive(:alive?).and_return(true) receiver.health = 5 - @attack.stubs(:unit).returns(receiver) + allow(@attack).to receive(:unit).and_return(receiver) @attack.perform(:backward) expect(receiver.health).to eq(3) end diff --git a/spec/ruby_warrior/abilities/base_spec.rb b/spec/ruby_warrior/abilities/base_spec.rb index 0cb9f895..411b0b74 100644 --- a/spec/ruby_warrior/abilities/base_spec.rb +++ b/spec/ruby_warrior/abilities/base_spec.rb @@ -2,7 +2,7 @@ describe RubyWarrior::Abilities::Base do before(:each) do - @unit = stub + @unit = double @ability = RubyWarrior::Abilities::Base.new(@unit) end @@ -22,7 +22,7 @@ end it "should fetch unit at given direction with distance" do - @ability.expects(:space).with(:right, 3, 1).returns(stub(:unit => 'unit')) + expect(@ability).to receive(:space).with(:right, 3, 1).and_return(double(:unit => 'unit')) expect(@ability.unit(:right, 3, 1)).to eq('unit') end diff --git a/spec/ruby_warrior/abilities/bind_spec.rb b/spec/ruby_warrior/abilities/bind_spec.rb index 164af8cd..5906dfe7 100644 --- a/spec/ruby_warrior/abilities/bind_spec.rb +++ b/spec/ruby_warrior/abilities/bind_spec.rb @@ -2,18 +2,18 @@ describe RubyWarrior::Abilities::Bind do before(:each) do - @bind = RubyWarrior::Abilities::Bind.new(stub(:say => nil)) + @bind = RubyWarrior::Abilities::Bind.new(double(:say => nil)) end it "should bind recipient" do receiver = RubyWarrior::Units::Base.new - @bind.stubs(:unit).returns(receiver) + allow(@bind).to receive(:unit).and_return(receiver) @bind.perform expect(receiver).to be_bound end it "should do nothing if no recipient" do - @bind.stubs(:unit).returns(nil) + allow(@bind).to receive(:unit).and_return(nil) expect { @bind.perform }.to_not raise_error end end diff --git a/spec/ruby_warrior/abilities/direction_of_spec.rb b/spec/ruby_warrior/abilities/direction_of_spec.rb index 843d34c1..075479d9 100644 --- a/spec/ruby_warrior/abilities/direction_of_spec.rb +++ b/spec/ruby_warrior/abilities/direction_of_spec.rb @@ -2,12 +2,12 @@ describe RubyWarrior::Abilities::DirectionOf do before(:each) do - @position = stub - @distance = RubyWarrior::Abilities::DirectionOf.new(stub(:position => @position, :say => nil)) + @position = double + @distance = RubyWarrior::Abilities::DirectionOf.new(double(:position => @position, :say => nil)) end it "should return relative direction of given space" do - @position.stubs(:relative_direction_of).with(:space).returns(:left) + allow(@position).to receive(:relative_direction_of).with(:space).and_return(:left) expect(@distance.perform(:space)).to eq(:left) end end diff --git a/spec/ruby_warrior/abilities/direction_of_stairs_spec.rb b/spec/ruby_warrior/abilities/direction_of_stairs_spec.rb index 0cff28a3..5265738c 100644 --- a/spec/ruby_warrior/abilities/direction_of_stairs_spec.rb +++ b/spec/ruby_warrior/abilities/direction_of_stairs_spec.rb @@ -2,12 +2,12 @@ describe RubyWarrior::Abilities::DirectionOfStairs do before(:each) do - @position = stub - @distance = RubyWarrior::Abilities::DirectionOfStairs.new(stub(:position => @position, :say => nil)) + @position = double + @distance = RubyWarrior::Abilities::DirectionOfStairs.new(double(:position => @position, :say => nil)) end it "should return relative direction of stairs" do - @position.stubs(:relative_direction_of_stairs).returns(:left) + allow(@position).to receive(:relative_direction_of_stairs).and_return(:left) expect(@distance.perform).to eq(:left) end end diff --git a/spec/ruby_warrior/abilities/distance_of_spec.rb b/spec/ruby_warrior/abilities/distance_of_spec.rb index 39fc138c..219a9db9 100644 --- a/spec/ruby_warrior/abilities/distance_of_spec.rb +++ b/spec/ruby_warrior/abilities/distance_of_spec.rb @@ -2,12 +2,12 @@ describe RubyWarrior::Abilities::DistanceOf do before(:each) do - @position = stub - @distance = RubyWarrior::Abilities::DistanceOf.new(stub(:position => @position, :say => nil)) + @position = double + @distance = RubyWarrior::Abilities::DistanceOf.new(double(:position => @position, :say => nil)) end it "should return distance from stairs" do - @position.stubs(:distance_of).with(:space).returns(5) + allow(@position).to receive(:distance_of).with(:space).and_return(5) expect(@distance.perform(:space)).to eq(5) end end diff --git a/spec/ruby_warrior/abilities/feel_spec.rb b/spec/ruby_warrior/abilities/feel_spec.rb index 9b413b7a..9f61b649 100644 --- a/spec/ruby_warrior/abilities/feel_spec.rb +++ b/spec/ruby_warrior/abilities/feel_spec.rb @@ -2,12 +2,12 @@ describe RubyWarrior::Abilities::Feel do before(:each) do - @unit = stub(:position => stub, :say => nil) + @unit = double(:position => double, :say => nil) @feel = RubyWarrior::Abilities::Feel.new(@unit) end it "should get object at position from offset" do - @unit.position.expects(:relative_space).with(1, 0) + expect(@unit.position).to receive(:relative_space).with(1, 0) @feel.perform(:forward) end end diff --git a/spec/ruby_warrior/abilities/look_spec.rb b/spec/ruby_warrior/abilities/look_spec.rb index acfb6926..cd92335d 100644 --- a/spec/ruby_warrior/abilities/look_spec.rb +++ b/spec/ruby_warrior/abilities/look_spec.rb @@ -2,14 +2,14 @@ describe RubyWarrior::Abilities::Look do before(:each) do - @unit = stub(:position => stub, :say => nil) + @unit = double(:position => double, :say => nil) @feel = RubyWarrior::Abilities::Look.new(@unit) end it "should get 3 objects at position from offset" do - @unit.position.expects(:relative_space).with(1, 0).returns(1) - @unit.position.expects(:relative_space).with(2, 0).returns(2) - @unit.position.expects(:relative_space).with(3, 0).returns(3) + expect(@unit.position).to receive(:relative_space).with(1, 0).and_return(1) + expect(@unit.position).to receive(:relative_space).with(2, 0).and_return(2) + expect(@unit.position).to receive(:relative_space).with(3, 0).and_return(3) expect(@feel.perform(:forward)).to eq([1, 2, 3]) end end diff --git a/spec/ruby_warrior/abilities/pivot_spec.rb b/spec/ruby_warrior/abilities/pivot_spec.rb index da6feb85..eee3ef80 100644 --- a/spec/ruby_warrior/abilities/pivot_spec.rb +++ b/spec/ruby_warrior/abilities/pivot_spec.rb @@ -2,17 +2,17 @@ describe RubyWarrior::Abilities::Pivot do before(:each) do - @position = stub - @pivot = RubyWarrior::Abilities::Pivot.new(stub(:position => @position, :say => nil)) + @position = double + @pivot = RubyWarrior::Abilities::Pivot.new(double(:position => @position, :say => nil)) end it "should flip around when not passing arguments" do - @position.expects(:rotate).with(2) + expect(@position).to receive(:rotate).with(2) @pivot.perform end it "should rotate 1 when pivoting right" do - @position.expects(:rotate).with(1) + expect(@position).to receive(:rotate).with(1) @pivot.perform(:right) end end diff --git a/spec/ruby_warrior/abilities/rescue_spec.rb b/spec/ruby_warrior/abilities/rescue_spec.rb index 0e57bdc1..f63afa9f 100644 --- a/spec/ruby_warrior/abilities/rescue_spec.rb +++ b/spec/ruby_warrior/abilities/rescue_spec.rb @@ -8,20 +8,20 @@ it "should rescue captive" do captive = RubyWarrior::Units::Captive.new - captive.position = stub - @rescue.expects(:space).with(:forward).returns(stub(:captive? => true)) - @rescue.expects(:unit).with(:forward).returns(captive) - @warrior.expects(:earn_points).with(20) + captive.position = double + expect(@rescue).to receive(:space).with(:forward).and_return(double(:captive? => true)) + expect(@rescue).to receive(:unit).with(:forward).and_return(captive) + expect(@warrior).to receive(:earn_points).with(20) @rescue.perform expect(captive.position).to be_nil end it "should do nothing to other unit if not bound" do unit = RubyWarrior::Units::Base.new - unit.position = stub - @rescue.expects(:space).with(:forward).returns(stub(:captive? => false)) - @rescue.expects(:unit).with(:forward).never - @warrior.expects(:earn_points).never + unit.position = double + expect(@rescue).to receive(:space).with(:forward).and_return(double(:captive? => false)) + expect(@rescue).to receive(:unit).with(:forward).never + expect(@warrior).to receive(:earn_points).never @rescue.perform expect(unit.position).to_not be_nil end @@ -29,10 +29,10 @@ it "should release other unit when bound" do unit = RubyWarrior::Units::Base.new unit.bind - unit.position = stub - @rescue.expects(:space).with(:forward).returns(stub(:captive? => true)) - @rescue.expects(:unit).with(:forward).returns(unit) - @warrior.expects(:earn_points).never + unit.position = double + expect(@rescue).to receive(:space).with(:forward).and_return(double(:captive? => true)) + expect(@rescue).to receive(:unit).with(:forward).and_return(unit) + expect(@warrior).to receive(:earn_points).never @rescue.perform expect(unit).to_not be_bound expect(unit.position).to_not be_nil diff --git a/spec/ruby_warrior/abilities/rest_spec.rb b/spec/ruby_warrior/abilities/rest_spec.rb index 88a341ae..d1812aab 100644 --- a/spec/ruby_warrior/abilities/rest_spec.rb +++ b/spec/ruby_warrior/abilities/rest_spec.rb @@ -7,21 +7,21 @@ end it "should give 10% health back" do - @warrior.stubs(:max_health).returns(20) + allow(@warrior).to receive(:max_health).and_return(20) @warrior.health = 10 @rest.perform expect(@warrior.health).to eq(12) end it "should add health when at max" do - @warrior.stubs(:max_health).returns(20) + allow(@warrior).to receive(:max_health).and_return(20) @warrior.health = 20 @rest.perform expect(@warrior.health).to eq(20) end it "should not go over max health" do - @warrior.stubs(:max_health).returns(20) + allow(@warrior).to receive(:max_health).and_return(20) @warrior.health = 19 @rest.perform expect(@warrior.health).to eq(20) diff --git a/spec/ruby_warrior/abilities/shoot_spec.rb b/spec/ruby_warrior/abilities/shoot_spec.rb index 2674f5f0..0a7cd246 100644 --- a/spec/ruby_warrior/abilities/shoot_spec.rb +++ b/spec/ruby_warrior/abilities/shoot_spec.rb @@ -2,21 +2,21 @@ describe RubyWarrior::Abilities::Shoot do before(:each) do - @shooter = stub(:position => stub, :shoot_power => 2, :say => nil) + @shooter = double(:position => double, :shoot_power => 2, :say => nil) @shoot = RubyWarrior::Abilities::Shoot.new(@shooter) end it "should shoot only first unit" do - receiver = stub(:alive? => true) - receiver.expects(:take_damage).with(2) - other = stub(:alive? => true) - other.expects(:take_damage).never - @shoot.expects(:multi_unit).with(:forward, anything).returns([nil, receiver, other, nil]) + receiver = double(:alive? => true) + expect(receiver).to receive(:take_damage).with(2) + other = double(:alive? => true) + expect(other).to receive(:take_damage).never + expect(@shoot).to receive(:multi_unit).with(:forward, anything).and_return([nil, receiver, other, nil]) @shoot.perform end it "should shoot and do nothing if no units in the way" do - @shoot.expects(:multi_unit).with(:forward, anything).returns([nil, nil]) + expect(@shoot).to receive(:multi_unit).with(:forward, anything).and_return([nil, nil]) expect { @shoot.perform }.to_not raise_error end end diff --git a/spec/ruby_warrior/abilities/walk_spec.rb b/spec/ruby_warrior/abilities/walk_spec.rb index fb6892cc..1309b46e 100644 --- a/spec/ruby_warrior/abilities/walk_spec.rb +++ b/spec/ruby_warrior/abilities/walk_spec.rb @@ -2,24 +2,24 @@ describe RubyWarrior::Abilities::Walk do before(:each) do - @space = stub(:empty? => true, :unit => nil) - @position = stub(:relative_space => @space, :move => nil) - @walk = RubyWarrior::Abilities::Walk.new(stub(:position => @position, :say => nil)) + @space = double(:empty? => true, :unit => nil) + @position = double(:relative_space => @space, :move => nil) + @walk = RubyWarrior::Abilities::Walk.new(double(:position => @position, :say => nil)) end it "should move position forward when calling perform" do - @position.expects(:move).with(1, 0) + expect(@position).to receive(:move).with(1, 0) @walk.perform end it "should move position right if that is direction" do - @position.expects(:move).with(0, 1) + expect(@position).to receive(:move).with(0, 1) @walk.perform(:right) end it "should keep position if something is in the way" do - @position.stubs(:move).raises("shouldn't be called") - @space.stubs(:empty?).returns(false) + allow(@position).to receive(:move).and_raise("shouldn't be called") + allow(@space).to receive(:empty?).and_return(false) expect { @walk.perform(:right) }.to_not raise_error end end diff --git a/spec/ruby_warrior/game_spec.rb b/spec/ruby_warrior/game_spec.rb index 9cef7587..2c59f35f 100644 --- a/spec/ruby_warrior/game_spec.rb +++ b/spec/ruby_warrior/game_spec.rb @@ -8,14 +8,14 @@ # GAME DIR it "should make game directory if player says so" do - RubyWarrior::UI.stubs(:ask).returns(true) - Dir.expects(:mkdir).with('./rubywarrior') + allow(RubyWarrior::UI).to receive(:ask).and_return(true) + expect(Dir).to receive(:mkdir).with('./rubywarrior') @game.make_game_directory end it "should not make game and exit if player says no" do - RubyWarrior::UI.stubs(:ask).returns(false) - Dir.stubs(:mkdir).raises('should not be called') + allow(RubyWarrior::UI).to receive(:ask).and_return(false) + allow(Dir).to receive(:mkdir).and_raise('should not be called') expect { @game.make_game_directory }.to raise_error(SystemExit) end @@ -23,43 +23,43 @@ # PROFILES it "should load profiles for each profile path" do - RubyWarrior::Profile.expects(:load).with('foo/.profile').returns(1) - RubyWarrior::Profile.expects(:load).with('bar/.profile').returns(2) - @game.stubs(:profile_paths).returns(['foo/.profile', 'bar/.profile']) + expect(RubyWarrior::Profile).to receive(:load).with('foo/.profile').and_return(1) + expect(RubyWarrior::Profile).to receive(:load).with('bar/.profile').and_return(2) + allow(@game).to receive(:profile_paths).and_return(['foo/.profile', 'bar/.profile']) expect(@game.profiles).to eq([1, 2]) end it "should find profile paths using Dir[] search" do - Dir.expects(:[]).with("./rubywarrior/**/.profile") + expect(Dir).to receive(:[]).with("./rubywarrior/**/.profile") @game.profile_paths end it "should try to create profile when no profile paths are specified" do - @game.stubs(:profiles).returns([]) - @game.expects(:new_profile).returns('profile') + allow(@game).to receive(:profiles).and_return([]) + expect(@game).to receive(:new_profile).and_return('profile') expect(@game.profile).to eq('profile') end it "should ask a player to choose a profile if multiple profiles are available, but only once" do - RubyWarrior::UI.expects(:choose).with('profile', [:profile1, [:new, 'New Profile']]).returns(:profile1) - @game.stubs(:profiles).returns([:profile1]) + expect(RubyWarrior::UI).to receive(:choose).with('profile', [:profile1, [:new, 'New Profile']]).and_return(:profile1) + allow(@game).to receive(:profiles).and_return([:profile1]) 2.times { expect(@game.profile).to eq(:profile1) } end it "should ask user to choose a tower when creating a new profile" do - RubyWarrior::UI.stubs(:gets).returns('') - @game.stubs(:towers).returns([:tower1, :tower2]) - RubyWarrior::UI.expects(:choose).with('tower', [:tower1, :tower2]).returns(stub(:path => '/foo/bar')) + allow(RubyWarrior::UI).to receive(:gets).and_return('') + allow(@game).to receive(:towers).and_return([:tower1, :tower2]) + expect(RubyWarrior::UI).to receive(:choose).with('tower', [:tower1, :tower2]).and_return(double(path: '/foo/bar')) @game.new_profile end it "should pass name and selected tower to profile" do - profile = stub - RubyWarrior::UI.stubs(:choose).returns(stub(:path => 'tower_path')) - RubyWarrior::UI.stubs(:request).returns('name') - RubyWarrior::Profile.expects(:new).returns(profile) - profile.expects(:tower_path=).with('tower_path') - profile.expects(:warrior_name=).with('name') + profile = double + allow(RubyWarrior::UI).to receive(:choose).and_return(double(path: 'tower_path')) + allow(RubyWarrior::UI).to receive(:request).and_return('name') + expect(RubyWarrior::Profile).to receive(:new).and_return(profile) + expect(profile).to receive(:tower_path=).with('tower_path') + expect(profile).to receive(:warrior_name=).with('name') expect(@game.new_profile).to eq(profile) end @@ -67,14 +67,14 @@ # TOWERS it "load towers for each tower path" do - RubyWarrior::Tower.expects(:new).with('towers/foo').returns(1) - RubyWarrior::Tower.expects(:new).with('towers/bar').returns(2) - @game.stubs(:tower_paths).returns(['towers/foo', 'towers/bar']) + expect(RubyWarrior::Tower).to receive(:new).with('towers/foo').and_return(1) + expect(RubyWarrior::Tower).to receive(:new).with('towers/bar').and_return(2) + allow(@game).to receive(:tower_paths).and_return(['towers/foo', 'towers/bar']) expect(@game.towers).to eq([1, 2]) end it "should find tower paths using Dir[] search" do - Dir.expects(:[]).with(File.expand_path('../../../towers/*', __FILE__)) + expect(Dir).to receive(:[]).with(File.expand_path('../../../towers/*', __FILE__)) @game.tower_paths end @@ -82,21 +82,21 @@ # LEVEL it "should fetch current level from profile and cache it" do - @game.stubs(:profile).returns(stub) - @game.profile.expects(:current_level).returns('foo') + allow(@game).to receive(:profile).and_return(double) + expect(@game.profile).to receive(:current_level).and_return('foo') 2.times { expect(@game.current_level).to eq('foo') } end it "should fetch next level from profile and cache it" do - @game.stubs(:profile).returns(stub) - @game.profile.expects(:next_level).returns('bar') + allow(@game).to receive(:profile).and_return(double) + expect(@game.profile).to receive(:next_level).and_return('bar') 2.times { expect(@game.next_level).to eq('bar') } end it "should report final grade" do profile = RubyWarrior::Profile.new profile.current_epic_grades = { 1 => 0.7, 2 => 0.9 } - @game.stubs(:profile).returns(profile) + allow(@game).to receive(:profile).and_return(profile) report = @game.final_report expect(report).to include("Your average grade for this tower is: B") expect(report).to include("Level 1: C\n Level 2: A") @@ -105,7 +105,7 @@ it "should have an empty final report if no epic grades are available" do profile = RubyWarrior::Profile.new profile.current_epic_grades = {} - @game.stubs(:profile).returns(profile) + allow(@game).to receive(:profile).and_return(profile) expect(@game.final_report).to be_nil end @@ -113,7 +113,7 @@ RubyWarrior::Config.practice_level = 2 profile = RubyWarrior::Profile.new profile.current_epic_grades = { 1 => 0.7, 2 => 0.9 } - @game.stubs(:profile).returns(profile) + allow(@game).to receive(:profile).and_return(profile) expect(@game.final_report).to be_nil end end diff --git a/spec/ruby_warrior/level_loader_spec.rb b/spec/ruby_warrior/level_loader_spec.rb index bcc6449e..8b9cfb1c 100644 --- a/spec/ruby_warrior/level_loader_spec.rb +++ b/spec/ruby_warrior/level_loader_spec.rb @@ -24,7 +24,7 @@ end it "should be able to add stairs" do - @level.floor.expects(:place_stairs).with(1, 2) + expect(@level.floor).to receive(:place_stairs).with(1, 2) @loader.stairs 1, 2 end diff --git a/spec/ruby_warrior/level_spec.rb b/spec/ruby_warrior/level_spec.rb index 00c08750..10519975 100644 --- a/spec/ruby_warrior/level_spec.rb +++ b/spec/ruby_warrior/level_spec.rb @@ -7,7 +7,7 @@ @floor = RubyWarrior::Floor.new @level = RubyWarrior::Level.new(@profile, 1) @level.floor = @floor - @level.stubs(:failed?).returns(false) + allow(@level).to receive(:failed?).and_return(false) end it "should consider passed when warrior is on stairs" do @@ -38,80 +38,80 @@ end it "should load file contents into level" do - @level.stubs(:load_path).returns('path/to/level.rb') - File.expects(:read).with('path/to/level.rb').returns("description 'foo'") + allow(@level).to receive(:load_path).and_return('path/to/level.rb') + expect(File).to receive(:read).with('path/to/level.rb').and_return("description 'foo'") @level.load_level expect(@level.description).to eq('foo') end it "should have a player path from profile" do - @profile.stubs(:player_path).returns('path/to/player') + allow(@profile).to receive(:player_path).and_return('path/to/player') expect(@level.player_path).to eq('path/to/player') end it "should have a load path from profile tower with level number in it" do - @profile.stubs(:tower_path).returns('path/to/tower') + allow(@profile).to receive(:tower_path).and_return('path/to/tower') expect(@level.load_path).to eq(File.expand_path('towers/tower/level_001.rb')) end it "should exist if file exists" do - @level.stubs(:load_path).returns('/foo/bar') - File.expects(:exist?).with('/foo/bar').returns(true) + allow(@level).to receive(:load_path).and_return('/foo/bar') + expect(File).to receive(:exist?).with('/foo/bar').and_return(true) expect(@level.exists?).to eq(true) end it "should load player and player path" do - @level.stubs(:player_path).returns('player/path') - $:.expects(:<<).with('player/path') - @level.expects(:load).with('player.rb') + allow(@level).to receive(:player_path).and_return('player/path') + expect($:).to receive(:<<).with('player/path') + expect(@level).to receive(:load).with('player.rb') @level.load_player end it "should generate player files" do - @level.expects(:load_level) - generator = stub - generator.expects(:generate) - RubyWarrior::PlayerGenerator.expects(:new).with(@level).returns(generator) + expect(@level).to receive(:load_level) + generator = double + expect(generator).to receive(:generate) + expect(RubyWarrior::PlayerGenerator).to receive(:new).with(@level).and_return(generator) @level.generate_player_files end it "should setup warrior with profile abilities" do @profile.abilities = [:foo, :bar] - warrior = stub_everything - warrior.expects(:add_abilities).with(:foo, :bar) + warrior = double.as_null_object + expect(warrior).to receive(:add_abilities).with(:foo, :bar) @level.setup_warrior(warrior) end it "should setup warrior with profile name" do @profile.warrior_name = "Joe" - warrior = stub_everything - warrior.expects(:name=).with("Joe") + warrior = double.as_null_object + expect(warrior).to receive(:name=).with("Joe") @level.setup_warrior(warrior) end describe "playing" do before(:each) do - @level.stubs(:load_level) + allow(@level).to receive(:load_level) end it "should load level once when playing multiple turns" do - @level.expects(:load_level) + expect(@level).to receive(:load_level) @level.play(2) end it "should call prepare_turn and play_turn on each object specified number of times" do object = RubyWarrior::Units::Base.new - object.expects(:prepare_turn).times(2) - object.expects(:perform_turn).times(2) + expect(object).to receive(:prepare_turn).exactly(2).times + expect(object).to receive(:perform_turn).exactly(2).times @floor.add(object, 0, 0, :north) @level.play(2) end it "should return immediately when passed" do object = RubyWarrior::Units::Base.new - object.expects(:turn).times(0) + expect(object).not_to receive(:turn) @floor.add(object, 0, 0, :north) - @level.stubs(:passed?).returns(true) + allow(@level).to receive(:passed?).and_return(true) @level.play(2) end @@ -146,20 +146,20 @@ describe "tallying points" do before(:each) do - @warrior = stub(:score => 0, :abilities => {}) - @level.stubs(:warrior).returns(@warrior) - @level.floor.stubs(:other_units).returns([stub]) + @warrior = double(:score => 0, :abilities => {}) + allow(@level).to receive(:warrior).and_return(@warrior) + allow(@level.floor).to receive(:other_units).and_return([double]) end it "should add warrior score to profile" do - @warrior.stubs(:score).returns(30) + allow(@warrior).to receive(:score).and_return(30) @level.tally_points expect(@profile.score).to eq(30) end it "should add warrior score to profile for epic mode" do @profile.enable_epic_mode - @warrior.stubs(:score).returns(30) + allow(@warrior).to receive(:score).and_return(30) @level.tally_points expect(@profile.current_epic_score).to eq(30) end @@ -167,7 +167,7 @@ it "should add level grade percent to profile for epic mode" do @level.ace_score = 100 @profile.enable_epic_mode - @warrior.stubs(:score).returns(30) + allow(@warrior).to receive(:score).and_return(30) @level.tally_points expect(@profile.current_epic_grades).to eq({ 1 => 0.3 }) end @@ -175,13 +175,13 @@ it "should not add level grade if ace score is not set" do @level.ace_score = nil @profile.enable_epic_mode - @warrior.stubs(:score).returns(30) + allow(@warrior).to receive(:score).and_return(30) @level.tally_points expect(@profile.current_epic_grades).to eq({}) end it "should apply warrior abilities to profile" do - @warrior.stubs(:abilities).returns({:foo => nil, :bar => nil}) + allow(@warrior).to receive(:abilities).and_return({:foo => nil, :bar => nil}) @level.tally_points expect(@profile.abilities.to_set).to eq([:foo, :bar].to_set) end @@ -193,8 +193,8 @@ end it "should give 20% bonus when no other units left" do - @level.floor.stubs(:other_units).returns([]) - @warrior.stubs(:score).returns(10) + allow(@level.floor).to receive(:other_units).and_return([]) + allow(@warrior).to receive(:score).and_return(10) @level.time_bonus = 10 @level.tally_points expect(@profile.score).to eq(24) diff --git a/spec/ruby_warrior/profile_spec.rb b/spec/ruby_warrior/profile_spec.rb index b3ef1e75..4521e2b1 100644 --- a/spec/ruby_warrior/profile_spec.rb +++ b/spec/ruby_warrior/profile_spec.rb @@ -36,9 +36,9 @@ it "load should read file, decode and set player path" do profile = 'profile' - profile.expects(:player_path=).with('path/to') - File.expects(:read).with('path/to/.profile').returns('encoded_profile') - RubyWarrior::Profile.expects(:decode).with('encoded_profile').returns(profile) + expect(profile).to receive(:player_path=).with('path/to') + expect(File).to receive(:read).with('path/to/.profile').and_return('encoded_profile') + expect(RubyWarrior::Profile).to receive(:decode).with('encoded_profile').and_return(profile) expect(RubyWarrior::Profile.load('path/to/.profile')).to eq(profile) end @@ -129,10 +129,10 @@ end it "save should write file with encoded profile" do - file = stub - file.expects(:write).with('encoded_profile') - File.expects(:open).with(@profile.player_path + '/.profile', 'w').yields(file) - @profile.expects(:encode).returns('encoded_profile') + file = double + expect(file).to receive(:write).with('encoded_profile') + expect(File).to receive(:open).with(@profile.player_path + '/.profile', 'w').and_yield(file) + expect(@profile).to receive(:encode).and_return('encoded_profile') @profile.save end @@ -164,7 +164,7 @@ end it "should load tower from path" do - RubyWarrior::Tower.expects(:new).with('tower').returns('tower') + expect(RubyWarrior::Tower).to receive(:new).with('tower').and_return('tower') expect(@profile.tower).to eq('tower') end end diff --git a/spec/ruby_warrior/turn_spec.rb b/spec/ruby_warrior/turn_spec.rb index 3b441a78..81fa64f0 100644 --- a/spec/ruby_warrior/turn_spec.rb +++ b/spec/ruby_warrior/turn_spec.rb @@ -29,8 +29,8 @@ describe "with senses" do before(:each) do @feel = RubyWarrior::Abilities::Feel.new(Object.new) - @feel.stubs(:space).returns(Object.new) - @feel.stubs(:space).with(:backward).returns(Object.new) + allow(@feel).to receive(:space).and_return(Object.new) + allow(@feel).to receive(:space).with(:backward).and_return(Object.new) @turn = RubyWarrior::Turn.new({:feel => @feel}) end diff --git a/spec/ruby_warrior/ui_spec.rb b/spec/ruby_warrior/ui_spec.rb index d5379b38..47407dd8 100644 --- a/spec/ruby_warrior/ui_spec.rb +++ b/spec/ruby_warrior/ui_spec.rb @@ -39,22 +39,22 @@ end it "should ask for yes/no and return true when yes" do - @ui.expects(:request).with('foo? [yn] ').returns('y') + expect(@ui).to receive(:request).with('foo? [yn] ').and_return('y') expect(@ui.ask("foo?")).to eq(true) end it "should ask for yes/no and return false when no" do - @ui.stubs(:request).returns('n') + allow(@ui).to receive(:request).and_return('n') expect(@ui.ask("foo?")).to eq(false) end it "should ask for yes/no and return false for any input" do - @ui.stubs(:request).returns('aklhasdf') + allow(@ui).to receive(:request).and_return('aklhasdf') expect(@ui.ask("foo?")).to eq(false) end it "should present multiple options and return selected one" do - @ui.expects(:request).with(includes('item')).returns('2') + expect(@ui).to receive(:request).with(include('item')).and_return('2') expect(@ui.choose('item', [:foo, :bar, :test])).to eq(:bar) expect(@out.string).to include('[1] foo') expect(@out.string).to include('[2] bar') @@ -62,15 +62,15 @@ end it "choose should accept array as option" do - @ui.stubs(:request).returns('3') + allow(@ui).to receive(:request).and_return('3') expect(@ui.choose('item', [:foo, :bar, [:tower, 'easy']])).to eq(:tower) expect(@out.string).to include('[3] easy') end it "choose should return option without prompt if only one item" do - @ui.expects(:puts).never - @ui.expects(:gets).never - @ui.stubs(:request).returns('3') + expect(@ui).to receive(:puts).never + expect(@ui).to receive(:gets).never + allow(@ui).to receive(:request).and_return('3') expect(@ui.choose('item', [:foo])).to eq(:foo) end @@ -80,14 +80,14 @@ it "should delay after puts when specified" do @config.delay = 1.3 - @ui.expects(:puts).with("foo") - @ui.expects(:sleep).with(1.3) + expect(@ui).to receive(:puts).with("foo") + expect(@ui).to receive(:sleep).with(1.3) @ui.puts_with_delay("foo") end it "should not delay puts when delay isn't specified" do - @ui.expects(:puts).with("foo") - @ui.expects(:sleep).never + expect(@ui).to receive(:puts).with("foo") + expect(@ui).to receive(:sleep).never @ui.puts_with_delay("foo") end end diff --git a/spec/ruby_warrior/units/base_spec.rb b/spec/ruby_warrior/units/base_spec.rb index 09affe32..3247fe12 100644 --- a/spec/ruby_warrior/units/base_spec.rb +++ b/spec/ruby_warrior/units/base_spec.rb @@ -15,7 +15,7 @@ end it "should consider itself alive with position" do - @unit.position = stub + @unit.position = double expect(@unit).to be_alive end @@ -28,12 +28,12 @@ end it "should default health to max health" do - @unit.stubs(:max_health).returns(10) + allow(@unit).to receive(:max_health).and_return(10) expect(@unit.health).to eq(10) end it "should subtract health when taking damage" do - @unit.stubs(:max_health).returns(10) + allow(@unit).to receive(:max_health).and_return(10) @unit.take_damage(3) expect(@unit.health).to eq(7) end @@ -43,14 +43,14 @@ end it "should set position to nil when running out of health" do - @unit.position = stub - @unit.stubs(:max_health).returns(10) + @unit.position = double + allow(@unit).to receive(:max_health).and_return(10) @unit.take_damage(10) expect(@unit.position).to be_nil end it "should print out line with name when speaking" do - RubyWarrior::UI.expects(:puts_with_delay).with("Base foo") + expect(RubyWarrior::UI).to receive(:puts_with_delay).with("Base foo") @unit.say "foo" end @@ -60,27 +60,27 @@ end it "should prepare turn by calling play_turn with next turn object" do - @unit.stubs(:next_turn).returns('next_turn') - @unit.expects(:play_turn).with('next_turn') + allow(@unit).to receive(:next_turn).and_return('next_turn') + expect(@unit).to receive(:play_turn).with('next_turn') @unit.prepare_turn end it "should perform action when calling perform on turn" do - @unit.position = stub - RubyWarrior::Abilities::Walk.any_instance.expects(:perform).with(:backward) + @unit.position = double + expect_any_instance_of(RubyWarrior::Abilities::Walk).to receive(:perform).with(:backward) @unit.add_abilities(:walk!) - turn = stub(:action => [:walk!, :backward]) - @unit.stubs(:next_turn).returns(turn) + turn = double(:action => [:walk!, :backward]) + allow(@unit).to receive(:next_turn).and_return(turn) @unit.prepare_turn @unit.perform_turn end it "should not perform action when dead (no position)" do @unit.position = nil - RubyWarrior::Abilities::Walk.any_instance.stubs(:perform).raises("action should not be called") + allow_any_instance_of(RubyWarrior::Abilities::Walk).to receive(:perform).and_raise("action should not be called") @unit.add_abilities(:walk!) - turn = stub(:action => [:walk!, :backward]) - @unit.stubs(:next_turn).returns(turn) + turn = double(:action => [:walk!, :backward]) + allow(@unit).to receive(:next_turn).and_return(turn) @unit.prepare_turn @unit.perform_turn end @@ -91,13 +91,13 @@ end it "should pass abilities to new turn when calling next_turn" do - RubyWarrior::Turn.expects(:new).with({:walk! => nil, :attack! => nil, :feel => nil}).returns('turn') - @unit.stubs(:abilities).returns(:walk! => nil, :attack! => nil, :feel => nil) + expect(RubyWarrior::Turn).to receive(:new).with(:walk! => nil, :attack! => nil, :feel => nil).and_return('turn') + allow(@unit).to receive(:abilities).and_return(:walk! => nil, :attack! => nil, :feel => nil) expect(@unit.next_turn).to eq('turn') end it "should add ability" do - RubyWarrior::Abilities::Walk.expects(:new).with(@unit).returns('walk') + expect(RubyWarrior::Abilities::Walk).to receive(:new).with(@unit).and_return('walk') @unit.add_abilities(:walk!) expect(@unit.abilities).to eq({ :walk! => 'walk' }) end @@ -107,7 +107,7 @@ end it "should be released from bonds when taking damage" do - @unit.stubs(:max_health).returns(10) + allow(@unit).to receive(:max_health).and_return(10) @unit.bind expect(@unit).to be_bound @unit.take_damage(2) @@ -121,12 +121,12 @@ end it "should not perform action when bound" do - @unit.position = stub + @unit.position = double @unit.bind - RubyWarrior::Abilities::Walk.any_instance.stubs(:perform).raises("action should not be called") + allow_any_instance_of(RubyWarrior::Abilities::Walk).to receive(:perform).and_raise("action should not be called") @unit.add_abilities(:walk!) - turn = stub(:action => [:walk!, :backward]) - @unit.stubs(:next_turn).returns(turn) + turn = double(:action => [:walk!, :backward]) + allow(@unit).to receive(:next_turn).and_return(turn) @unit.prepare_turn @unit.perform_turn end diff --git a/spec/ruby_warrior/units/golem_spec.rb b/spec/ruby_warrior/units/golem_spec.rb index 54c53dd1..76f290bf 100644 --- a/spec/ruby_warrior/units/golem_spec.rb +++ b/spec/ruby_warrior/units/golem_spec.rb @@ -7,7 +7,7 @@ it "should execute turn proc when playing turn" do proc = Object.new - proc.expects(:call).with(:turn) + expect(proc).to receive(:call).with(:turn) @golem.turn = proc @golem.play_turn(:turn) end diff --git a/spec/ruby_warrior/units/warrior_spec.rb b/spec/ruby_warrior/units/warrior_spec.rb index 9691fe73..ff8f1fa0 100644 --- a/spec/ruby_warrior/units/warrior_spec.rb +++ b/spec/ruby_warrior/units/warrior_spec.rb @@ -33,14 +33,14 @@ def turn(warrior) end it "should call player.play_turn and pass turn to player" do - player = stub - player.expects(:play_turn).with('turn') - @warrior.stubs(:player).returns(player) + player = double + expect(player).to receive(:play_turn).with('turn') + allow(@warrior).to receive(:player).and_return(player) @warrior.play_turn('turn') end it "should call Player.new the first time loading player, and return same object next time" do - Player.expects(:new).returns('player').times(1) + expect(Player).to receive(:new).and_return('player').once 2.times do expect(@warrior.player).to eq('player') end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6c9171b5..2f11d2a4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,7 +3,6 @@ require File.dirname(__FILE__) + '/../lib/ruby_warrior' RSpec.configure do |config| - config.mock_with :mocha config.before(:each) do RubyWarrior::Config.reset end From aedb326ea6ddf0d83f705be53409bf57cfed41c3 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Wed, 27 Mar 2024 09:22:07 -0700 Subject: [PATCH 24/32] Ignore .tool-versions --- .gitignore | 3 ++- .tool-versions | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 .tool-versions diff --git a/.gitignore b/.gitignore index 66c46dce..08411b82 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ rubywarrior/* *.gem .project .buildpath -Gemfile.lock \ No newline at end of file +.tool-versions +Gemfile.lock diff --git a/.tool-versions b/.tool-versions deleted file mode 100644 index 3294aeda..00000000 --- a/.tool-versions +++ /dev/null @@ -1 +0,0 @@ -ruby 3.3.0 From 45df5bbd77467e79e1ad747cb2f738139af205cf Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Wed, 27 Mar 2024 09:20:53 -0700 Subject: [PATCH 25/32] Add GitHub CI workflow --- .github/workflows/ci.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..0b9a2bfa --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,28 @@ +name: Ruby Warrior CI +on: + push: + branches: + - 'master' + pull_request: + branches: + - '*' +jobs: + test: + name: Ruby ${{ matrix.ruby }} + runs-on: ubuntu-20.04 + strategy: + matrix: + ruby: + - '3.3' + - '3.2' + - '3.1' + - '3.0' + - '2.7' + fail-fast: false + steps: + - uses: actions/checkout@v4 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + bundler-cache: true + - run: bundle exec rake From 3fc6654ab1f2d47b4fb80d857c7a711ff2ab33ac Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Wed, 27 Mar 2024 09:28:18 -0700 Subject: [PATCH 26/32] Fix spec failing due to hash of options passed to RubyWarrior::Turn mock --- spec/ruby_warrior/units/base_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/ruby_warrior/units/base_spec.rb b/spec/ruby_warrior/units/base_spec.rb index 3247fe12..be1a282b 100644 --- a/spec/ruby_warrior/units/base_spec.rb +++ b/spec/ruby_warrior/units/base_spec.rb @@ -91,8 +91,8 @@ end it "should pass abilities to new turn when calling next_turn" do - expect(RubyWarrior::Turn).to receive(:new).with(:walk! => nil, :attack! => nil, :feel => nil).and_return('turn') - allow(@unit).to receive(:abilities).and_return(:walk! => nil, :attack! => nil, :feel => nil) + expect(RubyWarrior::Turn).to receive(:new).with({:walk! => nil, :attack! => nil, :feel => nil}).and_return('turn') + allow(@unit).to receive(:abilities).and_return({:walk! => nil, :attack! => nil, :feel => nil}) expect(@unit.next_turn).to eq('turn') end From c4edb5ecd2cda663de5a4e5409b28701856b55cc Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Wed, 27 Mar 2024 09:30:27 -0700 Subject: [PATCH 27/32] Pass array to cucumber_opts to avoid warning --- Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rakefile b/Rakefile index 666c4a75..e5530594 100644 --- a/Rakefile +++ b/Rakefile @@ -10,7 +10,7 @@ RSpec::Core::RakeTask.new(:spec) do |t| end Cucumber::Rake::Task.new(:features) do |t| - t.cucumber_opts = "features --format progress" + t.cucumber_opts = %w[features --format progress] end task :default => [:spec, :features] From aa2097138a8efb7587818556e3861b7f45c889c1 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 11:55:17 -0700 Subject: [PATCH 28/32] Add syntax_tree gem --- .streerc | 3 +++ Gemfile | 1 + 2 files changed, 4 insertions(+) create mode 100644 .streerc diff --git a/.streerc b/.streerc new file mode 100644 index 00000000..37d03e08 --- /dev/null +++ b/.streerc @@ -0,0 +1,3 @@ +--print-width=100 +--plugins=plugin/trailing_comma +--ignore-files='rubywarrior/*' diff --git a/Gemfile b/Gemfile index d1fc2fca..cf11b005 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,7 @@ source "https://rubygems.org/" gem 'base64' +gem 'syntax_tree' group :test do gem 'rake' From f64a6e1b1c5e1d93e07a7f8b446e059bd46db527 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Tue, 26 Mar 2024 12:02:35 -0700 Subject: [PATCH 29/32] Add bin/format script to format Ruby code with syntax tree --- bin/format | 2 ++ 1 file changed, 2 insertions(+) create mode 100755 bin/format diff --git a/bin/format b/bin/format new file mode 100755 index 00000000..4263ca0e --- /dev/null +++ b/bin/format @@ -0,0 +1,2 @@ +#!/bin/sh +bundle exec stree write '**/*.rb' bin/rubywarrior Gemfile Rakefile From 34d7a332f78bd1f65259a587f3b2bfd8eb8c39e5 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Wed, 27 Mar 2024 09:35:49 -0700 Subject: [PATCH 30/32] Format all Ruby code using Syntax Tree --- Gemfile | 10 +- Rakefile | 20 ++-- bin/rubywarrior | 2 +- .../step_definitions/interaction_steps.rb | 12 +-- features/support/env.rb | 14 +-- features/support/mockio.rb | 14 +-- lib/ruby_warrior.rb | 82 ++++++++--------- lib/ruby_warrior/abilities/attack.rb | 4 +- lib/ruby_warrior/abilities/base.rb | 26 +++--- lib/ruby_warrior/abilities/bind.rb | 2 +- lib/ruby_warrior/abilities/detonate.rb | 8 +- lib/ruby_warrior/abilities/direction_of.rb | 2 +- .../abilities/direction_of_stairs.rb | 2 +- lib/ruby_warrior/abilities/distance_of.rb | 2 +- lib/ruby_warrior/abilities/explode.rb | 10 +- lib/ruby_warrior/abilities/feel.rb | 2 +- lib/ruby_warrior/abilities/form.rb | 4 +- lib/ruby_warrior/abilities/health.rb | 2 +- lib/ruby_warrior/abilities/listen.rb | 6 +- lib/ruby_warrior/abilities/look.rb | 6 +- lib/ruby_warrior/abilities/pivot.rb | 6 +- lib/ruby_warrior/abilities/rescue.rb | 2 +- lib/ruby_warrior/abilities/rest.rb | 6 +- lib/ruby_warrior/abilities/shoot.rb | 4 +- lib/ruby_warrior/abilities/walk.rb | 2 +- lib/ruby_warrior/config.rb | 8 +- lib/ruby_warrior/core_additions.rb | 12 +-- lib/ruby_warrior/floor.rb | 32 +++---- lib/ruby_warrior/game.rb | 92 +++++++++---------- lib/ruby_warrior/level.rb | 71 +++++++------- lib/ruby_warrior/level_loader.rb | 24 ++--- lib/ruby_warrior/player_generator.rb | 30 +++--- lib/ruby_warrior/position.rb | 44 +++++---- lib/ruby_warrior/profile.rb | 75 ++++++++------- lib/ruby_warrior/runner.rb | 29 ++++-- lib/ruby_warrior/space.rb | 30 +++--- lib/ruby_warrior/tower.rb | 6 +- lib/ruby_warrior/turn.rb | 10 +- lib/ruby_warrior/ui.rb | 22 ++--- lib/ruby_warrior/units/archer.rb | 26 +++--- lib/ruby_warrior/units/base.rb | 44 +++++---- lib/ruby_warrior/units/captive.rb | 4 +- lib/ruby_warrior/units/golem.rb | 6 +- lib/ruby_warrior/units/sludge.rb | 10 +- lib/ruby_warrior/units/thick_sludge.rb | 2 +- lib/ruby_warrior/units/warrior.rb | 28 +++--- lib/ruby_warrior/units/wizard.rb | 26 +++--- spec/ruby_warrior/abilities/attack_spec.rb | 8 +- spec/ruby_warrior/abilities/base_spec.rb | 12 +-- spec/ruby_warrior/abilities/bind_spec.rb | 6 +- .../abilities/direction_of_spec.rb | 4 +- .../abilities/direction_of_stairs_spec.rb | 4 +- .../abilities/distance_of_spec.rb | 4 +- spec/ruby_warrior/abilities/explode_spec.rb | 2 +- spec/ruby_warrior/abilities/feel_spec.rb | 6 +- spec/ruby_warrior/abilities/form_spec.rb | 2 +- spec/ruby_warrior/abilities/health_spec.rb | 2 +- spec/ruby_warrior/abilities/listen_spec.rb | 2 +- spec/ruby_warrior/abilities/look_spec.rb | 4 +- spec/ruby_warrior/abilities/pivot_spec.rb | 8 +- spec/ruby_warrior/abilities/rescue_spec.rb | 8 +- spec/ruby_warrior/abilities/rest_spec.rb | 2 +- spec/ruby_warrior/abilities/shoot_spec.rb | 12 ++- spec/ruby_warrior/abilities/throw_spec.rb | 2 +- spec/ruby_warrior/abilities/walk_spec.rb | 8 +- spec/ruby_warrior/core_additions_spec.rb | 2 +- spec/ruby_warrior/floor_spec.rb | 2 +- spec/ruby_warrior/game_spec.rb | 60 ++++++------ spec/ruby_warrior/level_loader_spec.rb | 2 +- spec/ruby_warrior/level_spec.rb | 44 ++++----- spec/ruby_warrior/player_generator_spec.rb | 2 +- spec/ruby_warrior/position_spec.rb | 6 +- spec/ruby_warrior/profile_spec.rb | 47 +++++----- spec/ruby_warrior/space_spec.rb | 18 ++-- spec/ruby_warrior/tower_spec.rb | 8 +- spec/ruby_warrior/turn_spec.rb | 10 +- spec/ruby_warrior/ui_spec.rb | 30 +++--- spec/ruby_warrior/units/archer_spec.rb | 8 +- spec/ruby_warrior/units/base_spec.rb | 40 ++++---- spec/ruby_warrior/units/captive_spec.rb | 6 +- spec/ruby_warrior/units/golem_spec.rb | 6 +- spec/ruby_warrior/units/sludge_spec.rb | 6 +- spec/ruby_warrior/units/thick_sludge_spec.rb | 6 +- spec/ruby_warrior/units/warrior_spec.rb | 18 ++-- spec/ruby_warrior/units/wizard_spec.rb | 8 +- spec/spec_helper.rb | 10 +- towers/beginner/level_001.rb | 1 - 87 files changed, 645 insertions(+), 672 deletions(-) diff --git a/Gemfile b/Gemfile index cf11b005..5b9d00fa 100644 --- a/Gemfile +++ b/Gemfile @@ -1,10 +1,10 @@ source "https://rubygems.org/" -gem 'base64' -gem 'syntax_tree' +gem "base64" +gem "syntax_tree" group :test do - gem 'rake' - gem 'rspec', '~> 3.13.0' - gem 'cucumber' + gem "rake" + gem "rspec", "~> 3.13.0" + gem "cucumber" end diff --git a/Rakefile b/Rakefile index e5530594..4b129f11 100644 --- a/Rakefile +++ b/Rakefile @@ -1,16 +1,12 @@ -require 'rake' -require 'rspec' -require 'rspec/core/rake_task' -require 'cucumber' -require 'cucumber/rake/task' +require "rake" +require "rspec" +require "rspec/core/rake_task" +require "cucumber" +require "cucumber/rake/task" desc "Run specs" -RSpec::Core::RakeTask.new(:spec) do |t| - t.pattern = "spec/**/*_spec.rb" -end +RSpec::Core::RakeTask.new(:spec) { |t| t.pattern = "spec/**/*_spec.rb" } -Cucumber::Rake::Task.new(:features) do |t| - t.cucumber_opts = %w[features --format progress] -end +Cucumber::Rake::Task.new(:features) { |t| t.cucumber_opts = %w[features --format progress] } -task :default => [:spec, :features] +task default: %i[spec features] diff --git a/bin/rubywarrior b/bin/rubywarrior index 062ee7a6..9b9a4ff2 100755 --- a/bin/rubywarrior +++ b/bin/rubywarrior @@ -1,5 +1,5 @@ #!/usr/bin/env ruby -require_relative '../lib/ruby_warrior' +require_relative "../lib/ruby_warrior" runner = RubyWarrior::Runner.new(ARGV, STDIN, STDOUT) runner.run diff --git a/features/step_definitions/interaction_steps.rb b/features/step_definitions/interaction_steps.rb index 784c0831..b2975876 100644 --- a/features/step_definitions/interaction_steps.rb +++ b/features/step_definitions/interaction_steps.rb @@ -1,5 +1,5 @@ Given /^a profile named "([^\"]*)" on "([^\"]*)"$/ do |name, tower| - step 'I run rubywarrior' + step "I run rubywarrior" step 'I answer "y" to "create one?"' step 'I choose "' + tower + '" for "tower"' step 'I answer "' + name + '" to "name"' @@ -27,9 +27,7 @@ When /^I run rubywarrior with options "([^\"]*)"$/ do |options| RubyWarrior::Config.reset @io = MockIO.new - @io.start do |io| - RubyWarrior::Runner.new(options.split, io, io).run - end + @io.start { |io| RubyWarrior::Runner.new(options.split, io, io).run } end When /^I answer "([^\"]*)" to "([^\"]*)"$/ do |answer, question| @@ -40,11 +38,7 @@ When /^I choose "([^\"]*)" for "([^\"]*)"$/ do |choice, phrase| answer = nil content = @io.gets_until_include(phrase) - content.split("\n").each do |line| - if line.include?(choice) && line =~ /\[(\d)\]/ - answer = $1 - end - end + content.split("\n").each { |line| answer = $1 if line.include?(choice) && line =~ /\[(\d)\]/ } if answer @io.puts(answer) else diff --git a/features/support/env.rb b/features/support/env.rb index 97914c01..6204e7c6 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -1,12 +1,8 @@ -require 'cucumber' -require 'rspec' +require "cucumber" +require "rspec" -require File.dirname(__FILE__) + '/../../lib/ruby_warrior' +require File.dirname(__FILE__) + "/../../lib/ruby_warrior" -Before do - RubyWarrior::Config.reset -end +Before { RubyWarrior::Config.reset } -After do - FileUtils.rm_rf "towers/short" -end +After { FileUtils.rm_rf "towers/short" } diff --git a/features/support/mockio.rb b/features/support/mockio.rb index d664d463..1bc7ee3d 100644 --- a/features/support/mockio.rb +++ b/features/support/mockio.rb @@ -1,12 +1,12 @@ class MockIO class Timeout < Exception end - + def initialize(receiver = nil) @buffer = "" @receiver = receiver || MockIO.new(self) end - + def gets 1000.times do |n| sleep 0.01 if n > 800 # throttle if it seems to be taking a while @@ -18,7 +18,7 @@ def gets end raise Timeout, "MockIO Timeout: No content was received for gets." end - + def gets_until_include(phrase) content = gets while !content.include?(phrase) @@ -30,20 +30,20 @@ def gets_until_include(phrase) end content end - + # TODO make this thread safe def puts(str) @receiver << "#{str}\n" end - + def print(str) @receiver << str.to_s end - + def <<(str) @buffer << str end - + def start main_thread = Thread.current Thread.new do diff --git a/lib/ruby_warrior.rb b/lib/ruby_warrior.rb index 0943c93d..da2bcabf 100644 --- a/lib/ruby_warrior.rb +++ b/lib/ruby_warrior.rb @@ -1,47 +1,47 @@ $: << File.dirname(__FILE__) -require 'set' +require "set" -require 'ruby_warrior/core_additions' +require "ruby_warrior/core_additions" -require 'ruby_warrior/runner' -require 'ruby_warrior/game' -require 'ruby_warrior/profile' -require 'ruby_warrior/ui' -require 'ruby_warrior/config' -require 'ruby_warrior/player_generator' -require 'ruby_warrior/level_loader' -require 'ruby_warrior/tower' -require 'ruby_warrior/level' -require 'ruby_warrior/turn' -require 'ruby_warrior/floor' -require 'ruby_warrior/space' -require 'ruby_warrior/position' +require "ruby_warrior/runner" +require "ruby_warrior/game" +require "ruby_warrior/profile" +require "ruby_warrior/ui" +require "ruby_warrior/config" +require "ruby_warrior/player_generator" +require "ruby_warrior/level_loader" +require "ruby_warrior/tower" +require "ruby_warrior/level" +require "ruby_warrior/turn" +require "ruby_warrior/floor" +require "ruby_warrior/space" +require "ruby_warrior/position" -require 'ruby_warrior/units/base' -require 'ruby_warrior/units/warrior' -require 'ruby_warrior/units/sludge' -require 'ruby_warrior/units/archer' -require 'ruby_warrior/units/thick_sludge' -require 'ruby_warrior/units/captive' -require 'ruby_warrior/units/wizard' -require 'ruby_warrior/units/golem' +require "ruby_warrior/units/base" +require "ruby_warrior/units/warrior" +require "ruby_warrior/units/sludge" +require "ruby_warrior/units/archer" +require "ruby_warrior/units/thick_sludge" +require "ruby_warrior/units/captive" +require "ruby_warrior/units/wizard" +require "ruby_warrior/units/golem" -require 'ruby_warrior/abilities/base' -require 'ruby_warrior/abilities/walk' -require 'ruby_warrior/abilities/attack' -require 'ruby_warrior/abilities/feel' -require 'ruby_warrior/abilities/rest' -require 'ruby_warrior/abilities/health' -require 'ruby_warrior/abilities/look' -require 'ruby_warrior/abilities/shoot' -require 'ruby_warrior/abilities/rescue' -require 'ruby_warrior/abilities/pivot' -require 'ruby_warrior/abilities/distance_of' -require 'ruby_warrior/abilities/bind' -require 'ruby_warrior/abilities/listen' -require 'ruby_warrior/abilities/direction_of_stairs' -require 'ruby_warrior/abilities/direction_of' -require 'ruby_warrior/abilities/explode' -require 'ruby_warrior/abilities/detonate' -require 'ruby_warrior/abilities/form' +require "ruby_warrior/abilities/base" +require "ruby_warrior/abilities/walk" +require "ruby_warrior/abilities/attack" +require "ruby_warrior/abilities/feel" +require "ruby_warrior/abilities/rest" +require "ruby_warrior/abilities/health" +require "ruby_warrior/abilities/look" +require "ruby_warrior/abilities/shoot" +require "ruby_warrior/abilities/rescue" +require "ruby_warrior/abilities/pivot" +require "ruby_warrior/abilities/distance_of" +require "ruby_warrior/abilities/bind" +require "ruby_warrior/abilities/listen" +require "ruby_warrior/abilities/direction_of_stairs" +require "ruby_warrior/abilities/direction_of" +require "ruby_warrior/abilities/explode" +require "ruby_warrior/abilities/detonate" +require "ruby_warrior/abilities/form" diff --git a/lib/ruby_warrior/abilities/attack.rb b/lib/ruby_warrior/abilities/attack.rb index 6b4ef096..454f2e75 100644 --- a/lib/ruby_warrior/abilities/attack.rb +++ b/lib/ruby_warrior/abilities/attack.rb @@ -4,14 +4,14 @@ class Attack < Base def description "Attacks a unit in given direction (forward by default)." end - + def perform(direction = :forward) verify_direction(direction) receiver = unit(direction) if receiver @unit.say "attacks #{direction} and hits #{receiver}" if direction == :backward - power = (@unit.attack_power/2.0).ceil + power = (@unit.attack_power / 2.0).ceil else power = @unit.attack_power end diff --git a/lib/ruby_warrior/abilities/base.rb b/lib/ruby_warrior/abilities/base.rb index 04b03cf5..f1c38430 100644 --- a/lib/ruby_warrior/abilities/base.rb +++ b/lib/ruby_warrior/abilities/base.rb @@ -4,36 +4,40 @@ class Base def initialize(unit) @unit = unit end - + def offset(direction, forward = 1, right = 0) case direction - when :forward then [forward, -right] - when :backward then [-forward, right] - when :right then [right, forward] - when :left then [-right, -forward] + when :forward + [forward, -right] + when :backward + [-forward, right] + when :right + [right, forward] + when :left + [-right, -forward] end end - + def space(direction, forward = 1, right = 0) @unit.position.relative_space(*offset(direction, forward, right)) end - + def unit(direction, forward = 1, right = 0) space(direction, forward, right).unit end - + def damage(receiver, amount) receiver.take_damage(amount) @unit.earn_points(receiver.max_health) unless receiver.alive? end - + def description end - + def pass_turn # callback which is triggered every turn end - + def verify_direction(direction) unless Position::RELATIVE_DIRECTIONS.include? direction raise "Unknown direction #{direction.inspect}. Should be :forward, :backward, :left or :right." diff --git a/lib/ruby_warrior/abilities/bind.rb b/lib/ruby_warrior/abilities/bind.rb index f418c18c..24dd1f32 100644 --- a/lib/ruby_warrior/abilities/bind.rb +++ b/lib/ruby_warrior/abilities/bind.rb @@ -4,7 +4,7 @@ class Bind < Base def description "Binds a unit in given direction to keep him from moving (forward by default)." end - + def perform(direction = :forward) verify_direction(direction) receiver = unit(direction) diff --git a/lib/ruby_warrior/abilities/detonate.rb b/lib/ruby_warrior/abilities/detonate.rb index 54cd0150..e1a6fd7a 100644 --- a/lib/ruby_warrior/abilities/detonate.rb +++ b/lib/ruby_warrior/abilities/detonate.rb @@ -4,18 +4,16 @@ class Detonate < Base def description "Detonate a bomb in a given direction (forward by default) which damages that space and surrounding 4 spaces (including yourself)." end - + def perform(direction = :forward) verify_direction(direction) if @unit.position @unit.say "detonates a bomb #{direction} launching a deadly explosion." bomb(direction, 1, 0, 8) - [[1, 1], [1, -1], [2, 0], [0, 0]].each do |x, y| - bomb(direction, x, y, 4) - end + [[1, 1], [1, -1], [2, 0], [0, 0]].each { |x, y| bomb(direction, x, y, 4) } end end - + def bomb(direction, x, y, damage_amount) if @unit.position receiver = space(direction, x, y).unit diff --git a/lib/ruby_warrior/abilities/direction_of.rb b/lib/ruby_warrior/abilities/direction_of.rb index 6fb827d4..74c12614 100644 --- a/lib/ruby_warrior/abilities/direction_of.rb +++ b/lib/ruby_warrior/abilities/direction_of.rb @@ -4,7 +4,7 @@ class DirectionOf < Base def description "Pass a Space as an argument, and the direction (:left, :right, :forward, :backward) to that space will be returned." end - + def perform(space) @unit.position.relative_direction_of(space) end diff --git a/lib/ruby_warrior/abilities/direction_of_stairs.rb b/lib/ruby_warrior/abilities/direction_of_stairs.rb index dd7f0c1b..221828bb 100644 --- a/lib/ruby_warrior/abilities/direction_of_stairs.rb +++ b/lib/ruby_warrior/abilities/direction_of_stairs.rb @@ -4,7 +4,7 @@ class DirectionOfStairs < Base def description "Returns the direction (:left, :right, :forward, :backward) the stairs are from your location." end - + def perform @unit.position.relative_direction_of_stairs end diff --git a/lib/ruby_warrior/abilities/distance_of.rb b/lib/ruby_warrior/abilities/distance_of.rb index ea5788a2..d9a4578a 100644 --- a/lib/ruby_warrior/abilities/distance_of.rb +++ b/lib/ruby_warrior/abilities/distance_of.rb @@ -4,7 +4,7 @@ class DistanceOf < Base def description "Pass a Space as an argument, and it will return an integer representing the distance to that space." end - + def perform(space) @unit.position.distance_of(space) end diff --git a/lib/ruby_warrior/abilities/explode.rb b/lib/ruby_warrior/abilities/explode.rb index c6749aa8..0fe28237 100644 --- a/lib/ruby_warrior/abilities/explode.rb +++ b/lib/ruby_warrior/abilities/explode.rb @@ -2,20 +2,18 @@ module RubyWarrior module Abilities class Explode < Base attr_accessor :time - + def description "Kills you and all surrounding units. You probably don't want to do this intentionally." end - + def perform if @unit.position @unit.say "explodes, collapsing the ceiling and damanging every unit." - @unit.position.floor.units.map do |unit| - unit.take_damage(100) - end + @unit.position.floor.units.map { |unit| unit.take_damage(100) } end end - + def pass_turn if @time && @unit.position @unit.say "is ticking" diff --git a/lib/ruby_warrior/abilities/feel.rb b/lib/ruby_warrior/abilities/feel.rb index 8a7dcb54..aeb5c75f 100644 --- a/lib/ruby_warrior/abilities/feel.rb +++ b/lib/ruby_warrior/abilities/feel.rb @@ -4,7 +4,7 @@ class Feel < Base def description "Returns a Space for the given direction (forward by default)." end - + def perform(direction = :forward) verify_direction(direction) space(direction) diff --git a/lib/ruby_warrior/abilities/form.rb b/lib/ruby_warrior/abilities/form.rb index 5b3d1b89..d17a6064 100644 --- a/lib/ruby_warrior/abilities/form.rb +++ b/lib/ruby_warrior/abilities/form.rb @@ -4,12 +4,12 @@ class Form < Base def description "Forms a golem in given direction taking half of invoker's health. The passed block is executed for each golem turn." end - + def perform(direction = :forward, &block) verify_direction(direction) if space(direction).empty? x, y = @unit.position.translate_offset(*offset(direction)) - health = (@unit.health/2.0).floor + health = (@unit.health / 2.0).floor golem = @unit.base_golem golem.max_health = health golem.turn = block diff --git a/lib/ruby_warrior/abilities/health.rb b/lib/ruby_warrior/abilities/health.rb index 5023b2f3..0bfe2368 100644 --- a/lib/ruby_warrior/abilities/health.rb +++ b/lib/ruby_warrior/abilities/health.rb @@ -4,7 +4,7 @@ class Health < Base def description "Returns an integer representing your health." end - + def perform @unit.health end diff --git a/lib/ruby_warrior/abilities/listen.rb b/lib/ruby_warrior/abilities/listen.rb index 43ec16c0..5ade2561 100644 --- a/lib/ruby_warrior/abilities/listen.rb +++ b/lib/ruby_warrior/abilities/listen.rb @@ -4,11 +4,9 @@ class Listen < Base def description "Returns an array of all spaces which have units in them." end - + def perform - @unit.position.floor.units.map do |unit| - unit.position.space unless unit == @unit - end.compact + @unit.position.floor.units.map { |unit| unit.position.space unless unit == @unit }.compact end end end diff --git a/lib/ruby_warrior/abilities/look.rb b/lib/ruby_warrior/abilities/look.rb index 1b224d85..7565b2f4 100644 --- a/lib/ruby_warrior/abilities/look.rb +++ b/lib/ruby_warrior/abilities/look.rb @@ -4,12 +4,10 @@ class Look < Base def description "Returns an array of up to three Spaces in the given direction (forward by default)." end - + def perform(direction = :forward) verify_direction(direction) - [1, 2, 3].map do |amount| - space(direction, amount) - end + [1, 2, 3].map { |amount| space(direction, amount) } end end end diff --git a/lib/ruby_warrior/abilities/pivot.rb b/lib/ruby_warrior/abilities/pivot.rb index 2fc2f551..9c0c0a6b 100644 --- a/lib/ruby_warrior/abilities/pivot.rb +++ b/lib/ruby_warrior/abilities/pivot.rb @@ -1,12 +1,12 @@ module RubyWarrior module Abilities class Pivot < Base - ROTATION_DIRECTIONS = [:forward, :right, :backward, :left] - + ROTATION_DIRECTIONS = %i[forward right backward left] + def description "Rotate :left, :right or :backward (default)" end - + def perform(direction = :backward) verify_direction(direction) @unit.position.rotate(ROTATION_DIRECTIONS.index(direction)) diff --git a/lib/ruby_warrior/abilities/rescue.rb b/lib/ruby_warrior/abilities/rescue.rb index e454d903..32f2749a 100644 --- a/lib/ruby_warrior/abilities/rescue.rb +++ b/lib/ruby_warrior/abilities/rescue.rb @@ -4,7 +4,7 @@ class Rescue < Base def description "Rescue a captive from his chains (earning 20 points) in given direction (forward by default)." end - + def perform(direction = :forward) verify_direction(direction) if space(direction).captive? diff --git a/lib/ruby_warrior/abilities/rest.rb b/lib/ruby_warrior/abilities/rest.rb index b07d19ec..7d6b3d60 100644 --- a/lib/ruby_warrior/abilities/rest.rb +++ b/lib/ruby_warrior/abilities/rest.rb @@ -4,11 +4,11 @@ class Rest < Base def description "Gain 10% of max health back, but do nothing more." end - + def perform if @unit.health < @unit.max_health - amount = (@unit.max_health*0.1).round - amount = @unit.max_health-@unit.health if (@unit.health + amount) > @unit.max_health + amount = (@unit.max_health * 0.1).round + amount = @unit.max_health - @unit.health if (@unit.health + amount) > @unit.max_health @unit.health += amount @unit.say "receives #{amount} health from resting, up to #{@unit.health} health" else diff --git a/lib/ruby_warrior/abilities/shoot.rb b/lib/ruby_warrior/abilities/shoot.rb index 363daad5..54849107 100644 --- a/lib/ruby_warrior/abilities/shoot.rb +++ b/lib/ruby_warrior/abilities/shoot.rb @@ -4,7 +4,7 @@ class Shoot < Base def description "Shoot your bow & arrow in given direction (forward by default)." end - + def perform(direction = :forward) verify_direction(direction) receiver = multi_unit(direction, 1..3).compact.first @@ -15,7 +15,7 @@ def perform(direction = :forward) @unit.say "shoots and hits nothing" end end - + def multi_unit(direction, range) range.map { |n| unit(direction, n) } end diff --git a/lib/ruby_warrior/abilities/walk.rb b/lib/ruby_warrior/abilities/walk.rb index 6af18714..51d712bd 100644 --- a/lib/ruby_warrior/abilities/walk.rb +++ b/lib/ruby_warrior/abilities/walk.rb @@ -4,7 +4,7 @@ class Walk < Base def description "Move in the given direction (forward by default)." end - + def perform(direction = :forward) verify_direction(direction) if @unit.position diff --git a/lib/ruby_warrior/config.rb b/lib/ruby_warrior/config.rb index c8d3e88f..2742335d 100644 --- a/lib/ruby_warrior/config.rb +++ b/lib/ruby_warrior/config.rb @@ -3,17 +3,17 @@ class Config class << self attr_accessor :delay, :in_stream, :out_stream, :practice_level attr_writer :path_prefix, :skip_input - + def path_prefix @path_prefix || "." end - + def skip_input? @skip_input end - + def reset - [:@path_prefix, :@skip_input, :@delay, :@in_stream, :@out_stream, :@practice_level].each do |i| + %i[@path_prefix @skip_input @delay @in_stream @out_stream @practice_level].each do |i| remove_instance_variable(i) if instance_variable_defined?(i) end end diff --git a/lib/ruby_warrior/core_additions.rb b/lib/ruby_warrior/core_additions.rb index 672bd6b3..48c8f305 100644 --- a/lib/ruby_warrior/core_additions.rb +++ b/lib/ruby_warrior/core_additions.rb @@ -2,19 +2,19 @@ class String def camelize gsub(/(?:^|_)(.)/) { $1.upcase } end - + def constantize unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!" end - + Object.module_eval("::#{$1}", __FILE__, __LINE__) end - + def underscore - gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase + gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').tr("-", "_").downcase end - + def humanize gsub(/_/, " ").capitalize end @@ -22,7 +22,7 @@ def humanize def titleize underscore.humanize.gsub(/\b('?[a-z])/) { $1.capitalize } end - + def hard_wrap(width = 80) gsub(/(.{1,#{width}})(\s+|$)/, "\\1\n").strip end diff --git a/lib/ruby_warrior/floor.rb b/lib/ruby_warrior/floor.rb index e5278f57..5ef530b1 100644 --- a/lib/ruby_warrior/floor.rb +++ b/lib/ruby_warrior/floor.rb @@ -2,64 +2,60 @@ module RubyWarrior class Floor attr_accessor :width, :height, :grid attr_reader :stairs_location - + def initialize @width = 0 @height = 0 @units = [] @stairs_location = [-1, -1] end - + def add(unit, x, y, direction = nil) @units << unit unit.position = Position.new(self, x, y, direction) end - + def place_stairs(x, y) @stairs_location = [x, y] end - + def stairs_space space(*@stairs_location) end - + def units @units.reject { |u| u.position.nil? } end - + def other_units units.reject { |u| u.kind_of? Units::Warrior } end - + def get(x, y) - units.detect do |unit| - unit.position.at?(x, y) - end + units.detect { |unit| unit.position.at?(x, y) } end - + def space(x, y) Space.new(self, x, y) end - + def out_of_bounds?(x, y) - x < 0 || y < 0 || x > @width-1 || y > @height-1 + x < 0 || y < 0 || x > @width - 1 || y > @height - 1 end - + def character rows = [] rows << " " + ("-" * @width) @height.times do |y| row = "|" - @width.times do |x| - row << space(x, y).character - end + @width.times { |x| row << space(x, y).character } row << "|" rows << row end rows << " " + ("-" * @width) rows.join("\n") + "\n" end - + def unique_units unique_units = [] units.each do |unit| diff --git a/lib/ruby_warrior/game.rb b/lib/ruby_warrior/game.rb index 582cc42e..9e159c11 100644 --- a/lib/ruby_warrior/game.rb +++ b/lib/ruby_warrior/game.rb @@ -1,38 +1,33 @@ module RubyWarrior class Game - def start UI.puts "Welcome to Ruby Warrior" - - if File.exist?(Config.path_prefix + '/.profile') - @profile = Profile.load(Config.path_prefix + '/.profile') + + if File.exist?(Config.path_prefix + "/.profile") + @profile = Profile.load(Config.path_prefix + "/.profile") else - if File.exist?(Config.path_prefix + '/ruby-warrior') - FileUtils.mv(Config.path_prefix + '/ruby-warrior', Config.path_prefix + '/rubywarrior') + if File.exist?(Config.path_prefix + "/ruby-warrior") + FileUtils.mv(Config.path_prefix + "/ruby-warrior", Config.path_prefix + "/rubywarrior") end - make_game_directory unless File.exist?(Config.path_prefix + '/rubywarrior') + make_game_directory unless File.exist?(Config.path_prefix + "/rubywarrior") end - + if profile.epic? - if profile.level_after_epic? - go_back_to_normal_mode - else - play_epic_mode - end + profile.level_after_epic? ? go_back_to_normal_mode : play_epic_mode else play_normal_mode end end - + def make_game_directory if UI.ask("No rubywarrior directory found, would you like to create one?") - Dir.mkdir(Config.path_prefix + '/rubywarrior') + Dir.mkdir(Config.path_prefix + "/rubywarrior") else UI.puts "Unable to continue without directory." exit end end - + def play_epic_mode Config.delay /= 2 if Config.delay # speed up UI since we're going to be doing a lot here profile.current_epic_score = 0 @@ -51,7 +46,7 @@ def play_epic_mode profile.save # saves the score for epic mode end end - + def play_normal_mode if Config.practice_level UI.puts "Unable to practice level while not in epic mode, remove -l option." @@ -64,7 +59,7 @@ def play_normal_mode end end end - + def play_current_level continue = true current_level.load_player @@ -86,17 +81,24 @@ def play_current_level else continue = false UI.puts "Sorry, you failed level #{current_level.number}. Change your script and try again." - if !Config.skip_input? && current_level.clue && UI.ask("Would you like to read the additional clues for this level?") + if !Config.skip_input? && current_level.clue && + UI.ask("Would you like to read the additional clues for this level?") UI.puts current_level.clue.hard_wrap end end continue end - + def request_next_level if Config.skip_input? UI.puts "Staying on current level. Remove --skip option to continue on to the next level." - elsif (next_level.exists? ? UI.ask("Would you like to continue on to the next level?") : UI.ask("Would you like to continue on to epic mode?")) + elsif ( + if next_level.exists? + UI.ask("Would you like to continue on to the next level?") + else + UI.ask("Would you like to continue on to epic mode?") + end + ) if next_level.exists? prepare_next_level UI.puts "See the updated README in the rubywarrior/#{profile.directory_name} directory." @@ -108,70 +110,67 @@ def request_next_level UI.puts "Staying on current level." end end - + def prepare_next_level next_level.generate_player_files profile.level_number += 1 profile.save # this saves score and new abilities too end - + def prepare_epic_mode profile.enable_epic_mode profile.level_number = 0 profile.save # this saves score too end - + def go_back_to_normal_mode profile.enable_normal_mode prepare_next_level UI.puts "Another level has been added since you started epic, going back to normal mode." UI.puts "See the updated README in the rubywarrior/#{profile.directory_name} directory." end - - + # profiles - + def profiles profile_paths.map { |profile| Profile.load(profile) } end - + def profile_paths - Dir[Config.path_prefix + '/rubywarrior/**/.profile'] + Dir[Config.path_prefix + "/rubywarrior/**/.profile"] end - + def profile @profile ||= choose_profile end - + def new_profile profile = Profile.new - profile.tower_path = UI.choose('tower', towers).path - profile.warrior_name = UI.request('Enter a name for your warrior: ') + profile.tower_path = UI.choose("tower", towers).path + profile.warrior_name = UI.request("Enter a name for your warrior: ") profile end - - + # towers - + def towers tower_paths.map { |path| Tower.new(path) } end - + def tower_paths - Dir[File.expand_path('../../../towers/*', __FILE__)] + Dir[File.expand_path("../../../towers/*", __FILE__)] end - - + # levels - + def current_level @current_level ||= profile.current_level end - + def next_level @next_level ||= profile.next_level end - + def final_report if profile.calculate_average_grade && !Config.practice_level report = "" @@ -183,11 +182,11 @@ def final_report report end end - + private - + def choose_profile # REFACTORME - profile = UI.choose('profile', profiles + [[:new, 'New Profile']]) + profile = UI.choose("profile", profiles + [[:new, "New Profile"]]) if profile == :new profile = new_profile if profiles.any? { |p| p.player_path == profile.player_path } @@ -205,6 +204,5 @@ def choose_profile # REFACTORME profile end end - end end diff --git a/lib/ruby_warrior/level.rb b/lib/ruby_warrior/level.rb index 174b070e..98f084de 100644 --- a/lib/ruby_warrior/level.rb +++ b/lib/ruby_warrior/level.rb @@ -2,54 +2,59 @@ module RubyWarrior class Level attr_reader :profile, :number attr_accessor :description, :tip, :clue, :warrior, :floor, :time_bonus, :ace_score - + def self.grade_letter(percent) - if percent >= 1.0 then "S" - elsif percent >= 0.9 then "A" - elsif percent >= 0.8 then "B" - elsif percent >= 0.7 then "C" - elsif percent >= 0.6 then "D" - else "F" + if percent >= 1.0 + "S" + elsif percent >= 0.9 + "A" + elsif percent >= 0.8 + "B" + elsif percent >= 0.7 + "C" + elsif percent >= 0.6 + "D" + else + "F" end end - + def initialize(profile, number) @profile = profile @number = number @time_bonus = 0 end - + def player_path @profile.player_path end - + def load_path File.join( - File.expand_path('../../../towers/', __FILE__), - File.basename(@profile.tower_path) + "/level_" + - @number.to_s.rjust(3, '0') + ".rb" + File.expand_path("../../../towers/", __FILE__), + File.basename(@profile.tower_path) + "/level_" + @number.to_s.rjust(3, "0") + ".rb", ) end - + def load_level LevelLoader.new(self).instance_eval(File.read(load_path)) end - + def load_player $: << player_path - load 'player.rb' + load "player.rb" end - + def generate_player_files load_level PlayerGenerator.new(self).generate end - + def play(turns = 1000) load_level turns.times do |n| return if passed? || failed? - UI.puts "- turn #{n+1} -" + UI.puts "- turn #{n + 1} -" UI.print @floor.character @floor.units.each { |unit| unit.prepare_turn } @floor.units.each { |unit| unit.perform_turn } @@ -57,21 +62,21 @@ def play(turns = 1000) @time_bonus -= 1 if @time_bonus > 0 end end - + def tally_points score = 0 - + UI.puts "Level Score: #{warrior.score}" score += warrior.score - + UI.puts "Time Bonus: #{time_bonus}" score += @time_bonus - + if floor.other_units.empty? UI.puts "Clear Bonus: #{clear_bonus}" score += clear_bonus end - + if @profile.epic? UI.puts "Level Grade: #{grade_for(score)}" if grade_for(score) UI.puts "Total Score: " + score_calculation(@profile.current_epic_score, score) @@ -83,17 +88,15 @@ def tally_points @profile.abilities = warrior.abilities.keys end end - + def grade_for(score) - if ace_score - self.class.grade_letter(score / ace_score.to_f) - end + self.class.grade_letter(score / ace_score.to_f) if ace_score end - + def clear_bonus - ((warrior.score + time_bonus)*0.2).round + ((warrior.score + time_bonus) * 0.2).round end - + def score_calculation(current_score, addition) if current_score.zero? addition.to_s @@ -101,15 +104,15 @@ def score_calculation(current_score, addition) "#{current_score} + #{addition} = #{current_score + addition}" end end - + def passed? @floor.stairs_space.warrior? end - + def failed? !@floor.units.include?(warrior) end - + def exists? File.exist? load_path end diff --git a/lib/ruby_warrior/level_loader.rb b/lib/ruby_warrior/level_loader.rb index 322037ea..5f2f9f3f 100644 --- a/lib/ruby_warrior/level_loader.rb +++ b/lib/ruby_warrior/level_loader.rb @@ -5,51 +5,51 @@ def initialize(level) @level = level @level.floor = @floor end - + def description(desc) @level.description = desc end - + def tip(tip) @level.tip = tip end - + def clue(clue) @level.clue = clue end - + def time_bonus(bonus) @level.time_bonus = bonus end - + def ace_score(score) @level.ace_score = score end - + def size(width, height) @floor.width = width @floor.height = height end - + def stairs(x, y) @floor.place_stairs(x, y) end - + def unit(unit, x, y, facing = :north) unit = unit_to_constant(unit).new unless unit.kind_of? Units::Base @floor.add(unit, x, y, facing) yield unit if block_given? unit end - + def warrior(*args, &block) @level.setup_warrior unit(Units::Warrior.new, *args, &block) end - + private - + def unit_to_constant(name) - camel = name.to_s.split('_').map { |s| s.capitalize }.join + camel = name.to_s.split("_").map { |s| s.capitalize }.join eval("Units::#{camel}") end end diff --git a/lib/ruby_warrior/player_generator.rb b/lib/ruby_warrior/player_generator.rb index 3c482853..c079506c 100644 --- a/lib/ruby_warrior/player_generator.rb +++ b/lib/ruby_warrior/player_generator.rb @@ -1,41 +1,41 @@ -require 'rubygems' -require 'fileutils' -require 'erb' +require "rubygems" +require "fileutils" +require "erb" module RubyWarrior class PlayerGenerator def initialize(level) @level = level end - + def level @level end - + def previous_level - @previous_level ||= Level.new(level.profile, level.number-1) + @previous_level ||= Level.new(level.profile, level.number - 1) end - + # TODO refactor and test this method def generate if level.number == 1 FileUtils.mkdir_p(level.player_path) unless File.exist? level.player_path - FileUtils.cp(templates_path + '/player.rb', level.player_path) + FileUtils.cp(templates_path + "/player.rb", level.player_path) end - - File.open(level.player_path + '/README', 'w') do |f| - f.write read_template(templates_path + '/README.erb') + + File.open(level.player_path + "/README", "w") do |f| + f.write read_template(templates_path + "/README.erb") end end - + def templates_path File.expand_path("../../../templates", __FILE__) end - + private - + def read_template(path) - ERB.new(File.read(path), trim_mode: '-').result(binding) + ERB.new(File.read(path), trim_mode: "-").result(binding) end end end diff --git a/lib/ruby_warrior/position.rb b/lib/ruby_warrior/position.rb index f5aab757..f4ab3d9e 100644 --- a/lib/ruby_warrior/position.rb +++ b/lib/ruby_warrior/position.rb @@ -1,59 +1,59 @@ module RubyWarrior class Position attr_reader :floor - DIRECTIONS = [:north, :east, :south, :west] - RELATIVE_DIRECTIONS = [:forward, :right, :backward, :left] - + DIRECTIONS = %i[north east south west] + RELATIVE_DIRECTIONS = %i[forward right backward left] + def initialize(floor, x, y, direction = nil) @floor = floor @x = x @y = y @direction_index = DIRECTIONS.index(direction || :north) end - + def at?(x, y) @x == x && @y == y end - + def direction DIRECTIONS[@direction_index] end - + def rotate(amount) @direction_index += amount @direction_index -= 4 while @direction_index > 3 @direction_index += 4 while @direction_index < 0 end - + def relative_space(forward, right = 0) @floor.space(*translate_offset(forward, right)) end - + def space @floor.space(@x, @y) end - + def move(forward, right = 0) @x, @y = *translate_offset(forward, right) end - + def distance_from_stairs distance_of(@floor.stairs_space) end - + def distance_of(space) x, y = *space.location (@x - x).abs + (@y - y).abs end - + def relative_direction_of_stairs relative_direction_of(@floor.stairs_space) end - + def relative_direction_of(space) relative_direction(direction_of(space)) end - + def direction_of(space) space_x, space_y = *space.location if (@x - space_x).abs > (@y - space_y).abs @@ -62,20 +62,24 @@ def direction_of(space) space_y > @y ? :south : :north end end - + def relative_direction(direction) offset = DIRECTIONS.index(direction) - @direction_index offset -= 4 while offset > 3 offset += 4 while offset < 0 RELATIVE_DIRECTIONS[offset] end - + def translate_offset(forward, right) case direction - when :north then [@x + right, @y - forward] - when :east then [@x + forward, @y + right] - when :south then [@x - right, @y + forward] - when :west then [@x - forward, @y - right] + when :north + [@x + right, @y - forward] + when :east + [@x + forward, @y + right] + when :south + [@x - right, @y + forward] + when :west + [@x - forward, @y - right] end end end diff --git a/lib/ruby_warrior/profile.rb b/lib/ruby_warrior/profile.rb index 2eadcd4f..037a19ad 100644 --- a/lib/ruby_warrior/profile.rb +++ b/lib/ruby_warrior/profile.rb @@ -1,9 +1,19 @@ -require 'base64' +require "base64" module RubyWarrior class Profile - attr_accessor :score, :epic_score, :current_epic_score, :average_grade, :current_epic_grades, :abilities, :level_number, :last_level_number, :tower_path, :warrior_name, :player_path - + attr_accessor :score, + :epic_score, + :current_epic_score, + :average_grade, + :current_epic_grades, + :abilities, + :level_number, + :last_level_number, + :tower_path, + :warrior_name, + :player_path + def initialize @tower_path = nil @warrior_name = nil @@ -16,75 +26,76 @@ def initialize @level_number = 0 @last_level_number = nil end - + def encode Base64.encode64(Marshal.dump(self)) end - + def save update_epic_score @level_number = 0 if epic? - File.open(player_path + '/.profile', 'w') { |f| f.write(encode) } + File.open(player_path + "/.profile", "w") { |f| f.write(encode) } end - + def self.decode(str) Marshal.load(Base64.decode64(str)) end - + def self.load(path) player = decode(File.read(path)) player.player_path = File.dirname(path) player end - + def player_path @player_path || Config.path_prefix + "/rubywarrior/#{directory_name}" end - + def directory_name - [warrior_name.downcase.gsub(/[^a-z0-9]+/, '-'), tower.name].join('-') + [warrior_name.downcase.gsub(/[^a-z0-9]+/, "-"), tower.name].join("-") end - + def to_s if epic? - [warrior_name, tower.name, "first score #{score}", "epic score #{epic_score_with_grade}"].join(' - ') + [ + warrior_name, + tower.name, + "first score #{score}", + "epic score #{epic_score_with_grade}", + ].join(" - ") else - [warrior_name, tower.name, "level #{level_number}", "score #{score}"].join(' - ') + [warrior_name, tower.name, "level #{level_number}", "score #{score}"].join(" - ") end end - + def epic_score_with_grade - if average_grade - "#{epic_score} (#{Level.grade_letter(average_grade)})" - else - epic_score - end + average_grade ? "#{epic_score} (#{Level.grade_letter(average_grade)})" : epic_score end - + def tower Tower.new(File.basename @tower_path) end - + def current_level Level.new(self, level_number) end - + def next_level - Level.new(self, level_number+1) + Level.new(self, level_number + 1) end - + def add_abilities(*abilities) @abilities += abilities @abilities.uniq! end - + def enable_epic_mode @epic = true @epic_score ||= 0 @current_epic_score ||= 0 @last_level_number ||= @level_number end - + def enable_normal_mode @epic = false @epic_score = 0 @@ -94,22 +105,22 @@ def enable_normal_mode @level_number = @last_level_number @last_level_number = nil end - + def epic? @epic end - + def level_after_epic? - Level.new(self, last_level_number+1).exists? if last_level_number + Level.new(self, last_level_number + 1).exists? if last_level_number end - + def update_epic_score if @current_epic_score > @epic_score @epic_score = @current_epic_score @average_grade = calculate_average_grade end end - + def calculate_average_grade if @current_epic_grades.size > 0 sum = @current_epic_grades.values.inject(0) { |sum, value| sum + value } diff --git a/lib/ruby_warrior/runner.rb b/lib/ruby_warrior/runner.rb index d2cb6c68..4c1bf7ad 100644 --- a/lib/ruby_warrior/runner.rb +++ b/lib/ruby_warrior/runner.rb @@ -1,4 +1,4 @@ -require 'optparse' +require "optparse" module RubyWarrior class Runner @@ -8,7 +8,7 @@ def initialize(arguments, stdin, stdout) @stdout = stdout @game = RubyWarrior::Game.new end - + def run Config.in_stream = @stdin Config.out_stream = @stdout @@ -16,17 +16,26 @@ def run parse_options @game.start end - + private - + def parse_options - options = OptionParser.new + options = OptionParser.new options.banner = "Usage: rubywarrior [options]" - options.on('-d', '--directory DIR', "Run under given directory") { |dir| Config.path_prefix = dir } - options.on('-l', '--level LEVEL', "Practice level on epic") { |level| Config.practice_level = level.to_i } - options.on('-s', '--skip', "Skip user input") { Config.skip_input = true } - options.on('-t', '--time SECONDS', "Delay each turn by seconds") { |seconds| Config.delay = seconds.to_f } - options.on('-h', '--help', "Show this message") { puts(options); exit } + options.on("-d", "--directory DIR", "Run under given directory") do |dir| + Config.path_prefix = dir + end + options.on("-l", "--level LEVEL", "Practice level on epic") do |level| + Config.practice_level = level.to_i + end + options.on("-s", "--skip", "Skip user input") { Config.skip_input = true } + options.on("-t", "--time SECONDS", "Delay each turn by seconds") do |seconds| + Config.delay = seconds.to_f + end + options.on("-h", "--help", "Show this message") do + puts(options) + exit + end options.parse!(@arguments) end end diff --git a/lib/ruby_warrior/space.rb b/lib/ruby_warrior/space.rb index 0d36b7ed..c9c2f745 100644 --- a/lib/ruby_warrior/space.rb +++ b/lib/ruby_warrior/space.rb @@ -3,51 +3,51 @@ class Space def initialize(floor, x, y) @floor, @x, @y = floor, x, y end - + def wall? @floor.out_of_bounds? @x, @y end - + def warrior? unit.kind_of? Units::Warrior end - + def golem? unit.kind_of? Units::Golem end - + def player? warrior? || golem? end - + def enemy? unit && !player? && !captive? end - + def captive? unit && unit.bound? end - + def empty? unit.nil? && !wall? end - + def stairs? @floor.stairs_location == location end - + def ticking? unit && unit.abilities.include?(:explode!) end - + def unit @floor.get(@x, @y) end - + def location [@x, @y] end - + def character if unit unit.character @@ -57,14 +57,14 @@ def character " " end end - + def to_s if unit unit.to_s elsif wall? - 'wall' + "wall" else - 'nothing' + "nothing" end end end diff --git a/lib/ruby_warrior/tower.rb b/lib/ruby_warrior/tower.rb index e4c55ea3..6fa3c67d 100644 --- a/lib/ruby_warrior/tower.rb +++ b/lib/ruby_warrior/tower.rb @@ -1,11 +1,11 @@ module RubyWarrior class Tower attr_reader :path - + def initialize(path) - @path = File.join(File.expand_path('../../../towers/', __FILE__), path) + @path = File.join(File.expand_path("../../../towers/", __FILE__), path) end - + def name File.basename(path) end diff --git a/lib/ruby_warrior/turn.rb b/lib/ruby_warrior/turn.rb index 411a9272..bcb962b9 100644 --- a/lib/ruby_warrior/turn.rb +++ b/lib/ruby_warrior/turn.rb @@ -1,11 +1,11 @@ module RubyWarrior class Turn attr_reader :action - + def initialize(abilities) @action = nil @senses = {} - + abilities.each do |name, sense| if name.to_s =~ /\!$/ add_action(name) @@ -14,9 +14,9 @@ def initialize(abilities) end end end - + private - + def add_action(action) instance_eval <<-EOS def #{action}(*args) @@ -25,7 +25,7 @@ def #{action}(*args) end EOS end - + def add_sense(name, sense) instance_eval <<-EOS def #{name}(*args) diff --git a/lib/ruby_warrior/ui.rb b/lib/ruby_warrior/ui.rb index a616d54c..7d389083 100644 --- a/lib/ruby_warrior/ui.rb +++ b/lib/ruby_warrior/ui.rb @@ -4,30 +4,30 @@ class << self def puts(msg) Config.out_stream.puts(msg) if Config.out_stream end - + def puts_with_delay(msg) result = puts(msg) sleep(Config.delay) if Config.delay result end - + def print(msg) Config.out_stream.print(msg) if Config.out_stream end - + def gets - Config.in_stream ? Config.in_stream.gets : '' + Config.in_stream ? Config.in_stream.gets : "" end - + def request(msg) print(msg) gets.chomp end - + def ask(msg) - request("#{msg} [yn] ") == 'y' + request("#{msg} [yn] ") == "y" end - + # REFACTORME def choose(item, options) if options.length == 1 @@ -35,13 +35,13 @@ def choose(item, options) else options.each_with_index do |option, i| if option.kind_of? Array - puts "[#{i+1}] #{option.last}" + puts "[#{i + 1}] #{option.last}" else - puts "[#{i+1}] #{option}" + puts "[#{i + 1}] #{option}" end end choice = request("Choose #{item} by typing the number: ") - response = options[choice.to_i-1] + response = options[choice.to_i - 1] end if response.kind_of? Array response.first diff --git a/lib/ruby_warrior/units/archer.rb b/lib/ruby_warrior/units/archer.rb index e2d9f0f7..a42f17fe 100644 --- a/lib/ruby_warrior/units/archer.rb +++ b/lib/ruby_warrior/units/archer.rb @@ -4,28 +4,30 @@ class Archer < Base def initialize add_abilities :shoot!, :look end - + def play_turn(turn) - [:forward, :left, :right].each do |direction| - turn.look(direction).each do |space| - if space.player? - turn.shoot!(direction) - return - elsif !space.empty? - break + %i[forward left right].each do |direction| + turn + .look(direction) + .each do |space| + if space.player? + turn.shoot!(direction) + return + elsif !space.empty? + break + end end - end end end - + def shoot_power 3 end - + def max_health 7 end - + def character "a" end diff --git a/lib/ruby_warrior/units/base.rb b/lib/ruby_warrior/units/base.rb index a7a2abd0..2fb6a343 100644 --- a/lib/ruby_warrior/units/base.rb +++ b/lib/ruby_warrior/units/base.rb @@ -3,22 +3,22 @@ module Units class Base attr_writer :health attr_accessor :position - + def attack_power 0 end - + def max_health 0 end - + def earn_points(points) end - + def health @health ||= max_health end - + def take_damage(amount) unbind if bound? if health @@ -30,68 +30,66 @@ def take_damage(amount) end end end - + def alive? !position.nil? end - + def bound? @bound end - + def unbind say "released from bonds" @bound = false end - + def bind @bound = true end - + def say(msg) UI.puts_with_delay "#{name} #{msg}" end - + def name - self.class.name.split('::').last.titleize + self.class.name.split("::").last.titleize end alias_method :to_s, :name - + def add_abilities(*new_abilities) new_abilities.each do |ability| - abilities[ability] = eval("Abilities::#{ability.to_s.sub('!', '').camelize}").new(self) # TODO use something similar to constantize here + abilities[ability] = eval("Abilities::#{ability.to_s.sub("!", "").camelize}").new(self) # TODO use something similar to constantize here end end - + def next_turn Turn.new(abilities) end - + def prepare_turn @current_turn = next_turn play_turn(@current_turn) end - + def perform_turn if @position - abilities.values.each do |ability| - ability.pass_turn - end + abilities.values.each { |ability| ability.pass_turn } if @current_turn.action && !bound? name, *args = @current_turn.action abilities[name].perform(*args) end end end - + def play_turn(turn) # to be overriden by subclass end - + def abilities @abilities ||= {} end - + def character "?" end diff --git a/lib/ruby_warrior/units/captive.rb b/lib/ruby_warrior/units/captive.rb index d9d510c1..c25657a3 100644 --- a/lib/ruby_warrior/units/captive.rb +++ b/lib/ruby_warrior/units/captive.rb @@ -4,11 +4,11 @@ class Captive < Base def initialize bind end - + def max_health 1 end - + def character "C" end diff --git a/lib/ruby_warrior/units/golem.rb b/lib/ruby_warrior/units/golem.rb index 889f50d8..880bad8b 100644 --- a/lib/ruby_warrior/units/golem.rb +++ b/lib/ruby_warrior/units/golem.rb @@ -3,15 +3,15 @@ module Units class Golem < Base attr_writer :turn attr_accessor :max_health - + def play_turn(turn) @turn.call(turn) if @turn end - + def attack_power 3 end - + def character "G" end diff --git a/lib/ruby_warrior/units/sludge.rb b/lib/ruby_warrior/units/sludge.rb index 070762ab..c53d4b85 100644 --- a/lib/ruby_warrior/units/sludge.rb +++ b/lib/ruby_warrior/units/sludge.rb @@ -4,24 +4,24 @@ class Sludge < Base def initialize add_abilities :attack!, :feel end - + def play_turn(turn) - [:forward, :left, :right, :backward].each do |direction| + %i[forward left right backward].each do |direction| if turn.feel(direction).player? turn.attack!(direction) return end end end - + def attack_power 3 end - + def max_health 12 end - + def character "s" end diff --git a/lib/ruby_warrior/units/thick_sludge.rb b/lib/ruby_warrior/units/thick_sludge.rb index 25f6eae2..d80fa41d 100644 --- a/lib/ruby_warrior/units/thick_sludge.rb +++ b/lib/ruby_warrior/units/thick_sludge.rb @@ -4,7 +4,7 @@ class ThickSludge < Sludge def max_health 24 end - + def character "S" end diff --git a/lib/ruby_warrior/units/warrior.rb b/lib/ruby_warrior/units/warrior.rb index 026959b6..5a045a24 100644 --- a/lib/ruby_warrior/units/warrior.rb +++ b/lib/ruby_warrior/units/warrior.rb @@ -3,37 +3,37 @@ module Units class Warrior < Base attr_writer :name attr_reader :score - + def initialize @score = 0 # TODO make score dynamic @golem_abilities = [] end - + def play_turn(turn) player.play_turn(turn) end - + def player @player ||= Player.new end - + def earn_points(points) @score += points say "earns #{points} points" end - + def attack_power 5 end - + def shoot_power 3 end - + def max_health 20 end - + def name if @name && !@name.empty? @name @@ -41,28 +41,28 @@ def name "Warrior" end end - + def to_s name end - + def character "@" end - + def perform_turn say "does nothing" if @current_turn.action.nil? super end - + def add_golem_abilities(*abilities) @golem_abilities += abilities end - + def has_golem? !@golem_abilities.empty? end - + def base_golem golem = Golem.new golem.add_abilities *@golem_abilities diff --git a/lib/ruby_warrior/units/wizard.rb b/lib/ruby_warrior/units/wizard.rb index 1870869c..a4c4d67c 100644 --- a/lib/ruby_warrior/units/wizard.rb +++ b/lib/ruby_warrior/units/wizard.rb @@ -4,28 +4,30 @@ class Wizard < Base def initialize add_abilities :shoot!, :look end - + def play_turn(turn) - [:forward, :left, :right].each do |direction| - turn.look(direction).each do |space| - if space.player? - turn.shoot!(direction) - return - elsif !space.empty? - break + %i[forward left right].each do |direction| + turn + .look(direction) + .each do |space| + if space.player? + turn.shoot!(direction) + return + elsif !space.empty? + break + end end - end end end - + def shoot_power 11 end - + def max_health 3 end - + def character "w" end diff --git a/spec/ruby_warrior/abilities/attack_spec.rb b/spec/ruby_warrior/abilities/attack_spec.rb index e207c02a..51de986e 100644 --- a/spec/ruby_warrior/abilities/attack_spec.rb +++ b/spec/ruby_warrior/abilities/attack_spec.rb @@ -1,8 +1,8 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Attack do before(:each) do - @attacker = double(:position => double, :attack_power => 3, :say => nil) + @attacker = double(position: double, attack_power: 3, say: nil) @attack = RubyWarrior::Abilities::Attack.new(@attacker) end @@ -26,14 +26,14 @@ end it "should award points when killing unit" do - receiver = double(:take_damage => nil, :max_health => 8, :alive? => false) + receiver = double(take_damage: nil, max_health: 8, alive?: false) allow(@attack).to receive(:unit).and_return(receiver) expect(@attacker).to receive(:earn_points).with(8) @attack.perform end it "should not award points when not killing unit" do - receiver = double(:max_health => 8, :alive? => true) + receiver = double(max_health: 8, alive?: true) expect(receiver).to receive(:take_damage) allow(@attack).to receive(:unit).and_return(receiver) expect(@attacker).to receive(:earn_points).never diff --git a/spec/ruby_warrior/abilities/base_spec.rb b/spec/ruby_warrior/abilities/base_spec.rb index 411b0b74..daf66fde 100644 --- a/spec/ruby_warrior/abilities/base_spec.rb +++ b/spec/ruby_warrior/abilities/base_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Base do before(:each) do @@ -22,8 +22,8 @@ end it "should fetch unit at given direction with distance" do - expect(@ability).to receive(:space).with(:right, 3, 1).and_return(double(:unit => 'unit')) - expect(@ability.unit(:right, 3, 1)).to eq('unit') + expect(@ability).to receive(:space).with(:right, 3, 1).and_return(double(unit: "unit")) + expect(@ability.unit(:right, 3, 1)).to eq("unit") end it "should have no description" do @@ -31,8 +31,8 @@ end it "should raise an exception if direction isn't recognized" do - expect { - @ability.verify_direction(:foo) - }.to raise_error("Unknown direction :foo. Should be :forward, :backward, :left or :right.") + expect { @ability.verify_direction(:foo) }.to raise_error( + "Unknown direction :foo. Should be :forward, :backward, :left or :right.", + ) end end diff --git a/spec/ruby_warrior/abilities/bind_spec.rb b/spec/ruby_warrior/abilities/bind_spec.rb index 5906dfe7..32922a24 100644 --- a/spec/ruby_warrior/abilities/bind_spec.rb +++ b/spec/ruby_warrior/abilities/bind_spec.rb @@ -1,9 +1,7 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Bind do - before(:each) do - @bind = RubyWarrior::Abilities::Bind.new(double(:say => nil)) - end + before(:each) { @bind = RubyWarrior::Abilities::Bind.new(double(say: nil)) } it "should bind recipient" do receiver = RubyWarrior::Units::Base.new diff --git a/spec/ruby_warrior/abilities/direction_of_spec.rb b/spec/ruby_warrior/abilities/direction_of_spec.rb index 075479d9..85d5436e 100644 --- a/spec/ruby_warrior/abilities/direction_of_spec.rb +++ b/spec/ruby_warrior/abilities/direction_of_spec.rb @@ -1,9 +1,9 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::DirectionOf do before(:each) do @position = double - @distance = RubyWarrior::Abilities::DirectionOf.new(double(:position => @position, :say => nil)) + @distance = RubyWarrior::Abilities::DirectionOf.new(double(position: @position, say: nil)) end it "should return relative direction of given space" do diff --git a/spec/ruby_warrior/abilities/direction_of_stairs_spec.rb b/spec/ruby_warrior/abilities/direction_of_stairs_spec.rb index 5265738c..eca9f966 100644 --- a/spec/ruby_warrior/abilities/direction_of_stairs_spec.rb +++ b/spec/ruby_warrior/abilities/direction_of_stairs_spec.rb @@ -1,9 +1,9 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::DirectionOfStairs do before(:each) do @position = double - @distance = RubyWarrior::Abilities::DirectionOfStairs.new(double(:position => @position, :say => nil)) + @distance = RubyWarrior::Abilities::DirectionOfStairs.new(double(position: @position, say: nil)) end it "should return relative direction of stairs" do diff --git a/spec/ruby_warrior/abilities/distance_of_spec.rb b/spec/ruby_warrior/abilities/distance_of_spec.rb index 219a9db9..2b76b1d6 100644 --- a/spec/ruby_warrior/abilities/distance_of_spec.rb +++ b/spec/ruby_warrior/abilities/distance_of_spec.rb @@ -1,9 +1,9 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::DistanceOf do before(:each) do @position = double - @distance = RubyWarrior::Abilities::DistanceOf.new(double(:position => @position, :say => nil)) + @distance = RubyWarrior::Abilities::DistanceOf.new(double(position: @position, say: nil)) end it "should return distance from stairs" do diff --git a/spec/ruby_warrior/abilities/explode_spec.rb b/spec/ruby_warrior/abilities/explode_spec.rb index e066635f..6d2a1d02 100644 --- a/spec/ruby_warrior/abilities/explode_spec.rb +++ b/spec/ruby_warrior/abilities/explode_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Explode do before(:each) do diff --git a/spec/ruby_warrior/abilities/feel_spec.rb b/spec/ruby_warrior/abilities/feel_spec.rb index 9f61b649..1f7b181a 100644 --- a/spec/ruby_warrior/abilities/feel_spec.rb +++ b/spec/ruby_warrior/abilities/feel_spec.rb @@ -1,11 +1,11 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Feel do before(:each) do - @unit = double(:position => double, :say => nil) + @unit = double(position: double, say: nil) @feel = RubyWarrior::Abilities::Feel.new(@unit) end - + it "should get object at position from offset" do expect(@unit.position).to receive(:relative_space).with(1, 0) @feel.perform(:forward) diff --git a/spec/ruby_warrior/abilities/form_spec.rb b/spec/ruby_warrior/abilities/form_spec.rb index e62cfc59..481706ba 100644 --- a/spec/ruby_warrior/abilities/form_spec.rb +++ b/spec/ruby_warrior/abilities/form_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Form do before(:each) do diff --git a/spec/ruby_warrior/abilities/health_spec.rb b/spec/ruby_warrior/abilities/health_spec.rb index 6091992f..0865ad39 100644 --- a/spec/ruby_warrior/abilities/health_spec.rb +++ b/spec/ruby_warrior/abilities/health_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Health do before(:each) do diff --git a/spec/ruby_warrior/abilities/listen_spec.rb b/spec/ruby_warrior/abilities/listen_spec.rb index c771e0ec..2975243a 100644 --- a/spec/ruby_warrior/abilities/listen_spec.rb +++ b/spec/ruby_warrior/abilities/listen_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Listen do before(:each) do diff --git a/spec/ruby_warrior/abilities/look_spec.rb b/spec/ruby_warrior/abilities/look_spec.rb index cd92335d..dd70fd00 100644 --- a/spec/ruby_warrior/abilities/look_spec.rb +++ b/spec/ruby_warrior/abilities/look_spec.rb @@ -1,8 +1,8 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Look do before(:each) do - @unit = double(:position => double, :say => nil) + @unit = double(position: double, say: nil) @feel = RubyWarrior::Abilities::Look.new(@unit) end diff --git a/spec/ruby_warrior/abilities/pivot_spec.rb b/spec/ruby_warrior/abilities/pivot_spec.rb index eee3ef80..a5eac665 100644 --- a/spec/ruby_warrior/abilities/pivot_spec.rb +++ b/spec/ruby_warrior/abilities/pivot_spec.rb @@ -1,16 +1,16 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Pivot do before(:each) do @position = double - @pivot = RubyWarrior::Abilities::Pivot.new(double(:position => @position, :say => nil)) + @pivot = RubyWarrior::Abilities::Pivot.new(double(position: @position, say: nil)) end - + it "should flip around when not passing arguments" do expect(@position).to receive(:rotate).with(2) @pivot.perform end - + it "should rotate 1 when pivoting right" do expect(@position).to receive(:rotate).with(1) @pivot.perform(:right) diff --git a/spec/ruby_warrior/abilities/rescue_spec.rb b/spec/ruby_warrior/abilities/rescue_spec.rb index f63afa9f..29f027a1 100644 --- a/spec/ruby_warrior/abilities/rescue_spec.rb +++ b/spec/ruby_warrior/abilities/rescue_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Rescue do before(:each) do @@ -9,7 +9,7 @@ it "should rescue captive" do captive = RubyWarrior::Units::Captive.new captive.position = double - expect(@rescue).to receive(:space).with(:forward).and_return(double(:captive? => true)) + expect(@rescue).to receive(:space).with(:forward).and_return(double(captive?: true)) expect(@rescue).to receive(:unit).with(:forward).and_return(captive) expect(@warrior).to receive(:earn_points).with(20) @rescue.perform @@ -19,7 +19,7 @@ it "should do nothing to other unit if not bound" do unit = RubyWarrior::Units::Base.new unit.position = double - expect(@rescue).to receive(:space).with(:forward).and_return(double(:captive? => false)) + expect(@rescue).to receive(:space).with(:forward).and_return(double(captive?: false)) expect(@rescue).to receive(:unit).with(:forward).never expect(@warrior).to receive(:earn_points).never @rescue.perform @@ -30,7 +30,7 @@ unit = RubyWarrior::Units::Base.new unit.bind unit.position = double - expect(@rescue).to receive(:space).with(:forward).and_return(double(:captive? => true)) + expect(@rescue).to receive(:space).with(:forward).and_return(double(captive?: true)) expect(@rescue).to receive(:unit).with(:forward).and_return(unit) expect(@warrior).to receive(:earn_points).never @rescue.perform diff --git a/spec/ruby_warrior/abilities/rest_spec.rb b/spec/ruby_warrior/abilities/rest_spec.rb index d1812aab..29e6ca3e 100644 --- a/spec/ruby_warrior/abilities/rest_spec.rb +++ b/spec/ruby_warrior/abilities/rest_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Rest do before(:each) do diff --git a/spec/ruby_warrior/abilities/shoot_spec.rb b/spec/ruby_warrior/abilities/shoot_spec.rb index 0a7cd246..e0a61a39 100644 --- a/spec/ruby_warrior/abilities/shoot_spec.rb +++ b/spec/ruby_warrior/abilities/shoot_spec.rb @@ -1,17 +1,19 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Shoot do before(:each) do - @shooter = double(:position => double, :shoot_power => 2, :say => nil) + @shooter = double(position: double, shoot_power: 2, say: nil) @shoot = RubyWarrior::Abilities::Shoot.new(@shooter) end it "should shoot only first unit" do - receiver = double(:alive? => true) + receiver = double(alive?: true) expect(receiver).to receive(:take_damage).with(2) - other = double(:alive? => true) + other = double(alive?: true) expect(other).to receive(:take_damage).never - expect(@shoot).to receive(:multi_unit).with(:forward, anything).and_return([nil, receiver, other, nil]) + expect(@shoot).to receive(:multi_unit).with(:forward, anything).and_return( + [nil, receiver, other, nil], + ) @shoot.perform end diff --git a/spec/ruby_warrior/abilities/throw_spec.rb b/spec/ruby_warrior/abilities/throw_spec.rb index e928dea2..fd18b2ba 100644 --- a/spec/ruby_warrior/abilities/throw_spec.rb +++ b/spec/ruby_warrior/abilities/throw_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Detonate do before(:each) do diff --git a/spec/ruby_warrior/abilities/walk_spec.rb b/spec/ruby_warrior/abilities/walk_spec.rb index 1309b46e..027e3c3f 100644 --- a/spec/ruby_warrior/abilities/walk_spec.rb +++ b/spec/ruby_warrior/abilities/walk_spec.rb @@ -1,10 +1,10 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Abilities::Walk do before(:each) do - @space = double(:empty? => true, :unit => nil) - @position = double(:relative_space => @space, :move => nil) - @walk = RubyWarrior::Abilities::Walk.new(double(:position => @position, :say => nil)) + @space = double(empty?: true, unit: nil) + @position = double(relative_space: @space, move: nil) + @walk = RubyWarrior::Abilities::Walk.new(double(position: @position, say: nil)) end it "should move position forward when calling perform" do diff --git a/spec/ruby_warrior/core_additions_spec.rb b/spec/ruby_warrior/core_additions_spec.rb index 6a368d17..7a19eb41 100644 --- a/spec/ruby_warrior/core_additions_spec.rb +++ b/spec/ruby_warrior/core_additions_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe String do it "should wrap text at white space when over a specific character length" do diff --git a/spec/ruby_warrior/floor_spec.rb b/spec/ruby_warrior/floor_spec.rb index 77b60f42..d8dafce3 100644 --- a/spec/ruby_warrior/floor_spec.rb +++ b/spec/ruby_warrior/floor_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Floor do describe "2x3" do diff --git a/spec/ruby_warrior/game_spec.rb b/spec/ruby_warrior/game_spec.rb index 2c59f35f..468a105a 100644 --- a/spec/ruby_warrior/game_spec.rb +++ b/spec/ruby_warrior/game_spec.rb @@ -1,31 +1,28 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Game do - before(:each) do - @game = RubyWarrior::Game.new - end + before(:each) { @game = RubyWarrior::Game.new } # GAME DIR it "should make game directory if player says so" do allow(RubyWarrior::UI).to receive(:ask).and_return(true) - expect(Dir).to receive(:mkdir).with('./rubywarrior') + expect(Dir).to receive(:mkdir).with("./rubywarrior") @game.make_game_directory end it "should not make game and exit if player says no" do allow(RubyWarrior::UI).to receive(:ask).and_return(false) - allow(Dir).to receive(:mkdir).and_raise('should not be called') + allow(Dir).to receive(:mkdir).and_raise("should not be called") expect { @game.make_game_directory }.to raise_error(SystemExit) end - # PROFILES it "should load profiles for each profile path" do - expect(RubyWarrior::Profile).to receive(:load).with('foo/.profile').and_return(1) - expect(RubyWarrior::Profile).to receive(:load).with('bar/.profile').and_return(2) - allow(@game).to receive(:profile_paths).and_return(['foo/.profile', 'bar/.profile']) + expect(RubyWarrior::Profile).to receive(:load).with("foo/.profile").and_return(1) + expect(RubyWarrior::Profile).to receive(:load).with("bar/.profile").and_return(2) + allow(@game).to receive(:profile_paths).and_return(%w[foo/.profile bar/.profile]) expect(@game.profiles).to eq([1, 2]) end @@ -36,61 +33,64 @@ it "should try to create profile when no profile paths are specified" do allow(@game).to receive(:profiles).and_return([]) - expect(@game).to receive(:new_profile).and_return('profile') - expect(@game.profile).to eq('profile') + expect(@game).to receive(:new_profile).and_return("profile") + expect(@game.profile).to eq("profile") end it "should ask a player to choose a profile if multiple profiles are available, but only once" do - expect(RubyWarrior::UI).to receive(:choose).with('profile', [:profile1, [:new, 'New Profile']]).and_return(:profile1) + expect(RubyWarrior::UI).to receive(:choose).with( + "profile", + [:profile1, [:new, "New Profile"]], + ).and_return(:profile1) allow(@game).to receive(:profiles).and_return([:profile1]) 2.times { expect(@game.profile).to eq(:profile1) } end it "should ask user to choose a tower when creating a new profile" do - allow(RubyWarrior::UI).to receive(:gets).and_return('') - allow(@game).to receive(:towers).and_return([:tower1, :tower2]) - expect(RubyWarrior::UI).to receive(:choose).with('tower', [:tower1, :tower2]).and_return(double(path: '/foo/bar')) + allow(RubyWarrior::UI).to receive(:gets).and_return("") + allow(@game).to receive(:towers).and_return(%i[tower1 tower2]) + expect(RubyWarrior::UI).to receive(:choose).with("tower", %i[tower1 tower2]).and_return( + double(path: "/foo/bar"), + ) @game.new_profile end it "should pass name and selected tower to profile" do profile = double - allow(RubyWarrior::UI).to receive(:choose).and_return(double(path: 'tower_path')) - allow(RubyWarrior::UI).to receive(:request).and_return('name') + allow(RubyWarrior::UI).to receive(:choose).and_return(double(path: "tower_path")) + allow(RubyWarrior::UI).to receive(:request).and_return("name") expect(RubyWarrior::Profile).to receive(:new).and_return(profile) - expect(profile).to receive(:tower_path=).with('tower_path') - expect(profile).to receive(:warrior_name=).with('name') + expect(profile).to receive(:tower_path=).with("tower_path") + expect(profile).to receive(:warrior_name=).with("name") expect(@game.new_profile).to eq(profile) end - # TOWERS it "load towers for each tower path" do - expect(RubyWarrior::Tower).to receive(:new).with('towers/foo').and_return(1) - expect(RubyWarrior::Tower).to receive(:new).with('towers/bar').and_return(2) - allow(@game).to receive(:tower_paths).and_return(['towers/foo', 'towers/bar']) + expect(RubyWarrior::Tower).to receive(:new).with("towers/foo").and_return(1) + expect(RubyWarrior::Tower).to receive(:new).with("towers/bar").and_return(2) + allow(@game).to receive(:tower_paths).and_return(%w[towers/foo towers/bar]) expect(@game.towers).to eq([1, 2]) end it "should find tower paths using Dir[] search" do - expect(Dir).to receive(:[]).with(File.expand_path('../../../towers/*', __FILE__)) + expect(Dir).to receive(:[]).with(File.expand_path("../../../towers/*", __FILE__)) @game.tower_paths end - # LEVEL it "should fetch current level from profile and cache it" do allow(@game).to receive(:profile).and_return(double) - expect(@game.profile).to receive(:current_level).and_return('foo') - 2.times { expect(@game.current_level).to eq('foo') } + expect(@game.profile).to receive(:current_level).and_return("foo") + 2.times { expect(@game.current_level).to eq("foo") } end it "should fetch next level from profile and cache it" do allow(@game).to receive(:profile).and_return(double) - expect(@game.profile).to receive(:next_level).and_return('bar') - 2.times { expect(@game.next_level).to eq('bar') } + expect(@game.profile).to receive(:next_level).and_return("bar") + 2.times { expect(@game.next_level).to eq("bar") } end it "should report final grade" do diff --git a/spec/ruby_warrior/level_loader_spec.rb b/spec/ruby_warrior/level_loader_spec.rb index 8b9cfb1c..90ae259f 100644 --- a/spec/ruby_warrior/level_loader_spec.rb +++ b/spec/ruby_warrior/level_loader_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::LevelLoader do describe "with profile" do diff --git a/spec/ruby_warrior/level_spec.rb b/spec/ruby_warrior/level_spec.rb index 10519975..b5b22b6c 100644 --- a/spec/ruby_warrior/level_spec.rb +++ b/spec/ruby_warrior/level_spec.rb @@ -1,5 +1,5 @@ -require 'spec_helper' -require 'set' +require "spec_helper" +require "set" describe RubyWarrior::Level do before(:each) do @@ -38,32 +38,32 @@ end it "should load file contents into level" do - allow(@level).to receive(:load_path).and_return('path/to/level.rb') - expect(File).to receive(:read).with('path/to/level.rb').and_return("description 'foo'") + allow(@level).to receive(:load_path).and_return("path/to/level.rb") + expect(File).to receive(:read).with("path/to/level.rb").and_return("description 'foo'") @level.load_level - expect(@level.description).to eq('foo') + expect(@level.description).to eq("foo") end it "should have a player path from profile" do - allow(@profile).to receive(:player_path).and_return('path/to/player') - expect(@level.player_path).to eq('path/to/player') + allow(@profile).to receive(:player_path).and_return("path/to/player") + expect(@level.player_path).to eq("path/to/player") end it "should have a load path from profile tower with level number in it" do - allow(@profile).to receive(:tower_path).and_return('path/to/tower') - expect(@level.load_path).to eq(File.expand_path('towers/tower/level_001.rb')) + allow(@profile).to receive(:tower_path).and_return("path/to/tower") + expect(@level.load_path).to eq(File.expand_path("towers/tower/level_001.rb")) end it "should exist if file exists" do - allow(@level).to receive(:load_path).and_return('/foo/bar') - expect(File).to receive(:exist?).with('/foo/bar').and_return(true) + allow(@level).to receive(:load_path).and_return("/foo/bar") + expect(File).to receive(:exist?).with("/foo/bar").and_return(true) expect(@level.exists?).to eq(true) end it "should load player and player path" do - allow(@level).to receive(:player_path).and_return('player/path') - expect($:).to receive(:<<).with('player/path') - expect(@level).to receive(:load).with('player.rb') + allow(@level).to receive(:player_path).and_return("player/path") + expect($:).to receive(:<<).with("player/path") + expect(@level).to receive(:load).with("player.rb") @level.load_player end @@ -76,7 +76,7 @@ end it "should setup warrior with profile abilities" do - @profile.abilities = [:foo, :bar] + @profile.abilities = %i[foo bar] warrior = double.as_null_object expect(warrior).to receive(:add_abilities).with(:foo, :bar) @level.setup_warrior(warrior) @@ -90,9 +90,7 @@ end describe "playing" do - before(:each) do - allow(@level).to receive(:load_level) - end + before(:each) { allow(@level).to receive(:load_level) } it "should load level once when playing multiple turns" do expect(@level).to receive(:load_level) @@ -117,9 +115,7 @@ it "should yield to block in play method for each turn" do int = 0 - @level.play(2) do - int += 1 - end + @level.play(2) { int += 1 } expect(int).to eq(2) end @@ -146,7 +142,7 @@ describe "tallying points" do before(:each) do - @warrior = double(:score => 0, :abilities => {}) + @warrior = double(score: 0, abilities: {}) allow(@level).to receive(:warrior).and_return(@warrior) allow(@level.floor).to receive(:other_units).and_return([double]) end @@ -181,9 +177,9 @@ end it "should apply warrior abilities to profile" do - allow(@warrior).to receive(:abilities).and_return({:foo => nil, :bar => nil}) + allow(@warrior).to receive(:abilities).and_return({ foo: nil, bar: nil }) @level.tally_points - expect(@profile.abilities.to_set).to eq([:foo, :bar].to_set) + expect(@profile.abilities.to_set).to eq(%i[foo bar].to_set) end it "should apply time bonus to profile score" do diff --git a/spec/ruby_warrior/player_generator_spec.rb b/spec/ruby_warrior/player_generator_spec.rb index 99d05ef9..cc94d19c 100644 --- a/spec/ruby_warrior/player_generator_spec.rb +++ b/spec/ruby_warrior/player_generator_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::PlayerGenerator do before(:each) do diff --git a/spec/ruby_warrior/position_spec.rb b/spec/ruby_warrior/position_spec.rb index 16497802..ffdd702f 100644 --- a/spec/ruby_warrior/position_spec.rb +++ b/spec/ruby_warrior/position_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Position do before(:each) do @@ -12,7 +12,7 @@ it "should rotate clockwise" do expect(@position.direction).to eq(:north) - [:east, :south, :west, :north, :east].each do |dir| + %i[east south west north east].each do |dir| @position.rotate(1) expect(@position.direction).to eq(dir) end @@ -20,7 +20,7 @@ it "should rotate counterclockwise" do expect(@position.direction).to eq(:north) - [:west, :south, :east, :north, :west].each do |dir| + %i[west south east north west].each do |dir| @position.rotate(-1) expect(@position.direction).to eq(dir) end diff --git a/spec/ruby_warrior/profile_spec.rb b/spec/ruby_warrior/profile_spec.rb index 4521e2b1..398bd9df 100644 --- a/spec/ruby_warrior/profile_spec.rb +++ b/spec/ruby_warrior/profile_spec.rb @@ -1,13 +1,11 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Profile do - before(:each) do - @profile = RubyWarrior::Profile.new - end + before(:each) { @profile = RubyWarrior::Profile.new } it "should have warrior name" do - @profile.warrior_name = 'Joe' - expect(@profile.warrior_name).to eq('Joe') + @profile.warrior_name = "Joe" + expect(@profile.warrior_name).to eq("Joe") end it "should start level number at 0" do @@ -22,8 +20,8 @@ it "should have no abilities and allow adding" do expect(@profile.abilities).to eq([]) - @profile.abilities += [:foo, :bar] - expect(@profile.abilities).to eq([:foo, :bar]) + @profile.abilities += %i[foo bar] + expect(@profile.abilities).to eq(%i[foo bar]) end it "should encode with marshal + base64" do @@ -35,17 +33,16 @@ end it "load should read file, decode and set player path" do - profile = 'profile' - expect(profile).to receive(:player_path=).with('path/to') - expect(File).to receive(:read).with('path/to/.profile').and_return('encoded_profile') - expect(RubyWarrior::Profile).to receive(:decode).with('encoded_profile').and_return(profile) - expect(RubyWarrior::Profile.load('path/to/.profile')).to eq(profile) + profile = "profile" + expect(profile).to receive(:player_path=).with("path/to") + expect(File).to receive(:read).with("path/to/.profile").and_return("encoded_profile") + expect(RubyWarrior::Profile).to receive(:decode).with("encoded_profile").and_return(profile) + expect(RubyWarrior::Profile.load("path/to/.profile")).to eq(profile) end - it "should add abilities and remove duplicates" do @profile.add_abilities(:foo, :bar, :blah, :bar) - expect(@profile.abilities).to eq([:foo, :bar, :blah]) + expect(@profile.abilities).to eq(%i[foo bar blah]) end it "should fetch new level with current number" do @@ -125,37 +122,37 @@ describe "with tower path" do before(:each) do @profile.warrior_name = "John Smith" - @profile.tower_path = 'path/to/tower' + @profile.tower_path = "path/to/tower" end it "save should write file with encoded profile" do file = double - expect(file).to receive(:write).with('encoded_profile') - expect(File).to receive(:open).with(@profile.player_path + '/.profile', 'w').and_yield(file) - expect(@profile).to receive(:encode).and_return('encoded_profile') + expect(file).to receive(:write).with("encoded_profile") + expect(File).to receive(:open).with(@profile.player_path + "/.profile", "w").and_yield(file) + expect(@profile).to receive(:encode).and_return("encoded_profile") @profile.save end it "should have a nice string representation" do - @profile.warrior_name = 'Joe' + @profile.warrior_name = "Joe" expect(@profile.to_s).to eq("Joe - tower - level 0 - score 0") end it "should include epic score in string representation" do - @profile.warrior_name = 'Joe' + @profile.warrior_name = "Joe" @profile.enable_epic_mode expect(@profile.to_s).to eq("Joe - tower - first score 0 - epic score 0") end it "should include epic score with grade in string representation" do - @profile.warrior_name = 'Joe' + @profile.warrior_name = "Joe" @profile.enable_epic_mode @profile.average_grade = 0.7 expect(@profile.to_s).to eq("Joe - tower - first score 0 - epic score 0 (C)") end it "should guess at the player path" do - expect(@profile.player_path).to eq('./rubywarrior/john-smith-tower') + expect(@profile.player_path).to eq("./rubywarrior/john-smith-tower") end it "should use specified player path" do @@ -164,8 +161,8 @@ end it "should load tower from path" do - expect(RubyWarrior::Tower).to receive(:new).with('tower').and_return('tower') - expect(@profile.tower).to eq('tower') + expect(RubyWarrior::Tower).to receive(:new).with("tower").and_return("tower") + expect(@profile.tower).to eq("tower") end end end diff --git a/spec/ruby_warrior/space_spec.rb b/spec/ruby_warrior/space_spec.rb index 3c92c2bb..68f7c7c1 100644 --- a/spec/ruby_warrior/space_spec.rb +++ b/spec/ruby_warrior/space_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Space do before(:each) do @@ -8,9 +8,7 @@ end describe "with empty space" do - before(:each) do - @space = @floor.space(0, 0) - end + before(:each) { @space = @floor.space(0, 0) } it "should not be enemy" do expect(@space).to_not be_enemy @@ -37,7 +35,7 @@ end it "should say 'nothing' as name" do - expect(@space.to_s).to eq('nothing') + expect(@space.to_s).to eq("nothing") end it "should not be ticking" do @@ -46,9 +44,7 @@ end describe "out of bounds" do - before(:each) do - @space = @floor.space(-1, 1) - end + before(:each) { @space = @floor.space(-1, 1) } it "should be wall" do expect(@space).to be_wall @@ -59,7 +55,7 @@ end it "should have name of 'wall'" do - expect(@space.to_s).to eq('wall') + expect(@space.to_s).to eq("wall") end end @@ -114,9 +110,7 @@ end describe "bound" do - before(:each) do - @space.unit.bind - end + before(:each) { @space.unit.bind } it "should be captive" do expect(@space).to be_captive diff --git a/spec/ruby_warrior/tower_spec.rb b/spec/ruby_warrior/tower_spec.rb index d4f1d6bd..e3f5a384 100644 --- a/spec/ruby_warrior/tower_spec.rb +++ b/spec/ruby_warrior/tower_spec.rb @@ -1,12 +1,10 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Tower do - before(:each) do - @tower = RubyWarrior::Tower.new('path/to/tower') - end + before(:each) { @tower = RubyWarrior::Tower.new("path/to/tower") } it "should consider last part of path as name" do - expect(@tower.name).to eq('tower') + expect(@tower.name).to eq("tower") end it "should use name when converting to string" do diff --git a/spec/ruby_warrior/turn_spec.rb b/spec/ruby_warrior/turn_spec.rb index 81fa64f0..e54a373f 100644 --- a/spec/ruby_warrior/turn_spec.rb +++ b/spec/ruby_warrior/turn_spec.rb @@ -1,10 +1,8 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Turn do describe "with actions" do - before(:each) do - @turn = RubyWarrior::Turn.new({:walk! => nil, :attack! => nil}) - end + before(:each) { @turn = RubyWarrior::Turn.new({ walk!: nil, attack!: nil }) } it "should have no action performed at first" do expect(@turn.action).to be_nil @@ -17,7 +15,7 @@ it "should include arguments passed to action" do @turn.walk! :forward - expect(@turn.action).to eq([:walk!, :forward]) + expect(@turn.action).to eq(%i[walk! forward]) end it "should not be able to call multiple actions per turn" do @@ -31,7 +29,7 @@ @feel = RubyWarrior::Abilities::Feel.new(Object.new) allow(@feel).to receive(:space).and_return(Object.new) allow(@feel).to receive(:space).with(:backward).and_return(Object.new) - @turn = RubyWarrior::Turn.new({:feel => @feel}) + @turn = RubyWarrior::Turn.new({ feel: @feel }) end it "should be able to call sense with any argument and return expected results" do diff --git a/spec/ruby_warrior/ui_spec.rb b/spec/ruby_warrior/ui_spec.rb index 47407dd8..01fdc018 100644 --- a/spec/ruby_warrior/ui_spec.rb +++ b/spec/ruby_warrior/ui_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::UI do before(:each) do @@ -39,43 +39,43 @@ end it "should ask for yes/no and return true when yes" do - expect(@ui).to receive(:request).with('foo? [yn] ').and_return('y') + expect(@ui).to receive(:request).with("foo? [yn] ").and_return("y") expect(@ui.ask("foo?")).to eq(true) end it "should ask for yes/no and return false when no" do - allow(@ui).to receive(:request).and_return('n') + allow(@ui).to receive(:request).and_return("n") expect(@ui.ask("foo?")).to eq(false) end it "should ask for yes/no and return false for any input" do - allow(@ui).to receive(:request).and_return('aklhasdf') + allow(@ui).to receive(:request).and_return("aklhasdf") expect(@ui.ask("foo?")).to eq(false) end it "should present multiple options and return selected one" do - expect(@ui).to receive(:request).with(include('item')).and_return('2') - expect(@ui.choose('item', [:foo, :bar, :test])).to eq(:bar) - expect(@out.string).to include('[1] foo') - expect(@out.string).to include('[2] bar') - expect(@out.string).to include('[3] test') + expect(@ui).to receive(:request).with(include("item")).and_return("2") + expect(@ui.choose("item", %i[foo bar test])).to eq(:bar) + expect(@out.string).to include("[1] foo") + expect(@out.string).to include("[2] bar") + expect(@out.string).to include("[3] test") end it "choose should accept array as option" do - allow(@ui).to receive(:request).and_return('3') - expect(@ui.choose('item', [:foo, :bar, [:tower, 'easy']])).to eq(:tower) - expect(@out.string).to include('[3] easy') + allow(@ui).to receive(:request).and_return("3") + expect(@ui.choose("item", [:foo, :bar, [:tower, "easy"]])).to eq(:tower) + expect(@out.string).to include("[3] easy") end it "choose should return option without prompt if only one item" do expect(@ui).to receive(:puts).never expect(@ui).to receive(:gets).never - allow(@ui).to receive(:request).and_return('3') - expect(@ui.choose('item', [:foo])).to eq(:foo) + allow(@ui).to receive(:request).and_return("3") + expect(@ui.choose("item", [:foo])).to eq(:foo) end it "choose should return first value in array of option if only on item" do - expect(@ui.choose('item', [[:foo, :bar]])).to eq(:foo) + expect(@ui.choose("item", [%i[foo bar]])).to eq(:foo) end it "should delay after puts when specified" do diff --git a/spec/ruby_warrior/units/archer_spec.rb b/spec/ruby_warrior/units/archer_spec.rb index 3202e2a5..ced8bf50 100644 --- a/spec/ruby_warrior/units/archer_spec.rb +++ b/spec/ruby_warrior/units/archer_spec.rb @@ -1,12 +1,10 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Units::Archer do - before(:each) do - @archer = RubyWarrior::Units::Archer.new - end + before(:each) { @archer = RubyWarrior::Units::Archer.new } it "should have look and shoot abilities" do - expect(@archer.abilities.keys.to_set).to eq([:shoot!, :look].to_set) + expect(@archer.abilities.keys.to_set).to eq(%i[shoot! look].to_set) end it "should have shoot power of 3" do diff --git a/spec/ruby_warrior/units/base_spec.rb b/spec/ruby_warrior/units/base_spec.rb index be1a282b..57bbccce 100644 --- a/spec/ruby_warrior/units/base_spec.rb +++ b/spec/ruby_warrior/units/base_spec.rb @@ -1,9 +1,7 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Units::Base do - before(:each) do - @unit = RubyWarrior::Units::Base.new - end + before(:each) { @unit = RubyWarrior::Units::Base.new } it "should have an attack power which defaults to zero" do expect(@unit.attack_power).to be_zero @@ -55,13 +53,13 @@ end it "should return name in to_s" do - expect(@unit.name).to eq('Base') - expect(@unit.to_s).to eq('Base') + expect(@unit.name).to eq("Base") + expect(@unit.to_s).to eq("Base") end it "should prepare turn by calling play_turn with next turn object" do - allow(@unit).to receive(:next_turn).and_return('next_turn') - expect(@unit).to receive(:play_turn).with('next_turn') + allow(@unit).to receive(:next_turn).and_return("next_turn") + expect(@unit).to receive(:play_turn).with("next_turn") @unit.prepare_turn end @@ -69,7 +67,7 @@ @unit.position = double expect_any_instance_of(RubyWarrior::Abilities::Walk).to receive(:perform).with(:backward) @unit.add_abilities(:walk!) - turn = double(:action => [:walk!, :backward]) + turn = double(action: %i[walk! backward]) allow(@unit).to receive(:next_turn).and_return(turn) @unit.prepare_turn @unit.perform_turn @@ -77,9 +75,11 @@ it "should not perform action when dead (no position)" do @unit.position = nil - allow_any_instance_of(RubyWarrior::Abilities::Walk).to receive(:perform).and_raise("action should not be called") + allow_any_instance_of(RubyWarrior::Abilities::Walk).to receive(:perform).and_raise( + "action should not be called", + ) @unit.add_abilities(:walk!) - turn = double(:action => [:walk!, :backward]) + turn = double(action: %i[walk! backward]) allow(@unit).to receive(:next_turn).and_return(turn) @unit.prepare_turn @unit.perform_turn @@ -91,15 +91,17 @@ end it "should pass abilities to new turn when calling next_turn" do - expect(RubyWarrior::Turn).to receive(:new).with({:walk! => nil, :attack! => nil, :feel => nil}).and_return('turn') - allow(@unit).to receive(:abilities).and_return({:walk! => nil, :attack! => nil, :feel => nil}) - expect(@unit.next_turn).to eq('turn') + expect(RubyWarrior::Turn).to receive(:new).with( + { walk!: nil, attack!: nil, feel: nil }, + ).and_return("turn") + allow(@unit).to receive(:abilities).and_return({ walk!: nil, attack!: nil, feel: nil }) + expect(@unit.next_turn).to eq("turn") end it "should add ability" do - expect(RubyWarrior::Abilities::Walk).to receive(:new).with(@unit).and_return('walk') + expect(RubyWarrior::Abilities::Walk).to receive(:new).with(@unit).and_return("walk") @unit.add_abilities(:walk!) - expect(@unit.abilities).to eq({ :walk! => 'walk' }) + expect(@unit.abilities).to eq({ walk!: "walk" }) end it "should appear as question mark on map" do @@ -123,9 +125,11 @@ it "should not perform action when bound" do @unit.position = double @unit.bind - allow_any_instance_of(RubyWarrior::Abilities::Walk).to receive(:perform).and_raise("action should not be called") + allow_any_instance_of(RubyWarrior::Abilities::Walk).to receive(:perform).and_raise( + "action should not be called", + ) @unit.add_abilities(:walk!) - turn = double(:action => [:walk!, :backward]) + turn = double(action: %i[walk! backward]) allow(@unit).to receive(:next_turn).and_return(turn) @unit.prepare_turn @unit.perform_turn diff --git a/spec/ruby_warrior/units/captive_spec.rb b/spec/ruby_warrior/units/captive_spec.rb index 034e6eb7..562a1c00 100644 --- a/spec/ruby_warrior/units/captive_spec.rb +++ b/spec/ruby_warrior/units/captive_spec.rb @@ -1,9 +1,7 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Units::Captive do - before(:each) do - @captive = RubyWarrior::Units::Captive.new - end + before(:each) { @captive = RubyWarrior::Units::Captive.new } it "should have 1 max health" do expect(@captive.max_health).to eq(1) diff --git a/spec/ruby_warrior/units/golem_spec.rb b/spec/ruby_warrior/units/golem_spec.rb index 76f290bf..552639b9 100644 --- a/spec/ruby_warrior/units/golem_spec.rb +++ b/spec/ruby_warrior/units/golem_spec.rb @@ -1,9 +1,7 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Units::Golem do - before(:each) do - @golem = RubyWarrior::Units::Golem.new - end + before(:each) { @golem = RubyWarrior::Units::Golem.new } it "should execute turn proc when playing turn" do proc = Object.new diff --git a/spec/ruby_warrior/units/sludge_spec.rb b/spec/ruby_warrior/units/sludge_spec.rb index a73753c7..9296f35c 100644 --- a/spec/ruby_warrior/units/sludge_spec.rb +++ b/spec/ruby_warrior/units/sludge_spec.rb @@ -1,9 +1,7 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Units::Sludge do - before(:each) do - @sludge = RubyWarrior::Units::Sludge.new - end + before(:each) { @sludge = RubyWarrior::Units::Sludge.new } it "should have attack action" do expect(@sludge.abilities.keys).to include(:attack!) diff --git a/spec/ruby_warrior/units/thick_sludge_spec.rb b/spec/ruby_warrior/units/thick_sludge_spec.rb index 9ef57a2d..0687e8ff 100644 --- a/spec/ruby_warrior/units/thick_sludge_spec.rb +++ b/spec/ruby_warrior/units/thick_sludge_spec.rb @@ -1,9 +1,7 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Units::ThickSludge do - before(:each) do - @sludge = RubyWarrior::Units::ThickSludge.new - end + before(:each) { @sludge = RubyWarrior::Units::ThickSludge.new } it "should have 24 max health" do expect(@sludge.max_health).to eq(24) diff --git a/spec/ruby_warrior/units/warrior_spec.rb b/spec/ruby_warrior/units/warrior_spec.rb index ff8f1fa0..0275fedc 100644 --- a/spec/ruby_warrior/units/warrior_spec.rb +++ b/spec/ruby_warrior/units/warrior_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" class Player def turn(warrior) @@ -6,13 +6,11 @@ def turn(warrior) end describe RubyWarrior::Units::Warrior do - before(:each) do - @warrior = RubyWarrior::Units::Warrior.new - end + before(:each) { @warrior = RubyWarrior::Units::Warrior.new } it "should default name to warrior" do expect(@warrior.name).to eq("Warrior") - @warrior.name = '' + @warrior.name = "" expect(@warrior.name).to eq("Warrior") end @@ -34,16 +32,14 @@ def turn(warrior) it "should call player.play_turn and pass turn to player" do player = double - expect(player).to receive(:play_turn).with('turn') + expect(player).to receive(:play_turn).with("turn") allow(@warrior).to receive(:player).and_return(player) - @warrior.play_turn('turn') + @warrior.play_turn("turn") end it "should call Player.new the first time loading player, and return same object next time" do - expect(Player).to receive(:new).and_return('player').once - 2.times do - expect(@warrior.player).to eq('player') - end + expect(Player).to receive(:new).and_return("player").once + 2.times { expect(@warrior.player).to eq("player") } end it "should have an attack power of 5" do diff --git a/spec/ruby_warrior/units/wizard_spec.rb b/spec/ruby_warrior/units/wizard_spec.rb index 686a9dd2..a669afba 100644 --- a/spec/ruby_warrior/units/wizard_spec.rb +++ b/spec/ruby_warrior/units/wizard_spec.rb @@ -1,12 +1,10 @@ -require 'spec_helper' +require "spec_helper" describe RubyWarrior::Units::Wizard do - before(:each) do - @wizard = RubyWarrior::Units::Wizard.new - end + before(:each) { @wizard = RubyWarrior::Units::Wizard.new } it "should have look and shoot abilities" do - expect(@wizard.abilities.keys.to_set).to eq([:shoot!, :look].to_set) + expect(@wizard.abilities.keys.to_set).to eq(%i[shoot! look].to_set) end it "should have shoot power of 11" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2f11d2a4..01b78f12 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,10 +1,8 @@ -require 'rubygems' -require 'rspec' -require File.dirname(__FILE__) + '/../lib/ruby_warrior' +require "rubygems" +require "rspec" +require File.dirname(__FILE__) + "/../lib/ruby_warrior" RSpec.configure do |config| - config.before(:each) do - RubyWarrior::Config.reset - end + config.before(:each) { RubyWarrior::Config.reset } config.raise_errors_for_deprecations! end diff --git a/towers/beginner/level_001.rb b/towers/beginner/level_001.rb index 59b60139..ceeff597 100644 --- a/towers/beginner/level_001.rb +++ b/towers/beginner/level_001.rb @@ -5,7 +5,6 @@ description "You see before yourself a long hallway with stairs at the end. There is nothing in the way." tip "Call warrior.walk! to walk forward in the Player 'play_turn' method." - time_bonus 15 ace_score 10 size 8, 1 From bd8cf921cbc34d8a6f673dee5c8eaf74281a863a Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Wed, 27 Mar 2024 09:37:59 -0700 Subject: [PATCH 31/32] Clean up script/console and move to bin/console --- bin/console | 6 ++++++ script/console | 8 -------- 2 files changed, 6 insertions(+), 8 deletions(-) create mode 100755 bin/console delete mode 100755 script/console diff --git a/bin/console b/bin/console new file mode 100755 index 00000000..758f1983 --- /dev/null +++ b/bin/console @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby + +# loading the spec_helper file even though this isn't necessarily for specs +# it's just a convenient, quick way to load all the requirements. +puts "Loading console" +exec "irb -r #{File.dirname(__FILE__) + '/../lib/ruby_warrior.rb'} -r #{File.dirname(__FILE__) + '/../features/support/mockio.rb'} --simple-prompt" diff --git a/script/console b/script/console deleted file mode 100755 index 6f860a07..00000000 --- a/script/console +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env ruby -# File: script/console -irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb' - -# loading the spec_helper file even though this isn't necessarily for specs -# it's just a convenient, quick way to load all the requirements. -puts "Loading console" -exec "#{irb} -r irb/completion -r #{File.dirname(__FILE__) + '/../lib/ruby_warrior.rb'} -r #{File.dirname(__FILE__) + '/../features/support/mockio.rb'} --simple-prompt" From 6758e98a2fd695590522a81b027fb78874bf672b Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Wed, 27 Mar 2024 09:43:32 -0700 Subject: [PATCH 32/32] Add cucumber.yml config which quiets publish message --- Rakefile | 2 +- cucumber.yml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 cucumber.yml diff --git a/Rakefile b/Rakefile index 4b129f11..0fa39aed 100644 --- a/Rakefile +++ b/Rakefile @@ -7,6 +7,6 @@ require "cucumber/rake/task" desc "Run specs" RSpec::Core::RakeTask.new(:spec) { |t| t.pattern = "spec/**/*_spec.rb" } -Cucumber::Rake::Task.new(:features) { |t| t.cucumber_opts = %w[features --format progress] } +Cucumber::Rake::Task.new(:features) { |t| t.cucumber_opts = %w[features] } task default: %i[spec features] diff --git a/cucumber.yml b/cucumber.yml new file mode 100644 index 00000000..0b6b6836 --- /dev/null +++ b/cucumber.yml @@ -0,0 +1 @@ +default: --publish-quiet --format progress