Solved

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

Posted on 2004-04-28
6
190 Views
Last Modified: 2013-12-14
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
Comment
Question by:vanhowen
6 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 10944716
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
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 10944908
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
 
LVL 4

Expert Comment

by:PerryDK
ID: 10946106
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question