Hi there. So I'm supposed to make a program that solve a given maze with "m"(mouse) indicated the starting point, "c" (cheese)indicated the exit, "-" = traversable and "w" == wall. I can't think of a way to use my visited() method, i.e preventing the mouse from visiting the same space over and over (all base cases such as hit wall or index out of bound are handled with my traversable()). Any help would be greatly appreciated
Here is the input file (in .txt format)
4 5 ---ww wwm-w c-w-- w---w
Here is the Driver. There is nothing wrong with it (I believe as it just calls the methods in the Maze class)
package a07; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.InputMismatchException; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Tim */ public class A07 { /** * @param args the command line arguments */ public static void main(String[] args) { /* * Getting the file name from user's input... */ Scanner fileName = new Scanner(System.in); System.out.println("Please enter the file name..."); String name = fileName.nextLine(); try { Scanner scanInput = new Scanner(new FileReader(name)); //reading the file in a try-catch while (scanInput.hasNextLine()) { //checking for the file validity try { //reading the first 2 ints in a try-catch. If this block fails, break the loop //and return invalid file int row = scanInput.nextInt(); //scanning for the number of rows scanInput.nextLine(); int column = scanInput.nextInt(); //scanning for the number of columns scanInput.nextLine(); int actualRow = 0; boolean correctCol = true; //setting the boolean of checkCol to true while (scanInput.hasNextLine()) { //checking row if (scanInput.next().length() != column) { //during checking row, check column. correctCol = false;//if ONE column fails, change the correctCol boolean to false } actualRow+=1; if (scanInput.hasNextLine()) { scanInput.nextLine(); } } if (actualRow != row) { //comparing number of row by user AND the number of row we got from checking the input System.out.println("Invalid maze row input!");//if they don't match --> error break; } else if (actualRow == row) {//if they DO match,check if column inputs matches if (correctCol == true) { //if they do, move one to the next step //System.out.println("success! Your maze input is correct !"); Scanner scanMaze = new Scanner(new FileReader(name)); scanMaze.nextLine();//these 2 lines move the cursor scanMaze.nextLine();//past the first 2 ints //to get to the actual maze inputs String[][] maze = new String[row][column]; //assigning the maze to the dimension indicated by //the inputs from the user String[] mazeLines = new String[row];//create a String array of length *row* to store //each lines of the maze input /********************************************* * the 2 lines below store each maze input**** * lines into the array created above.******** *///****************************************** for (int i = 0; i < mazeLines.length; i++) { mazeLines[i] = scanMaze.nextLine(); } /******************************************************* * READ THE COMMENTS OF THIS CODE BLOCK CAREFULLY AND*** * TRY TO UNDERSTAND IT. THIS IS THE KEY BLOCK OF THIS** * METHOD !!! LET ME KNOW IF YOU HAVE ANY QUESTION !!!** *///*************************************************** for (int i = 0; i < mazeLines.length;i++) { //this code block pull each line of the mazeLines*************** for (int j = 0;j < column;j++) {//array, then pick out a character at the index j and convert************ char mazeChar = mazeLines[i].charAt(j);//it to a string character. Then, it will assigne that string* String mazeStr = Character.toString(mazeChar);//value to the matrix maze at position maze[i][j]****** maze[i][j] = mazeStr; } } //ASSIGNING THE MATRIX CREATED WITH THE INPUTS TO THE //MAZE.********************************************** Maze myMaze = new Maze(maze); myMaze.maze = maze; myMaze.solver(); System.out.println(myMaze.getMouse()); } else {//if they don't, print out error message System.out.println("Your actual maze input does not" + " match your indicated dimension!"); } } break; } catch(InputMismatchException e) { //catch mismatch input, returning invalidFile() System.out.println("Wrong input!"); break; } } } catch (FileNotFoundException ex) { System.out.println("Invalid file input !"); } } }
Here is the Maze class. This is where all my methods take place. This is the one causing the problems.
package a07; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Scanner; /** * * @author Tim */ public class Maze { protected String[][] maze; protected ArrayList<String> solutions; public Maze(String[][] maze) { this.maze = maze; } public Point getPos(String a) { //getting the coordinates of any given string for (int i = 0; i < this.maze.length; i++) {//in the maze. i is the number for (int j = 0; j < this.maze[0].length; j++) {//of row, j is the number if ((this.maze[i][j]).equals(a)) { //of column Point pos = new Point(i,j); return pos; } } } return null; } public Point getMouse() { //returning the coordinates of the mouse. return getPos("m"); } public String invalidFile() { //returning error when fail in reading the input return "No maze found !"; } //the traversable method checks whether the mouse can go to a point. //For a point (x,y), if either x or y is < 0, or if x > the number of row //of y > than the number of column, the Point is not traversable. public boolean traversable(int row, int col) { if (row < 0 || col < 0 || row > this.maze.length - 1 || col > this.maze[0].length - 1 || this.maze[row][col].equals("w")) { return false; } else { return true; } } //This method checks for whether //the mouse moved to a specific //slot already or not. public boolean visited(int row, int col) { while (row > 0 && row <= this.maze.length && col > 0 && col <= this.maze[0].length) { if (this.maze[row][col].equals("x")) { return true; } } return false; } /*MOUSE************* *TRAVERSE********** *///METHODS******** //before a move is made, each method checks for whether the mouse can move //there or not. If it can, it will change the coordinate of the new position //to "m" (mouse), and set the mouse's previous position to "x". public void moveDown(){ Point mouse = this.getMouse(); int oldRow = mouse.getRow(); int oldCol = mouse.getCol(); int newRow = oldRow + 1; int newCol = oldCol; System.out.println(this.traversable(newRow, newCol)); if (this.traversable(newRow, newCol)) { this.maze[newRow][newCol] = "m"; this.maze[oldRow][oldCol] = "x"; System.out.println("down"); System.out.println("mouse at: " + this.getMouse()); } else if (this.traversable(newRow, newCol) == false) { this.moveLeft(); } } public void moveUp() { Point mouse = this.getMouse(); int oldRow = mouse.getRow(); int oldCol = mouse.getCol(); int newRow = oldRow - 1; int newCol = oldCol; System.out.println(this.traversable(newRow, newCol)); if (this.traversable(newRow, newCol)) { this.maze[newRow][newCol] = "m"; this.maze[oldRow][oldCol] = "x"; System.out.println("up"); System.out.println("mouse at: " + this.getMouse()); } else if (this.traversable(newRow, newCol) == false) { this.moveDown(); } } public void moveLeft() { Point mouse = this.getMouse(); int oldRow = mouse.getRow(); int oldCol = mouse.getCol(); int newRow = oldRow; int newCol = oldCol - 1; System.out.println(this.traversable(newRow, newCol)); if (this.traversable(newRow, newCol)) { this.maze[newRow][newCol] = "m"; this.maze[oldRow][oldCol] = "x"; System.out.println("left"); System.out.println("mouse at: " + this.getMouse()); } else if (this.traversable(newRow, newCol) == false) { this.moveRight(); } } public void moveRight() { Point mouse = this.getMouse(); int oldRow = mouse.getRow(); int oldCol = mouse.getCol(); int newRow = oldRow; int newCol = oldCol + 1; System.out.println(this.traversable(newRow, newCol)); if (this.traversable(newRow, newCol)) { this.maze[newRow][newCol] = "m"; this.maze[oldRow][oldCol] = "x"; System.out.println("right"); System.out.println("mouse at: " + this.getMouse()); } else if (this.traversable(newRow, newCol) == false) { this.moveUp(); } } //This is the base case public boolean baseCase() { Point mouse = this.getMouse(); int mouseRow = mouse.getRow(); int mouseCol = mouse.getCol(); Point cheese = this.getPos("c"); int chRow = cheese.getRow(); int chCol = cheese.getCol(); if (mouseRow == chRow && mouseCol == chCol ) { return true; } else { return false; } } //this is the method used to solve the maze. public void solver() { Point currentMouse = this.getMouse(); if (this.baseCase()) { System.out.println("CHEESE!"); } else { this.moveDown(); this.solver(); }
This is the support Point class that gets coordinates for any given string in a Maze.
package a07; /** * * @author Tim */ public class Point { protected int row; protected int col; public Point(int row, int col ) { this.row = row; this.col = col; } public int getRow() { return row; } public void setRow(int row) { this.row = row; } public int getCol() { return col; } public void setCol(int col) { this.col = col; } @Override public String toString() { return "Row: " + row + " " + "Column: " + col; } }