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

Username:   Password: 
 RegisterRegister   
 Need help linking multiple procedures to each other.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JesseM




PostPosted: Thu Jan 30, 2014 1:40 pm   Post subject: Need help linking multiple procedures to each other.

What is it you are trying to achieve?
I am trying to code a text adventure game. I am using procedures, one for each room, and I'm trying to make it so that if a variable representing the player input is set to something, then the procedure for that corresponding action plays.


What is the problem you are having?
Only one of the five procedures actually works, but the code is identical in each case.


Describe what you have tried to solve this problem
I'm not very knowledgeable about Turing, unfortunately. I'm sort of a fledgling coder, and I don't really know why this isn't working. At least if I got an error message I'd know where to start! I think my problem might have to do with where the lines of code are placed but if that's the case I don't know how to fix it.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
The "game" works so far as going to the first hallway, getting the key from the keyroom and then leaving. So, the first, second and third procedure work, but the other ones don't. I am trying to teach myself Turing and from what I've seen of other errors I suspect that the reason only the keyroom procedure works is because the other procedures are below it? If I;m right or wrong, please tell me, as I would love to learn.

Turing:


put "You and a band of other adventurers were exploring a dungeon when the "
put "entrance collapsed! Only you made it out, but your survival came at the "
put "expense of your equipment! Now, you find yourself stranded in a strange "
put "cave system, with only one goal: find a way out!"
    %VARIABLES
    var input : string
    var knife : boolean
    var potion : boolean
    var key : boolean
    var sword : boolean
    var helmet : boolean
    var gold : int
    var maxhealth : int
    var currenthealth : int
    var maxstamina : int
    var currentstamina : int
    var torch : boolean
    var gottenkey : boolean
    var got_coin_one : boolean
    got_coin_one := false
    gottenkey := false
    torch := false
    knife := false
    potion := false
    key :=false
    sword := false
    helmet := false
    gold :=0
    maxhealth:= 20   
    currenthealth := 20
    maxstamina := 10     
    currentstamina := 10
%Procedures
forward procedure entrance
forward procedure firsthall
forward procedure keyroomONE
forward procedure tut_goldroom
forward procedure tut_gate_nokey
forward procedure tut_gate_key
forward procedure main_entrance
%Tutorial segment
body procedure entrance
loop
get input
if input = "look" and torch = false then put "Behind you is a pile of rubble which used to be the "
    put "entrance to the dungeon. In front of you is the cave mouth, shrouded in inky "
    put "blackness. To your right there flickers a dim torch. (type help for help)"
end if

if input = "look" and torch = true then put "Behind you is a pile of rubble which used to be the "
put "entrance to the dungeon. In front of you is the cave mouth, shrouded in inky "
put "blackness. The torch rests in your hand now, and its light plays off of the"
put "bronze stand which was once its home."
end if

if input = "inv" then put "You are carrying:"
if gold >0 then put gold, " gold pieces"
if knife = true then put "a knife"
if potion = true then put "a potion"
if key = true then put "a key"
if sword = true then put "a sword"
if helmet = true then put "a helmet"
end if
end if
end if
end if
end if
end if
end if
if input = "get torch" or input ="torch" then put "you pick up the dim torch"
torch :=true
end if
if input = "go forward" or input ="forward" and torch = false then put "The dark makes you uneasy"
end if
if input = "go forward" or input = "forward" and torch = true then put "you advance forward"
firsthall
end if
if input = "help" then put "inv opens inventory, look looks around, get (object) picks up an object,"
put " go (direction) or just (direction) will move in that direction, and nav open the navigation menu."
end if
exit when input = "go forward" or input = "forward" and torch = true
end loop
end entrance

%tutorial area hallway%
body procedure firsthall
put "You are in a long, smooth hallway carved into the rock of the cave. "
put "Immediately to your left is a large doorway. Ahead and to your right is another"
put "gaping hole in the wall. Straight ahead is a large gate with an oversized lock"
put "on it."
loop
get input

if input = "look" then put "You are in a long, smooth hallway carved into the rock of the cave. "
put "Immediately to your left is a large doorway. Ahead and to your right is another"
put "gaping hole in the wall. Straight ahead is a large gate with an oversized lock"
put "on it."
end if

if input = "inv" then put "You are carrying:"
if gold >0 then put gold, " gold pieces"
if knife = true then put "a knife"
if potion = true then put "a potion"
if key = true then put "a key"
if sword = true then put "a sword"
if helmet = true then put "a helmet"
end if
end if
end if
end if
end if
end if
end if

if input = "help" then put "inv opens inventory, look looks around, get (object) picks up an object,"
put " go (direction) or just (direction) will move in that direction, and nav open the navigation menu."
end if

if input = "left" or input = "go left" then put "You head left"            %This is the as far as the program works, once you try to go any direction other than left nothing happens%
keyroomONE
if input = "right" or input = "go right" then put "You go right"
tut_goldroom
if input = "forward" or input = "go forward" and key = false then put "You advance"
tut_gate_nokey
if input = "forward" or input = "go forward" and key = true then put "You advance"
tut_gate_key
end if
end if
end if
end if


end loop
end firsthall


%First Keyroom%
body procedure keyroomONE
if gottenkey = false then put "The room to your left is carved equally smoothly out of the solid rock. The room seems completely"
put "barren at first, but the light of your torch reflects of a small object in the center of the room."
put "It is a key!"
end if

if gottenkey = true then put "The room is empty, nothing remains but dust."
end if
loop
get input

if input = "look" then put "The room to your left is carved equally smoothly out of the solid rock. The room seems completely"
put "barren at first, but the light of your torch reflects of a small object in the center of the room."
put "It is a key!"
end if

if input = "look key" then put "It is hard to see from the doorway of the room, but the key seems gold or brass, and about the"
put "right size to fit the lock on the gate you saw earlier."
end if

if input = "inv" then put "You are carrying:"
if gold >0 then put gold, " gold pieces"
if knife = true then put "a knife"
if potion = true then put "a potion"
if key = true then put "a key"
if sword = true then put "a sword"
if helmet = true then put "a helmet"
end if
end if
end if
end if
end if
end if
end if

if input = "help" then put "inv opens inventory, look looks around, get (object) picks up an object,"
put " go (direction) or just (direction) will move in that direction, and nav open the navigation menu."
end if

if input = "key" or input = "get key" and gottenkey = false then put "You notice the key is strangely warm as you slip it into your"
put "pocket. Gained ONE KEY!"
gottenkey := true
end if

if input = "key" or input = "get key" and gottenkey = true then put "You already have the key."
end if

if input = "go back" or input = "back" or input = "return" or input = "door" then firsthall
end if


end loop
end keyroomONE

%hallway with gold piece 1%
body procedure tut_goldroom
if got_coin_one = false then put "You advance and head right to find yourself in another, narrower hallway. It banks right"
put "suddenly and then leads to a dead end. Just as you let out a groan and turn to leave, however,"
put "a glint catches your eye in the darkness! Some object is sitting on the floor before you!"
end if

if got_coin_one = true then put "The coin that was once here now rests comfortably in your pocket."
end if
loop
get input

if input = "look" and got_coin_one = false then put "You find yourself in another, narrower hallway. It banks right"
put "suddenly and then leads to a dead end. Just as you let out a groan and turn to leave, however,"
put "a glint catches your eye in the darkness! Some object is sitting on the floor before you!"
end if

if input = "look" and got_coin_one = true then put "There is nothing here but a dead end. You should head back and"
put "return to the main hallway."
end if

if input = "inv" then put "You are carrying:"
if gold >0 then put gold, " gold pieces"
if knife = true then put "a knife"
if potion = true then put "a potion"
if key = true then put "a key"
if sword = true then put "a sword"
if helmet = true then put "a helmet"
end if
end if
end if
end if
end if
end if
end if

if input = "look object" or input = "look glint" then put "Upon closer inspection, you realize the object is a small"
put "gold coin!"
end if

if input = "get coin" or input = "coin" or input = "object" or input = "get object" and got_coin_one = false then put "You crouch" ..
put " down and grab the object. It is a gold coin! Finally something good "
put "has come of this misadventure. Now you only need to escape!"
gold := gold+1
end if

if input = "get coin" or input = "coin" or input = "object" or input = "get object" and got_coin_one = true then put "You have it already"
end if

if input = "back" or input = "go back" or input = "return" and got_coin_one = false then put "You leave the hallway, and the faint glimmer fades."
firsthall
end if

if input = "back" or input = "go back" or input = "return" and got_coin_one = true then put "You head back to the main path, the slight weight in your pocket cheering you up already."
firsthall
end if


end loop
end tut_goldroom


%opening the gate with the key%
body procedure tut_gate_key
put "The key slides in easily and the gate vanishes. You head forward, eager to escape,"
put "but when you look behind you there is naught but a solid stone wall."
main_entrance
end tut_gate_key

%going to the gate without the key%
body procedure tut_gate_nokey
put "The lock is large and holds fast. You can sense magic coming off of it as soon as you approach, and despite the gate"
put "having many large gaps in it, you cannot see what lies beyond, even when you push your torch through the bars. Why"
put "was a mage enchanting a gate in this middle-of-nowhere cave? Regardless, there is nothing you can do to budge the"
put "gate without a key."
firsthall
end tut_gate_nokey

body procedure main_entrance
put "The cave system seems impossibly cold, and a tremendous gust of wind rushes past you and almost"
put "snuffs out you torch, as if to accentuate the icy air. The wind encourages you,"
put "however, as it means that the cave connects to the surface somewhere nearby!"
end main_entrance

entrance




Please specify what version of Turing you are using
I am using 4.1.1

Thank you in advance for taking the time to read my post!
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu Jan 30, 2014 2:06 pm   Post subject: RE:Need help linking multiple procedures to each other.

You should have a flag in your main loop that stores what room you are in. When you 'go left', set that flag to whatever room is left. Then let the procedure exit. Your program should include a main loop that repeatedly calls whatever room is the next one to run. It should look something like this:

code:

var nextRoom : int := 0 %let 0 be the first room (ie entrance)
loop
    switch nextRoom
        case 0: entrance
        case 1: room1
        case 2: room2
        %etc.
        case -1: exit %quit program when game ends
end loop

%sample room code
procedure entrance
    if left then
        nextRoom := room1
    else if right then
        nextRoom := room2
    end if
end entrance


There is a nicer way to run the main loop using an array of procedures, but I'm trying to keep it simple.
JesseM




PostPosted: Thu Jan 30, 2014 4:27 pm   Post subject: Re: Need help linking multiple procedures to each other.

Alright, I'll try to replicate that, thank you!
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  [ 3 Posts ]
Jump to:   


Style:  
Search: