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

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 1GZ
icon
Related questions
Question

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

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT