Math help

BlitzMax Forums/BlitzMax Programming/Math help

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

I need some help shrinking a function that contains a lot of if/endif statements. I have a 12x12 grid containing colored pieces, when the user clicks on a piece, I want the function to search all surrounding pieces and remove ones which are the same color as the clicked piece. Example:
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][2][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][2][2][ ][ ]
[ ][2][2][ ][ ][2][ ][ ][2][ ][ ][ ]
[ ][ ][2][ ][ ][2][ ][ ][2][ ][ ][ ]
[ ][ ][2][2][2][X][2][2][2][2][ ][ ]
[ ][ ][2][ ][ ][ ][ ][ ][ ][2][ ][ ]
[ ][ ][2][ ][ ][ ][ ][ ][ ][2][ ][ ]
[ ][ ][2][2][2][ ][ ][ ][ ][2][ ][ ]
[ ][ ][ ][ ][2][ ][ ][ ][ ][2][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][2]
If the user were to click the X (which is a 2), all connected two pieces would be removed, but the 2 in the bottom right corner would remain since it isn't connected. Anyone got an algorithm that can handle this kind of thing? The values are kept in an array, gridArr[12, 12]. It would also be helpful if the function could return how many pieces were removed.