samedi 2 juillet 2016

Recursive Maze solving, but returning a string of path taken

I'm not overly concerned about returning the string path right now, as for the time being I'm just trying to get the maze solving to work.

this is the maze:

string maze[21] = {
    "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+",
    "S   |   |   |   |     |   | |     |     |",
    "+-+ +-+ +-+ +-+ + +-+-+ + + + +-+ + +-+ +",
    "|   |         |       | |   |   | | |   |",
    "+ +-+ +-+-+-+ +-+-+-+ + + +-+-+ + + + + +",
    "|   |   |   |   |     | |   |   |   | | |",
    "+ + +-+ + + +-+ +-+ +-+-+-+ + + +-+-+ + +",
    "| |   | | |   |   |       | | | |   | | |",
    "+ +-+ + + +-+ +-+ + +-+-+ + +-+ + + + + +",
    "|   |   | | |     | |   |   |   | | | | |",
    "+-+ +-+-+ + +-+-+-+ + + +-+-+ +-+ + + + +",
    "|   |   | |   |     | | |     | | |   | |",
    "+ +-+ + + + + + + +-+-+ + +-+-+ + +-+-+-+",
    "|     | |   |   | |     |   |   | |     |",
    "+-+-+-+ +-+-+-+-+-+ + + +-+ + +-+ +-+-+ +",
    "|       |   |   |   | |     |   |     | |",
    "+ +-+-+-+ + + + + + + +-+-+-+ + +-+-+ + +",
    "| |       |   | | | | |       | |     | |",
    "+ + +-+-+ + +-+-+ + + +-+-+ +-+ + +-+-+ +",
    "|   |     |       | |       |   |       X",
    "+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+"
};

After countless digging around online, this is what I've come up with:

string solveMaze(int row, int col, string maze[], int mazeRows) {
if((row < 0) or (row > (mazeRows - 1)) or (col < 0) or (col > (maze[row].size()-1))) { return "ERROR: Point is outside of maze"; }

if(maze[row][col] == 'X')                                                                                       //check for win
{
    return "WIN!";
}

if(maze[row][col] == '+' or maze[row][col] == '-' or maze[row][col] == '|')   
{
    return "INVALID POSITION";
}

maze[row][col] = '*';

if((solveMaze(row-1, col, maze, mazeRows) == " ") || (solveMaze(row-1, col, maze, mazeRows) == "X"))            //check north
{
    return "WIN1";
}

if((solveMaze(row, col+1, maze, mazeRows) == " ") || (solveMaze(row, col+1, maze, mazeRows) == "X"))            //check east
{
    return "WIN2";
}

if((solveMaze(row+1, col, maze, mazeRows) == " ") || (solveMaze(row+1, col, maze, mazeRows) == "X"))            //check south
{
    return "WIN3";
}       

if((solveMaze(row, col-1, maze, mazeRows) == " ") || (solveMaze(row, col-1, maze, mazeRows) == "X"))            //check west
{
    return "WIN4";
}

return "FAIL";

}

maze is just the maze array, mazeRows is the number of rows in the array. Just at a point where I absolutely cannot figure out where I am going wrong.

Aucun commentaire:

Enregistrer un commentaire