Bulls and cows: Difference between revisions
Content added Content deleted
(Similar to, but distinct from, Mastermind.) |
m (→{{header|Python}}: Might as well keep size as a variable) |
||
Line 33: | Line 33: | ||
and len(set(guess)) == size: |
and len(set(guess)) == size: |
||
break |
break |
||
print "Problem, try again. You need to enter |
print "Problem, try again. You need to enter %i unique digits from 1 to 9" % size |
||
if guess == chosen: |
if guess == chosen: |
||
print '\nCongratulations you guessed correctly in',guesses,'attempts' |
print '\nCongratulations you guessed correctly in',guesses,'attempts' |
||
Line 49: | Line 49: | ||
Next guess [1]: 79 |
Next guess [1]: 79 |
||
Problem, try again. You need to enter |
Problem, try again. You need to enter 4 unique digits from 1 to 9 |
||
Next guess [1]: 7983 |
Next guess [1]: 7983 |
Revision as of 14:52, 3 May 2009
Bulls and cows
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
This is an old game played with pencil and paper that was later implemented on computer.
The task is for the program to create a four digit random number from the digits 1 to 9, without duplication. The program should ask for guesses to this number, reject guesses that are malformed, then print the score for the guess.
The score is computed as:
- The player wins if the guess is the same as the randomly chosen number, and the program ends.
- A score of one bull is accumulated for each digit in the guess that equals the corresponding digit in the randomly chosen initial number.
- A score of one cow is accumulated for each digit in the guess that also appears in the randomly chosen number, but in the wrong position.
Python
<lang python>
Bulls and cows. A game pre-dating, and similar to, Mastermind.
import random
digits = '123456789' size = 4 chosen = .join(random.sample(digits,size))
- print chosen # Debug
print I have chosen a number from %s unique digits from 1 to 9 arranged in a random order. You need to input a %i digit, unique digit number as a guess at what I have chosen % (size, size) guesses = 0 while True:
guesses += 1 while True: # get a good guess guess = raw_input('\nNext guess [%i]: ' % guesses).strip() if len(guess) == 4 and \ all(char in digits for char in guess) \ and len(set(guess)) == size: break print "Problem, try again. You need to enter %i unique digits from 1 to 9" % size if guess == chosen: print '\nCongratulations you guessed correctly in',guesses,'attempts' break bulls = cows = 0 for i in range(size): if guess[i] == chosen[i]: bulls += 1 elif guess[i] in chosen: cows += 1 print ' %i Bulls\n %i Cows' % (bulls, cows)</lang>
Sample output:
I have chosen a number from 4 unique digits from 1 to 9 arranged in a random order. You need to input a 4 digit, unique digit number as a guess at what I have chosen Next guess [1]: 79 Problem, try again. You need to enter 4 unique digits from 1 to 9 Next guess [1]: 7983 2 Bulls 2 Cows Next guess [2]: 7938 Congratulations you guessed correctly in 2 attempts