views:

49

answers:

1

Ok, I've a data structure that consists of a matrix of linked nodes, lets say 10x10. I would like to be able to choose any of these nodes arbitrarily, and - from there - process a number of the surrounding nodes following the pattern of expanding concentric squares (I used less nodes below for illustration purposes). As far as specifics go, the implementation will be in Actionscript - but feel free to answer in another language :) - and it's for a minesweeper game (specifically the feature where, if clicking on a patch of mine-less squares, they -all- get cleared regardless of irregular shape, up to the surrounding mines). It will also be event based - dispatching one for each node traversed, so the way I imagine it is that when the iterator or whatever comes across a field containing a mine, traversal would stop from going further in that direction while the rest would continue. The nodes are doubly linked to their top, right, bottom, and left neighbors, and I wouldn't be opposed to adding more links if necessary. There's probably some other way to do it, but I can think of a number of uses for this kind of solution and would like to include it in a framework I'm creating, so I'd like to walk away with something I can reuse.

first:
0 0 0 0 0
0 0 0 0 0
0 0 X 0 0
0 0 0 0 0
0 0 0 0 0

second:
0 0 0 0 0
0 X X X 0
0 X 0 X 0
0 X X X 0
0 0 0 0 0

third:
X X X X X
X 0 0 0 X
X 0 0 0 X
X 0 0 0 X
X X X X X

A: 
function scan( x, y ){
    queue toDo;
    set queued;

    toDo.push( x, y );
    queued.add( x, y );
    while ( !toDo.empty() ) {
        (x, y) = toDo.removeHead();

        if ( process( x, y ) != stop ) {
            for( xp = -1; xp <= 1; ++xp ) {
                for( yp = -1; yp <= 1; ++yp ) {
                    if ( !queued.contains( x+xp, y+yp ) ) {
                        toDo.push( x+xp, y+yp );
                        queued.add( x+xp, y+yp );
                    }
                }
            }
        }
    }
}
David Smith
Pardon my ignorance, but what language is this? Do x,y refer to the coordinates in the matrix, because its not really an index based kind of thing, but linked lists instead. perhaps I can translate this code, im just a little unfamiliar with these syntax conventions like (x,y) = etc...
Oh ok cool - I get it. Thanks :) I will post the modifications I make to it so that it works in actionscript with linked objects instead of index based ones.
Yes, sorry, in my excitement over a question I could answer, I didn't adhere to your problem specifics. My code above was more pseudo-code and not a real language. In order for it to truly follow an expanding square pattern, you'll need to link on the diagonals, or you'll need to store another piece of information in the queue to indicate from which direction the space was scanned so it knows which direction to move outward. (I also built my own minesweeper in College and I think I used an array matrix hence why my solution accesses it using indeces). I'm glad you understood it.
David Smith