Photo by Angelina Litvin on Unsplash
I found an algorithm I wrote in Python to simulate the Bulls and Cows game! Here is a simple description of the game:
Create a program that will play the “cows and bulls” game with the user. The game works like this:
Randomly generate a 4-digit number. Ask the user to guess a 4-digit number. For every digit that the user guessed correctly in the correct place, they have a “cow”. For every digit the user guessed correctly in the wrong place is a “bull.” Every time the user makes a guess, tell them how many “cows” and “bulls” they have.
import random
def generate_secret():
#Generates a 4 digit number with no repeat digits
#It converts the number to a string and returns it
# sets secret number to an empty string
secret = ''
# this while loop ensures that the secret number stops at 4 digits
while len(secret) <4:
k = random.randint(0,9)
k = str(k)
#set initial condition for inString
inString = False
'''iterates through whatever we have for secret to see if k = anything
in secret '''
for i in range (0, len(secret)):
if k == secret[i]:
inString = True
#only if k is not already in the string we can add it to secret
if inString == False:
secret = secret + k
return secret
def how_many_bulls(answer,guess):
#Returns the number of bulls the guess earns when the
#secret number is answer
bulls = 0
#the number after bulls_ represents where the bulls are within the string index
#currently the bulls we have found at each index is 0
bulls_0 = 0
bulls_1 = 0
bulls_2 = 0
bulls_3 = 0
if answer[0] == guess[0]:
bulls_0 = bulls_0 + 1
if answer[1] == guess[1]:
bulls_1 = bulls_1 + 1
if answer[2] == guess[2]:
bulls_2 = bulls_2 + 1
if answer[3] == guess [3]:
bulls_3 = bulls_3 + 1
# total amount of bulls are all the bulls added today
bulls = bulls_0 + bulls_1 + bulls_2 + bulls_3
'''we create an array so we can return number of bulls and
if there is a bull at each index'''
bulls_array= [bulls_0, bulls_1, bulls_2, bulls_3, bulls]
return bulls_array
def how_many_cows(answer, guess, bulls_0, bulls_1, bulls_2, bulls_3):
#Returns the number of bulls the guess earns when the secret number is answer
'''what the numbers mean: _0_1 means if there's a cow at the guess[0]
that corresponds to answer[1]'''
'''here we list all possible cows and since we haven't found any yet set
them equal to 0'''
cows_0_1, cows_0_2, cows_0_3 = 0, 0, 0
cows_1_0, cows_1_2, cows_1_3 = 0, 0, 0
cows_2_0, cows_2_1, cows_2_3 = 0, 0, 0
cows_3_0, cows_3_1, cows_3_2 = 0, 0, 0
if bulls_0 == 0: #ensures that there cannot be a bull at index 0 already
if guess[0] == answer[1]:
cows_0_1 = 1
if guess[0] == answer[2]:
cows_0_2 = 1
if guess[0] == answer[3]:
cows_0_3 = 1
''' we must check that there are no bulls or preexisting cows in those locations
because say we have 4676 as the guess and 4567 as the answer: you wouldn't
want two cows for the same 6 in the answer'''
if bulls_1 == 0:
if guess[1] == answer[0] and bulls_0 == 0: # can't be another cow or bull at answer[0]
cows_1_0 = 1
if cows_0_2 == 0 and bulls_2 == 0:
if guess[1] == answer[2]:
cows_1_2 = 1
if cows_0_3 == 0 and bulls_3 == 0:
if guess[1] == answer[3]:
cows_1_3 = 1
if bulls_2 == 0:
if cows_1_0 == 0 and bulls_0 == 0:
if guess[2] == answer[0]:
cows_2_0 = 1
if cows_0_1 == 0 and bulls_1 == 0:
if guess[2] == answer[1]:
cows_2_1 = 1
if cows_0_3 == 0 and cows_1_3 == 0 and bulls_3 == 0:
if guess[2] == answer[3]:
cows_2_3 = 1
if bulls_3 == 0:
if cows_1_0 == 0 and cows_2_0 == 0 and bulls_0 == 0:
if guess[3] == answer[0]:
cows_3_0 = 1
if cows_0_1 == 0 and cows_2_1 == 0 and bulls_1 == 0:
if guess[3] == answer[1]:
cows_3_1 = 1
if cows_0_2 == 0 and cows_1_2 == 0 and bulls_2 == 0:
if guess[3] == answer[2]:
cows_3_2 = 1
#total number of cows added up
cows = cows_0_1 + cows_0_2 + cows_0_3 + cows_1_0 + cows_1_2 + cows_1_3 + \
cows_2_0 + cows_2_1 + cows_2_3 + cows_3_0 + cows_3_1 + cows_3_2
return cows