Grid Array "Matching" Algo Needed!

BlitzMax Forums/BlitzMax Beginners Area/Grid Array "Matching" Algo Needed!

kRUZe(Posted 2009) [#1]


As you can see from the image depicted I need to come up with an algo that will build a list of grid positions that need to be set to 0 in the Array[x,y]

I've already written the gravity drop function to shift the tiles for when the "3 or more" tiles are removed but can't get my head around the actual building of the list of tiles to remove, arrghh lol

Any help would be greatly appreciated.


degac(Posted 2009) [#2]
Edit: I mis-read your request.

The simplest algo is:
1. read each coloumns and count the tiles of the same colour without 'break'
2. same for the rows
3. for every 'read' you can put the matrix coordinates in a list
4. use the list of coordinates to
4.1 start the dropping
4.2 set to '0' the value of each cell
5. clear the list

And if I remember there should be an example in the source folder (birdie/games/tiledrop) that can resolve your problem.


degac(Posted 2009) [#3]
Another solution (maybe more targeted to your request - from your image it seems you need to remove ALL the cyan tiles 'connected' togheter...)

1. read each tiles and check if the closer (left,right,up,above...) is of the same type, so put it in the list


kRUZe(Posted 2009) [#4]
This will clear ALL of the chosen colour (cyan) on the grid though won't it?

I wanted to leave the others on the grid (although there are none in the example) which are not connected to the swap (joined) tiles.


kRUZe(Posted 2009) [#5]
Yes I do need to remove all the tiles connected together, but I can't get my head around the recursion (if needed) or the loop to build the list.

Maybe I need to eat more Vitamin B efficient foods :)


Perturbatio(Posted 2009) [#6]
would you be able to adapt a flood fill routine to do it?


kRUZe(Posted 2009) [#7]
A flood fill routine seems logical. And given the grid is small it won't be slow either hehe :)


Kurator(Posted 2009) [#8]
look for recursive flood fill algorithm, instead of coloring fields, just remember the coordinates for deleting them later


kRUZe(Posted 2009) [#9]
Finally got it sorted, for some strange reason I had already written a working routine but had messed up the recursive calls (don't ask me how probably brain implode). Anyway I finally got the code sorted and this is what I came up with based on some other code I found on this forum.