So, I'm implementing a game as my private get-started-in-Java project (here's a link to a description of the game, if you're interested: http://www.boardgamegeek.com/game/201) and am trying to write the scoring code, which is becoming insanely complicated, so I think I'm doing it wrong.

In brief: The board is a 9x9 grid, and I'm storing the pieces in a multidimensional array. At the end of the game, each player scores points for their groups of adjacent* pieces. Each group of pieces is worth N^2 points (where N is the number of pieces in the group, so 3 adjacent pieces = 9 points)

My pseudo code for this right now is:

1) Start at 0,0. Mark this square as "checked". 2) Which player's piece is here? (if no piece, then skip to next square) 3) Check adjacent squares for matching pieces (but make sure to not check "out of bounds" squares, as this will produce errors) 4) If an adjacent square has a matching piece, then (a)increment the matching piece count, and (b)add this square to a "matching adjacent square that still needs to be checked" list 5) Complete steps 3-4 for all squares adjacent to original checked square 6) Look at the "matching adjacent square that still needs to be checked" list - if nothing is there, then figure the score (based on the "matching pieces count"); but if there are squares in this list, then pick the first one and repeat steps 3-6 until this list is empty (which means you can figure the score)

I'm not sure I get it, but this is how I think I would go about it. Search from (0,0) across and top to bottom. Once I find a piece, I enter a recursive method sending the coordinates for that position. This method will call itself for the position to the right, below, to the left, and above. But I need a mechanism to avoid checking a given position more than once. One way to do that would be to keep a Set with a collection of checked positions (each element a {x,y} pair). Before checking the current position, I ignore it if it's already in the Set. Otherwise, I check it and then I add it to the Set. I'm sure there are better ways, but this would probably be enough, without getting too complex.