• 欢迎光临~

# Implement the MasterMind game.

The computer “thinks” a number with 4 different digits.
You guess which digits. For every digit that matched both
in value, and in location the computer gives you a *. For every
digit that matches in value, but not in space the computer gives
you a +. Try to guess the given number in as few guesses as possible.

``````#Solution: MasterMind

import random

width = 4
USED = '_'

hidden = random.sample(range(10), width)
# print(hidden)

while True:
#print(hidden)

inp = input("you guess ({}digits):".format(width))
if inp == 'x':
print("Bye")
exit()
if len(inp) != width:
print("We need exactly {} characters".format(width))
continue

guess = list(map(int, inp))
#print(guess)

if hidden == guess:
print("Match!")
break
my_hidden = hidden[:]
my_guess = guess[:]

result = ''
for i in range(width):
if my_hidden[i] == my_guess[i]:
result += "*"
my_hidden[i] = USED
my_guess[i] = USED
for i in range(width):
if my_guess[i] ==USED:
continue
if my_guess[i] in my_hidden:
loc = my_hidden.index(my_guess[i])
my_hidden[loc] = USED
guess[i] = USED
result += '+'
print(''.join(result))
``````
``````    you guess (4digits):0123
+
+
you guess (4digits):4567
+
++
++
++
you guess (4digits):2458
+
++
+++
++++
you guess (4digits):4285
+
++
+++
++++
you guess (4digits):5824
**+
**++
you guess (4digits):5842
Match!
``````