Commit e1afbc8
Changed files (5)
bin
features
step_definitions
lib
mastermind
spec
mastermind
bin/mastermind
@@ -2,4 +2,7 @@ $LOAD_PATH.push File.join(File.dirname(__FILE__), "/../lib")
require 'mastermind'
game = Mastermind::Game.new(STDOUT)
-game.start
+game.start(%w[r g y c])
+while guess = gets
+ game.guess guess.split
+end
features/step_definitions/mastermind.rb
@@ -4,7 +4,7 @@ def messenger
end
def game
- @game = ||= Mastermind::Game.new(messenger)
+ @game ||= Mastermind::Game.new(messenger)
end
def messages_should_include(message)
features/codebreaker_submits_guess.feature
@@ -41,3 +41,11 @@ Feature: code-breaker submits guess
| code | guess | mark |
| r g y c | r w w w | b |
| r g y c | w w r w | w |
+
+ Scenarios: dups in guess match color in code
+ | code | guess | mark |
+ | r y g c | r y g g | bbb |
+ | r y g c | r y c c | bbb |
+ | r y g c | g y r g | bww |
+
+
lib/mastermind/game.rb
@@ -10,15 +10,15 @@ module Mastermind
@messenger.puts "Enter guess:"
end
def guess(guess)
- result = []
+ result = [nil,nil,nil,nil]
guess.each_with_index do |peg, index|
if @code[index] == peg
- result << "b"
+ result[index] = "b"
elsif @code.include?(peg)
- result << "w"
+ result[@code.index(peg)] ||= "w"
end
end
- @messenger.puts result.sort.join
+ @messenger.puts result.compact.sort.join
end
end
end
spec/mastermind/game_spec.rb
@@ -40,5 +40,21 @@ module Mastermind
end
end
end
+ context "with duplicates in the guess that match a peg in the code" do
+ context "by color and position" do
+ it "should add a single b to the mark" do
+ @game.start(%w[r y g c])
+ @messenger.should_receive(:puts).with("bbb")
+ @game.guess(%w[r y g g])
+ end
+ end
+ end
+ context "with three colors correct in the correct places" do
+ it "should mark the guess with bbb" do
+ @game.start(%w[r g y c])
+ @messenger.should_receive(:puts).with("bbb")
+ @game.guess(%w[r g y w])
+ end
+ end
end
end