I’m trying to build game like http://games.yahoo.com/game/bricks-breaking in actionscript 3 (flash builder).
I am able to create an array of bricks (that are visible on game start), but I have no idea how to find a group of bricks in array.
Lets say we have array like so:
- 1 2 2 1 3 3 1 1 1 1 1 1 1
- 1 2 1 1 1 3 1 1 1 1 1 1 1
- 1 2 1 1 1 3 1 1 1 1 1 1 3
- 1 1 2 1 1 3 3 3 1 1 1 1 3
- 1 1 1 2 1 3 1 3 3 1 1 1 3
- 1 1 1 3 3 3 1 3 3 1 1 1 3
- 1 1 1 1 1 1 1 3 3 1 1 1 1
When the user clicks any brick colored red (in array lets say it is 3) the array after removing all 3 will look like that:
- 1 2 2 0 0 0 0 0 0 1 1 1 1
- 1 2 1 1 0 0 1 0 0 1 1 1 1
- 1 2 1 1 1 0 1 0 0 1 1 1 3
- 1 1 2 1 1 0 1 0 1 1 1 1 3
- 1 1 1 1 1 0 1 1 1 1 1 1 3
- 1 1 1 2 1 0 1 1 1 1 1 1 3
- 1 1 1 1 1 1 1 1 1 1 1 1 1
Basicly I want to remove all the items that are in group and are the same color.
Any suggestions how to do that?
Is there any kind of algorythm that I should use?
Thanks for advice
A simple way to remove elements is to use a recursive function. It’s not the only way (or even a good one) but it should be enough for this kind of game. Basically something like this:
Begin with the position that the user clicked and the colour of that position. If the colour matches it will set that entry to
0, if not it leaves the element alone. It recursively does this to all neighbouring elements. What is missing in this code are boundary checks which you need to add.In the next step you could iterate over each of the arrays columns from bottom to top, keep reference of the position of the first
0element you find and move any non-emtpy values you find after that to the lowest empty row position.