PDA

View Full Version : Calculate surrounding mines Using 2D vector



anviori
27th April 2016, 18:40
I am creating a minesweeper game as a class project.


#include <iostream>
#include <cstdlib>
#include <vector>
#include <fstream>

void setupGameBoard();
void calculateSurrounding(int row, int col);
void incrementCellValue(int row, int col);

using namespace std;

int mines = 0;
int rows = 0;
int columns = 0;
vector<vector<int> > mineField;

int main(){

cout << "\nLoading game from program..." << endl;
srand (time(NULL));

cout << "\nCommon field dimensions include: 9x9:10 mines, "
"16x16:40 mines, 16x30:99 mines." << endl;

cout << "Input number of rows: ";
cin >> rows;
cout << "Input number of columns: ";
cin >> columns;
cout << "Input number of mines: ";
cin >> mines;

for (int i = 0; i < rows; i++){

mineField.push_back(vector<int>(columns, 0));

}

int num_of_mines = 0;
int minex, miney;

while(num_of_mines < mines){

minex = rand() % (columns);
miney = rand() % (rows);

if(mineField[miney][minex] != 1){

mineField[miney][minex] = 1;
num_of_mines++;
}
}

setupGameBoard();

return 0;
}

/*This method sets up the game Board by calculating the number of neighboring mines for each cell.
* calls calculateSurrounding() on each cell
*/

void setupGameBoard(){

int row, col;
row = mineField.size();
col = mineField[0].size();

for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(mineField[i][j] != 1){

calculateSurrounding(i,j);

}
}
}
}

/* Generates numbers for surrounding tiles of mines. The only
* tiles with numbers are those surrounding mines; these tiles are
* updated as mines are generated.
*/
void calculateSurrounding(int row, int col){
//should update surrounding tiles to reflect presence of adjacent mine



incrementCellValue(row, col);
}

/* increments the cell value(number of mines surrounding it) if the input location is valid
* called from calculateSurrounding() for each adjacent mine.
*/
void incrementCellValue(int row, int col){

}

Here, I need to populate the mine Field with mines randomly (which I have already done), and then set up the game board by calculating the number of neighboring mines for each cell(which is what I'm having trouble doing, I can't figure out how to start). (1 = mine)