Bulls and cows: Difference between revisions
Content added Content deleted
(→{{header|Oz}}: improve readibility) |
|||
Line 1,384: | Line 1,384: | ||
} |
} |
||
?></lang> |
?></lang> |
||
=={{header|PureBasic}}== |
|||
<lang PureBasic>Define.s secret, guess, c |
|||
Define.i bulls, cows, guesses, i |
|||
OpenConsole() |
|||
While Len(secret) < 4 |
|||
c = Chr(Random(9) + 48) |
|||
If FindString(secret, c, 1) = 0 |
|||
secret + c |
|||
EndIf |
|||
Wend |
|||
Repeat |
|||
Print("Guess a 4-digit number with no duplicate digits: ") |
|||
guess = Input() |
|||
If Len(guess) = 0 |
|||
Break ;break from loop |
|||
EndIf |
|||
isMalformedGuess = #False |
|||
If Len(guess) <> 4 |
|||
;guess is too short |
|||
isMalformedGuess = #True |
|||
Else |
|||
For i = 1 To 4 |
|||
c = Mid(guess, i, 1) |
|||
If Not FindString("0123456789", c, 1) Or CountString(guess, c) <> 1 |
|||
;guess contains either non-digits or duplicate digits |
|||
isMalformedGuess = #True |
|||
Break ;break from For/Next loop |
|||
EndIf |
|||
Next |
|||
EndIf |
|||
If isMalformedGuess |
|||
PrintN("** You should enter 4 different numeric digits!") |
|||
Continue ;continue loop |
|||
EndIf |
|||
bulls = 0: cows = 0: guesses = guesses + 1 |
|||
For i = 1 To 4 |
|||
c = Mid(secret, i, 1) |
|||
If Mid(guess, i, 1) = c |
|||
bulls + 1 |
|||
ElseIf FindString(guess, c, 1) |
|||
cows + 1 |
|||
EndIf |
|||
Next |
|||
Print( Str(bulls) + " bull") |
|||
If bulls <> 1 |
|||
Print( "s") |
|||
EndIf |
|||
Print( ", " + Str(cows) + " cow") |
|||
If cows <> 1 |
|||
PrintN( "s") |
|||
Else |
|||
PrintN("") |
|||
EndIf |
|||
If guess = secret |
|||
PrintN("You won after " + Str(guesses) + " guesses!") |
|||
PrintN("Press a key...") |
|||
Repeat: Until Inkey() <> "" |
|||
Break ;break from loop |
|||
EndIf |
|||
ForEver |
|||
</lang> |
|||
=={{header|Python}}== |
=={{header|Python}}== |