Solved

HELP!!! With 2D vector read from File

Posted on 2004-04-28
7
321 Views
Last Modified: 2013-12-14
Alright, I have another question for the experts out there.

I have this file that I'm reading into a 2D vector. it looks like this:

6 5
0 1 0 0 1 0
0 0 1 0 0 0
0 1 0 0 1 0
0 0 0 1 0 0
0 0 0 1 1 0

Where the 6 is Coloumns and the 5 is Rows.

I can read in the numbers as rows and colomns.  and the matrix too.

but I need all the zero's and 1's to become 'X'....(in other words, I'm trying to make a minesweeper game)

Here's my code:
My question is:  How would I go about doing that? Like where in the code?

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

using namespace std;

int main()
{
      ifstream in;
      string filename;
      vector<vector<int> > map;
      int numCols, numRows;

      cout<<"Input file name: ";
      cin>>filename;

      in.open( filename.c_str(), ios::in);

      if (in.is_open())
      {
            cout<<"A minesweeper game"<<endl<<endl;
            
            in >> numCols;
            in >> numRows;

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

                  for(int c = 0; c < numCols; c++)
                  {
                        if((c = 0) || (r = 0)){
                              c = 'X';
                              r = 'X';}
                        int temp;
                        in >> temp;
                        map[r].push_back(temp);
                  }
            }

            for(int r = 0; r < numRows; r++)
            {
                  for(int c = 0; c < numCols; c++)
                  {
                        
                        cout << map[r][c];

                  }
                  cout << endl;
            }
      }
      else
      {
            cerr<<"\n   **** Error opening File ****"<<"\n\n  Have a nice day!\n\n";
      }
      in.close();


}
0
Comment
Question by:vanhowen
7 Comments
 

Author Comment

by:vanhowen
ID: 10944383
Wait, here's the revised version...Sorry!

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

using namespace std;

#include "proj11mineSweeper.h"

int main()
{
      ifstream in;
      string filename;
      vector<vector<int> > map;
      int numCols, numRows;

      cout<<"Input file name: ";
      cin>>filename;

      in.open( filename.c_str(), ios::in);

      if (in.is_open())
      {
            cout<<"A minesweeper game"<<endl<<endl;
            
            in >> numCols;
            in >> numRows;

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

                  for(int c = 0; c < numCols; c++)
                  {
                        int temp;
                        in >> temp;
                        map[r].push_back(temp);
                  }
            }

            for(int r = 0; r < numRows; r++)
            {
                  for(int c = 0; c < numCols; c++)
                  {
                        
                        cout << map[r][c];

                  }
                  cout << endl;
            }
      }
      else
      {
            cerr<<"\n   **** Error opening File ****"<<"\n\n  Have a nice day!\n\n";
      }
      in.close();


}
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 168 total points
ID: 10944739
  for(int r = 0; r < numRows; r++)
         {
              for(int c = 0; c < numCols; c++)
              {
                   
                   cout << map[r][c];// actual 0 or 1
                   cout << 'X'

              }
              cout << endl;
         }

You're actually better off doing this with 2 vectors, one that just holds the values of the maps, IE: 0,1

and the actual game

IE:

x x 2 0 ? x

x ? ? 0 1 x

x x x x x x


where ? is a marked mine, the #'s represent the numbers, and x is not touched yet.

when they "click" on the game, go and check your answer vector to see if they didn't mess up.  Then modify game array.

(Better to make both vector's in this case global.)
0
 
LVL 5

Assisted Solution

by:rendaduiyan
rendaduiyan earned 166 total points
ID: 10945053
after you get data into a vector, you need to decide a policy for display.
char display(const int val)
{
swtich(val)
{

case 0:
case 1:return 'X';
}
}

and your donnot need a copy, you can get the value from the vector whenever you need to know
int getVal(const int row, const int col)
{
 //get row
// get col
return ..
}
0
 
LVL 4

Assisted Solution

by:PerryDK
PerryDK earned 166 total points
ID: 10946099
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now