Guess It in thinBasic
' Guess what number for thinBasic 1.5.0.1
' 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)
  Console_ShowWindow(%Console_SW_MAXIMIZE)

'---Main program loop.
DO

  '---Clear console screen
  console_cls

  '---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, "-")
  console_waitkey

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

    '---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
  IF Found = %FALSE THEN EXIT DO

  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 ---"
console_waitkey

'------------------------------------------------------------------------
' 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
  Console_SetTextAttribute(DescriptionColor) 
    '---Write text
    console_writeline sDescrition & " "
  '---Restore previous color
  Console_SetTextAttribute(OldColor)

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