Commit 4b8f713
Changed files (3)
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
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
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