#!/usr/bin/env python3
"""
Created on Mon Jan 29 06:47:58 2018

@author: Swathi
"""
'''
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. Once the user guesses the correct number, the game is over. 
 Keep track of the number of guesses the user makes throughout teh game and tell the user at the end.

Say the number generated by the computer is 1038. An example interaction could look like this:

  Welcome to the Cows and Bulls Game! 
  Enter a number: 
  >>> 1234
  2 cows, 0 bulls
  >>> 1256
  1 cow, 1 bull
  ...

Until the user guesses the number.
'''
import random

def numToList(num):
    return [i for i in str(num)]
    
def random_with_N_digits(n):
    range_start = 10**(n-1)
    range_end = (10**n)-1
    return random.randint(range_start, range_end)

def cowsandbulls(rand, usr):
    rand_list = numToList(rand)
    usr_list = numToList(usr)
    if usr_list != rand_list:
        cows = 0
        for i,j in zip(usr_list,rand_list):
            if i == j: 
                cows+=1
        return cows,4-cows
    else:
        print ("You have guessed the number\n")
        return 4,0          

if __name__=="__main__":
    print ("Welcome to the Cows and Bulls Game !")
    print ("------Guess the 4 digit number------\n-----Cow - correct,Bull - wrong-----")
    rand_num = random_with_N_digits(4)
    print ("Computer chose a random 4 digit number: {}".format(rand_num))
    cows = 0
    usr_attempts = 0
    
    while cows != 4:
        usr_guess = input ("\nEnter your 4 digit guess: ")
        usr_attempts += 1
        cows,bulls = cowsandbulls(rand_num, usr_guess)
        print ("Attempt #{}: {} cows, {} bulls".format(usr_attempts,cows,bulls))

        

#!/usr/bin/env python3
"""
Created on Mon Jan 29 06:47:58 2018

@author: Swathi
"""
'''
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. Once the user guesses the correct number, the game is over. 
 Keep track of the number of guesses the user makes throughout teh game and tell the user at the end.

Say the number generated by the computer is 1038. An example interaction could look like this:

  Welcome to the Cows and Bulls Game! 
  Enter a number: 
  >>> 1234
  2 cows, 0 bulls
  >>> 1256
  1 cow, 1 bull
  ...

Until the user guesses the number.
'''
import random

def numToList(num):
    return [i for i in str(num)]
    
def random_with_N_digits(n):
    range_start = 10**(n-1)
    range_end = (10**n)-1
    return random.randint(range_start, range_end)

def cowsandbulls(rand, usr):
    rand_list = numToList(rand)
    usr_list = numToList(usr)
    if usr_list != rand_list:
        cows = 0
        for i,j in zip(usr_list,rand_list):
            if i == j: 
                cows+=1
        return cows,4-cows
    else:
        print ("You have guessed the number\n")
        return 4,0          

if __name__=="__main__":
    print ("Welcome to the Cows and Bulls Game !")
    print ("------Guess the 4 digit number------\n-----Cow - correct,Bull - wrong-----")
    rand_num = random_with_N_digits(4)
    print ("Computer chose a random 4 digit number: {}".format(rand_num))
    cows = 0
    usr_attempts = 0
    
    while cows != 4:
        usr_guess = input ("\nEnter your 4 digit guess: ")
        usr_attempts += 1
        cows,bulls = cowsandbulls(rand_num, usr_guess)
        print ("Attempt #{}: {} cows, {} bulls".format(usr_attempts,cows,bulls))

        

#!/usr/bin/env python3 Created on Mon Jan 29 06:47:58 2018  @author: Swathi 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. Once the user guesses the correct number, the game is over.   Keep track of the number of guesses the user makes throughout teh game and tell the user at the end.  Say the number generated by the computer is 1038. An example interaction could look like this:    Welcome to the Cows and Bulls Game!    Enter a number:    >>> 1234   2 cows, 0 bulls   >>> 1256   1 cow, 1 bull   ...  Until the user guesses the number. #{}: {} cows, {} bulls".format(usr_attempts,cows,bulls))