Skip to content

Instantly share code, notes, and snippets.

@jeantil
Created February 24, 2011 18:04
Show Gist options
  • Select an option

  • Save jeantil/842574 to your computer and use it in GitHub Desktop.

Select an option

Save jeantil/842574 to your computer and use it in GitHub Desktop.
softeam dojo 5
; testmastermind.ik
use("ispec")
use("mastermind-kata")
; Quick reminder how ispec works
;describe(
; Object | "method" ,
; it("should be the title of the test",
; call should == result ?
; m should not be same(Foo)
; m should mimic(Foo)
; )
; )
;)
;unit tests
describe("solver",
it("should return [0,0] when all the pegs are wrong color",
answer([]("M","M","M","M"), []("B","B","B","B")) should == [0,0]
)
it("should return [1,0] when one peg is good and well placed",
answer([]("B","M","M","M"), []("B","B","B","B")) should == [1,0]
)
it("should return [2,0] when 2 pegs are good and well placed",
answer([]("B","B","M","M"), []("B","B","B","B")) should == [2,0]
)
; it("should return nil when guess and secret have a different length",
; answer([]("B"), []("B","B","B","B")) should == nil
; )
it("should return [0,1] when one peg is misplaced",
answer([]("M","R","M","M"), []("B","B","B","R")) should == [0,1]
)
it("should return [0,2] when one peg is misplaced",
answer([]("M","R","V","M"), []("B","V","B","R")) should == [0,2]
)
it("should return [0,2] when one peg is misplaced",
answer([]("J","M","N","M"), []("B","J","B","N")) should == [0,2]
)
)
; Acceptance tests
;describe("solver",
; it("should return [0,0] when the guess doesn't match the secret",
; answer guess=[]("B","R","J","V"), secret=[]("N","N","G","G") should == [](0,0)
; )
; it("should return [1,1] when the guess has one good well placed and one good misplaced",
; answer guess=[]("B","R","J","V"), secret=[]("B","N","R","G") should == [](1,1)
; )
; it("should return [2,2] when the guess has two good well placed and two good misplaced",
; answer guess=[]("B","R","B","V"), secret=[]("B","V","B","R") should == [](2,2)
; )
;)
;mastermind.ik
;Mastermind solver implementation
answer = method(guess, secret,
zipped= guess zip(secret)
filtered=zipped filter(inject(==))
good=filtered count
colors=guess map:set(x,x)
bad=colors map(x, misplacedColor(guess, secret, x)) sum
[](good,bad)
)
misplacedColor=method(guess, secret, color,
if(guess find(==color) and secret find(==color),
1,
0
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment