summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2009-05-27 09:04:07 -0600
committermo khan <mo@mokhan.ca>2009-05-27 09:04:07 -0600
commit4b8f713fc36eadf7f974d80c72ed22be413efcbe (patch)
tree8c94415ede4525b6a4980905caf2efe8fd52c0f7
parentda73fdb2d793f4190bd89fd210b2b6188c62e2d3 (diff)
testing git commit from gvim
-rw-r--r--features/step_definitions/mastermind.rb12
-rw-r--r--lib/mastermind/game.rb15
-rw-r--r--spec/mastermind/game_spec.rb24
3 files changed, 43 insertions, 8 deletions
diff --git a/features/step_definitions/mastermind.rb b/features/step_definitions/mastermind.rb
index b7f7c86..f07c541 100644
--- a/features/step_definitions/mastermind.rb
+++ b/features/step_definitions/mastermind.rb
@@ -2,19 +2,23 @@
Given /^I am not yet playing$/ do
end
-
When /^I start a new game$/ do
@messenger = StringIO.new
game =Mastermind::Game.new(@messenger)
game.start
end
-
Then /^the game should say "(.$)"$/ do |message|
@messenger.string.split("\n").should include(message)
end
Given /^the secret code is (. . . .)$/ do |code|
@messenger = StringIO.new
- game = Mastermind::Game.new(@messenger)
- game.start(code.split)
+ @game = Mastermind::Game.new(@messenger)
+ @game.start(code.split)
+end
+When /^I guess (. . . .)$/ do |code|
+ @game.guess(code.split)
+end
+Then /^the mark should be (.*)$/ do |mark|
+ @messenger.string.split("\n").should include(mark)
end
diff --git a/lib/mastermind/game.rb b/lib/mastermind/game.rb
index e4d3b17..a5e5665 100644
--- a/lib/mastermind/game.rb
+++ b/lib/mastermind/game.rb
@@ -4,10 +4,21 @@ module Mastermind
def initialize(messenger)
@messenger = messenger
end
- def start
+ def start(code)
+ @code = code
@messenger.puts "Welcome to Mastermind!"
@messenger.puts "Enter guess:"
-
+ end
+ def guess(guess)
+ result = ""
+ guess.each_with_index do |peg, index|
+ if @code[index] == peg
+ result << "b"
+ elsif @code.include?(peg)
+ result << "w"
+ end
+ end
+ @messenger.puts result
end
end
end
diff --git a/spec/mastermind/game_spec.rb b/spec/mastermind/game_spec.rb
index dd50e48..22c1716 100644
--- a/spec/mastermind/game_spec.rb
+++ b/spec/mastermind/game_spec.rb
@@ -10,11 +10,31 @@ module Mastermind
end
it "should send a welcome message" do
@messenger.should_receive(:puts).with("Welcome to Mastermind!")
- @game.start
+ @game.start(%w[r c g y])
end
it "should prompt for the first guess" do
@messenger.should_receive(:puts).with("Enter guess:")
- @game.start
+ @game.start(%w[r c g y])
+ end
+ end
+ context "marking a guess" do
+ context "with all 4 colors correct in the correct places" do
+ it "should mark the guess with bbbb" do
+ messenger = mock("messenger").as_null_object
+ game = Game.new(messenger)
+ game.start(%w[r g y c])
+ messenger.should_receive(:puts).with("bbbb")
+ game.guess(%w[r g y c])
+ end
+ end
+ context "with all 4 colors correct and 2 in the correct places" do
+ it "should mark the guess with bbww" do
+ messenger = mock("messenger").as_null_object
+ game = Game.new(messenger)
+ game.start(%w[r g y c])
+ messenger.should_receive(:puts).with("bbww")
+ game.guess(%w[r g c y])
+ end
end
end
end