Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

How do you use 2D vectors in functions???

I'm writing a class right now and I have a little problem.

How do you use two dimensional vectors in functions?

I have this function named "mark" which takes in a char and returns a 2D vector.
How do you write it in a header file???

My guess is:

vector mark(char);

or

vector<vector> mark(char);

or

vector<vector<int> > mark(char);

HELP!!
0
vanhowen
Asked:
vanhowen
2 Solutions
 
Kyle AbrahamsSenior .Net DeveloperCommented:
try

vector<int> *

or, create the 2D vector before hand, and pass it down as a reference

IE:

void mark (char, vector< vector<int> > &);
0
 
jkrCommented:
As I showed you in http://www.experts-exchange.com:8080/Programming/Programming_Languages/Cplusplus/Q_20970981.html and ged325 clarifieded: Use references - like with the function

void ReadValues ( string& s, vector<int>& v) {

    // ...
}

I used in the other Q, you should pass the vector as a parameter to avoid overhead. But, since this is the C++ area and therefor an OO thing: Why don't you create a class to handle that, you would not even have to pass the vector of vectors:

class My2DVector {

public:

// the usual stuff...

void mark(char) {

    //...

    // you can use m_data directly here without having to pass it
}

protected:

vector<vector<int> > m_data;

};
0
 
PerryDKCommented:
I think you will find my previous post in other thread enough to answer both questions.  Here is the entire post again so that you can get a feal for an entire project

Your lucky I'm interested in games...here is what I have done so far.  You will have to add code to check to see if the game is complete and also display output when the user selcted a bomb.

When you get it finished I'd like to see the final code.  If I get more time I may finish writing the program.
BTW what compiler are you using...do you want to make this a graphical game?


PARTIAL OUTPUT
MineSweeperGrid
5 6
1 0 1 0 1 0
0 0 0 0 1 0
0 0 0 1 0 0
0 1 0 0 0 0
1 0 0 0 0 0

DisplayGrid:
UUUUUU
UUUUUU
UUUUUU
UUUUUU
UUUUUU
Enter a grid a point to select: 4 4
DisplayGrid:
UUUUUU
UUUUUU
UUUU21
UU111
UU1
Enter a grid a point to select: 1 1
DisplayGrid:
U1UUUU
1 11UU
   U21
 U111
U21
Enter a grid a point to select: 4 4

PROGRAM

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <assert>

using namespace std;

class MineSweeper
{
public:
  typedef vector< vector<int> > MineSweeperGrid;
  typedef vector< vector<char> > MineSweeperDisplay;

  static const char UNKNOWN   = 'U';
  static const char CLEAR     = ' ';
  static const char BOMB      = 'X';

  MineSweeper(const int numRows = 5, const int numCols = 6, const int numMines = 7);
  ~MineSweeper();

  void clear();
  void randomInitialize();
  int getNumRows();
  int getNumCols();
  bool getBombSelected();

  void displayToUser(ostream& os = cout);
  void select(const int row, const int col);

  friend ostream& operator << (ostream& os, const MineSweeper& ms);
  friend istream& operator >> (istream& is, MineSweeper& ms);
protected:
private:
  void resizeGrid();
  void checkSelectedPoint(const int row, const int col);
  int bombsAroundPoint(const int row, const int col);

  int numRows;
  int numCols;
  int numMines;
  bool bombSelected;

  int currentlySelectedRow;
  int currentlySelectedCol;

  MineSweeperGrid grid;
  MineSweeperDisplay displayGrid;
};

MineSweeper::MineSweeper(const int numRows, const int numCols, const int numMines)
{
  assert(numRows > 0);
  assert(numCols > 0);
  assert(numMines >= 0);

  this->numRows = numRows;
  this->numCols = numCols;
  this->numMines = numMines;

  randomInitialize();
}

MineSweeper::~MineSweeper()
{
}

void MineSweeper::clear()
{
  grid.clear();
  resizeGrid();
  bombSelected = false;
}

void MineSweeper::randomInitialize()
{
  assert(numMines <= numRows * numCols);
  clear();

  int currentMines = 0;
  while(currentMines < numMines)
  {
    int r = rand() % numRows;
    int c = rand() % numCols;
    if(grid[r][c] == 0)
    {
      grid[r][c] = 1;
      currentMines++;
    }
  }
}

int MineSweeper::getNumRows()
{
  return numRows;
}

int MineSweeper::getNumCols()
{
  return numCols;
}

bool MineSweeper::getBombSelected()
{
  return bombSelected;
}

void MineSweeper::displayToUser(ostream& os)
{
  for(int r = 0; r < numRows; r++)
  {
    for(int c = 0; c < numCols; c++)
      os << displayGrid[r][c];

    cout << endl;
  }
}

void MineSweeper::select(const int row, const int col)
{
  assert(row >= 0 && row < numRows);
  assert(col >= 0 && col < numCols);

  if(grid[row][col] == 1)
  {
    for(int r = 0; r < numRows; r++)
      for(int c = 0; c < numCols; c++)
        if(grid[r][c] == 1)
          displayGrid[r][c] = BOMB;
        else
          displayGrid[r][c] = CLEAR;

    bombSelected = true;
    return;
  }

  currentlySelectedRow = row;
  currentlySelectedCol = col;
  checkSelectedPoint(row, col);
}

void MineSweeper::checkSelectedPoint(const int row, const int col)
{
  if(currentlySelectedRow == row && currentlySelectedCol == col)
    displayGrid[row][col] = CLEAR;

  bool readyToReturn = true;
  for(int r = row -1; r <= row + 1; r++)
  {
    if(r < 0 || r >= numRows)
      continue;
    for(int c = col -1; c <= col + 1; c++)
    {
      if(c < 0 || c >= numCols)
        continue;

      if(grid[r][c] != 1 && displayGrid[r][c] == UNKNOWN)
        readyToReturn = false;
    }
  }

  if(readyToReturn)
    return;

  if(displayGrid[row][col] != UNKNOWN && displayGrid[row][col] != CLEAR)
    return;



  for(int r = row -1; r <= row + 1; r++)
  {
    if(r < 0 || r >= numRows)
      continue;
    for(int c = col -1; c <= col + 1; c++)
    {
      if(c < 0 || c >= numCols)
        continue;
      if(displayGrid[r][c] == CLEAR)
        continue;
      if(grid[r][c] == 1)
        continue;


      int numBombs = bombsAroundPoint(r, c);
      switch( numBombs )
      {
        case 0:
          displayGrid[r][c] = CLEAR;
          break;
        case 1:
          displayGrid[r][c] = '1';
          break;
        case 2:
          displayGrid[r][c] = '2';
          break;
        case 3:
          displayGrid[r][c] = '3';
          break;
        case 4:
          displayGrid[r][c] = '4';
          break;
        case 5:
          displayGrid[r][c] = '5';
          break;
        case 6:
          displayGrid[r][c] = '6';
          break;
        case 7:
          displayGrid[r][c] = '7';
          break;
        case 8:
          displayGrid[r][c] = '8';
          break;
        default:
          displayGrid[r][c] = '?';
      }

      checkSelectedPoint(r, c);
    }
  }
}

int MineSweeper::bombsAroundPoint(const int row, const int col)
{
  int result = 0;
  for(int r = row -1; r < row + 1; r++)
  {
    if(r < 0 || r >= numRows)
      continue;
    for(int c = col -1; c < col + 1; c++)
    {
      if(c < 0 || c >= numCols)
        continue;

      if(grid[r][c] == 1)
        result++;
    }
  }

  return result;
}

ostream& operator << (ostream& out, const MineSweeper& ms)
{
  out << ms.numRows << " " << ms.numCols << endl;
  for(int r = 0; r < ms.numRows; r++)
  {
    for(int c = 0; c < ms.numCols; c++)
      out << ms.grid[r][c] << ' ';
    out << endl;
  }

  return out;
}

istream& operator >> (istream& in, MineSweeper& ms)
{
  in >> ms.numRows;
  in >> ms.numCols;

  ms.resizeGrid();

  ms.numMines = 0;
  for(int r = 0; r < ms.numRows; r++)
    for(int c = 0; c < ms.numCols; c++)
    {
      in >> ms.grid[r][c];
      if(ms.grid[r][c] == 1)
        ms.numMines++;
    }
  return in;
}

void MineSweeper::resizeGrid()
{
  assert(numRows > 0);
  assert(numCols > 0);
  assert(numMines >= 0);

  grid.clear();
  displayGrid.clear();

  for(int r = 0; r < numRows; r++)
  {
    vector<int> tmpVector;
    grid.push_back(tmpVector);

    vector<char> tmpCharVector;
    displayGrid.push_back(tmpCharVector);

    for(int c = 0; c < numCols; c++)
    {
      grid[r].push_back(0);
      displayGrid[r].push_back(UNKNOWN);
    }
  }
}

int main(int argc, char* argv[])
{
  MineSweeper ms(5, 6, 7);
  cout << "MineSweeperGrid " << endl
       << ms << endl;

  while(!ms.getBombSelected())
  {
    cout << "DisplayGrid: " << endl;
    ms.displayToUser();

    cout << "Enter a grid a point to select: ";
    int row, col;
    cin >> row;
    cin >> col;

    ms.select(row, col);
  }

}

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now