I did this exercise from Hands on DBPro and I'm running into an odd issue.
I wrote in a main loop for the game for replay and the second time through the loop the program just hangs until i hit ESC.
Any clues?
`==========================================
` Bull and Touch
` Chapter 11 - Hands on DBPro Vol 1
`==========================================
`-------------set Constants----------------
#constant screenWidth = 800
#constant screenHeight = 600
#constant fontName = "Courier New"
#constant fontColor = rgb(255,0,0)
#constant bgColor = rgb(0,0,0)
#constant promptX = 280
#constant promptY = 490
#constant titleX = 400
#constant titleY = 100
#constant closingHeadX = 400
#constant closingHeadY = 300
#constant closingBodyX = 400
#constant closingBodyY = 450
#constant titleSize = 36
#constant fontSize = 18
#constant yInc = 34
`------------------------------------------
`------------------Main--------------------
repeat `main game loop
`------set Globals---------
undim number()
undim guess()
dim number(4)
dim guess(4)
global bulls = 0
global touches = 0
global yOut = 168
global attempts = 0
global playAgain = 0
`--------------------------
InitializeGame()
GenerateNumber()
DrawInitialScreen()
`debug line to show the number
text 10,10,str$(number(0))+str$(number(1))+str$(number(2))+str$(number(3))
repeat
GetGuess()
CalculateBullsAndTouches()
DisplayResult()
until bulls = 4 or attempts = 9
FinishGame()
Replay()
until playAgain = 0 `end main game loop
cls
end
`------------------------------------------
`----------------Functions-----------------
function InitializeGame()
set display mode screenWidth, ScreenHeight, 32
set window on
set text to bold
set text transparent
set text font fontName
ink fontColor, bgColor
randomize timer()
endfunction
function GenerateNumber()
dim digits(9)
for c = 0 to 3
repeat
value = rnd(9)
until digits(value) = 0
digits(value) = 1
number(c) = value
next c
endfunction
function DrawInitialScreen()
cls bgColor
set text size titleSize
center text titleX,titleY,"~ Bull And Touch ~"
endfunction
function GetGuess()
set text size fontSize
repeat
set cursor promptX, promptY
ink bgColor,bgColor
set text opaque
print "Enter your guess: "
ink fontColor,bgColor
set text transparent
set cursor promptX, promptY
input "Enter your guess: ", readInput$
until ValidateInput(readInput$) = 1
SplitStringIntoDigits(readInput$)
attempts = attempts + 1
endfunction
function ValidateInput(readInput$)
result = 1
if len(readInput$) <> 4
result = 0
exitfunction
endif
for c = 1 to 4
if asc(mid$(readInput$,c)) > 57 or asc(mid$(readInput$,c)) < 48
result = 0
exitfunction
endif
next c
for d = 1 to 4
for e = 1 to 4
if d <> e and mid$(readInput$,d) = mid$(readInput$,e)
result = 0
exitfunction
endif
next e
next d
endfunction result
function SplitStringIntoDigits(readInput$)
for n = 0 to 3
guess(n) = int(val(mid$(readInput$,n+1)))
next n
endfunction
function CalculateBullsAndTouches()
bulls = CalculateBulls()
touches = CalculateTouches()
endfunction
function CalculateBulls()
result = 0
for n = 0 to 3
if guess(n) = number(n)
result = result + 1
endif
next n
endfunction result
function CalculateTouches()
result = 0
for n = 0 to 3
for m = 0 to 3
if guess(n) = number(m) and n <> m
result = result + 1
endif
next m
next n
endfunction result
function DisplayResult()
set text size fontSize
center text 340, yOut, str$(guess(0))+str$(guess(1))+str$(guess(2))+str$(guess(3))
BandT$ = str$(bulls)+" B "+str$(touches)+" T"
center text 460, yOut, BandT$
yOut = yOut + yInc
endfunction
function FinishGame()
if bulls = 4
WinScreen()
else
LoseScreen()
endif
endfunction
function WinScreen()
cls rgb(0,0,0)
set text size titleSize
center text closingHeadX, closingHeadY, "You've beat me in "+str$(attempts)+" guesses!!"
endfunction
function LoseScreen()
cls rgb(0,0,0)
set text size titleSize
center text closingHeadX, closingHeadY, "You lose! My number was "+str$(number(0))+str$(number(1))+str$(number(2))+str$(number(3))
endfunction
function Replay()
playAgain = 0
set text size fontSize
repeat
center text closingBodyX, closingBodyY, "Play Again? (Y/N)"
again$ = inkey$()
if again$ = "y" or again$ = "Y"
playAgain = 1
exitfunction
endif
if again$ = "n" or again$ = "N"
playAgain = 0
exitfunction
endif
until again$ = "y" or again$ = "Y" or again$ = "n" or again$ = "N"
endfunction
Thanks!
~Aspiring~