mastermind in pygame code example
Example: simple mastermind programpython
import random
import os
def clear():
os.system("clear")
def print_mastermind_board(passcode, guess_codes, guess_flags):
print("-----------------------------------------")
print("\t MASTERMIND")
print("-----------------------------------------")
print(" |", end="")
for x in passcode:
print("\t" + x[:3], end="")
print()
for i in reversed(range(len(guess_codes))):
print("-----------------------------------------")
print(guess_flags[i][0], guess_flags[i][1], "|")
print(guess_flags[i][2], guess_flags[i][3], end=" |")
for x in guess_codes[i]:
print("\t" + x[:3], end="")
print()
print("-----------------------------------------")
if __name__ == '__main__':
colors = ["RED", "GREEN", "YELLOW", "BLUE", "BLACK", "ORANGE"]
colors_map = {1:"RED", 2:"GREEN", 3:"YELLOW", 4:"BLUE", 5:"BLACK", 6:"ORANGE"}
random.shuffle(colors)
passcode = colors[:4]
chances = 8
show_passcode = ['UNK', 'UNK', 'UNK', 'UNK']
guess_codes = [['-', '-', '-', '-'] for x in range(chances)]
guess_flags = [['-', '-', '-', '-'] for x in range(chances)]
clear()
turn = 0
while turn < chances:
print("-----------------------------------------")
print("\t\tMenu")
print("-----------------------------------------")
print("Enter code using numbers.")
print("1 - RED, 2 - GREEN, 3 - YELLOW, 4 - BLUE, 5 - BLACK, 6 - ORANGE")
print("Example: RED YELLOW ORANGE BLACK ---> 1 3 6 5")
print("-----------------------------------------")
print_mastermind_board(show_passcode, guess_codes, guess_flags)
try:
code = list(map(int, input("Enter your choice = ").split()))
except ValueError:
clear()
print("\tWrong choice!! Try again!!")
continue
if len(code) != 4:
clear()
print("\tWrong choice!! Try again!!")
continue
flag = 0
for x in code:
if x > 6 or x < 1:
flag = 1
if flag == 1:
clear()
print("\tWrong choice!! Try again!!")
continue
for i in range(4):
guess_codes[turn][i] = colors_map]
dummy_passcode = [x for x in passcode]
pos = 0
for x in code:
if colors_map[x] in dummy_passcode:
if code.index(x) == passcode.index(colors_map[x]):
guess_flags[turn][pos] = 'R'
else:
guess_flags[turn][pos] = 'W'
pos += 1
dummy_passcode.remove(colors_map[x])
random.shuffle(guess_flags[turn])
if guess_codes[turn] == passcode:
clear()
print_mastermind_board(passcode, guess_codes, guess_flags)
print("Congratulations!! YOU WIN!!!!")
break
turn += 1
clear()
if turn == chances:
clear()
print_mastermind_board(passcode, guess_codes, guess_flags)
print("YOU LOSE!!! Better luck next time!!!")