Computer Science Canada Case selector out of range? |
Author: | Headedcorps [ Mon Oct 30, 2017 1:29 pm ] | ||
Post subject: | Case selector out of range? | ||
What is it you are trying to achieve? I'm trying to loop a case statement for a text based adventure. The goal is to be able to go back and forth between rooms. What is the problem you are having? The problem that I'm having is that when the player enters a character for the second time, the game stops and the code says that the case selector is out of range. Describe what you have tried to solve this problem I have tried to instead use an if statement, but it either doesn't work or its taking way too long to work. Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) Note: I'm not finished. If it won't run, remove the Music.PlayFileLoop command from the doghouse procedure.
Please specify what version of Turing you are using 4.1.1 |
Author: | Clayton [ Tue Oct 31, 2017 11:32 am ] | ||||||
Post subject: | RE:Case selector out of range? | ||||||
So the way you're organizing the structure of your code can work with some effort. However, having procedures modify global variables that affect other areas of your code is generally considered bad practice. Best practice would be having each procedure calling the next from within itself using local variables to keep track of any inputs given by the user. This also benefits you because it makes debugging much simpler, since you know exactly which block of code is causing you issues, saving you tons of time. Of course, with Turing you may run into issues because you cannot do something like this:
However, Turing gives us the tools to accomplish this task by forward declaring our functions and procedures allowing us to call a function or procedure, even if, top to bottom, they haven't been written yet:
I'm also not sure why you wrote the following the way you did, but it makes your code unnecessarily obfuscated. Stick to using true or false if you're hardcoding a default value for booleans.
|
Author: | Insectoid [ Tue Oct 31, 2017 8:26 pm ] |
Post subject: | RE:Case selector out of range? |
Wouldn't that structure eventually lead to a stack overflow Clayton? I prefer the looping case statement personally. Anyway, the error may be happening because ans is a char, while the case is expecting a string. You can fix this by changing the "double quotes" in the case into 'single quotes'. Turing interprets double quotes as strings and single quotes as chars. |
Author: | Clayton [ Wed Nov 01, 2017 1:50 pm ] |
Post subject: | Re: RE:Case selector out of range? |
Insectoid @ Tue Oct 31, 2017 8:26 pm wrote: Wouldn't that structure eventually lead to a stack overflow Clayton? I prefer the looping case statement personally.
Anyway, the error may be happening because ans is a char, while the case is expecting a string. You can fix this by changing the "double quotes" in the case into 'single quotes'. Turing interprets double quotes as strings and single quotes as chars. I suppose I didn't really word my response correctly. Theoretically, absolutely. Practically, and with context? Extremely unlikely. I personally don't like the looping case statement because you end up with needing unique responses for every single room you want to go to along with any subsequent actions within that room (searching, moving about, etc), and it makes any further additions a nightmare to organize and implement, and that only gets worse the larger you scale up. Assuming knowledge of what's in OP's code, I would probably approach this problem in such a way that each room is a function that returns an identifier to the main loop, which then calls the next room function on the next iteration of the loop. Still not ideal by any means but it cleans things up and creates an easier to follow logical flow. In my mind anyways. Personally, this project screams OOP to me, and would definitely be the way I approached it. Having a House object containing the instructions for movement between Room objects, with the Room objects having instructions for everything that can happen inside (finding loot, secrets, etc) just seems like the easy way to me. |
Author: | Headedcorps [ Tue Nov 14, 2017 3:05 pm ] |
Post subject: | Re: Case selector out of range? |
Thanks for the help. I have another small problem. I'm now using two different files, one of them has all of the rooms and the other has all of the variables and the start of the program. I have included the file with the rooms into the other file. Every once in a while, the file with the rooms will say that all of the variables and procedures have not been previously declared. I can temporarily fix this be removing the include command and re-adding it. Is there a more permanent solution? When I'm done, I'm just going to add the rooms onto the other file, but I would like to know for future projects. |