|
|
@@ -1,115 +0,0 @@
|
|
|
-#include "MineField.hpp"
|
|
|
-
|
|
|
-MineField::MineField(int cols, int rows, int mines): m_rows(rows),
|
|
|
- m_cols(cols),
|
|
|
- m_totalMines(mines),
|
|
|
- m_remainingFlags(mines),
|
|
|
- m_exploded(false) {
|
|
|
- for(int i=0; i< m_cols*m_rows; i++) {
|
|
|
- std::shared_ptr<Cell> cell = std::make_shared<Cell>();
|
|
|
- m_cells.push_back(cell);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void MineField::initBombs(int x, int y) {
|
|
|
-
|
|
|
- int remainingMines = m_totalMines;
|
|
|
- int startPos = x + y * m_rows;
|
|
|
-
|
|
|
- srand(time(NULL)); //initialize rand()
|
|
|
-
|
|
|
- while(remainingMines > 0) {
|
|
|
- int position = rand() % (m_cols * m_rows);
|
|
|
- if(isBomb(position % m_cols, position / m_rows) || position == startPos) {
|
|
|
- continue;
|
|
|
- }
|
|
|
- m_cells.at(position)->isBomb = true;
|
|
|
- --remainingMines;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-bool MineField::clearCell(int x, int y) {
|
|
|
- setClearCell(x, y);
|
|
|
-
|
|
|
- if(isBomb(x, y)) {
|
|
|
- m_exploded = true;
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- if (bombsNearby(x, y) == 0) {
|
|
|
- openNeighboorhood(x, y);
|
|
|
- }
|
|
|
- return true;
|
|
|
-}
|
|
|
-
|
|
|
-void MineField::computeBombsNearby(int x, int y) {
|
|
|
- int total = 0;
|
|
|
- //compute bombs in neighboorhood
|
|
|
- for(int i=-1; i<2; i++) {
|
|
|
- for(int j=-1; j<2; j++) {
|
|
|
- if(x+i >= 0 && x+i < m_cols && y+j >= 0 && y+j < m_rows) {
|
|
|
- if(isBomb(x+i, y+j)){
|
|
|
- ++total;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- m_cells.at(x + y * m_rows)->bombsNearby = total;
|
|
|
-}
|
|
|
-
|
|
|
-void MineField::openNeighboorhood(int x, int y) {
|
|
|
- //compute bombs in neighboorhood
|
|
|
- for(int i=-1; i<2; i++) {
|
|
|
- for(int j=-1; j<2; j++) {
|
|
|
- if(x+i >= 0 && x+i < m_cols && y+j >= 0 && y+j < m_rows) {
|
|
|
- if((isCleared(x+i, y+j) == false) && (isBomb(x+i, y+j) == false)){
|
|
|
- setClearCell((x+i), (y+j));
|
|
|
- if(bombsNearby(x+i, y+j) == 0) {
|
|
|
- openNeighboorhood(x+i, y+j);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-bool MineField::isCleared(int x, int y) {
|
|
|
- return m_cells.at(x + y * m_rows)->isCleared;
|
|
|
-}
|
|
|
-
|
|
|
-bool MineField::isFlagged(int x, int y) {
|
|
|
- return m_cells.at(x + y * m_rows)->isFlagged;
|
|
|
-}
|
|
|
-
|
|
|
-bool MineField::isBomb(int x, int y) {
|
|
|
- return m_cells.at(x + y * m_rows)->isBomb;
|
|
|
-}
|
|
|
-
|
|
|
-int MineField::bombsNearby(int x, int y) {
|
|
|
- if(m_cells.at(x + y * m_rows)->bombsNearby == -1) {
|
|
|
- computeBombsNearby(x, y);
|
|
|
- }
|
|
|
- return m_cells.at(x + y * m_rows)->bombsNearby;
|
|
|
-}
|
|
|
-
|
|
|
-void MineField::setClearCell(int x, int y) {
|
|
|
- m_cells.at(x + y * m_rows)->isCleared = true;
|
|
|
- clearCellSignal.emit(x, y);
|
|
|
-}
|
|
|
-
|
|
|
-bool MineField::toggleFlag(int x, int y) {
|
|
|
- if(m_cells.at(x + y * m_rows)->isFlagged == true) {
|
|
|
- m_cells.at(x + y * m_rows)->isFlagged = false;
|
|
|
- ++m_remainingFlags;
|
|
|
- remainingFlagsChangedSignal.emit(m_remainingFlags);
|
|
|
- return true;
|
|
|
- }
|
|
|
- else if(m_remainingFlags > 0) {
|
|
|
- m_cells.at(x + y * m_rows)->isFlagged = true;
|
|
|
- --m_remainingFlags;
|
|
|
- remainingFlagsChangedSignal.emit(m_remainingFlags);
|
|
|
- return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
-}
|