In: Computer Science
Fill in needed code to finish Mastermind program in Python 3.7 syntax
Plays the game of Mastermind.
The computer chooses 4 colored pegs (the code) from the
colors
Red, Green, Blue, Yellow, Turquoise, Magenta.
There can be more than one of each color, and order is
important.
The player guesses a series of 4 colors (the guess).
The computer compares the guess to the code, and tells the
player how
many colors in the guess are correct, and how many are in the
correct
place in the code. The hint is printed as a 4-character string,
where
* means correct color in correct place
o means correct color in incorrect place
- fills out remaining places to make 4
The player is allowed to make 12 guesses. If the player guesses
the colors
in the correct order before all 12 guesses are used, the player
wins.
'''
import random
def genCode(items, num):
'''
Generates a random code (a string of num characters)
from the characters available in the string of possible
items.
Characters can be repeated
items - a string of the possible items for the code
num - the number of characters in the code
returns the code string
'''
# write function body here
def valid(guess, items, num):
'''
Checks a guess string to see that it is the correct length and
composed
of valid characters.
guess - a string representing the guess
items - a string of the possible items for the code
num - the number of characters in the code
returns True if the guess is valid, False otherwise
'''
# write function body here
def getGuess(items, num):
'''
Gets a guess string from the user. If the guess is not valid length
and
characters, keeps asking until the guess is valid.
items - a string of the possible items for the code
num - the number of characters in the code
returns the valid guess
'''
# write function body here
def matched(code, guess):
'''
Checks to see that the code and the guess match.
code - the string with the secret code
guess - the string with the player's guess
returns True if they match, False otherwise
'''
# write function body here
def genHint(code, guess, items, num):
'''
Generates a string representing the hint to the user.
Tells the player how many colors in the guess are correct,
and how many are in the correct place in the code.
The hint is printed as a num-character string, where
* means correct color in correct place
o means correct color in incorrect place
- fills out remaining places to make num
code - the string with the secret code
guess - the string with the player's guess
items - a string of the possible items for the code
num - the number of characters in the code/hint
returns the valid hint as a string
'''
# write function body here
# Main program starts here
# colors for the code
colors = 'RGBYTM'
# length of the code
num = 4
# maximum number of guesses allowed
maxGuesses = 12
print('Plays the game of Mastermind.')
print()
print('The computer chooses', num, 'colored pegs (the code) from
the colors')
print(colors)
print('There can be more than one of each color, and order is
important.')
print()
print('The player guesses a series of', num, 'colors (the
guess).')
print()
print('The computer compares the guess to the code, and tells the
player how')
print('many colors in the guess are correct, and how many are in
the correct')
print('place in the code. The hint is printed as a', num,
'-character string, where')
print(' * means correct color in correct place')
print(' o means correct color in incorrect place')
print(' - fills out remaining places to make', num)
print()
print('The player is allowed to make', maxGuesses, 'guesses. If the
player guesses the colors')
print('in the correct order before all', maxGuesses, 'guesses are
used, the player wins.')
gameOver = False
guesses = 0
code = genCode(colors, num)
while not gameOver:
guess = getGuess(colors, num)
guesses = guesses + 1
if matched(code, guess):
print('You win!')
gameOver = True
continue
hint = genHint(code, guess, colors, num)
print(hint)
if guesses == maxGuesses:
print('You took to many guesses. The code was', code)
gameOver = True
Code:
import random
def genCode(items, num):
'''
Generates a random code (a string of num
characters)
from the characters available in the string of
possible items.
Characters can be repeated
items - a string of the possible items for the
code
num - the number of characters in the code
returns the code string
'''
# write function body here
code = ''.join(random.choices(items,k=num))
return code
def valid(guess, items, num):
'''
Checks a guess string to see that it is the correct
length and composed
of valid characters.
guess - a string representing the guess
items - a string of the possible items for the
code
num - the number of characters in the code
returns True if the guess is valid, False
otherwise
'''
# write function body here
length = len(guess)
subset = True
#checks if characters of guess are a subset of
items
for i in list(guess):
if i not in list(items) :
subset =
False
break
if subset and length == num :
return True
else:
return False
def getGuess(items, num):
'''
Gets a guess string from the user. If the guess is not
valid length and
characters, keeps asking until the guess is valid.
items - a string of the possible items for the
code
num - the number of characters in the code
returns the valid guess
'''
# write function body here
guess = input()
while not valid(guess,items,num):
guess = input("Invalid , please
enter again\n")
else :
return guess
def matched(code, guess):
'''
Checks to see that the code and the guess match.
code - the string with the secret code
guess - the string with the player's guess
returns True if they match, False otherwise
'''
# write function body here
if (code == guess):
return True
return False
def genHint(code, guess, items, num):
'''
Generates a string representing the hint to the
user.
Tells the player how many colors in the guess are
correct,
and how many are in the correct place in the
code.
The hint is printed as a num-character string,
where
* means correct color in correct place
o means correct color in incorrect place
- fills out remaining places to make num
code - the string with the secret code
guess - the string with the player's guess
items - a string of the possible items for the
code
num - the number of characters in the code/hint
returns the valid hint as a string
'''
# write function body here
#used this print for debugging
#print(guess+' '+code)
count = 0
correct = ['-']*num
for i in range(num):
if (code[i]==guess[i]):
correct[i] = '*'
else:
continue
for i in range(num):
var = guess[i]
if var in list(code) and
(guess[i]!=code[i]):
correct[i] =
'o'
return ''.join(correct)
# Main program starts here
# colors for the code
colors = 'RGBYTM'
# length of the code
num = 4
# maximum number of guesses allowed
maxGuesses = 12
print('Plays the game of Mastermind.')
print()
print('The computer chooses', num, 'colored pegs (the code) from
the colors')
print(colors)
print('There can be more than one of each color, and order is
important.')
print()
print('The player guesses a series of', num, 'colors (the
guess).')
print()
print('The computer compares the guess to the code, and tells the
player how')
print('many colors in the guess are correct, and how many are in
the correct')
print('place in the code. The hint is printed as a', num,
'-character string, where')
print(' * means correct color in correct place')
print(' o means correct color in incorrect place')
print(' - fills out remaining places to make', num)
print()
print('The player is allowed to make', maxGuesses, 'guesses. If the
player guesses the colors')
print('in the correct order before all', maxGuesses, 'guesses are
used, the player wins.')
gameOver = False
guesses = 0
code = genCode(colors, num)
while not gameOver:
guess = getGuess(colors, num)
guesses = guesses + 1
if matched(code, guess):
print('You win!')
gameOver = True
continue
hint = genHint(code, guess, colors, num)
print(hint)
if guesses == maxGuesses:
print('You took to many guesses.
The code was', code)
gameOver = True
Output :
o - here for Y was displayed and it had repeated twice at the beginning that is correct color but incorrect place
Here the string was tested for validity and several characters were correct but in incorrect place