C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 27th, 2016, 11:23 AM
anviori anviori is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2016
Posts: 1 anviori User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 35 sec
Reputation Power: 0
General - 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)

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - Calculate surrounding mines Using 2D vector


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap