I made Master Mind on Python!
import random
color = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
def gen_colors(code_size):
code = ""
for i in range(code_size):
if i > 25:
return code
code += color[i]
return code
def gen_code(code_size, colors):
code = ""
for i in range(code_size):
code += colors[random.randint(0, len(colors) - 1)]
return code
def check_guess(guess, code_size, colors):
if len(guess) != code_size:
return False
for i in range(len(guess)):
if guess[i] not in colors:
return False
return True
def score_guess(code, guess):
correct = 0 # correct is count of right letter in its place
correct1 = 0 # correct1 is count of right letter out of place
for i in range(0, len(code)):
if guess[i] == code[i]:
correct += 1
if guess[i] in code and guess[i] != code[i]:
correct1 += 1
return tuple([correct, correct1])
def play_cli(code_size, nb_colors):
attempts = 0
possible_colors = gen_colors(nb_colors)
print(f"Possible colors are {possible_colors}")
print(f"Code is size {code_size}.")
code = gen_code(code_size, possible_colors)
while True:
guess = str(input())
if guess == "fall":
print(f"The code is {code}")
break
attempts += 1
if guess == code:
print(f"Congrats, you won after {attempts} attempts!")
break
if check_guess(guess, code_size, possible_colors) == True:
print(score_guess(code, guess))
else:
attempts -= 1
print("Wrong size or color!")
play_cli(4, 6)
should output:
Possible colors are ABCDEF
Code is size 4.
But it has a inaccuracy:
Letās say the random code is FFEE
,
We introduce FFFF
,
It print:
(2, 2) # 2 letters in place, 2 letters out of place
But I want it to print:
(2, 0) # 2 letters in place