views:

213

answers:

1

I'm trying to make a vista minesweeper clone. Right now my uncover algorithm is the cascade algorithm. It kind of looks like a square that gets bigger and bigger. I noticed in vista mnesweeper, when its gameover, it iterates through the game in a circular manner to uncover the mines. Does anyone know what this algorithm is called? Thanks

A: 

The closest official name I could find for the algorithm you describe is the "Rectangular Spiral Search", which is an algorithm that output the path to follow to fully scan a grid in an spiral manner.

A simple algorithm for a clockwise scan of a square grid of size (2N + 1)^2, N > 0 would look like this:

(Assuming the coordinate (0,0) of the grid to be at the bottom left corner of the grid)

  1. Start at the center cell of the grid (N, N)
  2. Move left (-1,0) for the next cell
  3. Set the movement direction upward (0,+1)
  4. While the current cell (X,Y) is not a corner (i.e. (X-N)^2 == (Y-N)^2), move in the set direction
  5. If the current cell is not at (0,0)
    1. Turn the direction clockwise
      • (0,+1) becomes (+1,0)
      • (+1,0) -> (0,-1)
      • (0,-1) -> (-1,0)
      • (-1,0) -> (0,+1))
    2. Move towards new direction once
    3. Goto step 4
  6. If the current cell is (0,0), you are done

For (2N)^2 sized and non-square grids, you must adjust the first 3 steps.

An implementation for square grids is available for Perl in the Spiral object of the Array-Tour module.

TheQ