Question

In: Computer Science

Fill in needed code to finish Mastermind program in Python 3.7 syntax Plays the game of...

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

Solutions

Expert Solution

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


Related Solutions

Using Python, Assignment Write a program that plays a game of craps. The program should allow...
Using Python, Assignment Write a program that plays a game of craps. The program should allow the player to make a wager before each “turn”. Before each turn, allow the user to either place a bet or exit the game. After each turn display the player’s current balance. Specifics Each player will start with $500.00. Initially, and after each turn give the user the option of betting or leaving the program. Implement this any way you wish, but make it...
In python 3.7: You’re going to program a simulation of the following game. Like many probability...
In python 3.7: You’re going to program a simulation of the following game. Like many probability games, this one involves an infinite supply of ping-pong balls. No, this game is "not quite beer pong." The balls are numbered 1 through N. There is also a group of N cups, labeled 1 through N, each of which can hold an unlimited number of ping-pong balls (all numbered 1 through N). The game is played in rounds. A round is composed of...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out...
Python: Write a program that plays Tic-tac-toe with a user. Each round, the game prints out the state of the board, asks the user where they would like to place their mark, and implements this decision. The program then places its own mark on a randomly chosen available position. Once one of the player won, the program declares the result and asks if the user would like to continue. The first player is selected at random.
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this...
Create a C++ code for the mastermind game using classes(private classes and public classes). Using this uml as a reference.
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking...
PYTHON (Game: Tic-tac-toe): Write a program that plays the tic-tac-toe game. Two players take turns clicking an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has...
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
Please finish this code (Java) 1. Fill in the compare methods for the CompareByPlayoffsAndSalary and CompareByWinsLossesChamps...
Please finish this code (Java) 1. Fill in the compare methods for the CompareByPlayoffsAndSalary and CompareByWinsLossesChamps classes 2. In the CompareByPlayoffsAndSalary the compare method should list: (a) teams that made the playoffs last year before teams that did not. (b) If this is the same then it should list teams with lower salary first. 3. In the CompareByWinsLossesChamps list: (a) teams with more wins first. (b) If the same then list by teams with fewer losses. (c) If this is...
Write a program in Matlab where the program plays a hot and cold game. The user...
Write a program in Matlab where the program plays a hot and cold game. The user answers two inputs: x=input('what is the x location of the object?') y=input('what is the y location of the object?') You write the program so that the computer plays the game. Pick a starting point. Program Calculates the distance to the object. Program picks another point, calculates the distance to the object. Program knows its at the right spot when the distance is less than...
Write a program that plays an addition game with the user (imagine the user is a...
Write a program that plays an addition game with the user (imagine the user is a 5th grade student). First ask the user how many addition problems they want to attempt. Next, generate two random integers in the range between 10 and 50 inclusive. You must use the Math.random( ) method call.   Prompt the user to enter the sum of these two integers. The program then reports "Correct" or "Incorrect" depending upon the user's "answer".  If the answer was incorrect, show...
((PYTHON)) Finish the calories_burned_functions.py program that we started in class. Take the original calories_burned program and...
((PYTHON)) Finish the calories_burned_functions.py program that we started in class. Take the original calories_burned program and rework it so that it uses two functions/function calls. Use the following file to get your program started: """ ''' Women: Calories = ((Age x 0.074) - (Weight x 0.05741) + (Heart Rate x 0.4472) - 20.4022) x Time / 4.184 ''' ''' Men: Calories = ((Age x 0.2017) + (Weight x 0.09036) + (Heart Rate x 0.6309) - 55.0969) x Time / 4.184...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT