分类: C/C++
2008-03-12 08:57:34
void CMineWnd::LayMines(UINT row, UINT col)
{
srand( (unsigned)time( NULL ) );
UINT i, j;
for(UINT index = 0; index < m_uMineNum;) {
i = rand() % m_uYNum;
j = rand() % m_uXNum;
if (i == row && j == col) continue;
if(m_pMines[i][j].uAttrib != ATTRIB_MINE) {
m_pMines[i][j].uAttrib = ATTRIB_MINE;
index++;
}
}
}此处的srand( (unsigned)time( NULL ) );是为了每次产生的随机数都不相同。展开空白窗体周围的区域: void CMineWnd::ExpandMines(UINT row, UINT col)
{
UINT i, j;
UINT minRow = (row == 0) ? 0 : row - 1;
UINT maxRow = row + 2;
UINT minCol = (col == 0) ? 0 : col - 1;
UINT maxCol = col + 2;
UINT around = GetAroundNum(row, col);
m_pMines[row][col].uState = 15 - around;
m_pMines[row][col].uOldState = 15 - around;
// redraw special MINEWND
DrawSpecialMine(row, col);
if (around == 0) {
for (i = minRow; i < maxRow; i++) {
for (j = minCol; j < maxCol; j++) {
if (!(i == row && j == col) && m_pMines[i][j].uState == STATE_NORMAL
&& m_pMines[i][j].uAttrib != ATTRIB_MINE) {
if (!IsInMineArea(i, j)) continue;
ExpandMines(i, j);
}
}
}
}
} 此处为一个递归函数,查找需要展开的小窗体周围的8个小窗体,若再次查找到空白,再进行查找,直到把能展开的全部展开。其他的几个主要的函数代码量比较大,各位可以到附带的实例代码中去看,就不在这里祥述了 。