Ejemplo n.º 1
0
// _____________________________________________________________________________
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;
  }
}
Ejemplo n.º 2
0
	void GameBoard::revealCell(Cell *_cell) {
		if (!_cell->isRevealed() && !_cell->isMine()) {
			m_UnrevealedCells -= 1;
		}
		_cell->reveal();

		if (_cell->isMine()) {
			if (!m_PlayerHasLost) {
				revealAllMines();
				m_PlayerHasLost = true;
				Common::InstanceCollection::getInstance<NotificationManager>().NotifyLoss();
			}
			return;
		}

		if (_cell->getNeighbouringMines() == 0) {
			auto& neighbours = getNeighbours(_cell);

			for (auto& n : neighbours) {
				if (!n->isRevealed()) {
					revealCell(n);
				}
			}
		}
		handlePotentialWin();
	}
Ejemplo n.º 3
0
/**
  * Constructor for MainWindow. It will initialize the entire board and create the necessary starting elements for the game
  */
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //Global variables
    hasFinished = false; //Has the game finished?
    cellsRevealed = 0; //Number of current cells revealed
    flagsFlagged = 0; //Number of flags that have been flagged
    minesFlagged = 0; //Number of mines that have been flagged
    hasStarted = false; //If the game has started yet
    currentTime = 0; //The current time in seconds

    ui->setupUi(this);

    //Layout designs
    ui->mineContainer->setSpacing(0); //Forces the board cells to be spaced next to each other

    //Timer for the number of seconds that has passed by
    timer = new QTimer();

    //The display of the number of flags that have been put up (Mines left to solve)
    ui->lcdFlagCount->setDigitCount(2);
    ui->lcdFlagCount->display ( NUMBER_OF_MINES - flagsFlagged );

    //Initialize statuses
    // 0 = Empty, 1 = flagged, 2 = ?
    for ( int i = 0; i < 10; i++)
    {
        for ( int j = 0; j < 10; j++)
            mineStatus[i][j] = 0;
    }

    //Connect the UI elements
    connect(ui->actionHelp, SIGNAL(triggered()), this, SLOT(handleHelpButton()));
    connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(handleAboutButton()));
    connect(ui->action_Reset, SIGNAL(triggered()), this, SLOT(reset()));
    connect(ui->smileyFace, SIGNAL(clicked()), this, SLOT(handleSmileyFace()));
    connect(ui->actionTop_Ten, SIGNAL(triggered()), this, SLOT(handleTopTen()));
    connect(timer, SIGNAL(timeout()), this, SLOT(updateTimer()));

    //Now handle the actual game.. enough of this extra feature stuff. Now for the real deal!
    game = new Minesweeper();

    //We will need to map the click to an object's coordinates
    signalMapper = new QSignalMapper(this);
    signalMapper2 = new QSignalMapper(this);

    //Generate all the buttons for the game
    for( int i = 0; i < 10; i++)
    {
        for( int j = 0; j < 10; j++ )
        {
            MineSweeperButton* button = new MineSweeperButton("");

            //Button Styling
            button->setAttribute(Qt::WA_LayoutUsesWidgetRect); //Forces Mac OS X styled minesweeper to look like linux/windows
            button->setMaximumHeight(30);
            button->setMaximumWidth(30);
            button->setIcon (QIcon(QString(":/images/not_flat_button.png")));
            button->setIconSize (QSize(30,30));

            //Actually add the button to the container
            ui->mineContainer->addWidget(button, i, j);
            QString coordinates = QString::number(i)+","+QString::number(j); //Coordinate of the button
            //Map the coordinates to a particular MineSweeperButton
            signalMapper->setMapping(button, coordinates);
            signalMapper2->setMapping(button, coordinates);

            //Connections for the buttons
            connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
            connect(button, SIGNAL(rightButtonClicked()), signalMapper2, SLOT(map()));
            connect(button, SIGNAL(pressed()), this, SLOT(handleButtonPressed()));
            connect(button, SIGNAL(released()), this, SLOT(handleButtonReleased()));
        }
    }

    //Connect the signal mapper to this class so that we can handle its clicks
    connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(revealCell(QString))); //Left click
    connect(signalMapper2, SIGNAL(mapped(QString)), this, SLOT(hasRightClicked(QString))); //Right click
}