# HELP? Guess the number game.!!!

### Question

• Hello, i have just started computing, and have had trouble making a guess the number game on Small basic. I have to let the user have eight tries before the games ends below is a script of what i have done so far.

TextWindow.WriteLine ("Hello, can you guess the number i am thinking of?, it is between 1 and 100")
RandomNumber= Math.GetRandomNumber (100)
MakeGuess:
If Guess > RandomNumber Then
TextWindow.WriteLine ("Nope, that's wrong. My number is lower")
Goto MakeGuess
EndIf
If Guess < RandomNumber Then
TextWindow.WriteLine ("Nope, that's wrong. My number is higher")
Goto MakeGuess
EndIf
If Guess=RandomNumber Then
Sound.PlayBellRing()
TextWindow.WriteLine ("Yeah, you are correct!")
EndIf
If Goto MakeGuess=(8) Then
TextWindow.WriteLine ("You have had far too many guesses, so GAMES OVER (muhahahaha) LOSER!!!")
EndIf

the underlined and bold bit is the bit that does not work. HELP??? PlZ!!

Thursday, October 18, 2012 4:21 PM

• Hi again!

Re-modified the already modified Mainchip's version, to illustrate better the abnormal loop break check.

And guess what, shoved in an infinite Goto loop!  Mwaahahah!!!

```guessesMax = 8
numberMax  = 100
TextWindow.BackgroundColor = "DarkBlue"

GameLoop:

TextWindow.Clear()

TextWindow.WriteLine("Guess a number between 1 & " + numberMax + " :")
TextWindow.WriteLine("")

For guesses = 1 To guessesMax

TextWindow.Write("Guess #" + guesses + " ")

TextWindow.WriteLine("Guess Lower!")
TextWindow.WriteLine("Guess Higher!")
Else
TextWindow.WriteLine("WINNER! - You've guessed in " + guesses + " guess(es).")
guesses = guessesMax + 1  '  guesses will be greater than guessesMax + 1 in the end
EndIf

TextWindow.WriteLine("")

EndFor

If guesses = guessesMax + 1 Then  '  True if loop iterator had not been tampered with
TextWindow.WriteLine("You've had far too many guesses, so GAME OVER (mwaaahahahah) LOSER!!!")
EndIf

TextWindow.Pause()

Goto GameLoop```

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Friday, October 19, 2012 1:13 AM
• The line "if goto makeguess..." does not make sense

try this:

TextWindow.WriteLine ("Hello, can you guess the number i am thinking of?, it is between 1 and 100")
RandomNumber= Math.GetRandomNumber (100)

tries = 0

MakeGuess:

tries = tries + 1

If Guess > RandomNumber Then
TextWindow.WriteLine ("Nope, that's wrong. My number is lower")
ElseIf Guess < RandomNumber Then
TextWindow.WriteLine ("Nope, that's wrong. My number is higher")
EndIf

If Guess=RandomNumber Then
Sound.PlayBellRing()

TextWindow.WriteLine ("Yeah, you are correct!")

Else

if tries < 8 then

goto MakeGuess

else

TextWindow.WriteLine ("You have had far too many guesses, so GAMES OVER (muhahahaha) LOSER!!!")

endif

EndIf

The whole data on the internet is saved in 540.000.000.000.000.000.000.000.000 electrons, which is as much as a handful of peanuts

Thursday, October 18, 2012 4:50 PM
• The Pascal language is a prime example of the latter problem. Because it doesn't have a "break" statement, loops in (traditional) Pascal end up often looking like total shit, because you have to add totally arbitrary logic to say "I'm done now".

Small Basic doesn't have a keyword equivalent for 'break' to get outta loop, much less a 'continue' to force an immediate next iteration of it.

So, I do use Gotos for these cases. Or use the obscure & messy 'break hack emulation' in the case of For loops, making the iterator variable >= the end condition; which is equivalent of saying an encrypted "I'm done now"!

Moreover, it'd be gr8 if SB had some new keywords like -> BreakFor, BreakWhile & NextFor, NextWhile.

It would be nicey as well if we had a new type of loop where the condition is checked at the end of it. Like -> Repeat ... Until <condition becomes TRUE>.

And of course, the equivalents -> BreakRepeat & NextRepeat!  ;-P

It'd be excellent for user's input, where a loop has to happen at least once, and keep on repeating until its exit condition is satisfied!

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Sunday, October 21, 2012 3:24 AM

### All replies

• The line "if goto makeguess..." does not make sense

try this:

TextWindow.WriteLine ("Hello, can you guess the number i am thinking of?, it is between 1 and 100")
RandomNumber= Math.GetRandomNumber (100)

tries = 0

MakeGuess:

tries = tries + 1

If Guess > RandomNumber Then
TextWindow.WriteLine ("Nope, that's wrong. My number is lower")
ElseIf Guess < RandomNumber Then
TextWindow.WriteLine ("Nope, that's wrong. My number is higher")
EndIf

If Guess=RandomNumber Then
Sound.PlayBellRing()

TextWindow.WriteLine ("Yeah, you are correct!")

Else

if tries < 8 then

goto MakeGuess

else

TextWindow.WriteLine ("You have had far too many guesses, so GAMES OVER (muhahahaha) LOSER!!!")

endif

EndIf

The whole data on the internet is saved in 540.000.000.000.000.000.000.000.000 electrons, which is as much as a handful of peanuts

Thursday, October 18, 2012 4:50 PM
• Hi Ruweyda,

Welcome to the forums. Here is a simple example for you to look at, try and move away from using GoTo's.

```Answer = Math.GetRandomNumber(100)
For Guesses = 1 To 8
TextWindow.Write("Guess: ")

TextWindow.WriteLine("Guess Lower")
TextWindow.WriteLine("Guess Higher")
Else
TextWindow.WriteLine("WINNER - You guessed in " + Guesses + " guesses")
Guesses = 8
EndIf
EndFor```

Thursday, October 18, 2012 6:54 PM
• ...move away from using GoTo's.

What's so wrong in the use of goto? Does the computer explode if it's used too often?

It's the most simple (and probably the second important) command for controlling a computer. JMP was there before JSR - and JSR is a combination of 2 JMPs. So one day you might advice people not to use Subs, because they do two Goto's?

If goto would be such a dangerous command - why did they build it in?

And the other thing: Listing above doesn't tell, if game's over

The whole data on the internet is saved in 540.000.000.000.000.000.000.000.000 electrons, which is as much as a handful of peanuts

• Edited by Thursday, October 18, 2012 7:28 PM
Thursday, October 18, 2012 7:21 PM
• I think he said that because too many 'goto's can lead to spaghetti code (or very unreadable code).

I am a 10 year old that loves math, games, and computers. 'Binary is as easy as 1, 10, 11.'

Thursday, October 18, 2012 8:15 PM
• There is nothing wrong with using Goto's as such but why make your life harder? It's about using the right tools for the task at hand.

"For loops are the shorthand way to make loops when the number of iterations is known" - The original poster knew how many times he wanted to ask the same questions and perform the same checks, so why use a GoTo?

GoTo's are generally harder to read and follow, like Math Man said they can lead to spaghetti code, sub routines can too but then are more so than not placed inside a main loop keeping spaghetti code to a minimum.

Here is an example why you shouldn't use them

```ThisIsTrue = "True"
While ThisIsTrue
TextWindow.WriteLine("ThisIsTrue")

Goto ThisIsFalse

EndWhile
ThisIsFalse:
TextWindow.WriteLine("ThisIsTrue should now be false but it isn't!")```

So I have escaped my condition even though its not false, why would I do that?

• Edited by Thursday, October 18, 2012 11:01 PM
Thursday, October 18, 2012 10:58 PM
• I sometimes use Gotos myself, but try to keep them to a minimum. 90% of it is for infinite loops.

The other rare uses would be to escape from or immediately reach the end of a loop. Respectively emulating the keywords 'Break' & 'Continue', which don't exist in SB.

Specifically for the For...EndFor loop type, a 'Break' can be scheduled to happen next time the loop reaches the EndFor, by making the iterator = condition ( or greater than ). Just like Mainchip did w/ the iterator Guesses = 8.

In fact, it's the 1st time I see some1 using "for break hack" besides myself in this forum!  ;-P

A last example of use of Goto would be to jump ahead and skip a portion of code inside a loop, which shouldn't be executed at 1st, but should happen for all the other times. It's a little spaghetti doing so, but it spares the need & execution time to make an extra If...EndIf condition just to avoid that 1st execution!

In general, just avoid Gotos and use loops & subroutines instead!

@ Rene_Miner -> Java language creators deliberately removed Goto keyword from it!   >;-)

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Friday, October 19, 2012 12:40 AM
• And the other thing: Listing above doesn't tell, if game's over.

Nice spot! But there are solutions too!

A boolean flag variable can be used, which is the easiest method or...

Consider the following 3 For loops:

```For guesses = 1 To 8
EndFor

TextWindow.WriteLine("Guesses = " + guesses)

For guesses = 1 To 8
guesses = 8
EndFor

TextWindow.WriteLine("Guesses = " + guesses)

For guesses = 1 To 8
guesses = 8 + 1
EndFor

TextWindow.WriteLine("Guesses = " + guesses)```

The 1st 2 loops, by the end, have guesses = 9, which is the condition 8 increased by 1.

Now, 3rd one we obtain guesses = 10, which is condition 8 increased by 1, plus the additional 1 present in guesses = 8 + 1.

Thus, a For loop which hadn't its iterator manipulated (and w/ Step = 1), would always finish w/ its iterator = condition (after the To keyword) + 1. Otherwise, we can conclude the iterator has been manipulated to a greater number!

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Friday, October 19, 2012 12:56 AM
• Hi again!

Re-modified the already modified Mainchip's version, to illustrate better the abnormal loop break check.

And guess what, shoved in an infinite Goto loop!  Mwaahahah!!!

```guessesMax = 8
numberMax  = 100
TextWindow.BackgroundColor = "DarkBlue"

GameLoop:

TextWindow.Clear()

TextWindow.WriteLine("Guess a number between 1 & " + numberMax + " :")
TextWindow.WriteLine("")

For guesses = 1 To guessesMax

TextWindow.Write("Guess #" + guesses + " ")

TextWindow.WriteLine("Guess Lower!")
TextWindow.WriteLine("Guess Higher!")
Else
TextWindow.WriteLine("WINNER! - You've guessed in " + guesses + " guess(es).")
guesses = guessesMax + 1  '  guesses will be greater than guessesMax + 1 in the end
EndIf

TextWindow.WriteLine("")

EndFor

If guesses = guessesMax + 1 Then  '  True if loop iterator had not been tampered with
TextWindow.WriteLine("You've had far too many guesses, so GAME OVER (mwaaahahahah) LOSER!!!")
EndIf

TextWindow.Pause()

Goto GameLoop```

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Friday, October 19, 2012 1:13 AM
• Guys I did not mean it that way. Check the first line of this thread, Ruweyda is making her/his first steps and just learns that there is something like a "GoTo" and how to use it. So  now you frighten Ruweyda of doing something wrong by using that goto, but there's nothing wrong with it if it's used in the right place.

Peace?

The whole data on the internet is saved in 540.000.000.000.000.000.000.000.000 electrons, which is as much as a handful of peanuts

Friday, October 19, 2012 7:20 AM
• A quote from Linus Torvalds on the utility of Goto.

And sometimes structure is bad, and gets into the way, and using a "goto" is just much clearer.

For example, it is quite common to have conditionals THAT DO NOT NEST.

In which case you have two possibilities

• use goto, and be happy, since it doesn't enforce nesting

This makes the code more readable, since the code just does what the algorithm says it should do.

• duplicate the code, and rewrite it in a nesting form so that you can
use the structured jumps.

This often makes the code much LESS readable, harder to maintain, and bigger.

The Pascal language is a prime example of the latter problem. Because it doesn't have a "break" statement, loops in (traditional) Pascal end up often looking like total shit, because you have to add totally arbitrary logic to say "I'm done now".

Basically what it comes down to is this: If you only use goto's as your looping and branching mechanism your code is going to be a mess. However, if you religiously avoid goto's your code can wind up just as convoluted and confusing.

Saturday, October 20, 2012 9:38 PM
• The Pascal language is a prime example of the latter problem. Because it doesn't have a "break" statement, loops in (traditional) Pascal end up often looking like total shit, because you have to add totally arbitrary logic to say "I'm done now".

Small Basic doesn't have a keyword equivalent for 'break' to get outta loop, much less a 'continue' to force an immediate next iteration of it.

So, I do use Gotos for these cases. Or use the obscure & messy 'break hack emulation' in the case of For loops, making the iterator variable >= the end condition; which is equivalent of saying an encrypted "I'm done now"!

Moreover, it'd be gr8 if SB had some new keywords like -> BreakFor, BreakWhile & NextFor, NextWhile.

It would be nicey as well if we had a new type of loop where the condition is checked at the end of it. Like -> Repeat ... Until <condition becomes TRUE>.

And of course, the equivalents -> BreakRepeat & NextRepeat!  ;-P

It'd be excellent for user's input, where a loop has to happen at least once, and keep on repeating until its exit condition is satisfied!

Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

Sunday, October 21, 2012 3:24 AM