'-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ' Beginning Beginner Series ' Hi-Lo / Guess the number game ' Windows variant for Rapid-Q ' Steven Edwards ' March 2000 '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ' compiler directives $apptype GUI $typecheck ON '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ' function / subroutine declarations declare sub NumUpdate declare sub PlayerGuess '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ' global variables dim Target as byte dim LowGuess as byte dim HighGuess as byte dim PlayerTries as integer '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ' GUI form layout create MainForm as QForm caption = "HI/LO (Guess the Number)" width = 320 height = 200 center create btnGuess as QButton ' player clicks button to register new guess top = 95 left = 120 caption = "GUESS" onclick = PlayerGuess end create create TrackBar as QTrackBar ' player moves slide bar to select new guess top = 50 left = 80 ' guess from 1 to 100 min = 1 max = 100 ' tick marks every 25 positions frequency = 25 ' start player at 50th position position = 50 ' highlight available area for guesses selstart = 1 selend = 100 onchange = NumUpdate end create create lblSelectedNum as QLabel ' label lets player see exact number of ' trackbar position top = 55 left = 250 caption = str$(TrackBar.position) end create create lblAttempts as QLabel ' label shows a counter of attempted guesses top = 10 left = 120 caption = "Attempted : " end create create lblMessage as QLabel ' label for messages to player from program top = 30 left = 120 caption = "Make a Guess" end create create lblRating as QLabel ' label for rating the player performance top = 135 left = 10 caption = "RATING : ?" width = 300 alignment = 2 end create end create '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ' main startup code ' generate a random number from 1 to 100 randomize Target = rnd(100) + 1 ' set available numbers to select at lowest ' and highest points LowGuess = 1 HighGuess = 100 ' set player attempts counter at zero PlayerTries = 0 ' show form to start program running MainForm.showmodal '-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- ' function / subroutine implementation sub NumUpdate ' routine is called when trackbar is moved if TrackBar.position < LowGuess then ' do not let the player slide the selector ' blelow the current lowest guess TrackBar.position = LowGuess end if if TrackBar.position > HighGuess then ' do not let the player slide the selector ' above the current highest guess TrackBar.position = HighGuess end if ' update label to show exact position of ' trackbar selector lblSelectedNum.caption = str$(TrackBar.Position) end sub sub PlayerGuess ' routine is called when player clicks the ' GUESS button to try new guess inc(PlayerTries) ' increment player attempts counter and display lblAttempts.caption = "Attempted : " + str$(PlayerTries) if TrackBar.position < Target then ' too low, let player know that a HIGHER ' number is needed and lock out selection ' of numbers lower than current guess LowGuess = TrackBar.position lblMessage.caption = "Guess Higher" elseif TrackBar.position > Target then ' too high, let player know that a LOWER ' number is needed and lock out selection ' of numbers higher than current guess HighGuess = TrackBar.position lblMessage.caption = "Guess Lower" else ' must have nailed it, lock out all other ' numbers and disable the GUESS button lblMessage.caption = "CORRECT!!!!" LowGuess = TrackBar.position HighGuess = TrackBar.position btnGuess.enabled = 0 ' give player a rating select case PlayerTries case is < 5 lblRating.caption = "RATING : LUCKY GUESS!" case 5 lblRating.caption = "RATING : LUCKY MASTER STRATEGIST" case 6 to 7 lblRating.caption = "RATING : MASTER STRATEGIST" case 8 to 9 lblRating.caption = "RATING : STRATEGIST" case 9 to 11 lblRating.caption = "RATING : SERIOUS PLAYER" case 12 to 20 lblRating.caption = "RATING : BEGINNER" case is > 20 lblRating.caption = "RATING : ASK DADDY FOR HELP" end select ' re-set following or centering of text does ' not work lblRating.width = 300 lblRating.alignment = 2 end if ' highlight track bar selections from lowest ' to highest selectable numbers TrackBar.selstart = LowGuess TrackBar.selend = HighGuess end sub