Page 1 of 3 123 LastLast
Results 1 to 10 of 29

Thread: different kind of number guessing game

  1. #1

    different kind of number guessing game

    Here is a number guessing game that works in a different way. With this one there is a 4 digit number to guess. After a 4 digit guess is input the computer reports how many of the digits are correct and how many are in the correct position. I don't really have a proper name for this program.

    [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 string
    dim Num_only as long
    dim Player_guess as long
    dim build_code as long
    dim x as long
    dim pos_correct as long
    dim dig_correct as long
    dim y as string
    dim z as long

    Intro()

    Quit = %false
    while Quit = %False

    myNum = ""

    randomize
    myNum = int(rnd(0,9))
    while len(myNum) < 4
    x = int(rnd(0,9))
    if instr(myNum,x) = 0 then
    myNum = myNum + x
    end if
    wend

    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 = 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 = len(guess)
    if N = 4 then
    Num_only = %true
    else
    console_writeline("Your guess must be 4 digit!")
    console_writeline("Try again")
    console_writeline("")
    end if
    end select
    wend

    pos_correct = 0
    for x = 1 to 4
    if mid$(guess,x,1) = mid$(myNum,x,1) then
    pos_correct += 1

    end if
    next

    dig_correct = 0
    for x = 1 to 4
    y = mid$(myNum,x,1)

    for z = 1 to 4
    if y = mid$(guess,z,1) then
    dig_correct += 1
    exit for
    end if
    next

    next

    if guess = myNum then
    console_writeline("")
    console_writeline("You guessed correctly! You WON!!!!!")
    console_writeline("")
    Playing = %true
    else
    console_writeline("")
    console_writeline("You have " + dig_correct + " digits correct")
    console_writeline("You have " + pos_correct + " digits in the correct position")
    console_writeline("")
    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 4-digit number.")
    console_writeline("You will try to guess the number.")
    Console_writeline("All I will tell you is how many digits are correct")
    console_writeline("and how many are in the correct position.")
    console_writeline("When you have guessed the number, you win")
    console_writeline("")
    end function
    [/code]

    Thanks
    Sandy

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

    Re: different kind of number guessing game

    Sandy that is a fun game. I am having an error at times. I haven't figured out when it occurs. Sometimes right away and other times a guess or 2 later. I will try to see if I can catch what it is.

    The game name is MasterMind. I used to have the plastic model with the pins and played with it all the time when a kid and before I had computers to play with. Great game!!
    Attached Images Attached Images
    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

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

    Re: different kind of number guessing game

    Ken,

    I think error is in last thinBasic preview version 1.2.0.1 and occurs when a EXIT FOR statement is executed.
    Fix will be release in next preview version.

    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

  4. #4

    Re: different kind of number guessing game

    Thank you, kryton9. I've been running the script to see if I could get that error. So far, I haven't been able to. I'll keep trying.

    One thought though.... What version of thinbasic are you running?

    I'm not real sure of the version I have.

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

    Re: different kind of number guessing game

    Sandy,

    at thinBasic we have one stable version (current stable version is 1.2.0.0) and one preview version (current preview version is 1.2.0.1) of our software.
    Preview version is continuously updated with all the new features and bug fix we are able to find during testing.

    We do this because we update preview version almost every week (and sometimes even more frequently) in order to give users all the opportunities to test new features and have their bug fixed as soon as possible. Having a so frequent update, change version every time would be a nightmare.

    Regarding this specific error, I'm pretty sure I added it during recent update on ITERATE FOR optimization. I forgot to check about EXIT FOR that is the other clause used to exit from a current FOR/NEXT loop.

    So, if you are not experiencing this error, it means you didn't download recently thinBasic preview version 1.2.0.1.
    To be advised when an update occurs, please read Forum Announcements.

    When we move preview version as stable version we send a mail to all registered users.

    Ciao
    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,869
    Rep Power
    404

    Re: different kind of number guessing game

    Sandy, I am using the prerelease version and as Eros explained that is why I got that error. Your code looks fine. Good job in writing what is a pretty complicated game. when the next release comes out with the fix, I will be looking forward to playing it more.
    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

    Re: different kind of number guessing game

    I downloaded the latest preview and ran the program. I'm now getting the same error. I've made a change in the program so that it will run anyway.

    [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 string
    dim Num_only as long
    dim Player_guess as long
    dim build_code as long
    dim x as long
    dim pos_correct as long
    dim dig_correct as long
    dim y as string
    dim z as long

    Intro()

    Quit = %false
    while Quit = %False

    myNum = ""

    randomize
    myNum = int(rnd(0,9))
    while len(myNum) < 4
    x = int(rnd(0,9))
    if instr(myNum,x) = 0 then
    myNum = myNum + x
    end if
    wend

    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 = 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 = len(guess)
    if N = 4 then
    Num_only = %true
    else
    console_writeline("Your guess must be 4 digit!")
    console_writeline("Try again")
    console_writeline("")
    end if
    end select
    wend

    pos_correct = 0
    for x = 1 to 4
    if mid$(guess,x,1) = mid$(myNum,x,1) then
    pos_correct += 1

    end if
    next

    dig_correct = 0
    for x = 1 to 4
    y = mid$(myNum,x,1)

    for z = 1 to 4
    if y = mid$(guess,z,1) then
    dig_correct += 1
    y = 5
    ' exit for
    end if
    next
    next

    if guess = myNum then
    console_writeline("")
    console_writeline("You guessed correctly! You WON!!!!!")
    console_writeline("")
    Playing = %true
    else
    console_writeline("")
    console_writeline("You have " + dig_correct + " digits correct")
    console_writeline("You have " + pos_correct + " digits in the correct position")
    console_writeline("")
    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 4-digit number.")
    console_writeline("You will try to guess the number.")
    Console_writeline("All I will tell you is how many digits are correct")
    console_writeline("and how many are in the correct position.")
    console_writeline("When you have guessed the number, you win")
    console_writeline("")
    end function
    [/code]

    I just replaced the line 'exit for' with 'y = 5' and it works (on my computer, that is). I think this will work on any preview version.

    If there are any other problems I haven't found yet, Please let me know.

    Thanks
    Sandy

  8. #8

    Re: different kind of number guessing game

    I really goofed with the program. The change I made stopped the 'exit for' error but now there is a bug and it doesn't handle the number of correct digits properly. I'll post a fix as soon as I can come up with something.

    I'm so sorry about this.

    Sandy

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

    Re: different kind of number guessing game

    No worries. Since your old code is fine, I am sure Eros will have a fix for you soon. You can always download it from your post here in case you saved over your original version. I know having posts up here with code have saved me from such overwrites and mass deletions on my part.
    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

  10. #10

    Re: different kind of number guessing game

    I've been trying to think of ideas to make this game look nice but, so far, haven't been able to think of anything.

    I have included code so that the game now keeps up with the number of guesses the user takes to win.

    I've thought about keeping a winner score table so that the last 5 or 10 lowest scores and player names could be kept but I haven't been able to figure out how that is done.

    If any one has any suggestions I'd sure love to get them. By the way, my lowest score on this game is 12. Can't seem to get it any lower. Usually I get somewhere between 12 and 15.

    Thanks
    Sandy

Page 1 of 3 123 LastLast

Similar Threads

  1. number guessing game script
    By sandyrepope in forum Console: advanced source code examples
    Replies: 7
    Last Post: 24-02-2007, 08:17

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
  •