CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2016
    Posts
    2

    Calculate surrounding mines Using 2D vector

    I am creating a minesweeper game as a class project.

    Code:
    #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)

  2. #2
    Join Date
    Aug 2006
    Posts
    231

    Re: Calculate surrounding mines Using 2D vector

    Is it the coding or the problem-solving that you find difficult?

    So, what you need to do: In the function calculateSurrounding, add code to count how many mines there are in the adjacent cells, then store that number somewhere.

    The adjacent cells are always plus-minus 1 row and/or column away from the given row and col input parameters. There can be between 3 and 8 adjacent cells, so you need to detect if the given cell is close to the edges of the board. Just check against the boundaries (the size of mineField is already known).

    Sum the adjacent vector elements that are equal to 1. If you plan on storing that sum in the same vector, keep in mind that the sum can be 1. Putting that in the vector would indicate another mine and screw up your algorithm. Instead you can use some trick, like storing all sums as negative values.

    Does that make it more clear for you?
    Last edited by TubularX; April 29th, 2016 at 02:48 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured