Results 1 to 8 of 8

Thread: number guessing game script

  1. #1
    Member
    Join Date
    Jan 2007
    Age
    76
    Posts
    426
    Rep Power
    60

    number guessing game script

    I don't know if this is the right place for this so someone please let me know if it isn't.

    This is my script that plays the number guessing game:

    [code=thinbasic]uses "console"

    dim Quit as long
    dim myNum as string
    dim guess as string
    dim Playing as long
    dim Play_again as long
    dim Result as string
    dim n as long
    dim Num_check as long
    dim Num_only as long
    dim Player_guess as long

    Intro()

    Quit = %false
    while Quit = %False

    randomize
    myNum = int(rnd(1,100))

    console_writeline("I've picked a number")
    console_writeline("")

    Playing = %false
    while Playing = %false

    Num_only = %false
    while Num_only = %false

    console_write("What is your guess? ")
    guess = int(console_readline())
    Num_check = VERIFY(guess, "0123456789")

    select case Num_check
    case <> 0
    console_writeline("Your guess MUST be a whole number!")
    console_writeline("")
    case else
    N = between(guess,1,99)
    if N <> 0 then
    Num_only = %true
    else
    console_writeline("Your guess is out of range!")
    console_writeline("Try again")
    console_writeline("")
    end if
    end select
    wend

    if guess < myNum then
    console_writeline("Your guess is too low!")
    console_writeline("")
    end if

    if guess > myNum then
    console_writeline("Your guess is too high!")
    console_writeline("")
    end if

    if guess = myNum then
    console_writeline("CONGRADULATIONS!!! YOU WON!")
    console_writeline("")
    Playing = %true
    end if

    wend


    console_write("Play again? ( (Y)es or (N)o )")

    Play_again = %false
    WHILE Play_again = %FALSE

    '---Read keyboard input
    Result = Console_inKeyb

    '---Handle returned string
    select case Result

    case "n","N"
    Console_Writeline("")
    console_writeline("GOODBYE!!!")
    n = sleep(1000)
    Play_again = %TRUE
    Quit = %true
    case "y","Y"
    Console_writeline("")
    console_writeline("")
    Play_again = %true
    end select
    wend

    wend

    function Intro()
    console_writeline("A Guessing Game!")
    console_writeline("")
    console_writeline("I will choose a number between 1 and 100.")
    console_writeline("You will try to guess the number. Whole numbers only.")
    Console_writeline("If your guess is higher than my number, I'll say 'too high'")
    console_writeline("If your guess is lower than my number, I'll say 'too low'")
    console_writeline("When you have guessed the number, you win")
    console_writeline("")
    end function
    [/code]

    If someone finds something wrong with this script Please let me know about it.

    Thanks

  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736

    Re: number guessing game script

    Hi,

    your script works very nice !

    Little tip - try to optimize the number comparsion using ELSEIF... ( not mandatory, but will come handy to know how it works in your future projects )


    Bye,
    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #3
    Member
    Join Date
    Jan 2007
    Age
    76
    Posts
    426
    Rep Power
    60

    Re: number guessing game script

    Thanks for the reply and the kind words about the script. I'll keep the elseif in mind in future scripts.

    Thanks

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,817
    Rep Power
    10

    Re: number guessing game script

    Yes, Sandy. Good starting.

    I suppose Petr was referring to
    [code=thinbasic]
    IF guess < myNum THEN
    console_writeline ( "Your guess is too low!" )
    console_writeline ( "" )
    END IF

    IF guess > myNum THEN
    console_writeline ( "Your guess is too high!" )
    console_writeline ( "" )
    END IF

    IF guess = myNum THEN
    console_writeline ( "CONGRADULATIONS!!! YOU WON!" )
    console_writeline ( "" )
    Playing = %true
    END IF
    [/code]

    that can be rewritten to
    [code=thinbasic]
    IF guess < myNum THEN
    console_writeline ( "Your guess is too low!" )
    console_writeline ( "" )
    ELSEIF guess > myNum THEN
    console_writeline ( "Your guess is too high!" )
    console_writeline ( "" )
    ELSE '---here can only be equal
    console_writeline ( "CONGRADULATIONS!!! YOU WON!" )
    console_writeline ( "" )
    Playing = %true
    END IF[/code]

    or even
    [code=thinbasic]
    SELECT CASE guess
    CASE < myNum
    console_writeline ( "Your guess is too low!" )
    console_writeline ( "" )
    CASE > myNum
    console_writeline ( "Your guess is too high!" )
    console_writeline ( "" )
    CASE myNum
    console_writeline ( "CONGRADULATIONS!!! YOU WON!" )
    console_writeline ( "" )
    Playing = %true
    END SELECT[/code]

    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,817
    Rep Power
    10

    Re: number guessing game script

    Sandy,

    can I add you example into official thinBasic distribution in next preview version?
    Nice and perfect example for beginners.

    Thanks a lot
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    68
    Posts
    3,865
    Rep Power
    405

    Re: number guessing game script

    Sandy very nice version of the game. Thanks, it is fun playing it again.

    If you want, next you can keep track of the player's guesses and depending on how many guess they take,
    you can print out a custom win message and tell them their score.

    Another thing would be to add user input requests and input in different colors.

    None of the above are necessary, the game is already fun to play and works great, just it will be
    nice things to work on learning how to do to help learn.

    Now back to the game to see if I can beat in less than 5 guesses
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

  7. #7
    Member
    Join Date
    Jan 2007
    Age
    76
    Posts
    426
    Rep Power
    60

    Re: number guessing game script

    ErosOlmi: You have permission to do anything you want with the code I post. If you feel that it is good enough to be included in the next preview then I'd be honored.

    I'm aware that the program could have things added to it. Maybe another beginner will fancy it up a bit.

    Thanks

  8. #8
    thinBasic MVPs Michael Hartlef's Avatar
    Join Date
    Sep 2006
    Location
    Germany
    Age
    58
    Posts
    3,299
    Rep Power
    348

    Re: number guessing game script

    Hey, nice script Sandy. This reminds me of my childhood, where I played a boardgame called MASTERMIND a lot. You had to guess 4 colors on it.

Similar Threads

  1. different kind of number guessing game
    By sandyrepope in forum Console: advanced source code examples
    Replies: 28
    Last Post: 22-04-2007, 12:00

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •