Guess It in thinBasic
' Guess what number for thinBasic
' Eros Olmi. 2007, public domain

'---Load console module
  uses "Console"

'---Start random number generator
  Randomize TIMER

'---Define min and max bounds for random number
  %MinNumber = 1
  %MaxNumber = 100

'---Some variables
  Dim Plr_guess   as long   '---Used to get user input
  dim Randomed    as long   '---Number to guess
  dim Rounds      AS long   '---Used to count user try
  dim Found       as long   '---Used to indicate number has been found

'---Set size and window position    
  Console_SetScreenBufferSize(%MaxNumber, 25)

'---Main program loop.

  '---Clear console screen

  '---Generate a random number between %MinNumber and %MaxNumber
  Randomed = rnd(%MinNumber, %MaxNumber)

  '---Init variables that need to be initialized at every re-game
  Found   = %FALSE
  Rounds  = 0

  '---Gives some initial info about the sript
  console_writeline "Ok, i think random number between " & %MinNumber & " and " & %MaxNumber & "."
  console_writeline "Your job is to guess what it is in as minimal tries as possible."
  console_writeline "After your every guess, ill give you hint if my number higher or lower than your guess."
  console_writeline "You can exit by 'guessing' 0."
  console_writeline string$(%MaxNumber - 1, "-")
  console_writeline "Press any key to start game."
  console_writeline string$(%MaxNumber - 1, "-")

  '---OK, let's start guessing loop

    '---Increment rounds
    incr Rounds
    '---Get number from user
    console_write "This is round number: " & Rounds & ". "
    Console_Write("Give your guess: ")
    Plr_guess = val(Console_Read)

    '---Check entered number
    select case Plr_guess
      case 0
        exit do
      case > Randomed
        wInfo "My number is smaller...", -1
      case < Randomed
        wInfo "My number is higher...", 1
      case Randomed
        wInfo "--- You got it !!! ---"
        Found = %TRUE
    END select

  LOOP While (Plr_Guess <> 0) and (Found = %FALSE)

  '---If number was not found we have finished

  console_writeline "It took " &  Rounds & " rounds to guess right number."
  console_writeline "Press 'Y' to play again. Any other key to quit."

  if ucase$(Console_WaitKey) <> "[Y]" then exit do

Loop while %TRUE

console_writeline ""
console_writeline "--- Thanks for playing ---"

' Write a result output
function wInfo(byval sDescrition as string, optional Level as long)
  '---Save current color
  dim OldColor as long = Console_GetTextAttribute
  '---Use different color depending on level number
  dim DescriptionColor  as long = iif(Level < 0, 9, iif(Level > 0, 10, 12))

  '---Set new color
    '---Write text
    console_writeline sDescrition & " "
  '---Restore previous color

end function
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.