1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
inline bool mineField::canBeRevealed( int r, int c )
{
if( r < 0 ) return false;
if( c < 0 ) return false;
if( r >= rows ) return false;
if( c >= cols ) return false;
if( ppField[r][c].revealed ) return false;
if( ppField[r][c].mark == 'f' ) return false;
if( ppField[r][c].mark == '?' ) return false;
return true;
}
void mineField::clearSpace( int r, int c )
{
if(!canBeRevealed(r,c))
return;
ppField[r][c].revealed = true;
if( ppField[r][c].cnt != '0' )
return;
ppField[r][c].fillOrigin = -1; // this is our true origin, so it has no fill origin
/* 8 directions from a given tile arranged like so:
0 1 2
3 x 4
5 6 7
*/
static const int xadj[8] = {-1, 0, 1,-1, 1,-1, 0, 1};
static const int yadj[8] = {-1,-1,-1, 0, 0, 1, 1, 1};
while(true)
{
// r and c are our origin (the current center tile)
// y and x is the current neighbor we're examining for this tile.
// dir is the direction y,x are from r,c
int y, x, dir;
// look to check each direction. Note we will break out of this loop
// if we find a direction that has a 'zero neighbor'
for(dir = 0; dir < 8; ++dir)
{
y = r + yadj[dir];
x = c + xadj[dir];
if(canBeRevealed(y,x))
{
ppField[y][x].revealed = true;
if(ppField[y][x].cnt == '0')
break;
}
}
// if we found any zero neighbors, make them our new origin, but
// record where we came from so we can go back
if(dir < 8) // any zero neighbors?
{
ppField[y][x].fillOrigin = dir;
r = y;
c = x;
}
else // no zero neighbors
{
// can we move back?
int o = ppField[r][c].fillOrigin;
if(o >= 0) // we have an origin, move back to it
{
r -= yadj[o];
c -= xadj[o];
}
else // no origin, and no zero neighbors found, we're done
return;
}
}
}
|