Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Maze {
- private MazeRunnerStack path;
- private Boolean solved;
- private static int WIDTH;
- private static int HEIGHT;
- private static char[][] mazeInfo;
- private char[][] position ;
- private static int maxCount;
- public Maze(char[][] mazeInfo) {
- mazeInfo[0][0] = 'L';
- mazeInfo[0][1] = '.';
- mazeInfo[0][2] = '|';
- mazeInfo[1][0] = 'L';
- mazeInfo[1][1] = '_';
- mazeInfo[1][2] = '_';
- this.solved=false;
- this.maxCount=mazeInfo.length*mazeInfo[0].length*4;
- };
- public void setStart(int row, int col) {
- position=new char[2][3];
- for (int i = 0; i < mazeInfo.length; i++) {
- for (int j = 0; j < mazeInfo[i].length; j++) {
- position[i][j] = 'z';
- }
- }
- this.position[row][col] = 'S';
- };
- public void setFinish(int row, int col) {
- this.position[row][col] = 'F';
- };
- public void displayMaze() {
- if (true) { // change to !solved
- int count = 0;
- System.out.println("+---+---+---+");
- for (int i = 0; i < mazeInfo.length; i++) {
- int[] temp = new int[3];
- for (int j = 0; j < mazeInfo[i].length; j++) {
- // System.out.print(j);
- if (mazeInfo[i][j] == 'L' || mazeInfo[i][j] == '.' || mazeInfo[i][j] == '_'
- || mazeInfo[i][j] == '|') {
- char ch = mazeInfo[i][j];
- switch (ch) {
- case 'L': // 1
- if (position[i][j] == 'S')
- System.out.print("| S ");
- else if (position[i][j] == 'F')
- System.out.print("| F ");
- else
- System.out.print("| ");
- temp[j] = 1;
- break;
- case '.':// 2
- if (position[i][j] == 'S')
- System.out.print(" S ");
- else if (position[i][j] == 'F')
- System.out.print(" F ");
- else
- System.out.print(" ");
- temp[j] = 2;
- break;
- case '|': // 3
- if (position[i][j] == 'S')
- System.out.print("| S ");
- else if (position[i][j] == 'F')
- System.out.print("| F ");
- else
- System.out.print("| ");
- temp[j] = 3;
- break;
- case '_': // 4
- if (position[i][j] == 'S')
- System.out.print(" S ");
- else if (position[i][j] == 'F')
- System.out.print(" F ");
- else
- System.out.print(" ");
- temp[j] = 4;
- break;
- }
- }
- }
- if (count % 2 == 0) //the last char of each line ,
- System.out.print("|");
- else if (count % 2 == 1)
- System.out.print("+");
- System.out.println();
- for (int k = 0; k < temp.length; k++) {
- if (temp[k] == 1 || temp[k] == 4)
- System.out.print("+---");
- else if (temp[k] == 2 || temp[k] == 3)
- System.out.print("+ ");
- }
- if (count % 2 == 0) //the last char of the next line
- System.out.print("+");
- else if (count % 2 == 1)
- System.out.print("|");
- System.out.println();
- }
- count++;
- }
- };
- public void solveMaze() {
- int attempts=0;
- path = new MazeRunnerStack();
- char direction = 'E'; // Can be N, E, S, W to represent North West East and South
- Position currPosition;
- for(int i=0;i<position.length;i++) {
- for(int j=0;j<position[i].length;j++) {
- if(position[i][j]=='S') {
- currPosition=new Position(i,j);
- path.push(currPosition);
- break;
- }
- //path.push(item);
- }
- }
- for(int i=0;i<position.length;i++) {
- for(int j=0;j<position[i].length;j++) {
- while(!solved) {
- if(attempts==maxCount)
- break;
- if(position[i][j]=='F') {
- this.solved=true;
- break;
- }
- switch(direction) {
- case 'n': //north
- case 'N':
- if(mazeInfo[i-1]!=null) {
- direction='e';
- }else if(mazeInfo[i-1][j]=='L'||mazeInfo[i-1][j]=='_')
- direction='e';
- else {
- currPosition=new Position(i-1,j);
- path.push(currPosition);
- currPosition=path.peek();
- System.out.print(currPosition);
- }
- break;
- case 'e': //east
- case 'E':
- if(mazeInfo[i][j+1]=='L'||mazeInfo[i][j+1]=='|') {
- direction='s';
- }else {
- currPosition=new Position(i,j+1);
- path.push(currPosition);
- }
- break;
- case 's': //south
- case 'S':
- if(mazeInfo[i+1]!=null)
- direction='w';
- else if(mazeInfo[i][j]=='L'||mazeInfo[i][j]=='_') {
- direction='w';
- }else {
- currPosition=new Position(i+1,j);
- path.push(currPosition);
- }
- break;
- case 'w': //west
- case 'W':
- if(mazeInfo[i][j]=='L'||mazeInfo[i][j]=='|')
- direction='n';
- else {
- currPosition=new Position(i,j-1);
- path.push(currPosition);
- currPosition=path.peek();
- System.out.print(currPosition);
- }
- break;
- }
- attempts++;
- }
- }
- }
- };
- public static void main(String[] args) {
- mazeInfo=new char[WIDTH][HEIGHT];
- Maze test = new Maze(mazeInfo);
- test.setStart(0, 0);
- test.setFinish(0, 2);
- test.displayMaze();
- test.solveMaze();
- System.out.println("Solution is:");
- test.displayMaze();
- System.out.print("Path is:");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement