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;
}

Please be more specific about what the problem is. What is this function supposed to do and what does "it completely scrambles up my board" mean?

//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);                                         
                  }              
         }
    }
}

Right now it doesn't do anything as far as I can tell. You have a void function that declares and changes a local variable called numMines , then never does anything with numMines . You have a function call that DOES do something with numMines , but it is commented out.

As a side note, on lines like this:

if (board[i][j+1].getMine() == true )

you can leave == true off and it'll be the same code.

if (board[i][j+1].getMine())

Right now it doesn't do anything as far as I can tell. You have a void function that declares and changes a local variable called numMines , then never does anything with numMines . You have a function call that DOES do something with numMines , but it is commented out.

Its supposed to count the number of mines around a cell and add one to numMines each time it encounters a mine. After it tallies up the surrounding mines (1-8) it changes the label of the cell to reflect that. This is all assuming that the cell is unflagged, unchecked, and does not contain a mine.

I commented out the code because its what messes up the board.

I'll run the program and show you what I mean.

With the line put back in.

-bash-3.2$ ./a.out 10

1 2 3 4 5 6 7 8 9 10
1 0 0 0 0 0 0 0 0 M 0
2 0 0 00 000 0000 00000 000000 0000001 00000011 0
3 0 000000111 0000001111 00000011111 000000111110 0000001111100 00000011111000 000000111110000 0000001111100000 0
4 0 00000011111000001 M 000000111110000011 0000001111100000110 00000011111000001100 000000111110000011000 0000001111100000110000 00000011111000001100000 0
5 0 000000111110000011000002 0000001111100000110000022 00000011111000001100000222 000000111110000011000002221 0000001111100000110000022210 00000011111000001100000222100 000000111110000011000002221001 0000001111100000110000022210011 0
6 M 00000011111000001100000222100111 000000111110000011000002221001112 M 0000001111100000110000022210011122 00000011111000001100000222100111221 000000111110000011000002221001112211 0000001111100000110000022210011122112 M 0
7 0 00000011111000001100000222100111221121 000000111110000011000002221001112211212 M 0000001111100000110000022210011122112122 00000011111000001100000222100111221121221 M 000000111110000011000002221001112211212212 0000001111100000110000022210011122112122121 0
8 0 00000011111000001100000222100111221121221210 000000111110000011000002221001112211212212101 0000001111100000110000022210011122112122121012 00000011111000001100000222100111221121221210122 000000111110000011000002221001112211212212101222 0000001111100000110000022210011122112122121012221 00000011111000001100000222100111221121221210122211 000000111110000011000002221001112211212212101222110 0
9 0 0000001111100000110000022210011122112122121012221102 00000011111000001100000222100111221121221210122211022 000000111110000011000002221001112211212212101222110222 M 0000001111100000110000022210011122112122121012221102221 00000011111000001100000222100111221121221210122211022210 000000111110000011000002221001112211212212101222110222100 0000001111100000110000022210011122112122121012221102221000 0
10 0 M M 0 0 0 0 0 0 0
-bash-3.2$

With the line commented out. ( I have it that all cells are visible for testing purposes).

-bash-3.2$ g++ Minesweeper.cpp Cell.cpp
-bash-3.2$ ./a.out 10

1 2 3 4 5 6 7 8 9 10
1 0 0 0 0 M 0 0 0 0 0
2 0 0 0 0 M 0 0 0 0 0
3 M 0 0 0 0 0 0 0 0 0
4 M 0 0 0 0 0 0 0 0 M
5 0 0 0 0 M 0 M 0 0 0
6 0 0 0 0 M 0 0 0 0 0
7 M 0 0 0 0 0 0 0 0 0
8 0 0 0 M 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 0 0 0 0 0

Basically I need help coming up with a function that can properly read the surrounding cells without the mess up above.

Did you post Cell.h?

I've updated my code since then. I've also gotten a suggestion to determine the number of adjacent mines when planting them in the board.

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <ctime>
#include "Cell.h"

using namespace std;

Cell board[10][10];
string s;
stringstream out;


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++ ){
              
              if( board[i][j].getMine() == false && i != 0 && i != 9 && j != 0 && j != 9  ){
                  int numMines = 0;
                  if (board[i-1][j-1].getLabel() == "M" )
                     numMines++;
                  if (board[i-1][j].getLabel() == "M" )
                     numMines++;
                  if (board[i-1][j+1].getLabel() == "M" )
                     numMines++;                                          
                  if (board[i][j-1].getLabel() == "M" )
                     numMines++;
                  if (board[i][j+1].getLabel() == "M" )
                     numMines++;                                       
                  if (board[i+1][j-1].getLabel() == "M" )
                     numMines++;
                  if (board[i+1][j].getLabel() == "M" )
                     numMines++;
                  if (board[i+1][j+1].getLabel() == "M" )
                     numMines++;
		    out << numMines;                            
		    board[i][j].setLabel(out.str());
		} 
         }
    }
}

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;
}
#include <string>
#include "Cell.h"

using namespace std;


   Cell::Cell(){
      label = "0";
      mine = false;
      flag = false;
      checked = false;
   }
   
   void Cell::setLabel( string adjacentMines ){
      label = adjacentMines;
   }
   
   string 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;
   }
//Prototype for Cell object
#include <string>
using std::string;

class Cell
{
public:

   Cell();
   
   void setLabel( string );
   
   string getLabel();
   
   void setMine( bool );
   
   bool getMine();
   
   void setChecked( bool );
   
   bool getChecked();
   
   void setFlag( bool );
   
   bool getFlag();
   
   

private:
   string label;
   bool mine;
   bool checked;
   bool flag;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.