Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Improving a Tutorial

Author
Message
Super Guy
13
Years of Service
User Offline
Joined: 20th May 2011
Location: Canada
Posted: 1st Feb 2012 00:22
Ok, I saw a tutorial on youtube on making a CYOA game but I also saw it used goto. Is there an alternative? How would you avoid goto with this?



Also, for some reason, if I enter other numbers, it still goes to one of the labels. Why is this and how do I stop it?

Thanks.

I not only program but review video games too.

Get the latest video game reviews at http://v-g-r.blogspot.com/! Please follow.
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 1st Feb 2012 09:12
I think this is supposed to be an example of GOTO. Anyway the below code just a way to simplify it. Using functions and a data file to handle a story-based game are recommended as GOTO or my example whould be unmanageable. Here's you code anyhow:


Hope this answers what you're looking for.

Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 1st Feb 2012 09:23
Or use subroutines:



But nonZero's code has a far better structure than this.

Super Guy
13
Years of Service
User Offline
Joined: 20th May 2011
Location: Canada
Posted: 1st Feb 2012 12:00
nonZero, can you please explain your code to me... I am a real beginner.

Quote: "Also, for some reason, if I enter other numbers, it still goes to one of the labels. Why is this and how do I stop it?

Thanks."


Umm... if I still used the exact same method (the one in the OP) how would I make it only work with the 3 numbers?

I not only program but review video games too.

Get the latest video game reviews at http://v-g-r.blogspot.com/! Please follow.
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 1st Feb 2012 19:11 Edited at: 1st Feb 2012 19:13
@Super Guy:
Quote: "nonZero, can you please explain your code to me... I am a real beginner."

Sure. Here's the code, this time with comments. I'm not always brilliant at explaining things, I hope I've clarified this.


Quote: "if I still used the exact same method (the one in the OP) how would I make it only work with the 3 numbers?"


The easiest way is to trap the INPUT statement in a loop and only exit if the answer given is within a determined range, 1-3 in this case, so:



Note the use of flow. Manipulating program flow can save you lots of time and that's why ELSE is an indispensable part of BASIC.

ON A SIDE-NOTE:

If you enter "2" then it goes to "AnswerDoor:" and "ThreatenAttack:". It's because the example posted is missing an "END" at the end of the label "AnswerDoor:" (And "ThreatenAttack:" but this won't affect it as the program reaches its end here). This is why GOTO is not good for beginners and I really wish these folk who write tutorials would just leave it out. GOTO is a WMD for performance but it is a dangerous command for many reasons. GOSUB is a better alternative (though personally, I prefer Functions because it makes linear, unified code. But that's just my preference, you must find your own - so long as it doesn't involve using GOTO, lol).

Here's the original code with numbers in the print statement:



Super Guy
13
Years of Service
User Offline
Joined: 20th May 2011
Location: Canada
Posted: 1st Feb 2012 22:03
Oh, yeah, I wasn't really finished so I forgot to put END. XD

Don't blame that issue on GOTO. LOL.

Thanks, your code makes more sense now. Thanks for everything!

I not only program but review video games too.

Get the latest video game reviews at http://v-g-r.blogspot.com/! Please follow.
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 2nd Feb 2012 07:57
Quote: "Don't blame that issue on GOTO."

Oh, but I will Mwah ha ha haaaaa!
Seriously though, there's nothing wrong with GOTO but plenty wrong with the implementation of GOTO (generally). When using GOSUB, you'll instinctively place the label and a RETURN at the end. Also people are more inclined to indent labels treated as SUBs (dunno why. If I use labels, I indent the entire proceeding code segment - not that I use labels often). But, most importantly, you cannot return from a GOTO jump without having another label to jump to. The following code illustrates a mixture of human-nature and technical problems arising from GOTO...

Using GOSUB:


Using GOTO:


So those are some of the list of many downsides to GOTO. But we'll throw in an example of correct usage too:



In this instance, we're using GOTO to produce a high-performance loop. It's easy for the coder (and his/her co-workers) to keep track of and debug. The loop is used in a situation where the program does not need to flow beyond the GOTO line. Any calls ca be made from within the loop and you'll find you can get better speeds than if you used a DO..LOOP. Also note that we don't need RETURN here because we are working backwards, so the program's natural flow will take us back to the calling point. Here's a slightly more complex example:



Here we used GOTO in place of WHILE..ENDWHILE. So yes, GOTO can be used but it should only be used if absolutely necessary and it should be implemented correctly. In the above example, it wasn't necessary as WHILE would have sufficed. Getting into the habbit of jumping all over the place generates laziness and untidy coding practice.

Anyway, that's just some advice. I'm not one of those dogmatic people who insist on never using GOTO (anymore, I used to be until I started playing around with it more and learning how to use it) but I definitely will say avoid it if you can.

Login to post a reply

Server time is: 2024-11-23 23:15:26
Your offset time is: 2024-11-23 23:15:26