Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Case selector out of range?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Headedcorps




PostPosted: 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.

Turing:


<Add your code here>var piece1 : boolean
var piece2 : boolean
var piece3 : boolean
var flashlight : boolean

var font : int := Font.New ("arial:75")

var ans : char

procedure startup
    piece1 := 0>1
    piece2 := 0>1
    piece3 := 0>1
    flashlight := 0>1
    put "WELCOME TO THE HAUNTED MANSION!"
    put ""
    Time.Delay (2000)
    put "Your goal is to get all three pieces of an artifact."
    Time.Delay (3000)
    put ""
    put "Please enter the letter/number corresponding with the action."
    put ""
    Time.Delay (3000)
    put "Good luck"
    Time.Delay (2000)
    put ""
end startup

procedure door
    put "You are standing in front of the mansion."
    Time.Delay (2000)
    put ""
    put "The front door is unlocked. What would you like to do?"
    Time.Delay (3000)
    put ""
    put "(f) Enter"
    Time.Delay (500)
    put "(b) Go around back"
    Time.Delay (500)
    put "(l) Leave"
    get ans
end door

procedure backyard
    put "You go around the house to the backyard."
    Time.Delay (2000)
    put ""
    put "You see a broken window and a doghouse. What would you like to do?"
    Time.Delay (3000)
    put ""
    put "(k) Go through the window"
    Time.Delay (500)
    put "(o) Search the doghouse"
    get ans
end backyard

procedure front
    put "The door is locked. You can either go up stairs, into the living room or into the hallway."
    Time.Delay (4000)
    put ""
    put "What would you like to do?"
    Time.Delay (2000)
    put ""
    put "(u) Go upstairs"
    Time.Delay (500)
    put "(j) Take the hallway shortcut"
    Time.Delay (500)
    put "(i) Search the living room"
    get ans
end front

procedure leave
    drawfillbox (0, 0, 640, 640, black)
    Time.Delay (1500)
    Draw.Text ("COWARD", 100, 200, font, 12)
    Time.Delay (1000)
end leave

procedure doghouse
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Time.Delay (50)
    put "."
    Music.PlayFileLoop ("Text to Speech - The Rap.mp3")
    put "Congrats, you found the secret cheat room!"
    Time.Delay (3000)
    put ""
    put "You may want to get out a piece of paper and a pencil to help 'memorize' them!"
    Time.Delay (5000)
    put ""
    put "Here we go!"
end doghouse

procedure livingRoom
    put "You find yourself surrounded by chairs, paintings and broken pieces of a table."
    Time.Delay (4000)
    put ""
    put "What would you like to do"
    Time.Delay (2000)
    put ""
    put "(n) Go to the dinning room"
    Time.Delay (500)
    put "(f) Go to the front of the house"
    Time.Delay (500)
    put "(h) Look around"
    get ans
end livingRoom

procedure hint %Hint in the living room
    put "You find a note that reads:"
    Time.Delay (1000)
    put ""
    put "'I think my kid hates me. Every time I go take a shower, he flushes the toilet!"
    put "I need to teach him a lesson...'"
    Time.Delay (3000)
    put ""
    livingRoom
end hint

procedure dinningRoom
    put ""
end dinningRoom

startup
door
loop
exit when piece1 = true
exit when piece2 = true
exit when piece3 = true
    case ans of
        label "b", "B" :
            backyard
        label "d", "D" :
            door
        label "h", "H" :
            hint
        label "i", "I" :
            livingRoom
        label "l", "L" :
            leave
        label "o", "O" :
            doghouse
    end case
end loop



Please specify what version of Turing you are using
4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: 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:
Turing:

proc foo
    put "foo"
    bar
end foo

proc bar
    put "bar"
end bar

foo


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:
Turing:

forward proc bar

proc foo
    put "foo"
    bar
end foo

body proc bar
    put "bar"
end bar

foo


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.
Turing:
piece1 := 0>1
piece2 := 0>1
piece3 := 0>1
flashlight := 0>1

%Should be:
piece1 := false
piece2 := false
piece3 := false
flashlight := false
Insectoid




PostPosted: 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.
Clayton




PostPosted: 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.
Headedcorps




PostPosted: 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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: