I'm doing this minesweeper assignment and I need help. I tried to test the findMineValues() function and it completely scrambles up my board. Can someone help me pinpoint what I am doing wrong?
#include <string>
#include "Cell.h"
using namespace std;
Cell::Cell(){
label = 0;
mine = false;
flag = false;
checked = false;
}
void Cell::setLabel( int adjacentMines ){
label = adjacentMines;
}
int Cell::getLabel(){
return label;
}
void Cell::setMine( bool isMine ){
mine = isMine;
}
bool Cell::getMine(){
return mine;
}
void Cell::setChecked( bool newValue ){
checked = newValue;
}
bool Cell::getChecked(){
return checked;
}
void Cell::setFlag( bool newValue ){
flag = newValue;
}
bool Cell::getFlag(){
return flag;
}
#include <iostream>
#include <cstdlib>
#include <string>
#include <ctime>
#include "Cell.h"
using namespace std;
Cell board[10][10];
string topRow = "\n 1 2 3 4 5 6 7 8 9 10\n";
char action;
int row;
int column;
bool gameOver = false;
//show game board: COMPLETE (when I change it back to ?)
void displayBoard(){
cout << topRow;
for(int i = 0; i < 10; i++){
if( i <= 8){
cout << " " << i + 1 << " ";
}
else
cout << i + 1 << " ";
for(int j = 0; j < 10; j++){
if( board[i][j].getChecked() == true ){
cout << board[i][j].getLabel() << " ";
}
else if( board[i][j].getFlag() == true ){
cout << "F" << " ";
}
else
cout << board[i][j].getLabel() << " ";
}
cout << endl;
}
}//end displayBoard function
//Plant mines randomly in the field: Complete
void plantMines( int n ){
for( int count = 0; count < n; count++ ){
int x = rand() % 10;
int y = rand() % 10;
if( board[x][y].getMine() == true ){
count--;
continue;
}
else
board[x][y].setMine(true);
board[x][y].setLabel( 'M' );
}
}
//Determines the amount of mines adjecent to a cell: BUGGY
void findMineValues(){
for( int i = 0; i < 10; i++ ){
for( int j = 0; j < 10; j++ ){
int numMines = 0;
if( board[i][j].getMine() == false && i != 0 && i != 9 && j != 0 && j != 9 ){
if (board[i-1][j-1].getMine() == true )
numMines++;
if (board[i-1][j].getMine() == true )
numMines++;
if (board[i-1][j+1].getMine() == true )
numMines++;
if (board[i][j-1].getMine() == true )
numMines++;
if (board[i][j+1].getMine() == true )
numMines++;
if (board[i+1][j-1].getMine() == true )
numMines++;
if (board[i+1][j].getMine() == true )
numMines++;
if (board[i+1][j+1].getMine() == true )
numMines++;
//board[i][j].setLabel(numMines);
}
}
}
}
void playGame(){
findMineValues();
displayBoard();
}
int main( int argc, char* argv[] ){
srand((unsigned)time(0));
if( argc > 1 ){
plantMines( atoi(argv[1]) );
playGame();
}
else
cout << "\nEnter a number of mines (0-99) in the command line please" << endl;
return 0;
}