1
+ import random
2
+ import string
3
+ import os
4
+ # Create a program that will play the “cows and bulls” game with the user.
5
+ # The game works like this:
6
+ #/1. Randomly generate a 4-digit number *where each digit is unique.
7
+ #/2. Ask the user to guess a 4-digit number.
8
+ #/3. For every digit that the user guessed correctly in the correct place,
9
+ # they have a “cow”.
10
+ #/4. For every digit the user guessed correctly in the wrong place,
11
+ # they have a “bull.”
12
+ #/5. Every time the user makes a guess, tell them how many “cows” and
13
+ # “bulls” they have.
14
+ #/6. Once the user guesses the correct number, the game is over.
15
+ #/7. Keep track of the number of guesses the user makes throughout the
16
+ # game and tell the user at the end.
17
+ # I actually watched Etho make a version of this game in his Minecraft
18
+ # world recently. Dunno if he ever finished it....
19
+
20
+ def isgoodnum (num ):
21
+ '''
22
+ Takes in a number and determines if it is four in length and only contains digits
23
+ Returns 1 if yes and 0 if no
24
+ '''
25
+ if len (num ) == 4 and not 0 in [1 if each .isdigit () else 0 for each in num ]:
26
+ return 1
27
+ else :
28
+ return 0
29
+
30
+ def askforguess ():
31
+ guess = input ("Please guess a four digit number.\n " )
32
+ while (not isgoodnum (guess )):
33
+ guess = input ("You must guess a four digit number.\n " )
34
+ return guess
35
+
36
+
37
+ def cowsandbulls (mynum , guess ):
38
+ #calc number of digits that overlap in mynum and guess = overlap
39
+ #calc number of digits that are in the same spot in mynum and guess = cows
40
+ #calc overlap - cows = bulls
41
+ overlap = 0
42
+ cows = 0
43
+ bulls = 0
44
+ for index in range (4 ):
45
+ if guess [index ] in mynum :
46
+ overlap += 1
47
+ if guess [index ] == mynum [index ]:
48
+ cows += 1
49
+ bulls = overlap - cows
50
+ return "You have " + "" .join (["1 cow" if cows == 1 else str (cows ) + " cows" ]) + " and " + "" .join (["1 bull." if bulls == 1 else str (bulls ) + " bulls." ])
51
+
52
+
53
+ if __name__ == "__main__" :
54
+ mynum = set ()
55
+ while (len (mynum ) < 4 ):
56
+ mynum .add (str (random .choice (string .digits )))
57
+ mynum = "" .join (each for each in mynum ) # mynum is saved as a string
58
+ numguesses = 0 # numguesses saved as int
59
+
60
+ print ("My number is " + mynum )
61
+
62
+ print ("Welcome to the game of Cows and Bulls! I have written down a number containing four unique digits, and you must guess it!" )
63
+ input ("Hit enter when you're ready to start." )
64
+
65
+ os .system ('CLS' )
66
+
67
+ while (True ): #Will break once player guesses correctly
68
+ guess = askforguess () # guess is saved as a string
69
+ numguesses += 1
70
+ if guess == mynum :
71
+ break
72
+ else :
73
+ print (cowsandbulls (mynum , guess ))
74
+
75
+ if numguesses == 1 :
76
+ print ("You guessed my number in only 1 guess. Sheer luck!" )
77
+ else :
78
+ print ("You guessed my number in " + str (numguesses ) + " guesses." )
0 commit comments