1 Project Description
Given the starting point in a 10 row 20 column maze, your program is to discover and report
if there is a path out of the maze. A maze consists of only hedges, footpaths, and a single exit.
The program asks the user for the starting position in the maze (you may start at any point on a
footpath except the exit, and the upper left corner is considered row 1, column 1) and then reports
if they are free (i.e. you have found a way out) or trapped.
You may move vertically or horizontally (not diagonally!) in any direction in the maze as long as
you are on a footpath or at the exit; you may not move into (onto) a hedge. If you move into the
exit square, you have successfully exited the maze. If you have tried all of the possible paths out
of the maze without success, you are trapped. You must use a recursive routine to search for the
exit. If you are in a square and no progress is possible (i.e. the squares surrounding you either are
blocked with hedges and/or you have already tried those paths), you must `go back the way you
came' and try another path (this will be handled `automatically' by the recursive algorithm). The
path you nd out of the maze, if one exists, need not be the shortest path.
Input is a 10 row by 20 column array of characters (containing only 1s, 0s, a single E, with no
spaces) from an text data le specied on the command line; you may assume the maze data le
is correct. Each row in the data le corresponds to a row in your maze. Hedges are indicated by
the character 1, paths by the character 0, and the exit is marked by an upper case E. The starting
point (i.e. a row, column pair) in the maze will be input from the keyboard; you should make sure
it is a valid position (see the sample run).
You should format your output exactly as the sample run shown below; also use exactly the same
labels, spacing, data prompts, and error messages. Echo print the (initial) maze prior to asking the
user for their starting point. After acquiring the start position, print the complete maze with an S
in the starting point followed by the words `I am free' if you have found a path out of the maze or
the words `Help, I am trapped' if you cannot.
Your maze should be printed using one screen column and screen row per maze column and row
using exactly the same characters as shown in the sample run. You should print a border around
the maze and label all sides with the appropriate row and column number.
4 Sample Run
1 2
12345678901234567890
+--------------------+
1| ### # # |1
2|### ### # # ####|2
3|##### # ### |3
4|E #### # ## # # |4
5| # ## # # #### ## |5
6| # ## #### |6
7|## ### ## ## ### |7
8| #### ## ####### #|8
9| # ## #### ## #|9
10| # ## ## ###|10
+--------------------+
1 2
12345678901234567890
Starting point? row: -3
col: 5
Illegal starting position; try again
Starting point? row: 4
col: 1
Try again - you cannot start at the exit
Starting point? row: 2
col: 12
And here is my code and question
How can I make it so that the user inputs the starting location and then store that into a final char variable which in turn makes that location on the maze into an 'S' symbol
(By all means this code is nowhere complete for I have still doubts about the traverse and valid (checking for legal moves) methods and I am not suppose to have a maze. I know i need to make an empty Array so my teacher and use his maze as test data but for now I did create the sample maze for testing. I know for a fact that my toString is damn near flawless but I could use help on my question)
import java.io.IOException;
import java.util.Scanner;
public class Proj71773 {
public static void main(String[] args) throws IOException {
Maze maze = new Maze();
System.out.println(maze);
Maze.location();
}
}
class Maze {
private final int TRIED = 3;
private final int PATH = 7;
private final char E = 'E';
private char [][] maze = {
{ 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
{ 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 },
{ E, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0 },
{ 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0 },
{ 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0 },
{ 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1 },
{ 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1 },
{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1 } };
// -----------------------------------------------------------------
// Attempts to recursively traverse the maze. Inserts special
// characters indicating locations that have been tried and that
// eventually become part of the solution.
// -----------------------------------------------------------------
public static void location(){
Scanner input = new Scanner(System.in);
int row;
System.out.print ("Starting point? row: ");
row = input.nextInt();
int col;
System.out.print (" col: " );
col = input.nextInt();
}
public boolean traverse (int row, int column) {
boolean done = false;
if (valid (row, column))
{
maze[row][column] = TRIED;
if (row == E && column == E)
done = true;
else
{
done = traverse (row+1, column);
if (!done)
done = traverse (row, column=1);
if (!done)
done = traverse (row-1, column);
if (!done)
done = traverse (row, column-1);
}
if (done)
maze[row][column] = PATH;
}
return done;
}
// -----------------------------------------------------------------
// Determines if a specific location is valid.
// -----------------------------------------------------------------
public boolean valid (int row, int column){
boolean result = false;
if (row >= 0 && row < maze.length && column >= 0 && column < maze[row].length)
if (maze[row][column] == 1)
result = true;
return result;
}
// -----------------------------------------------------------------
// Returns the maze as a string.
// -----------------------------------------------------------------
public String toString() {
StringBuffer titleBuf = new StringBuffer();
StringBuffer tagStr = new StringBuffer();
tagStr.append("\n");
int rowCount = 1;
int colCount = 0;
for (int row = 0; row < maze.length; row++) {
if (row == 0 || row == maze[row].length-1) {
titleBuf.append(titlePadding(maze[row].length));
titleBuf.append(lineDraw(maze[row].length));
}
for (int column = 0; column < maze[row].length; column++) {
// padding row # & pipe symbol in left end
if (column == 0) {
// padding an empty space for single digit number
if ( rowCount < 10) {
tagStr.append(" ");
}
// print row count & pipe symbol
tagStr.append(rowCount);
tagStr.append("|");
}
if (maze[row][column] == E) {
tagStr.append("E");
} else if (maze[row][column] != 0) {
tagStr.append("#");
} else {
tagStr.append(" ");
}
// padding row # & pipe symbol in right end
if (column == maze[row].length - 1) {
tagStr.append("|");
tagStr.append(rowCount);
rowCount++;
}
colCount = maze[row].length;
}
tagStr.append("\n");
}
titleBuf.append(tagStr);
titleBuf.append(lineDraw(colCount));
titleBuf.append("\n");
titleBuf.append(titlePadding(colCount));
return titleBuf.toString();
}
// -----------------------------------------------------------------
// Returns the the number title format of string as stringbuffer.
// It read the number of column, divided by 10.
// If the number is less than 10, print " " string.
// if 10 is reached, print 1. To drop after 10, filter out by modular
// number.
// The second line is mod by 10 to keep 0-9 repeat
// -----------------------------------------------------------------
private StringBuffer titlePadding(int colSize) {
StringBuffer sbuf = new StringBuffer();
sbuf.append(" ");
for (int i=1; i <= colSize; i++) {
// divide by 10
if (i/10 > 0) {
// print only 10 mod number
if(i % 10 == 0 ) {
sbuf.append(i/10);
} else {
sbuf.append(" ");
}
} else {
sbuf.append(" ");
}
}
sbuf.append("\n");
sbuf.append(" ");
for (int i=1; i <= colSize; i++) {
sbuf.append(i % 10);
}
sbuf.append("\n");
return sbuf;
}
// -----------------------------------------------------------------
// Returns the line draw as stringbuffer.
// The first and last symbol is "+", and others "=".
// -----------------------------------------------------------------
private StringBuffer lineDraw(int colSize) {
StringBuffer tbuf = new StringBuffer();
int formatSize = colSize+2;
// space padding
tbuf.append(" ");
for (int i=0; i < formatSize; i++) {
if (i == 0 || i == formatSize-1) {
tbuf.append("+");
} else {
tbuf.append("-");
}
}
return tbuf;
}
}

New Topic/Question
Reply



MultiQuote



|