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

Username:   Password: 
 RegisterRegister   
 Understanding parameter pass..
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
basketball4ever




PostPosted: Wed Jan 19, 2005 9:47 pm   Post subject: Understanding parameter pass..

K... so in my code, i want whatever applies to player 1, apply to players 2,3, and 4 (aka: i'm doing snakes and ladders. thus, whatever happens to player 1, can also happen to player 2... like going up a snake or going down a snake.. or the position at which they're at:

code:
%S & L map2
proc slmap
    % Going Up ladder 2 to 11
    if pos1 = 2 then
        delay (1000)
        pos1 := 11
        put "You just went on a ladder!" ..
        map2pos1
    end if
    if pos2 = 2 then
        delay (1000)
        pos2 := 11
        put "You just went on a ladder!" ..
        map2pos2
    end if
    if pos3 = 2 then
        delay (1000)
        pos3 := 11
        put "You just went on a ladder!" ..
        map2pos3
    end if
    if pos4 = 2 then
        delay (1000)
        pos4 := 11
        put "You just went on a ladder!" ..
        map2pos4
    end if
    % Going Up ladder 6 to 9
    if pos1 = 6 then
        delay (1000)
        pos1 := 9
        put "You just went on a ladder!" ..
        map2pos1
    end if
    if pos2 = 6 then
        delay (1000)
        pos2 := 9
        put "You just went on a ladder!" ..
        map2pos2
    end if
    if pos3 = 6 then
        delay (1000)
        pos3 := 9
        put "You just went on a ladder!" ..
        map2pos3
    end if
    if pos4 = 6 then
        delay (1000)
        pos4 := 9
        put "You just went on a ladder!" ..
        map2pos4
    end if
    % Going up ladder 7 to 21
    if pos1 = 7 then
        delay (1000)
        pos1 := 21
        put "You just went on a ladder!" ..
        map2pos1
    end if
    if pos2 = 7 then
        delay (1000)
        pos2 := 21
        put "You just went on a ladder!" ..
        map2pos2
    end if
    if pos3 = 7 then
        delay (1000)
        pos3 := 21
        put "You just went on a ladder!" ..
        map2pos3
    end if
    if pos4 = 7 then
        delay (1000)
        pos4 := 21
        put "You just went on a ladder!" ..
        map2pos4
    end if
    % Going up ladder 10 to 25
    if pos1 = 10 then
        delay (1000)
        pos1 := 25
        put "You just went on a ladder!" ..
        map2pos1
    end if
    if pos2 = 10 then
        delay (1000)
        pos2 := 25
        put "You just went on a ladder!" ..
        map2pos2
    end if
    if pos3 = 10 then
        delay (1000)
        pos3 := 25
        put "You just went on a ladder!" ..
        map2pos3
    end if
...continued.


See how whatever i had to do for pos1 i had to do for pos2 and pos3 and pos 4 also... is there a way i can simplify this to make 400 lines into 100 Wink
Sponsor
Sponsor
Sponsor
sponsor
cycro1234




PostPosted: Wed Jan 19, 2005 9:54 pm   Post subject: (No subject)

Well, since the rules may apply to more than one player, why don't you use AND in your if statements. Also, why don't you have any elsifs in ur code?
Bacchus




PostPosted: Wed Jan 19, 2005 9:55 pm   Post subject: (No subject)

use arrays and fors
eNc




PostPosted: Wed Jan 19, 2005 9:59 pm   Post subject: Re: Understanding parameter pass..

Do you know how to use a case statement ? This may help you out a bit, also you can try setting a variable to the delay number so that you don't have to type as much, just some thoughts
cycro1234




PostPosted: Wed Jan 19, 2005 10:05 pm   Post subject: (No subject)

Here's an example:


var mark : int
put "Enter a mark from 1 - 10: " ..
get mark
case mark of
label 9, 10 :
put "Excellent"
label 7, 8 :
put "Good"
label 6 :
put "Fair"
label :
put "Poor"
end case

as opposed to:

var mark : int
put "Enter a mark from 1 - 10: " ..
get mark
if mark = 9 or mark = 10 then
put "Excellent"
elsif mark = 7 or mark = 8 then
put "Good"
etc. etc.

[/code]
basketball4ever




PostPosted: Wed Jan 19, 2005 10:15 pm   Post subject: (No subject)

Quote:
Well, since the rules may apply to more than one player, why don't you use AND in your if statements. Also, why don't you have any elsifs in ur code?


Lol well i'm waiting for my whole thing to be simplified first. Razz i figured i wouldnt use arrays already : P cos i'm too lazy. But i know using a simple parameter pass can fix up the whole code
cycro1234




PostPosted: Wed Jan 19, 2005 10:18 pm   Post subject: (No subject)

And what, might I ask, is a parameter pass???
basketball4ever




PostPosted: Wed Jan 19, 2005 10:19 pm   Post subject: (No subject)

parameter pass =
proc cycro (var:n:int) something like that :S ionno ms.**** said to use it : P
Sponsor
Sponsor
Sponsor
sponsor
Neo




PostPosted: Wed Jan 19, 2005 10:30 pm   Post subject: (No subject)

First setup your variables into an array, then you can make a procedure like this:
code:

proc slmap (player : int)
    % Going Up ladder 2 to 11
    if pos (player) = 2 then
        delay (1000)
        pos (player) := 11
        put "You just went on a ladder!" ..
        map2pos (player)
    end if
end slmap


That shortened your first 4 if statements into 1. I'll leave the rest up to you
Once you have that you can call ur procedure for each player by using a for loop:
code:

for i : 1 .. 4
    slmap (i)
end for
basketball4ever




PostPosted: Wed Jan 19, 2005 10:32 pm   Post subject: (No subject)

lol as i mentioned... i'm not an array user : P
Cervantes




PostPosted: Wed Jan 19, 2005 10:34 pm   Post subject: (No subject)

You should be. If you don't know how to use arrays, read one of CompSci.ca's tutorials. They can be found in the Turing Tutorials section. You can also do a search.
Arrays are easy to learn, and incredibly useful. You'll be glad you learned them, when you do.
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  [ 11 Posts ]
Jump to:   


Style:  
Search: