Maze game movement

I'm working on some kind of maze game, I'm not an expert at C++ programming, but with my basic knowledge, I'm able to get something done.

What happens in this code is that you can move around inside a square.
The square has boundaries and you cannot move onto it or move past it.
Moving left and moving up works perfectly fine, but moving down and right, it isn't working as it should.
It just moves all the way to the end of their respective sides, so if I'm in the middle and I move right, it goes all the way to the end instead of moving just once, same goes for down.

Does anyone know what the problem is?

Here is what I have:
Last edited on
Because you're cycling through all x and y.

You are on say....

0,2 and you want to move right.

The loop cycles through x and y positions until it gets to that position and moves you to 0,3.

Then loop then continues and moves to 0,3 and sees that you're there now and moves you again, etc... All the way to the end.

Store the current position to avoid needing to run through the whole map to find the 6.

either that or put a break statement at the end of each map part of the if statements.



The above might be slightly unclear so look at it like this.

You're on [0][2] and want to move to [0][3] so you press r.

The loop cycles like this...

x = 0.

y = 0, 1, 2 <- See's you're there so shifts you to y+1 which is 3.

Your new position is [0][3] and the loop is at x=0, y=2.

The loop increments again and is now at x=0, y=3.

It sees that you are on [0][3] so shifts you to [0][4] and so on until it has shifted you all the way to the far right.
Last edited on
Ahh, I can see you mean now. I might need more time to think through this.

Anyhow, I'm just quickly do one of your suggestions.
I did the second part of your suggestion by placing the break; statement.
It seems the right direction works now, but the down direction does not.

Oh yeah that's right the break will only exit the y loop not the x one too.

Best suggestion is just to store the position of the 6 somewhere, perhaps a struct/class. That avoids the need to scan through the whole map.

If you're set on using a double loop though, consider using the goto command but... Much better idea to store positions.
Last edited on
Topic archived. No new replies allowed.