A nord-agile meeting took place today after a long lull. We started a MasterMind in Java Randori using Eclipse. My contribution was insignificant so I decided to tackle the problem in Python using doctests and my beloved Vim.
In a coding dojo it is really the process that matters but ... here is the result ;) Maybe I'll present it as a Kata.
I initially misread the rules which Arnaud noticed :P and forgot at least one test case which Raphael noticed :(
"""
cf. http://en.wikipedia.org/wiki/Mastermind_(board_game)
6 colors : (r)ed, (g)reen, (b)lue, (y)ellow, (o)range, (v)iolet
a 4 pegs long secret
the number of white pegs (misplaced) is the extra number of black pegs
(correctly placed) that it is possible to get using exactly the code pegs in
that guess
"""
class MasterMind(object):
"""
>>> mmind = MasterMind()
Dealing with a one peg secret
>>> mmind.secret = 'r'
>>> mmind.play('r')
(1, 0)
>>> mmind.play('b')
(0, 0)
Now evolving to a 2 pegs secret
>>> mmind.secret = 'rb'
Secret broken
>>> mmind.play('rb')
(2, 0)
One peg in position
>>> mmind.play('rg')
(1, 0)
One Peg misplaced
>>> mmind.play('gr')
(0, 1)
All wrong
>>> mmind.play('vo')
(0, 0)
One peg in position and one misplaced (of the same color)
>>> mmind.play('rr')
(1, 0)
Raphael's 3 peg wonder
>>> mmind.secret = 'rgr'
>>> mmind.play('rro')
(1, 1)
The full monty i.e. 4 pegs
>>> mmind.secret = 'rgbo'
>>> mmind.play('rgbo')
(4, 0)
>>> mmind.play('rrrr')
(1, 0)
>>> mmind.play('yyyy')
(0, 0)
>>> mmind.play('obgr')
(0, 4)
>>> mmind.play('grbo')
(2, 2)
Added following Arnaud's feedback
>>> mmind.play('gygo')
(1, 1)
>>> mmind.play('gygg')
(0, 1)
Added following Raphael's feedback
>>> mmind.secret = 'ggrr'
>>> mmind.play('goog')
(1, 1)
"""
def play(self, guess):
""" play return a tuple (correct, misplaced) """
correct = list()
new_secret = list()
new_guess = list()
for index, peg in enumerate(guess):
if self.secret[index]==peg:
correct.append(peg)
else:
new_secret.append(self.secret[index])
new_guess.append(guess[index])
misplaced = list()
for peg in new_guess:
if peg in new_secret:
misplaced.append(peg)
new_secret.remove(peg)
return len(correct), len(misplaced)
if __name__=='__main__':
import doctest
doctest.testmod()
cf. http://en.wikipedia.org/wiki/Mastermind_(board_game)
6 colors : (r)ed, (g)reen, (b)lue, (y)ellow, (o)range, (v)iolet
a 4 pegs long secret
the number of white pegs (misplaced) is the extra number of black pegs
(correctly placed) that it is possible to get using exactly the code pegs in
that guess
"""
class MasterMind(object):
"""
>>> mmind = MasterMind()
Dealing with a one peg secret
>>> mmind.secret = 'r'
>>> mmind.play('r')
(1, 0)
>>> mmind.play('b')
(0, 0)
Now evolving to a 2 pegs secret
>>> mmind.secret = 'rb'
Secret broken
>>> mmind.play('rb')
(2, 0)
One peg in position
>>> mmind.play('rg')
(1, 0)
One Peg misplaced
>>> mmind.play('gr')
(0, 1)
All wrong
>>> mmind.play('vo')
(0, 0)
One peg in position and one misplaced (of the same color)
>>> mmind.play('rr')
(1, 0)
Raphael's 3 peg wonder
>>> mmind.secret = 'rgr'
>>> mmind.play('rro')
(1, 1)
The full monty i.e. 4 pegs
>>> mmind.secret = 'rgbo'
>>> mmind.play('rgbo')
(4, 0)
>>> mmind.play('rrrr')
(1, 0)
>>> mmind.play('yyyy')
(0, 0)
>>> mmind.play('obgr')
(0, 4)
>>> mmind.play('grbo')
(2, 2)
Added following Arnaud's feedback
>>> mmind.play('gygo')
(1, 1)
>>> mmind.play('gygg')
(0, 1)
Added following Raphael's feedback
>>> mmind.secret = 'ggrr'
>>> mmind.play('goog')
(1, 1)
"""
def play(self, guess):
""" play return a tuple (correct, misplaced) """
correct = list()
new_secret = list()
new_guess = list()
for index, peg in enumerate(guess):
if self.secret[index]==peg:
correct.append(peg)
else:
new_secret.append(self.secret[index])
new_guess.append(guess[index])
misplaced = list()
for peg in new_guess:
if peg in new_secret:
misplaced.append(peg)
new_secret.remove(peg)
return len(correct), len(misplaced)
if __name__=='__main__':
import doctest
doctest.testmod()
blog comments powered by Disqus


