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

Tip: To determine how many correct capital letters were guessed, you can use a frequency table that stores how many times each letter appears. This frequence table can be stored as a dictionary with letters as keys, and the number of appearances of those letters as values.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.