(HW) Using array for a maze to get an object through to winning area

So I am trying to complete an assignment where we must move an object through a maze of 0's and 1's where the 0 is open to movement, 1's are blocked blocks, and 2 is the goal.

I do not know how to go from here to call the values in my maze and move the object through the maze. Basically I want to learn how to use the elements in my array to allow my object to move from the bottom left corner to the top right corner all the while printing out that it has moved to this location and once it hits the 2 it gives the response "Congratulations!"

I unfortunately have no idea how to access my values and use them to guide the object. (Which is another problem I have no clue how to do)

If someone could help me understand arrays, how to access them, and how to move an object through the 2D maze that would be great!


1
2
3
4
5
6
7
8
9
10
11
12
    int solveMaze(int[4][4]){

    int maze[4][4] =
    {
    { 1, 1, 0, 2},
    { 0, 0, 0, 1},
    { 0, 1, 0, 1},
    { 0, 0, 0, 1}
    };


}
This is a classic recursion problem. From the starting point, mark the current position as "visited" (3). Then adjust your position to one of the four possible directions. Be sure to check that the new position is not out of bounds. If the new position is a 2, you're done. If the new position is a 1 or a 3, you can't move there, so go to the next of the 4 directions. If the new position is a 0, you can move there. Call solveMaze recursively passing a copy of the current maze. Let solveMaze explore from the new position. If solveMaze finds the exit, return a 1. If solveMaze can't move in any direction, return a 0.

BTW, line 1 is missing a argument name for the array. Lines 3-9 should be inside main, not inside your solveMaze function
So I do not know what recursion is, and what would the code actually look like. An example of a recursion piece would be really helpful. I fixed that other mistake you stated, and have a for loop that now lets me see the table so I now know the function is being called and more importantly the maze of numbers are there, but how do I create a function for moving the object through to the 2.

This is where I am stuck and am super beginning in programming.
1
2
3
4
int x = posX;
int y = posY;
while (x and y != maze [0][3]) //I don't even think this is correct, but what would I do.
         

I know if I do a while statement that it would have to check the position until x and y both equaled the position of 2 within the maze array, but how do I do that? I feel like there should be other variables used to take the position, but I am so new to programming that I don't even know how to track and find position let alone move through the 2d space. I'm lost.

Edit: I think while loop is pointless and probably should go with a for loop correct?
Last edited on
Topic archived. No new replies allowed.