I am developing a program that creates random mazes. One part of the program involves keeping track of the correct path. Each point on the path is a (row, column) coordinate. I am trying to store each coordinate as a 2 element integer array and push it back onto a vector of all the coordinates in the correct path, but I'm not having much success. Here's the code, stripped down to get right to the error, which is in line 58.

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;


class Maze
{
private:
    char wallChar;
    char pathChar;
    char spaceChar;
    char startChar;
    char finishChar;
    char curPosChar;
    int numRows;
    int numCols;
    int curPosition[2];
    int startPosition[2];
    int finishPosition[2];
    char** mazeGrid;
    vector <int[2]> correctPath;
    int facingDirection;


public:
    Maze (int numrows, int numcols, char wallchar, char pathchar, char spacechar, char startchar, char finishchar, char curposchar);
    ~Maze ();
    void Maze::DisplayMaze (bool showSolution, bool showPosition, bool showDirection);
    void DisplayMaze (bool showSolution);
    void PositionAtStart ();
    void PositionAtFinish ();
    void setPosition ();
    bool MazeSolved ();
    bool Move (int newX, int newY);
    bool Move (int direction);
    bool Move ();
    bool Turn (bool turnRight);
    void GetMazePath ();
};


int main ()
{
    srand (time(NULL));
    Maze* maze = new Maze (21, 21, '#', '@', ' ', 'S', 'F', '?');
    return 0;
}


Maze::Maze (int numrows, int numcols, char wallchar, char pathchar, char spacechar, char startchar, char finishchar, char curposchar)
{
     int point[2];
     point[0] = 1;
     point[1] = 3;
     
     correctPath.push_back (point);
}


Maze::~Maze ()
{
}

So the questions are:

1) What am I doing wrong?
2) Is it possible to store the coordinates this way (a vector of 2-element arrays) and if so, what do I need to change?
3) If not, do I need to create a Coordinate class that has the 2 element array as the lone data member and have a vector of Coordinate? Or should I use http://www.cplusplus.com/reference/std/utility/pair.html ?

Thank you.

Recommended Answers

All 3 Replies

Well arrays cannot be copied as such.

Consider using something like

struct point {
  int x;
  int y;
};

which is more meaningful than an array of 2 points, and would have proper copy semantics for the vector.

1> i dont know if this will work(i mean the vector thing, not struct way). but assuming it does,
one issue i see with using a vector is if you have to verify that a particular point is on the right path or not. Suppose you have a maze and someone is trying to go through it and you need to verify that he's on the right path how do you do that? do you fetch each element which is an array and then compare each element of that array with the present co-ordinates? That would be pretty slow.
3> May be we can create a string of x-y coordinates.
row = 2, col =3
create a string "0203" and store strings in vectors, comparison might also be easier.
4> All these are as per my understanding of the problem ;)

Well arrays cannot be copied as such.

Consider using something like

struct point {
  int x;
  int y;
};

which is more meaningful than an array of 2 points, and would have proper copy semantics for the vector.

Good idea. That's what I'll do. Thank you.

1> i dont know if this will work(i mean the vector thing, not struct way). but assuming it does,
one issue i see with using a vector is if you have to verify that a particular point is on the right path or not. Suppose you have a maze and someone is trying to go through it and you need to verify that he's on the right path how do you do that? do you fetch each element which is an array and then compare each element of that array with the present co-ordinates? That would be pretty slow.
3> May be we can create a string of x-y coordinates.
row = 2, col =3
create a string "0203" and store strings in vectors, comparison might also be easier.
4> All these are as per my understanding of the problem ;)

I have a 2-dimensional character array called mazeGrid in addition to the vector. It has different characters for walls, spaces where you can walk, but which are not on the correct path, and spaces where you can walk which are on the correct path, so given any coordinate pair (say (3,5)), you can immediately tell whether it is a wall, part of the path, or part of a dead end by simply checking mazeGrid[3][5] . The vector is intended to be a redundant storage of the correct path coordinates so that you don't have to do a brute force scanning of the 2-D array mazeGrid to find the path if you didn't know it already (by "you", I mean the programmer, not the player). The game may have a feature where you can click a button to display the actual route though depending on the level the player chooses.

Thanks for the input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.