// _____________________________________________________________________________
void MineSweeperState::revealCell(size_t row, size_t col) {
  int mineCount = 0;

  // the game is lost if an unrevealed mine is revealed
  if (getCellInfo(row, col) == UNREVEALED_MINE) {
    _status = LOST;
    _mineField[row][col] = REVEALED_MINE;
  }

  // get number of mines in surrounding cells
  // do if the field is unrevealed and not a mine or marked as one
  if (getCellInfo(row, col) == UNREVEALED) {
    for (int i = -1; i < 2; i++) {
      for (int j = -1; j < 2; j++) {
        if (getCellInfo(row + i, col + j) <= MARKED_CORRECT) mineCount++;
      }
    }
    _mineField[row][col] = CellInfo(mineCount);
    _numRevealed++;

    // reveal cells recursively if there are no mines in surrounding cells
    if (mineCount == 0) {
      for (int i = -1; i < 2; i++) {
        for (int j = -1; j < 2; j++) {
          revealCell(row + i, col + j);
        }
      }
    }

    // check whether the game is won already
    if (_numMarked + _numRevealed == _numCols * _numRows) _status = WON;
  }
}
// _____________________________________________________________________________
void MineSweeperState::markCell(size_t row, size_t col) {
  // set CellInfo to marked (correctly/ẃrongly) if cell was unrevealed before
  if (getCellInfo(row, col) == UNREVEALED_MINE) {
    _mineField[row][col] = MARKED_CORRECT;
    _numMarked++;
  } else if (getCellInfo(row, col) == UNREVEALED) {
    _mineField[row][col] = MARKED_WRONG;

  // set CellInfo to unrevealed (no mine/mine) if cell was marked before
  } else if (getCellInfo(row, col) == MARKED_CORRECT) {
    _mineField[row][col] = UNREVEALED_MINE;
    _numMarked--;
  } else if (getCellInfo(row, col) == MARKED_WRONG) {
    _mineField[row][col] = UNREVEALED;
  }

  // check whether the game is won already
  if (_numMarked + _numRevealed == _numCols * _numRows) _status = WON;
}
Example #3
0
int main()
{
	printf("iPhone Cell Stumbler\n");
	cellconnect();
	int cellcount;
	while(1)
	{
		getCellInfo();
		sleep(1);
	}
	return 0;
}
// _____________________________________________________________________________
void MineSweeperState::drawField() {
  for (int i = 0; i < _numRows; i++) {
    for (int j = 0; j < _numCols; j++) {
      printf("%s\x1b[%d;%dH", RESET, i * 2 + _yAl + 1, j * 2 + _xAl + 1);
      CellInfo cellInfo = getCellInfo(i, j);

      // draw '? if cell is unrevealed and not a mine
      if (cellInfo == UNREVEALED) printf("?");

      // draw '?' if cell is unrevealed (mine) and 'x' when the game is lost
      else if (cellInfo == UNREVEALED_MINE) {
        if (_status == LOST) {
          printf("%sx", RED);
        } else {
          printf("?");
        }

      // draw 'X' if cell is marked (wrongly) and '-' when the game is lost
      } else if (cellInfo == MARKED_WRONG) {
        if (_status == LOST) {
          printf("%s-", RED);
        } else {
          printf("%sX", RED);
        }

      // draw 'X' if cell is marked (correctly)
      } else if (cellInfo == MARKED_CORRECT) {
        printf("%sX", RED);

      // draw '*' if cell is a revealed mine
      } else if (cellInfo == REVEALED_MINE) {
        printf("%s*", RED);

      // draw number of surrounding mines in diff. colors if cell is revealed
      } else if (cellInfo >= 0) {
        switch (cellInfo) {
          case(REVEALED_ZERO):
            printf("%s", WHITE);
            break;
          case(REVEALED_ONE):
            printf("%s", CYAN);
            break;
          case(REVEALED_TWO):
            printf("%s", GREEN);
            break;
          case(REVEALED_THREE):
            printf("%s", MAGENTA);
            break;
          case(REVEALED_FOUR):
            printf("%s", BLUE);
            break;
          case(REVEALED_FIVE):
            printf("%s", YELLOW);
            break;
          case(REVEALED_SIX):
            printf("%s", CYAN);
            break;
          case(REVEALED_SEVEN):
            printf("%s", GREEN);
            break;
          case(REVEALED_EIGHT):
            printf("%s", RED);
            break;
        }
        printf("%d", cellInfo);
      }
    }
  }
  printf("%s", RESET);
}