Jump to content

Bulls and cows: Difference between revisions

m
→‎{{header|Julia}}: forgot to add score.
(→‎{{header|Julia}}: another version with better input checking)
m (→‎{{header|Julia}}: forgot to add score.)
Line 1,945:
The following version checks thoroughly that the input of the player is constituted of four distincts digits.
<lang julia>function bullsandcows ()
bulls = cows = turns = 0
result = (s = {} ; while length(unique(s))<4 push!(s,rand('1':'9')) end; unique(s))
println("A game of bulls and cows!")
Line 1,957:
bulls = sum(map(==, guess, result))
cows = length(intersect(guess,result)) - bulls
println("$bulls bulls and $cows cows!") ; turns += 1
end
println("You win! You succeeded in $turns guesses.")
end</lang>
{{Out}}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.