Mastermind In this assignment, you will program the well-known game Mastermind. In Mastermind, player 1 creates a pattern consisting of a pre-approved number of colored pins. Then player 2 has to guess this pattern in as few turns as possible. Each turn, player 2 can guess what the pattern might be. Player 1 will then tell player 2 how many correct colors were guessed, and how many of those correctly guessed colors were guessed in the right place. Player 2 can then use this information to improve their guess for the next turn. When player 2 guesses the correct pattern, the game ends. Write a program that acts as player 1 of the Mastermind game. This program will generate a (random) pattern, and will play according to the game’s rules, with a human acting as player 2. The program should stop when the user guesses the pattern. Additionally, the program has to conform with the following specifications: 1. Because of practical reasons, the pattern will consist of capital letters instead of colors. 2. The number of different letters that can be used in a pattern must be specified by the human player. This number cannot be larger than 15 (A until O). 3. The length of the pattern must be specified by the human player. To generate a random pattern, the following function can be used: # This function generates a random string with length pattern_length. # The string can contain characters 'A'until 'A'+ number_of_letters - 1. import random def generate_pattern(number_of_letters, pattern_length): result = '' for i in range(pattern_length): letter_index = random.randint(1, number_of_letters) letter = chr(ord('A') + letter_index – 1) result += letter return result For example, generate_pattern(3, 4) will generate a random string of length 4, that consists of the letters A, B, and C. Example: Number of different letters: 14 Pattern length: 4 > AAAA correct letters: 2, letters in correct place: 2 > DCBA correct letters: 2, letters in correct place: 1 > AABN correct
Mastermind
In this assignment, you will program the well-known game Mastermind. In Mastermind, player 1
creates a pattern consisting of a pre-approved number of colored pins. Then player 2 has to guess
this pattern in as few turns as possible. Each turn, player 2 can guess what the pattern might
be. Player 1 will then tell player 2 how many correct colors were guessed, and how many of those
correctly guessed colors were guessed in the right place. Player 2 can then use this information to
improve their guess for the next turn. When player 2 guesses the correct pattern, the game ends.
Write a program that acts as player 1 of the Mastermind game. This program will generate a
(random) pattern, and will play according to the game’s rules, with a human acting as player 2.
The program should stop when the user guesses the pattern.
Additionally, the program has to conform with the following specifications:
1. Because of practical reasons, the pattern will consist of capital letters instead of colors.
2. The number of different letters that can be used in a pattern must be specified by the human
player. This number cannot be larger than 15 (A until O).
3. The length of the pattern must be specified by the human player.
To generate a random pattern, the following function can be used:
# This function generates a random string with length pattern_length.
# The string can contain characters 'A'until 'A'+ number_of_letters - 1.
import random
def generate_pattern(number_of_letters, pattern_length):
result = ''
for i in range(pattern_length):
letter_index = random.randint(1, number_of_letters)
letter = chr(ord('A') + letter_index – 1)
result += letter
return result
For example, generate_pattern(3, 4) will generate a random string of length 4, that consists of
the letters A, B, and C.
Example:
Number of different letters: 14
Pattern length: 4
> AAAA
correct letters: 2, letters in correct place: 2
> DCBA
correct letters: 2, letters in correct place: 1
> AABN
correct
Trending now
This is a popular solution!
Step by step
Solved in 2 steps