Page 1 of 1

checking groups in a 2d array

Posted: Mon Nov 09, 2009 6:10 am
by imyourhero
Hey,

I'm working on a puzzle game of the puyo-puyo style falling colored blocks variety. I've got the graphics set up and all the dropping action in place, but now I seem to be a bit stumped on how to test if the blocks are in groups of four together.

The board is currently represented by a 2d array that I just set up in load like this:

row[1] = {0,0,0,0,0,0,0,0}
...
row[8] = {0,0,0,0,0,0,0,0}

There's four colors, so each color is represented by a 1-4 in that space, and 0 is empty. Simple, right? Unfortunately now I can't seem to think of an efficient way to handle checking if the blocks are in groups of four or larger (any shape is fine, square, L-shape, line, whatever). I've sort of come up with ideas, but they all seem to take huge amounts of coding and processes that I feel should be able to be handled more...efficiently? Has anyone else done something like this? Does it just take more work than it seems like it would?

Re: checking groups in a 2d array

Posted: Mon Nov 09, 2009 9:45 pm
by subrime
You don't need to test the whole array, just when a block drops... in this case:
1) you only check the dropped block colour
2) the dropped block must be in the new (potential) shape
This should limit your search significantly.

Re: checking groups in a 2d array

Posted: Tue Nov 10, 2009 1:38 am
by imyourhero
Yeah this is a good idea. When I first thought of this, I thought it would be better to just have it check what I dropped, but then I started thinking about combos, i.e. blocks that drop into a breaking position as a result of other blocks breaking, and decided it might be better to just have a check for the whole board. Now that I think about it though, you're right, I should probably just check the blocks that the player drops, and then copy that code over into the section where the blocks drop as a result of breaking. Thanks.

The simplest idea I've got going is still something along these lines though:

Every time a block drops, you add its new coordinates to an array for testing and check every block around it. You then add any similar colored adjacent blocks to that array and proceed to check every space around those, checking to exclude ones already added into the array. If the group is found to have more than three blocks, you add that to a larger array of blocks to be broken, clear out the testing array, and move on to any other blocks that were just dropped. After all the blocks that were dropped are checked, you then break those blocks, drop any resulting blocks, and then it would loop. The checking seems a bit cumbersome still, but way more doable than checking the entire board all the time.