How do i use my array within my solveMaze function (java)? -


here code:

public class solving {     public static void main(string[] args) throws filenotfoundexception {         readmazefile("maze0.txt");         solvemaze(0, 0);      }      static void readmazefile(string mazefile) throws filenotfoundexception {        scanner input = new scanner(new file(mazefile));        //find height , width       int height = input.nextint();       int width = input.nextint();       int finalheight = (2*height) + 1;       int finalwidth = (2*width) + 1;      //create array , put data file in       char maze[][] = new char[finalheight][finalwidth];       input.nextline();          (int row = 0; row < finalheight; row++) {             string fileline = input.nextline();             (int col = 0; col < finalwidth; col++) {                   char nextchar = fileline.charat(col);                   maze[row][col] = nextchar;              }           }        }       static boolean solvemaze(int row, int col) {       char right = maze[row][col + 1];       char left = maze[row][col - 1];       char = maze[row - 1][col];       char down = maze[row + 1][col];       //base case @ end of maze       if (right == 'e' || left == 'e' || == 'e' || down == 'e'){          return true;       }       if (right == ' '){          return solvemaze(row, col+1);       }        if (down == ' ') {          return solvemaze(row+1,col);   }         if (left == ' ') {          return solvemaze(row,col-1);   }          if (up == ' ') {           return solvemaze(row-1,col);   }        return false; }        } 

i trying solve maze created using text file, sure there many other things wrong code, cant test out because don't know how maze array work inside solvemaze function. appreciated, thanks.

you have 2 options:

  1. make global defining outside readmaze() function , use in solvemaze() function.

    public class solving {     /* define here */     private static char maze[][] = new char[finalheight][finalwidth];      /* rest of code */ } 
  2. return readmaze() main function , pass in solvemaze() function main function.

    main function:

    public static void main(string[] args) throws filenotfoundexception {     char[][] maze = readmazefile("maze0.txt");     solvemaze(maze, 0, 0); } 

    readmaze function:

    static char[][] readmazefile(string mazefile) throws filenotfoundexception {      /* rest of code */      /* return maze array */     return maze; } 

    solvemaze function:

    static boolean solvemaze(char[][] maze, int row, int col) {     /* code here */ } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -