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
def valid_comp(the_input):
if not the_input.isnumeric() or int(the_input) > 4 or int(the_input) < 0:
return valid_comp(input("Invalid input. Please type a number between 1 and 4: "))
return int(the_input)
def valid_hum(the_input):
if not the_input.isnumeric() or int(the_input) < 0 or tuple(the_input) not in checking:
return valid_hum(input("Invalid input. Please type 4 UNIQUE digits: "))
return the_input
print("Wellcom to bulls and cows.")
rules = input("If you want to see the rules of the game please type 'yes'.\nIf you alredy know the rules please type'no'.\n").strip().lower()
if rules == "yes":
print("""\n\nThe player must write a 4-digit secret number (on a piece of paper or on your computer somewhere).
The digits must be all different. Then, in turn, the player try to guess their opponent's number
who gives the number of matches.
If the matching digits are in their right positions, they are "bulls", if in different positions,
they are "cows".
Example:
Secret number: 4271
Opponent's try: 1234
Answer: 1 bull and 2 cows. (The bull is "2", the cows are "4" and "1".)
The first player to reveal the other's secret number wins the game.\n\n""")
choices = list(permutations('0123456789', 4))
checking = list(permutations('0123456789', 4))
random.shuffle(choices)
answers, scores = [], []
comp_number = ''.join(random.sample("0123456789", 4))
print("Ready to try it out?")
while True:
hum_guess = valid_hum(input("\nTry to guess my number: ").strip())
hum_cows = 0
hum_bulls = 0
if hum_guess == comp_number:
print(f"\nGood 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"\nThere are no other options available, so your number is {''.join(choices[0])}")
print("\nThanks for playing :)")
break
print(f"\nMy guess is {''.join(choices[0])}")
comp_bulls = valid_comp(input("Tell me how many Bulls I have: ").strip())
if comp_bulls == 4:
print("\nYay. I guessed your number.")
print("Thanks for playing :)")
break
comp_cows = valid_comp(input("Tell me how many Cows I have: ").strip())
if comp_cows + comp_bulls > 4:
print(f"\nInvalid 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 = valid_comp(input("\nHow many Bulls I have: ").strip())
comp_cows = valid_comp(input("How many Cows I have: ").strip())
score = (comp_bulls, 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("\nSomething 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