Maze game inputting into 2d vector

I am attempting to create a maze that is imported from a file and then placed into a vector that holds a vector of bools but I am not good yet with vectors. My problem is that I have taken the info from the file but I am unsure of how to then process it into the 2d vector. In the maze any coordinate that has a "+" is a path while anything else (blank space, etc) is a wall. The start and finish locations are Location objects, but i have not coded that yet. Any help would be appreciated, thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
vector<vector<bool> > mazeSpec;
string buffer; //holds lines while they are read in
int length; //holds length of each line/# of columns
bool path; //test if path or not
Location start, finish;

vector<vector<bool> > mazeSpec;
ifstream mazeFile("maze.txt");

if (!mazeFile)
    cout << "Unable to open file";

getline(mazeFile, buffer); // read in first line
cout << buffer << endl; //output first line
length = buffer.length(); //length now set so can be compared

while (!mazeFile.eof()) {   // read in maze til eof
    getline(mazeFile, buffer);
        if (buffer == "*") //set path to true or false
            path = true;
        else
            path = false;
    cout << buffer << endl;
}
Don't loop on EOF.

Each line of the file == each row of your maze.

Each character of the line == each element of the row.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
vector <vector <bool> > mazeSpec;

ifstream mazeFile( ... );
if (!mazeFile)
{
   ... // fooey!
}
else 
{
  string buffer;
  while (getline( mazeFile, buffer ))  // for each line
  {
    vector <bool> row;
    for (auto c : buffer)  // for each character
    {
      row.push_back( c == '*' );
    }
    mazeSpec.push_back( row );

    // If you wanted to make sure that the rows are all the 
    // same length (as row 0), you can do it here:
    if (mazeSpec.back().size() != mazeSpec.front().size())
    {
      ... // fooey!
    }
  }
  mazeFile.close();
}

Hope this helps.
that does help, however I'm not familiar with "auto" since i don't really know the new stuff from c++ 11. what does that mean/how would I implement it not using that?
auto infers the type from the context in which it is used. You can use char instead in this case.

If you're not familiar with C++11 then you'll probably want to circumvent the range-based loop as well.
Topic archived. No new replies allowed.