Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 60 additions & 50 deletions mastermind colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,73 @@

import random

print (" --- MASTERMIND --- \n")
print ("Guess the secret color code in as few tries as possible.\n")
print ("Please, enter your color code.\nYou can use red(R), green(G), blue(B), yellow(Y), white(W) and pink(P)")
print(" --- MASTERMIND --- \n")
print("Guess the secret color code in as few tries as possible.\n")
print("Please, enter your color code.\nYou 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

# computer randomly picks four-color code
color_code = random.sample(colors,4)
print (color_code)
#color_code = random.sample(colors, 4)
color_code = "BGRY"
print(color_code)

# player guesses the number
# player guesses the number
while game:
correct_color = ""
guessed_color = ""
player_guess = input().upper()
attempts += 1

# checking if player's input is correct
if len(player_guess) != len(color_code):
print ("\nThe 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 ("\nLook up what colors you can use in this game. You are not a daltonist, are you?")
continue

# comparison between player's input and secret code
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 + "\n")

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))
correct_color = ""
guessed_color = ""
mem_color = ""
player_guess = input().upper()
attempts += 1

# play or not to play
while game == False:
finish_game = input("\nDo 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: ")
# checking if player's input is correct
if len(player_guess) != len(color_code):
print("\nThe 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("\nLook up what colors you can use in this game. You are not a daltonist, are you?")
continue

# --- end --- #

# comparison between player's input and secret code
if correct_color != "XXXX":
for i in range(4):
if player_guess[i] == color_code[i]:
correct_color += "X"
if player_guess[i] not in mem_color:
mem_color += player_guess[i]
elif player_guess[i] in mem_color:
guessed_color = guessed_color[:-1]
if player_guess[i] != color_code[i] and player_guess[i] in color_code and player_guess[i] not in mem_color:
guessed_color += "O"
if player_guess[i] not in mem_color:
mem_color += player_guess[i]
print(correct_color + guessed_color + "\n")


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))
game = False
# play or not to play
while game == False:
finish_game = input("\nDo 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: ")

# --- end --- #