import random
from itertools import permutations
def scorecalc(guess, chosen):
bulls = cows = 0
for g,c in zip(guess, chosen):
if g == c:
bulls += 1
elif g in chosen:
cows += 1
return bulls, cows
print("Wellcom to bulls and cows.")
choices = list(permutations('0123456789', 4))
checking = list(permutations('0123456789', 4))
random.shuffle(choices)
answers, scores = [], []
comp_number = ''.join(random.sample("0123456789", 4))
while True:
print("Your turn.")
hum_guess = input("Try to guess my number: ")
while not hum_guess.isnumeric() or len(hum_guess) != 4 or tuple(hum_guess) not in checking:
hum_guess = input("Invalid input. Please type 4 UNIQUE digits: ")
hum_cows = 0
hum_bulls = 0
if hum_guess == comp_number:
print(f"Good job! My number was {comp_number}")
break
for i in range(4):
if hum_guess[i] == comp_number[i]:
hum_bulls += 1
elif hum_guess[i] in comp_number:
hum_cows += 1
print(f"You have {hum_bulls} bulls and {hum_cows} cows")
answers.append(choices[0])
if len(choices) == 1:
print (f"There are no other options available, so your number is {''.join(choices[0])}")
print("Thanks for playing :)")
break
print(f"My guess is {''.join(choices[0])}")
comp_bulls = input("Tell me how many Bulls I have: ")
while not comp_bulls.isnumeric() or len(comp_bulls) != 1 or int(comp_bulls) > 4:
comp_bulls = input("Invalid input. Please type a number between 1 and 4: ")
if int(comp_bulls) == 4:
print("Yay. I guessed your number.")
print("Thanks for playing :)")
break
comp_cows = input("Tell me how many Cows I have: ")
while not comp_cows.isnumeric() or len(comp_cows) != 1 or int(comp_cows) > 4:
comp_cows = input("Invalid input. Please type a number between 1 and 4: ")
while int(comp_cows) + int(comp_bulls) > 4:
print(f"Invalid input for Bulls: {comp_bulls} and Cows: {comp_cows}.")
print("Please check again and tell me the actual number of Bulls and Cows I have.")
comp_bulls = input("How many Bulls I have: ")
comp_cows = input("How many Cows I have: ")
score = (int(comp_bulls), int(comp_cows))
scores.append(score)
found = score == (4, 0)
choices = [c for c in choices if scorecalc(c, choices[0]) == score]
if not choices:
print("Something is wrong.. Nothing fits those scores you gave:")
print(' ' + '\n '.join(f"{''.join(an)} -> Bulls: {sc[0]} Cows: {sc[1]}" for an,sc in zip(answers, scores)))
break