Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

change code breaker and maker to use string code representation

  • Loading branch information...
commit f92a6a5e735e2cfca20c4a63057aa0f3f0c8949c 1 parent f6c3783
@avalanche123 authored
View
4 features/step_definitions/codebreaker_steps.rb
@@ -1,9 +1,9 @@
Given /^the secret code is "([^"]*)"$/ do |code|
- @codebreaker = Codebreaker.new(code.split(""))
+ @codebreaker = Codebreaker.new(code)
end
When /^I guess "([^"]*)"$/ do |guess|
- @matches = @codebreaker.guess guess.split("")
+ @matches = @codebreaker.guess guess
end
Then /^the number of matches should be "([^"]*)"$/ do |matches|
View
6 lib/mastermind/codebreaker.rb
@@ -1,10 +1,10 @@
class Codebreaker
def initialize(code)
- @code = code
+ @code = code.split('')
end
-
+
def guess(guess)
code = @code.clone
- guess.inject(0) {|matches, number| matches + (code.shift === number ? 1 : 0)}
+ guess.split('').inject(0) {|matches, number| matches + (code.shift === number ? 1 : 0)}
end
end
View
5 lib/mastermind/codemaker.rb
@@ -1,9 +1,10 @@
class Codemaker
def initialize(length, max=2)
- @length, @max = length, max
+ @length = length
+ @max = max
end
def make
- @length.times.collect_concat { [@max - 1, rand(@max)].min }
+ @length.times.collect { [@max - 1, rand(@max)].min.to_s }.join
end
end
View
13 spec/mastermind/codebreaker_spec.rb
@@ -1,14 +1,15 @@
require 'spec_helper'
describe Codebreaker do
- let(:code) { [1, 0, 1, 0] }
+ code ||= '1010'
+
let(:codebreaker) { Codebreaker.new(code) }
it "returns number of correct pegs in a guess" do
- codebreaker.guess([0, 0, 0, 0]).should eq(2)
- codebreaker.guess([0, 1, 1, 0]).should eq(2)
- codebreaker.guess([1, 1, 1, 0]).should eq(3)
- codebreaker.guess([0, 1, 1, 1]).should eq(1)
- codebreaker.guess([1, 0, 1, 0]).should eq(4)
+ codebreaker.guess('0000').should eq(2)
+ codebreaker.guess('0110').should eq(2)
+ codebreaker.guess('1110').should eq(3)
+ codebreaker.guess('0111').should eq(1)
+ codebreaker.guess('1010').should eq(4)
end
end
View
6 spec/mastermind/codemaker_spec.rb
@@ -1,6 +1,6 @@
describe Codemaker do
- let(:length) { rand(10) }
- let(:max) { rand(10) }
+ let(:length) { [2, rand(10)].max }
+ let(:max) { [2, rand(10)].max }
let(:codemaker) { Codemaker.new(length, max) }
it "makes code of a pre-defind length" do
@@ -8,6 +8,6 @@
end
it "makes code with each number less than the max" do
- codemaker.make.all? {|number| number < max }.should be_true
+ codemaker.make.split('').each {|number| number.to_i.should be < max}
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.