My program is to traverse through a maze and recursively search for '$' starting at element (1,1). I don't know if i'm running the recursion properly since my program skipps that there is a solid wall '&' and tests for it anyways. My traversable path is indicated by '#'.
This is getting extremely annoying, any help will be very appreciated.
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Maze { public static char[][] slideData; private int ReturnCount = 0; public Maze(String s) { try { File slideFile = new File(s); FileReader in = new FileReader(slideFile); BufferedReader readSlide = new BufferedReader(in); int length = Integer.parseInt(readSlide.readLine()); int width = Integer.parseInt(readSlide.readLine()); slideData = new char[length][width]; for(int row = 0; row < length; row++){ for (int col = 0; col < width; col++){ slideData[row][col] = (char)readSlide.read(); } readSlide.readLine(); } readSlide.close(); in.close(); }catch (FileNotFoundException e) { System.out.println("File does not exist/ could not be found."); System.err.println("FileNotFoundException: " + e.getMessage()); }catch(IOException e){ System.out.println("Problem reading file."); System.err.println("IOException: " + e.getMessage()); } } public void displayColonies() { char[][] temp; int count; for(int row = 0; row < slideData.length; row++) { for(int col = 0; col < slideData[0].length; col++) { if(slideData[row][col] >= 0 ) { count = getPath(row, col); if(count != 0) { System.out.println("(" +row + "," + col + ") Cn"+ count); } } } } } public void displaySlide() { char[][] temp; int count = 0; if(count == 0) { System.out.println("Provided Slide:"); count++; } else System.out.println("Ending Slide:"); for(int row = 0; row < slideData.length; row++){ for(int col = 0; col < slideData[0].length; col++){ System.out.print(slideData[row][col]); } System.out.println(); } System.out.println(); } private int getPath (int row, int col) { if (slideData[row][col] == '$') { System.out.println("Maze found"); return(0); } else { if (slideData[row][col] == '#') { slideData[row][col] = '%'; // this cell has been tried if (slideData[row+1][col] == '#') { slideData[row][col] = slideData[row+1][col]; slideData[row][col] = '%'; ReturnCount = ReturnCount+1; ReturnCount = getPath(row +1, col); System.out.println(ReturnCount); } if (slideData[row-1][col] == '#') { slideData[row][col] = slideData[row-1][col]; slideData[row][col] = '%'; ReturnCount = ReturnCount+1;; ReturnCount = getPath(row +1, col); System.out.println(ReturnCount); } if (slideData[row][col-1] == '#') { slideData[row][col] = slideData[row][col-1]; slideData[row][col] = '%'; ReturnCount = ReturnCount+1; ReturnCount = getPath(row +1, col); System.out.println(ReturnCount); } if (slideData[row][col+1] == '#') { slideData[row][col] = slideData[row][col+1]; slideData[row][col] = '%'; ReturnCount = ReturnCount+1; ReturnCount = getPath(row +1, col); System.out.println(ReturnCount); } } } return ReturnCount ; } public static void main (String[] args) { Maze culture = new Maze("Maze.txt"); culture.displaySlide(); culture.displayColonies(); culture.displaySlide(); } }
the Maze.txt slide..
7 9 &&&&&&&&& &#&&&&&&& &######&& &#&&#&&&& &#&&$&&&& &#######& &&&&&&&&&