Last active
October 4, 2020 17:12
-
-
Save vaebhav/757b34e5d35708c5ae9d36bb711b74bc to your computer and use it in GitHub Desktop.
An implementation of Cows and Bulls Game in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Cows and Bulls game | |
#!/usr/local/bin/python3 | |
import math | |
import random | |
def cowsandbulls(): | |
r = random.randint(1111,9999) | |
tryagain = 'yes' | |
print ('Hello! Welcome to the game of cows and bulls') | |
print ('------------------------------------------------------------- ') | |
print ('Try and guess the 4 digit number in the least number of tries ') | |
print ('------------------------------------------------------------- ') | |
cows = 0 | |
bulls = 0 | |
guess_counter = 0 | |
while tryagain == 'yes' or tryagain=='y': | |
guess = 0 | |
guess = input('Guess:--->') | |
#guess = int(input('Guess:--->')) | |
if guess.isnumeric(): | |
guess = int(guess) | |
else: | |
print ('Guess should be numeric only!!!') | |
break | |
if len(str(guess)) > 5: | |
print ('Input Digits should not be greater then 4') | |
tryagain = str(input('Try New Guess (Yes/Y): ')).lower() | |
if tryagain.isalpha(): | |
if tryagain == 'yes' or tryagain == 'y': | |
#guess = int(input('New Guess:--->')) | |
continue | |
else: | |
break | |
else: | |
print('Input should be string') | |
random_num = r | |
#print("Random--->",random_num) | |
while guess > 0: | |
reminder_guess = guess % 10 | |
reminder_rand = random_num % 10 | |
if reminder_guess == reminder_rand: | |
cows += 1 | |
else: | |
bulls += 1 | |
#print("Number Guess--->",reminder_guess) | |
#print("Number Rand--->",reminder_rand) | |
guess //= 10 | |
random_num //= 10 | |
print("Cows--->",cows) | |
print("Bulls--->",bulls) | |
if cows == 4: | |
print("You Won!!!") | |
break | |
cows = 0 | |
bulls = 0 | |
tryagain = str(input('Try Again (Yes/Y) : ')).lower() | |
if not tryagain.isalpha(): | |
print ('Input should be string\n') | |
guess_counter += 1 | |
if guess_counter == 10: | |
print("Random--->",random_num) | |
print("No of Guesses------>",guess_counter) | |
cowsandbulls() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment