Pass maze vector to function for reading

Hello, I'm working on a maze assignment and need help with passing my maze vector into my placePlayer() function so I can check where the entrance is and place the player onto the maze. So right now I read in the file and print it on the screen with testVector() but I'm not sure how to essentially move the maze[x][y] vector into my placePlayer() function.

Whenever I try to use maze[x][y]
anywhere outside of the nested for loop, it says error: name lookup of 'x' changed for ISO 'for' scoping.

Once I find the entrance, would I then replace 'E' with player, then how do I save this for use in the movement function. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  void Floor::testVector() {
    ifstream fin("FloorA.txt");
    if (!fin) cout << "failed";

    vector<string> maze;
    string line;
    while (getline(fin, line)) {
    maze.push_back(line);
  }
    for (int x = 0; x < maze.size(); x++)
    {
        for (int y = 0; y < maze[x].size(); y++)
        {
            cout<<maze[x][y]<< " ";
        }
        cout << endl;
        }
        cout << maze[x][y];
}

void Floor::player() {
 //use vector here
}


header:
1
2
3
4
5
6
7
8
9
10
11
class Floor {
	char tiles[20][30];
	public:
	//Floor();
	void printToScreen() const;
	void Read (const char * filename);
	void readTest();
	void testVector();
	vector<string> maze;
	void placePlayer();
};
Well, you don't have x or y variables, so you can't print out maze[x][y]. x and y go out of scope after you leave the loops.
Okay, but how could I use the vector<string> maze in the placePlayer() function? When declaring a default int x, int y in my placePlayer, this doesn't really do anything because I want to find where in maze the 'E' is located so I need to utilize the vector in the function.
Where you output the characters in the maze, check for 'E', and save its location. Use that location for what you need.
Last edited on
Well, I wanted to do it in a separate function since my testVector will be used to print the maze after each iteration but how do I save it's location. Using an if statement I can find 'E' and say that the entrance was found but I'm not sure how to save a location like you mentioned?

Thanks.
You can still do it in another function, you would just do the same thing without the outputs. By save, I really meant store the coordinates in some variables that you can use to tell you where the entrance is when you actually place the player.
So like this is what I'm thinking... and maybe you are suggesting.

1
2
3
4
5
//for (int x=0; x< maze.size(); x++)
...
if (maze[x][y] == 'E'){
                char saveLocation = maze[x][y];
            }


but then what would the call look like in placePlayer()? Also, char is probably not the right type...
Last edited on
Your code is sort of correct, but saveLocation in your code will just have an 'E' in it. If you want to save the location of the 'E', you'll need some sort of reference to x and y. Most people use two variables like entryX and entryY. And that's pretty much how it will look in the placePlayer method. You get the location, leave the loops, and place your player at that location (How you place the player is your choice).
What variable type should the entries be? Also do you mean get the location, leave the loops, and place your player, all this is in placePlayer()? Right now, most of this would be in testVector()? Sorry, I'm still a bit confused on how this would look, because it seems like every time you call testVector(), you would be checking for 'E' which isn't necessary.

So incorporating what I think you are saying, but this wouldn't work obviously:

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
29
30
31
32
void Floor::testVector() {
    ifstream fin("FloorA.txt");
    if (!fin) cout << "failed";

    vector<string> maze;
    string line;
    while (getline(fin, line)) {
    maze.push_back(line);
  }
    for (int x = 0; x < maze.size(); x++)
    {
        for (int y = 0; y < maze[x].size(); y++)
        {
            cout<<maze[x][y]<< " ";
            if (maze[x][y] == 'E'){
                int entryX = maze[x];
                int entryY = maze[y];
            }
        }
        cout << endl;
        }
}

void Floor::placePlayer() {

    //I want to pass entryX and entryY here so I can do the following
    char player = 'P';
    if (entryX && entryY == 'E') {
       //replace 'E' with 'P'
    }
        
}


If you have a code example, that would be hugely helpful. Just in relation to what you are talking about in your post. Thanks.
Sorry I wasn't very clear. This needs to be done in the placePlayer. I was indicating your testVector because placePlayer is going to look a lot the same.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void Floor::testVector() {
	ifstream fin("FloorA.txt");
	if (!fin) cout << "failed";

	vector<string> maze;
	string line;
	while (getline(fin, line)) {
		maze.push_back(line);
	}
	for (int x = 0; x < maze.size(); x++)
	{
		for (int y = 0; y < maze[x].size(); y++)
		{
			cout << maze[x][y] << " ";
		}
		cout << endl;
	}
}

void Floor::placePlayer() {
	ifstream fin("FloorA.txt");
	if (!fin) cout << "failed";

	bool foundEntry = false;	//this will let you escape the embedded for after finding the entrance
	vector<string> maze;
	string line;
	int entryX;
	int entryY;

	while (getline(fin, line)) {
		maze.push_back(line);
	}

	for (int x = 0; x < maze.size() && !foundEntry; x++)
	{
		for (int y = 0; y < maze[x].size() && !foundEntry; y++)
		{
			if (maze[x][y] == 'E'){
				entryX = x;	//store x location of entrance
				entryY = y;	//store y location of entrance
			}
		}
	}

	//place the player in the maze at entryX and entryY
}
Ahhh okay, yeah this is really helpful seeing what you were suggesting.

Then how would I replace the E on the map because 'P' will start at E, then move off of it the next move, but for the first refresh of the screen, the 'P' would be exactly where 'E' is. Does placing the 'P' into the vector require anything extra? Or just maze[x][y] is now == 'P'.

Thanks again!
It depends on how you place your player. You could store the player's location and display 'P' when you print the maze. Or, you could replace every location the player visits with 'P', but you'll have to be more careful about putting stuff back. It's all your choice.
Topic archived. No new replies allowed.