A map of the tunnels will be stored in a simple text file. The map is used by the application to determine if the
explorer can move in a certain direction. The map could look like this:
---#--###----
-#---#----##-
####-#-#-#-##
---#---#-#---
-#-####---##-
-#------#----
-############
------------@
Here, the – characters indicate that you are free to move in this direction. The # character indicates that you cannot
move any further in this direction and you should go somewhere else. The @ character indicates the location of the
treasure. In this case, it is in the bottom right corner, but it could be anywhere in the map. In your application, you
will always begin searching in the top left corner of the map. So when you run your code, you might print something
like the following to the screen:
I found the treasure :-)
+++#--###--#-
!#+++#+++-##-
####+#+#+#!##
+++#+++#+#!!!
+#+####++!##!
+#++++++#!!!!
+############
++++++++++++@
In this case, we were successful. You will also see that the map has been updated to indicate how the walk was done. The + characters indicate the path that led to the treasure. The ! characters indicate the tunnels that were tried but did not to lead to a viable path. Note, for example, that after starting in the top left corner, the explorer tried to go down but that path was a dead end. So a ! was used to mark a bad path and then the explorer went to the right, which eventually lead to the treasure.
Let’s modify the input file and try another treasure hunt.
---#--###--#@
-#---#----##-
####-#-#-#-##
---#---#-#---
-#-####---##-
-#------#----
-############
-------------
Uh oh, I could not find the treasure :-(
!!!#!!###!!#@
!#!!!#!!!!##-
####!#!#!#!##
!!!#!!!#!#!!!
!#!####!!!##!
!#!!!!!!#!!!!
!############
!!!!!!!!!!!!!
In this case, there was no way to get to the treasure. The ! characters indicate that we tried almost every possible pathway but eventually had to give up (there is one – location that we couldn’t reach). Please help, anyone!
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Maze
{
private static char[][] maze;
private static int startrow, startcol, finishrow, finishcol;
private static ArrayList<String> mazeBuffer;
public static void initializeMaze(String fileName)
{
System.out.println("This is my challenge ");
}
public static void printMaze(char[][] maze)
{
for (char[] row: maze)
{
for (char c: row)
System.out.print(c);
System.out.println();
}
System.out.println();
}
public static void main (String[] args)
{
char[][] maze = {
{'S','-','-','#','-','-','#','#','#','-','-','-','-'},
{'-','#','-','-','-','#','-','-','-','-','#','#','-'},
{'#','#','#','#','-','#','-','#','-','#','-','#','#'},
{'-','-','-','#','-','-','-','#','-','#','-','-','-'},
{'-','#','-','#','#','#','#','-','-','-','#','#','-'},
{'-','#','-','-','-','-','-','-','#','-','-','-','-'},
{'-','#','#','#','#','#','#','#','#','#','#','#','#'},
{'-','-','-','-','-','-','-','-','-','-','-','-','@'}
};
startrow = startcol = 0;
finishrow = finishcol = -1;
initializeMaze("simplemaze.dat");
printMaze(maze);
if (solveMaze(startrow, startcol, maze)) {
System.out.println("Woo hoo, I found a solution ");
System.out.println(" ");
printMaze(maze);
}
else {
System.out.println("Unsolvable.");
printMaze(maze);
}
}
public static boolean solveMaze(int r, int c, char[][] maze) {
//check for boundaries
if(r < 0 || c < 0 || r >= maze.length || c >= maze[0].length)
return false;
//base case, did i reach the finish?
if(maze[r][c] == '@')
return true;
//check if I'm at a valid point
if(maze[r][c] != '-' && maze[r][c] != 'S')
//I have hit a wall, not a valid solution
return false;
//must be on a path, may be the right path
//leave a bread crumb
maze[r][c] = '!';
//check above
if(solveMaze(r-1,c, maze)) {
maze[r][c] = '+';
return true;
}
//check below
if(solveMaze(r+1,c, maze)) {
maze[r][c] = '+';
return true;
}
//check left
if(solveMaze(r,c-1, maze)) {
maze[r][c] = '+';
return true;
}
//check right
if(solveMaze(r,c+1, maze)) {
maze[r][c] = '+';
return true;
}
//if I reach this point...
return false;
}
}
--
You received this message because you are subscribed to the Google Groups "4Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to 4clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/4clojure/4a64a4f4-67f0-4477-bf12-b45a4d666ef4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to 4clo...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to 4clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/4clojure/f5c7d86d-e488-49b4-8107-71da57447b98%40googlegroups.com.