I keep getting an index out of bounds error and dont know why

74 views Asked by At
    public bool Solve_Maze(int X_Pos, int Y_Pos)
    {
        bool Move = false;

        //Checking if the position isnt on the finish
        if (maze_board[X_Pos,Y_Pos] == 'e')
        {
            Move = true;
            maze_board[X_Pos,Y_Pos] = '+';
            return Move;
        }

        // Check for a wall
        if (maze_board[X_Pos, Y_Pos] == '1')
            return false;

        if (maze_board[X_Pos, Y_Pos] == 'X')
            return false;

        maze_board[X_Pos, Y_Pos] = 'X';

        Move = Solve_Maze(X_Pos + 1, Y_Pos);
        Move = Solve_Maze(X_Pos, Y_Pos + 1);
        Move = Solve_Maze(X_Pos - 1, Y_Pos);
        Move = Solve_Maze(X_Pos, Y_Pos - 1);

        maze_board[X_Pos, Y_Pos] = '+';

        return Move;

    }

This is the piece of code that is giving me the error

    if (maze_board[X_Pos,Y_Pos] == 'e')

I am a complete newbie when it comes to programming and was kind of thrown into the deep end with a university assignment so any help would be much appreciated

1

There are 1 answers

7
otto-null On

In order to get that error, you must have already declared maze_board[] as a variable outside of that method. Instead, pass the maze_board in to this method as a parameter so that you are sure it is initialized and holding the current maze. This is a guess at this point because I can't see the original declaration of the maze_board variable. Can you post the entire code file?