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

Username:   Password: 
 RegisterRegister   
 Deleting Flexible arrays
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
shoobyman




PostPosted: Wed Apr 18, 2007 6:15 pm   Post subject: Deleting Flexible arrays

I have this piece of code, and as you can see (where it says deleting flexible arrays), it only deletes the sparkles one at a time. This works fine if i only have 1 sparkle, but i have hundreds. Does anyone know a way to delete more than one part of a flexible array at once (within the same loop iteration)?

this is the code

code:
View.Set ("offscreenonly")
colourback (black)
var sparkx, sparky, velx, vely : flexible array 1 .. 0 of real
var explx, exply, duration, ctr : flexible array 1 .. 0 of int
var x, y, button : int
var sparkdelete, expldelete, sparknum : int := 0

proc create_explosion
    new explx, upper (explx) + 1
    new exply, upper (exply) + 1
    new duration, upper (duration) + 1
    new ctr, upper (ctr) + 1
    explx (upper (explx)) := x
    exply (upper (exply)) := y
    duration (upper (duration)) := 10
    ctr (upper (ctr)) := 0
    sparknum := 3
end create_explosion

loop
    mousewhere (x, y, button)
    View.Update
    Time.DelaySinceLast (30)
    cls
    for i : 1 .. upper (explx)
        for pppp : 1 .. sparknum
            new sparkx, upper (sparkx) + 1
            new sparky, upper (sparky) + 1
            new velx, upper (velx) + 1
            new vely, upper (vely) + 1
            sparkx (upper (sparkx)) := explx (i)
            sparky (upper (sparky)) := exply (i)
            velx (upper (velx)) := (Rand.Int (-5, 5) * Rand.Real)
            vely (upper (vely)) := (Rand.Int (-5, 5) * Rand.Real)
        end for
        Text.Colour (0)
        ctr (i) += 1
        if ctr (i) >= duration (i) then
            expldelete := i
        end if
    end for
    %%%% DELETING EXPLOSIONS %%%%
    if expldelete > 0 then
        for i : expldelete + 1 .. upper (explx)
            explx (i - 1) := explx (i)
            exply (i - 1) := exply (i)
            duration (i - 1) := duration (i)
            ctr (i - 1) := ctr (i)
        end for
        new explx, upper (explx) - 1
        new exply, upper (exply) - 1
        new duration, upper (duration) - 1
        new ctr, upper (ctr) - 1
        expldelete := 0
    end if
    if button = 1 then
        create_explosion
    end if
    put upper (sparkx)
    for i : 1 .. upper (sparkx)
        sparkx (i) += velx (i)
        sparky (i) += vely (i)
        drawdot (round (sparkx (i)), round (sparky (i)), 0)
        vely (i) -= 0.607
        if sparkx (i) < 0 or sparkx (i) > maxx or sparky (i) < 0 or sparky (i) > maxy then
            sparkdelete := i
        end if
    end for
    %%%% DELETING SPARKLES %%%%
    if sparkdelete > 0 then
        for i : sparkdelete + 1 .. upper (sparkx)
            sparkx (i - 1) := sparkx (i)
            sparky (i - 1) := sparky (i)
            velx (i - 1) := velx (i)
            vely (i - 1) := vely (i)
        end for
        new sparkx, upper (sparkx) - 1
        new sparky, upper (sparky) - 1
        new velx, upper (velx) - 1
        new vely, upper (vely) - 1
        sparkdelete := 0
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: Thu Apr 19, 2007 5:07 pm   Post subject: Re: Deleting Flexible arrays

First store all the array elements that need to be deleted inside a flexible array.
code:
   
var useless:flexible array 0..0 of int


Then store all the elements that need to be deleted inside useless (eg. if spark(4) need to be deleted,then increase the size of useless and store 4 inside useless(1), and if spark (50) needs to be deleted, then increase the size of useless and store 50 inside useless(2) etc.). Then you use it in a for loop like so:
Turing:

 %%%% DELETING SPARKLES %%%%
 for x:1..upper useless
        for i : useless(x)+1  .. upper (sparkx)
            sparkx (i - 1) := sparkx (i)
            sparky (i - 1) := sparky (i)
            velx (i - 1) := velx (i)
            vely (i - 1) := vely (i)
        end for
        new sparkx, upper (sparkx) - 1
        new sparky, upper (sparky) - 1
        new velx, upper (velx) - 1
        new vely, upper (vely) - 1
end for
new useless,0
Cervantes




PostPosted: Thu Apr 19, 2007 6:08 pm   Post subject: RE:Deleting Flexible arrays

Lots of material can be found here.

It looks like codemonkey2000 has a good idea. It's discussed in the above linked article too. There maybe be better ways to do it, but this is probably the simplest that works as you want it to.

Using a linked list to store the indeces of the elements you want to remove would be better, since it takes far less work to add an element to a linked list than it does to a flexible array. However, this will be more work for you, since linked lists are not built into Turing.
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: