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

Username:   Password: 
 RegisterRegister   
 Procedure problems
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
StarFeind




PostPosted: Wed Jun 15, 2011 9:12 am   Post subject: Procedure problems

I am creating a text game and want to mkae it so that depending on the outcome of this procedure either the part of the story with a monster will be put or the part without the monster will be put, but I cant seem to get it to work. Any ideas?

Turing:


%MONSTER GENERATOR :
procedure Monsters
    var monster : int := 0
    var monsterno : int
    var monsteryes : int

    var ran : array 1 .. 1 of int
    for i : 1 .. 1
        randint (ran (i), 1, 100)
        if ran (i) > 60 then
            monster := monsterno
        elsif ran (i) < 60 then
            monster := monsteryes
        end if
    end for
end Monsters

Monsters
    if monster := monsterno then % If ran number is not over 60 it will not generate a monster
put ?text?
 elsif monster := monsteryes then % If ran number is  over 60 it will generate a monster
put ? text?
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Wed Jun 15, 2011 10:20 am   Post subject: RE:Procedure problems

The reason it's not working is because the monster variable is local to the Monsters procedure and cannot be accessed outside of it. As soon as the procedure ends, that variable vanishes.

What you really want is a function. I function is exactly like a procedure, except that it returns a value; in this case, probably a boolean value (true or false). The function declaration looks like this:

Turing:

function Monsters : boolean

    % fill in your code...
   
    result true
    % OR:
    result false
end Monsters


As soon as you use result, you will exit the function. Then, you can take the value returned like this:

Turing:

var there_are_monsters : boolean := Monsters()
if there_are_monsters then
    % do stuff for monsters...
else
    % do stuff for no monsters...
end if


You could also just use the result directly, without storing it locally:

Turing:

if Monsters then
    % do stuff for monsters
else
    % do stuff for no monsters
end if


But if you use that, be careful, because each time you reference Monsters, it will run the procedure again (possibly with different results). This can lead to confusing bugs:

Turing:

if Monsters then
    put "foo"
elsif Monsters then
    put "bar"
else
    put "baz"
end if


You would think that this would only ever say "foo" or "baz", but it turns out it says "bar" pretty frequently: whenever the first call to Monsters returns false, but the second returns true. You can fix this bug by doing the following:

Turing:

var there_are_monsters : boolean := Monsters()
if there_are_monsters then
    put "foo"
elsif there_are_monsters then
    put "bar"
else
    put "baz"
end if


This code will never output "bar".
StarFeind




PostPosted: Fri Jun 17, 2011 8:54 am   Post subject: Re: Procedure problems

Ok I got it working but its still got a bug I need ot fix and was wondering if anyone could help me with it. It only words if a monster is generated, if not it quits the program

function Monsters : boolean
randint (ran, 1, 100)
%result true
if ran > 60 then
result false
elsif ran < 60 then
end if

end Monsters

put Monsters
if Monsters then % If ran number is over 60 it will not generate a monster
put " You live"
else % If ran number is less then 60 it will generate a monster
put "Your dead
end if
DemonWasp




PostPosted: Fri Jun 17, 2011 11:09 am   Post subject: RE:Procedure problems

Look at the code inside your function. If ran > 60, then you return false. If ran is less than 60, you...never return anything. Every function must explicitly return a value, so add a "result true" line in there somewhere (up to you where).
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  [ 4 Posts ]
Jump to:   


Style:  
Search: