summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2009-05-28 13:07:10 -0600
committermo khan <mo@mokhan.ca>2009-05-28 13:07:10 -0600
commite1afbc888054063cb471affdd2b0ff02f423b8ef (patch)
tree3a3840637e88992740bd132342deaa910c0259eb
parent1b3e97664a6fe0af00cb0aef406ebba8de69f7c9 (diff)
made it up to page 88main
-rw-r--r--bin/mastermind5
-rw-r--r--features/codebreaker_submits_guess.feature8
-rw-r--r--features/step_definitions/mastermind.rb2
-rw-r--r--lib/mastermind/game.rb8
-rw-r--r--spec/mastermind/game_spec.rb16
5 files changed, 33 insertions, 6 deletions
diff --git a/bin/mastermind b/bin/mastermind
index 43fe304..97af74b 100644
--- a/bin/mastermind
+++ b/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
diff --git a/features/codebreaker_submits_guess.feature b/features/codebreaker_submits_guess.feature
index dac2141..a36422f 100644
--- a/features/codebreaker_submits_guess.feature
+++ b/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 |
+
+
diff --git a/features/step_definitions/mastermind.rb b/features/step_definitions/mastermind.rb
index 680a2b6..91ec22b 100644
--- a/features/step_definitions/mastermind.rb
+++ b/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)
diff --git a/lib/mastermind/game.rb b/lib/mastermind/game.rb
index 4485b58..1ed91a2 100644
--- a/lib/mastermind/game.rb
+++ b/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
diff --git a/spec/mastermind/game_spec.rb b/spec/mastermind/game_spec.rb
index 4788aa9..dd14dc8 100644
--- a/spec/mastermind/game_spec.rb
+++ b/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