Bulls and cows: Difference between revisions

Content deleted Content added
No edit summary
Line 1,474: Line 1,474:
Guess a 4-digit number with no duplicate digits: 3957
Guess a 4-digit number with no duplicate digits: 3957
You won after 10 guesses!</pre>
You won after 10 guesses!</pre>

=={{header|Liberty BASIC}}==
<lang lb>

do while len( secret$) <4
c$ =chr$( int( rnd( 1) *9) +49)
if not( instr( secret$, c$)) then secret$ =secret$ +c$
loop

print " Secret number has been guessed.... "; secret$

guesses = 0

[loop]
print " Your guess ";
input " "; guess$

guesses = guesses +1

r$ =score$( guess$, secret$)

bulls =val( word$( r$, 1, ","))
cows =val( word$( r$, 2, ","))

print " Result: "; bulls; " bulls, and "; cows; " cows"
print

if guess$ =secret$ then
print " You won after "; guesses; " guesses!"
print " You guessed it in "; guesses
print " Thanks for playing!"
wait
end if

goto [loop]

end ' _____________________________________________________________

function check( i$) ' check =0 if no digit repeats, else =1
check =0
for i =1 to 3
for j =i +1 to 4
if mid$( i$, i, 1) =mid$( i$, j, 1) then check =1
next j
next i
end function

function score$( a$, b$) ' return as a csv string the number of bulls & cows.
bulls = 0: cows = 0
for i = 1 to 4
c$ = mid$( a$, i, 1)
if mid$( b$, i, 1) = c$ then
bulls = bulls + 1
else
if instr( b$, c$) <>0 and instr( b$, c$) <>i then
cows = cows + 1
end if
end if
next i
score$ =str$( bulls); ","; str$( cows)
end function

[quit]
close #w
end
</lang>


=={{header|Logo}}==
=={{header|Logo}}==