Bulls and cows: Difference between revisions

Added shorter uBasic/4tH version
No edit summary
(Added shorter uBasic/4tH version)
Line 7,307:
Next
Return (c@) ' Return number of valid digits</lang>
The addition of strings allow for much simpler code.
<lang>Do
s = 1234 + RND(8643)
Until FUNC(_Check(Str(s)))
Next
 
s = Str(s)
 
Do
Do
g = Ask("Enter your guess: ")
If (Val(g) = Info("nil")) + (Len(g) # 4) Then Continue
Until FUNC(_Check(g))
Loop
 
b = 0 : c = 0
 
For i = 0 To 3
For j = 0 to 3
If Peek(s, i) = Peek(g, j) Then
If i = j Then
b = b + 1
Else
c = c + 1
EndIf
EndIf
Next
Next
 
Print "You scored ";b;" bulls and ";c; " cows.\n"
 
Until b = 4
Loop
End
 
_Check
Param (1)
Local (2)
 
b@ = 0
 
For c@ = 0 To 3
If Peek(a@, c@) = Ord ("0") Then Unloop : Return (0)
If And(b@, 2^(Peek(a@, c@) - Ord ("0"))) Then Unloop : Return (0)
b@ = b@ + 2^(Peek(a@, c@) - Ord ("0"))
Next
Return (1)</lang>
 
=={{header|UNIX Shell}}==
374

edits