'bulls and cows no duplicates.sdlbas copied 2016-02-20
'fix more room for guessing and saving info as per Rick suggestion 2016-02-20 repost
'fix secret maker 2016-02-18 repost
'requested no duplicate numbers , OK try that yes much better game!
' Converted from BBC Basic
' <a href="http://www.rosettacode.org/wiki/Bulls_and_cows#BASIC" target="_blank">http://www.rosettacode.org/wiki/Bulls_and_cows#BASIC</a>
'
setDisplay(640,740,32,1)
randomize(timer)
'while 1 'test output of shuffle
deck$="123456789"
r=rnd(10)
secret$=mid$(deck$,r,1)
if r<9 then
deck$=mid$(deck$,1,r-1)+mid$(deck$,r+1)+"0"
else
deck$=mid$(deck$,1,r-1)+"0"
end if
for i=1 to 3
r=rnd(len(deck$)+1)
secret$=secret$+mid$(deck$,r,1)
if r<len(deck$) then
deck$=mid$(deck$,1,r-1)+mid$(deck$,r+1)
else
deck$=mid$(deck$,1,r-1)
end if
next
'prints(secret$) 'test
't$=inputs("press enter... ","") 'test
'wend 'test
prints("Guess a four-digit number (no repeating digits).")
guesses = 0
lc=0
while 1=1
do
lc=lc+1
locate(0,lc)
guess$ = inputs("Enter your guess: ","")
if len(guess$) <> 4 then: locate(25,lc):prints("Must be a four-digit number"): end if
OK=1
if len(guess$) = 4 then 'galileo had a check to see if a duplicate number was in the guess
for i=1 to 4
for j=i+1 to 4
if mid(guess$,i,1)=mid(guess$,j,1) then: OK=0:end if
next
next
if OK then
exit do
else
locate(25,lc)
prints("Digits must not repeat in 4 digit guess.")
end if
end if
loop
guesses += 1
if guess$ = secret$ then
prints("You won after " & guesses & " guesses!")
wait(3000)
end
end if
bulls = 0
cows = 0
for i = 1 to 4
c$ = mid$(secret$, i, 1)
if mid$(guess$, i, 1) = c$ then
bulls += 1
elseif instr(guess$, c$) then
cows += 1
end if
next
locate(25,lc)
prints("You got " & bulls & " bull(s) and " & cows & " cow(s).")
wend


 

Make a free website with Yola