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

Username:   Password: 
 RegisterRegister   
 Multidimensional flexible arrays using classes
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
NikG




PostPosted: Sun Sep 10, 2006 1:50 am   Post subject: Multidimensional flexible arrays using classes

I've been trying to figure out a solution to Turing's inability to resize multidimensional flexible arrays for my rts game. One of the posts on this site suggested using classes, so I decided to try it:
code:
class yAxis
    export setUpper, setY, Y

    var fullY : flexible array 1 .. 0 of int

    proc setUpper (MaxY : int)
        new fullY, MaxY
    end setUpper

    proc setY (which, what : int)
        fullY (which) := what
    end setY

    fcn Y (which : int) : int
        result fullY (which)
    end Y
end yAxis

var X : flexible array 1 .. 0 of ^yAxis

%test: 10x10 array
new X, 10
for i : 1 .. 10
    new X (i)
    X (i) -> setUpper (10)
    for j : 1 .. 10
        X (i) -> setY (j, i * j)
    end for
end for

for i : 1 .. 10
    for j : 1 .. 10
        put X (i) -> Y(j):4 ..
    end for
    put ""
end for

new X, 0

This works, and resizing it simply involves the following:
code:
%test: 20x20 array
new X, 20
for i : 1 .. 20
    new X (i)
    X (i) -> setUpper (20)
end for

The only problem with this is that I'm not sure about how well it can remember the old values when you resize it.

Hope this helps those of you trying to solve this problem.
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Sun Sep 10, 2006 8:53 am   Post subject: (No subject)

Interesting... its a fairly round about way of doing it but it gets the job done (even if it is tedious Razz), although i would have to say, dont get into the habit of:

code:

new X (i)


for a pointer to reference an object, all turing is doing here is assuming what object you want, and while you only have one here, its not so bad, but its just a general bad coding practice.
NikG




PostPosted: Sun Sep 10, 2006 12:48 pm   Post subject: (No subject)

Right you are Freakman. Guess I was just being lazy...

For those of you who don't know, the correct way should be:
code:
new yAxis, X (i)
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: