views:

65

answers:

2

First of all: this is not a homework assignment, it's for a hobby project of mine.

Background: For my Java puzzle game I use a very simple recursive algorithm to check if certain spaces on the 'map' have become isolated after a piece is placed. Isolated in this case means: where no pieces can be placed in.

Current Algorithm:

public int isolatedSpace(Tile currentTile, int currentSpace){
        if(currentTile != null){
            if(currentTile.isOpen()){
                currentTile.flag(); // mark as visited
                currentSpace += 1;
                currentSpace = isolatedSpace(currentTile.rightNeighbor(),currentSpace);
                currentSpace = isolatedSpace(currentTile.underNeighbor(),currentSpace);
                currentSpace = isolatedSpace(currentTile.leftNeighbor(),currentSpace);
                currentSpace = isolatedSpace(currentTile.upperNeighbor(),currentSpace);
                if(currentSpace < 3){currentTile.markAsIsolated();} // <-- the problem
            }
        }
        return currentSpace;
    }

This piece of code returns the size of the empty space where the starting tile is part of. That part of the code works as intented. But I came across a problem regarding the marking of the tiles and that is what makes the title of this question relevant ;)

The problem: The problem is that certain tiles are never 'revisited' (they return a value and terminate, so never get a return value themselves from a later incarnation to update the size of the empty space). These 'forgotten' tiles can be part of a large space but still marked as isolated because they were visited at the beginning of the process when currentSpace had a low value.

Question: How to improve this code so it sets the correct value to the tiles without too much overhead? I can think of ugly solutions like revisiting all flagged tiles and if they have the proper value check if the neighbors have the same value, if not update etc. But I'm sure there are brilliant people here on Stack Overflow with much better ideas ;)


Update: I've made some changes.

public int isolatedSpace(Tile currentTile, int currentSpace, LinkedList<Tile> visitedTiles){
        if(currentTile != null){
            if(currentTile.isOpen()){
                // do the same as before
                visitedTiles.add(); 
            }
        }
        return currentSpace;
    }

And the marktiles function (only called when the returned spacesize is smaller than a given value)

marktiles(visitedTiles){
       for(Tile t : visitedTiles){
            t.markAsIsolated();
       }
}

This approach is in line with the answer of Rex Kerr, at least if I understood his idea.

+2  A: 

This isn't a general solution, but you only mark spaces as isolated if they occur in a region of two or fewer spaces. Can't you simplify this test to "a space is isolated iff either (a) it has no open neighbours or (b) precisely one open neighbour and that neighbour has no other open neighbours".

Rafe
That´s a good idea, thanks! however the size of the isolated space is intended to change with the used pieces in the puzzle. Pieces are all of different shapes and come in three sizes: three blocks, four blocks, five blocks. So a space of less than 3 tiles is always isolated but when only four or five block pieces are available less than 4 tiles becomes an isolated space as well.
Erik1984
I thought that might be the case.Here's a thought which might make your algorithm a bit leaner: add a "space id" field to each space. When you place or remove a piece, pick a new, fresh, id for the space and just do the recursive flood-fill algorithm, marking every connected space with the new id and keeping track of how many spaces you have marked. If your space id has enough bits (an int is probably fine, a long is probably overkill), you don't need to keep a separate list of visited tiles or do a separate marking phase.
Rafe
+1  A: 

You need to have a two-step process: gathering info about whether a space is isolated, and then then marking as isolated separately. So you'll need to first count up all the spaces (using one recursive function) and then mark all connected spaces if the criterion passes (using a different recursive function).

Rex Kerr
Thanks for that solution! What you describe seems like what I've implemented in the meantime. Every tile that is flagged is now added to a list called visitedTiles. After the space is counted (and the final size is known) a second function called markTiles is called (only if the total size of the space is smaller than a certain value) that flags the listed tiles as isolated. This seems to work fine.
Erik1984