Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in Python that will play the game of MAstermind wher the compute

ID: 3825998 • Letter: W

Question

Write a program in Python that will play the game of MAstermind wher the computer chooses the hidden colors, and the human player attempts to guess the hidden colors.

The Game: In the game of Mastermind, one player, the codemaker (in this case the computer) chooses four colored "pegs" in a particular order. The codebreaker (the human player) tries to guess the chosen colors by placing four pegs in the guessed order. For each guess, the codemaker provides a clue about how well the codebreaker guessed. The codebreaker has 10 guesses to break the code. Hidden Colors The codemaker will randomly choose four colors as the hidden colors. You can define a global list: ALL COLORS 'red Orange yellow green blue purple The hidden colors that are chosen will not be displayed to the codebreaker. Opponent Guess The codebreaker will be asked to choose four colors, in a particular order, to guess what the hidden colors are. For simplicity, you may assign a number to each color so it is easier for the user to enter a guess. For instance, you can display a message like the following: Make a guess of four colors: 0 red. 1 Orange 2 yellow green. 4 blue 5 purple Guess color: 0 Guess color: 1 Guess color: 4 Guess color: 0 Your guess is: 'red Orange blue red. J

Explanation / Answer

import random

print (" --- MASTERMIND --- ")

print ("Guess the secret color code in as few tries as possible. ")

print ("Please, enter your color code. You can use red(R), green(G), blue(B), yellow(Y), white(W) and pink(P)")

colors = ["R", "G", "B", "Y", "W", "P"]

attempts = 0

game = True

color_code = random.sample(colors,4)

print (color_code)

while game:

correct_color = ""

guessed_color = ""

player_guess = input().upper()

attempts += 1

if len(player_guess) != len(color_code):

print (" The secret code has exactly four colors. I know, you can count to four. Try again!")

continue

for i in range(4):

if player_guess[i] not in colors:

print (" Look up what colors you can use in this game. You are not a daltonist, are you?")

continue

if correct_color != "XXXX":

for i in range(4):

if player_guess[i] == color_code[i]:

correct_color += "X"

if player_guess[i] != color_code[i] and player_guess[i] in color_code:

guessed_color += "O"

print (correct_color + guessed_color + " ")                     

if correct_color == "XXXX":

if attempts == 1:

print ("Wow! You guessed at the first attempt!")

else:

print ("Well done... You needed " + str(attempts) + " attempts to guess.")

game = False

if attempts >= 1 and attempts <6 and correct_color != "XXXX":

print ("Next attempt: ")

elif attempts >= 6:

print ("You didn't guess! The secret color code was: " + str(color_code))               

while game == False:

finish_game = input(" Do you want to play again (Y/N)?").upper()       

attempts = 0

if finish_game =="N":

print ("Thanks for the game! Bye, bye!")

elif finish_game == "Y":

game = True

print ("So, let's play again... Guess the secret code: ")

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote