Homework
boolean searchMaze(char[][] maze, int positionX, int positionY, int[] mazeBounds, boolean[][] mazeSearched){
mazeSearched[positionX][positionY] = true;
if (maze[positionX][positionY] == 'E'){return true;};
if(!(positionX-1 < mazeBounds[0])){ //check up
if (mazeSearched[positionX-1][positionY] == false){
if (! (maze[positionX-1][positionY] == '#')){
if (searchMaze(maze,positionX-1,positionY,mazeBounds,mazeSearched)== true){
return true;
}
}
else{
mazeSearched[positionX-1][positionY] = true;
}
}
}
if(!(positionX+1 > mazeBounds[2])){ //check down
if (mazeSearched[positionX+1][positionY] == false){
if (!(maze[positionX+1][positionY] == '#')){
if (searchMaze(maze,positionX+1,positionY,mazeBounds,mazeSearched) == true){
return true;
}
}
else{
mazeSearched[positionX+1][positionY] = true;
}
}
}
if(!(positionY-1 < mazeBounds[1])){ //check left
if (mazeSearched[positionX][positionY-1] == false){
if (! (maze[positionX][positionY-1] == '#')){
if (searchMaze(maze,positionX,positionY-1,mazeBounds,mazeSearched)== true){
return true;
}
}
else{
mazeSearched[positionX][positionY-1] = true;
}
}
}
if(!(positionY+1 > mazeBounds[3])){ //check right
if (mazeSearched[positionX][positionY+1] == false){
if (!(maze[positionX][positionY+1] == '#')){
if (searchMaze(maze,positionX,positionY+1,mazeBounds,mazeSearched) == true){
return true;
}
}
else{
mazeSearched[positionX][positionY+1] = true;
}
}
}
return false;
}
boolean solveMaze(char[][] maze, int startX, int startY){
int[] mazeBounds = new int[4];
mazeBounds[0] = 0; //top
mazeBounds[1] = 0; //left
mazeBounds[2] = maze.length-1; //bottom
mazeBounds[3] = maze[0].length-1; //right
//create an array saying that all blocks in maze are unsearched (false)
boolean[][] mazeSearched = new boolean[maze.length][];
for(int i = 0; i < mazeSearched.length; i++){
mazeSearched[i] = new boolean[maze[0].length];
for (int j = 0; j < mazeSearched[i].length; j++){
mazeSearched[i][j] = false;
}
}
return searchMaze(maze,startX,startY,mazeBounds,mazeSearched);
}
void main(){
char[][] maze = {
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', ' ', '#', ' ', ' ', ' ', '#', '#', '#', '#', '#'},
{'#', ' ', '#', ' ', '#', ' ', '#', ' ', ' ', ' ', '#'},
{'#', ' ', '#', ' ', '#', ' ', '#', ' ', '#', ' ', '#'},
{'#', ' ', ' ', ' ', '#', ' ', ' ', ' ', '#', ' ', '#'},
{'#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#'},
{'#', ' ', '#', ' ', '#', ' ', ' ', ' ', ' ', '#', '#'},
{'#', ' ', '#', ' ', ' ', ' ', '#', '#', ' ', ' ', '#'},
{'#', ' ', ' ', ' ', '#', '#', '#', ' ', ' ', ' ', '#'},
{'#', ' ', '#', ' ', '#', ' ', ' ', ' ', '#', ' ', '#'},
{'#', ' ', '#', ' ', '#', 'E', '#', '#', '#', ' ', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
};
for (char[] rowArray: maze){ //draw maze to output
String row = "";
for(char c : rowArray){
row += c+""+c;
}
System.out.println(row);
}
///// runs method with expected parameters
boolean mazeSolved = solveMaze(maze,1,1);
System.out.println(mazeSolved);
}
main();
######################
## ## ##########
## ## ## ## ##
## ## ## ## ## ##
## ## ## ##
###### ##############
## ## ## ####
## ## #### ##
## ###### ##
## ## ## ## ##
## ## ##EE###### ##
######################
true