Dropping items in in a stack

I am making a game where i have items or blocks stacked on top of each other.

What i would like to do is, when the first item(or the item at the bottom of the stack) is removed, i would like to shift all the blocks above, down into the next position below it and add another block to the top of the stack.

Sort of like in a match 3 game.

Could someone provide instructions for doing this? or some type of psuedocode?

Thanks guys

ArrayList does this automatically: [icode]list.remove(0); // pull out first item, all other items shift down 1 place[/icode]

Right but in my game,

How do i shift each block down to replace the item below it?

something like:

blockA.moveTo(x,y);

Where y is equal to the position of the item below it?

I’m assuming blocks store their position somehow. Subtract 1 from the y coordinate of every block above the one that is removed.

Yes each block has its position stored.

The problem is, how do i subtract 1 or the amount needed from each item ABOVE the item that was removed??

How do you store your blocks? If it’s a simple Block[][] (or similar) you can just do this:


Block[][] blocks; // assuming [y][x] indexing

void removeBlock(int x, int y) {
    for(int t = y; t < blocks.length - 1; t++)
        blocks[t][x] = blocks[t + 1][x]; // move (copy) each block down (assuming t+1 is above t)
    blocks[blocks.length - 1][x] = null; // or blocks[][] = BLOCK_AIR or something
}

Work up from the block that is removed, shifting each one down.
If each Block has position fields don’t forget to update those as well.

public void removeBottomBlock(){
arraylistOfBlocks.remove(0);

for(Block b:arraylistOfBlocks){
b-=blockHeight;
}
}

Something like that?