Py Master Mind

Created by Antoine Mazières

About the game

During this exercise you will implement the Master Mind game.

The game is about cracking a code of colors chosen by the program, given we know its length and the possible colors its made of. At each attempt, the player is given feedback on how well he performed, so he can improve its next guess.

Example

We'll represent colors by uppercase letters, starting at the begining of the alphabet. Therefore, if asked to generate a code of size 4 with six colors 'ABCDEF', such as, for example:

$ python solution.py
Possible colors are ABCDEF
Code is size 4.
CODE = BFEB
0 --> ABCD
(0, 1)
1 --> BFDE
(2, 1)
2 --> BFEB
Congrats, you won after 3 attempts !

The player is given feedback with a tuple of length 2. The first element indicates the number of elements in the correct position AND color. The second number indicates the number of elements with the right color BUT in a wrong position.

Exercises

Generate colors

You must provide a function gen_colors(code_size) taking the number of colors as parameter and returning a string of the corresponding uppercase letters. If the argument is superior to the number of letters in the alphabet, it should only return the 26 letters of the alphabet. Such as:

In [2]: gen_colors(6)
Out[2]: 'ABCDEF'

In [4]: gen_colors(295)
Out[4]: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

Generate the code

You must provide a function gen_code(code_size, colors) taking the code size and the string containing all colors as parameters and returning a random generated code. Such as:

In [5]: gen_code(4, 'ABCDEF')
Out[5]: 'DCBF'

In [6]: gen_code(5, 'ABCD')
Out[6]: 'CBADD'

Check guess

You must provide a function check_guess(guess, code_size, colors) taking the guess, the code size and the string of colors as parameters. The function should check if the guess has the same length as the code, and if each of its colors is part of the colors' list. Such as:

In [6]: check_guess('ZZZZ', 4, 'ABCDEF')
Out[6]: False

In [7]: check_guess('EEBBAA', 4, 'ABCDEF')
Out[7]: False

In [8]: check_guess('AABB', 4, 'ABCDEF')
Out[8]: True

Score guess

You must provide a score_guess(code, guess) function taking the code and the guess as parameters and returning a tuple indicating:

  1. The number of elements in the right position AND color
  2. The number of elements with the right color BUT in a wrong position.
In [9]: score_guess('ABCD', 'ABCD')
Out[9]: (4, 0)

In [10]: score_guess('AAAA', 'ABCD')
Out[10]: (1, 0)

In [11]: score_guess('AADA', 'ABCD')
Out[11]: (1, 1)

In [12]: score_guess('ADDA', 'ABCD')
Out[12]: (1, 1)

In [13]: score_guess('ADDB', 'ABCD')
Out[13]: (1, 2)

Play on the command-line

Put it all together ! You must provide a function play_cli(code_size, nb_colors) taking the size of the code and the number of colors as parameters, and enabling one to play such as:

$ python solution.py
Possible colors are ABCDEF
Code is size 4.
0 --> FFRJ
Wrong size or color !
0 --> FFFFF
Wrong size or color !
0 --> ABCD
(0, 0)
1 --> FFEE
(3, 0)
2 --> FFFE
Congrats, you won after 3 attempts !

Don't forgot to "hide" your play_cli call in a if __name == "__main__":, so I can still import your functions individually without starting an interactive session!

There's no corrections yet, hit the `Submit` button to send your code to the correction bot.

Keyboard shortcuts:

  • Ctrl-Enter: Send your code to the correction bot.
  • Escape: Get back to the instructions tab.

See solutions