PDA

View Full Version : different kind of number guessing game



sandyrepope
25-02-2007, 22:39
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.


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


Thanks
Sandy

kryton9
25-02-2007, 22:56
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!!

ErosOlmi
25-02-2007, 23:46
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

sandyrepope
26-02-2007, 02:36
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.

ErosOlmi
26-02-2007, 02:48
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 (http://community.thinbasic.com/index.php?board=19.0).

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

Ciao
Eros

kryton9
26-02-2007, 03:45
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.

sandyrepope
26-02-2007, 04:19
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.


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


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

sandyrepope
26-02-2007, 05:37
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

kryton9
26-02-2007, 06:02
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.

sandyrepope
13-04-2007, 02:25
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

kryton9
13-04-2007, 05:47
Sandy, I can't seem to find the latest version that works. Can you post the version you are using, thanks.

sandyrepope
14-04-2007, 00:34
Here it is:

'---******************************************************
'---* Game of MasterMind *
'---* This version uses a four digit *
'---* code instead of using different *
'---* colors. *
'---******************************************************



'---******************************************************
'---* This is a console script so it has to have *
'---* the uses "console" to work *
'---******************************************************

uses "console"

'---******************************************************
'---* Here are all the variables used in the script *
'---******************************************************

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
dim a_guess as string
dim guess_number as long

Intro() 'tell about game & how to play

Quit = %false 'variable changed to %true when time to quit while loop
while Quit = %False 'while loop where game played
guess_number = 1 'begin counting the number of guesses to win
myNum = "" 'variable to hold computer's number

randomize 'seeds the rnd function
myNum = int(rnd(0,9)) 'get first digit
while len(myNum) < 4 'while loop to build 4 digit number - quit when we have 4
x = int(rnd(0,9)) 'pick next random digit
if instr(myNum,x) = 0 then 'Make sure each digit is unique
myNum = myNum + x 'if it is add to computer's number
end if
wend 'move on when finished

console_writeline("I've picked a number") 'tell player computer has number
console_writeline("") 'then print blank line

Playing = %false 'variable = %true when player has
while Playing = %false 'guessed number. There is no offer for player to quit.

Num_only = %false 'variable = %true if player guess is acceptable
while Num_only = %false 'loop to check guess
Console_SetInputMode(%Console_ENABLE_PROCESSED_INPUT) 'lets player use backspace to edit guess before enter key pressed
console_writeline("Guess Number: " + guess_number) 'tell player the number of guess

console_write("What is your guess? ") 'ask for guess
guess = console_readline() 'get the guess
a_guess = left$(guess,(len(guess)-1)) 'take off cr/enter character from end of guess

Num_check = VERIFY(a_guess, "0123456789") 'make sure guess has only digits

select case Num_check
case <> 0
console_writeline("Your guess MUST be numbers ONLY!") 'in case player put a wrong character in guess
console_writeline("")

case else
N = len(a_guess)
if N = 4 then
Num_only = %true
else
console_writeline("Your guess must be 4 digit!") 'don't let guess be < 4 or > 4 digits
console_writeline("Try again")
console_writeline("")
end if
end select
wend

pos_correct = 0 'keep count of number in correct position
for x = 1 to 4 'look at all 4 digits and
if mid$(a_guess,x,1) = mid$(myNum,x,1) then 'add to count when needed
pos_correct += 1

end if
next

dig_correct = 0 'keep count of all digits that are correct
for x = 1 to 4 'even if they aren't in correct position
y = mid$(myNum,x,1)

for z = 1 to 4 'loop to keep count of digits that are correct
if y = mid$(a_guess,z,1) then
dig_correct += 1
exit for 'exit for loop when correct digit found
end if
next
next

if a_guess = myNum then 'see if player guessed computer's number
console_writeline("")
console_writeline("You guessed correctly! You WON!!!!!") 'tell player about win and
console_writeline("")
console_writeline("You took " + guess_number + " guesses") 'how many guesses it took
console_writeline("")
Playing = %true
else
console_writeline("")
guess_number += 1 'if not a win then increment the guess number by one


console_writeline("You have " + dig_correct + " digits correct") 'give number of digits correct
console_writeline("You have " + pos_correct + " digits in the correct position") 'and number of digits in correct position
console_writeline("")

end if

wend


console_write("Play again? ( (Y)es or (N)o )") 'after win ask if player wants to play again or quit

Play_again = %false
WHILE Play_again = %FALSE

'---Read keyboard input
Result = Console_inKeyb

'---Handle returned string
select case Result

case "n","N" 'all this if player says no
console_cls
Console_Writeline("")
console_writeline("GOODBYE!!!")
n = sleep(1000)
Play_again = %TRUE
Quit = %true
case "y","Y"
guess_number = 1 'all this if player says yes
console_cls
Play_again = %true
end select
wend

wend

function Intro() 'this function gives instructions about how to play
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
'---******************************************************
'---* End of script *
'---* That's all there is *
'---******************************************************



Thanks
Sandy

kryton9
14-04-2007, 08:26
Thanks Sandy. I lucked out and got a 9 after a few games of average 16 scores. Great game and fun as always.
Let me know who ever gets a lower score, so we have a target to shoot for!!

sandyrepope
14-04-2007, 17:51
I hate to admit it but I've never gotten a score that low. I plan to keep trying.

If you have any suggestions about how to make the game better please let me know..... I'm kinda out of ideas for now.

Thanks
Sandy

kryton9
15-04-2007, 06:26
Sandy,

For console, it would be neat if it cleared the screen and presented your guesses in a nice easy way without the questions. Sort of like an updated score board. Then it will be easier to analyze your guesses.
2 Guesses so far:
1234 2 correct numbers - 0 correct position
2134 2 correct numbers - 2 correct position

Your next guess please?

Michael Hartlef
15-04-2007, 08:16
I needed 12 tries :) Kent's sugeestion would help a lot. A help function that shows the correct numbers and positions with different colors would be nice. Of course with a penalty on the scores.

sandyrepope
15-04-2007, 20:20
Thank you for the replies.

MikeHart, I'm sorry but I don't completely understand your suggestion about using colors. Could you explain that a little more, please?

Thanks
Sandy

kryton9
16-04-2007, 07:51
Mike 12 is very good. I just lucked out with that 9. My average was around 16 till that lucky game.

Sandy, this is how the mastermind game sort of works in color.
http://www.valil.com/netcf/mastermind.gif

Instead of numbers you use colored pegs.
Then you see how many are correct color but in wrong spot (white dot)
Black dot means right color, right position.

This is a photo of the plastic game that I used to play and am sure others have played.
http://upload.wikimedia.org/wikipedia/commons/2/2d/Mastermind.jpg

Hope this gives you some ideas!

Petr Schreiber
16-04-2007, 08:04
Hi sandyrepope,

your game is very good, but I won't post here no. of my tries until I beat Mike and krytons score :D. Till know my guessing is terrible.

kryton, this is shocking - I didn't know mastermind exists as a real life toy :o


Bye,
Petr

Michael Hartlef
17-04-2007, 11:13
Petr, yes I played that alot when I was a kid.

Sandy, I will give you a visual example, when I'm back froim work tonight.

kryton9
17-04-2007, 23:18
Yes Petr, it was one of my favorites as it is small and you can take it anywhere and play when you have time.
The only problem was you had to find someone to play with. Now with computers you can play anytime you want to
strengthen your logic skills.

Petr Schreiber
18-04-2007, 13:15
Thanks for clarification guys,

so somebody else has to create "solution" and you guess it ?


Bye,
Petr

kryton9
18-04-2007, 21:08
Yes, they have a hidden section at the end where they put their pegs and they have to tell you the results of your guesses.
The cool thing was the person setting it up had the fun of watching the guesser suffer in trying to figure out what the other setup.
So it was fun to play.

Michael Hartlef
19-04-2007, 10:12
Sorry Sandy, my daily job is very busy right now and I didn't find time to be productive at home in the night.
I will try to do better. :-[

sandyrepope
19-04-2007, 17:38
MikeHart, don't worry about it. I know what it's like to be too busy. It happens to me too, sometimes. Besides, I've been sick for the last week and haven't gotten anything done. I probably won't get anything done for the next week, at least. I'll get serious about programming when I can.

Thanks
Sandy

Michael Hartlef
19-04-2007, 19:17
I hope you get better soon. I will ask my wife to light a candle for you.

sandyrepope
21-04-2007, 05:12
I've just downloaded and installed the latest preview and have found that the program doesn't want to work with the work arounds that are in the script. I've started working to getting the script working and will have something as soon as I can.

I do have a question though. If I add a winner score board, should I test for the file and if it isn't there to build one or should I just have the program say there isn't a winner score board file on the hard disk?

I'm just not sure what the normal way to handle this is.

Thanks
Sandy

Michael Hartlef
22-04-2007, 09:15
If would test and if it doesn't exist, create one.

Now about the visual help thingy. I thought you could create
something that would tell a person in colors the state of the number
he/she guest. Numbers in green are correct and on the correct position,
numbers in yellow exist but in a different position, numbers in red don't exist.

Example:

Correct number is 2017
Guess is 6215

Help output could be: 6215

Using the help should give a penalty on the score.

Michael

kryton9
22-04-2007, 12:00
Interesting idea Mike. The penalty hurts, ouch. Like getting candy that is too hard to chew :)