PDA

View Full Version : number guessing game script



sandyrepope
23-02-2007, 22:56
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:


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


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

Thanks

Petr Schreiber
23-02-2007, 23:32
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

sandyrepope
24-02-2007, 00:00
Thanks for the reply and the kind words about the script. I'll keep the elseif in mind in future scripts.

Thanks

ErosOlmi
24-02-2007, 00:11
Yes, Sandy. Good starting.

I suppose Petr was referring to


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


that can be rewritten to


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

or even


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

ErosOlmi
24-02-2007, 00:14
Sandy,

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

Thanks a lot
Eros

kryton9
24-02-2007, 01:27
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 :)

sandyrepope
24-02-2007, 02:31
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

Michael Hartlef
24-02-2007, 08:17
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.