Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.42 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class Maze {
  4.  
  5.     private MazeRunnerStack path;
  6.     private MazeRunnerStack shortenPath;
  7.     private Boolean solved;
  8.     private static int WIDTH=4; //4
  9.     private static int HEIGHT=3; //3
  10.     private static char[][] mazeInfo = new char[WIDTH][HEIGHT];;
  11.     private char[][] position ;//= new char[WIDTH][HEIGHT];
  12.     private static int maxCount;
  13.    
  14.     public Maze(char[][] mazeInfo) {   //initalize width and height of the maze, set max count to the value width*height*4
  15.  
  16.         mazeInfo[0][0] = 'L';
  17.         mazeInfo[0][1] = '.';
  18.         mazeInfo[0][2] = '|';
  19.         mazeInfo[1][0] = '|';
  20.         mazeInfo[1][1] = '_';
  21.         mazeInfo[1][2] = '.';
  22.         mazeInfo[2][0] = '|';
  23.         mazeInfo[2][1] = '|';
  24.         mazeInfo[2][2] = '|';
  25.         mazeInfo[3][0]= 'L';
  26.         mazeInfo[3][1]='_';
  27.         mazeInfo[3][2]='_';
  28.  
  29.         WIDTH = mazeInfo[0].length;
  30.         HEIGHT = mazeInfo.length;
  31.  
  32.         this.solved = false;
  33.         this.maxCount = WIDTH*HEIGHT* 4;position = new char[HEIGHT][WIDTH];
  34.         for (int i = 0; i < mazeInfo.length; i++) {
  35.             for (int j = 0; j < mazeInfo[i].length; j++) {
  36.                 position[i][j] = 'z';
  37.             }
  38.         }
  39.        
  40.         //System.out.print(maxCount);
  41.  
  42.     };
  43.  
  44.     public void setStart(int row, int col) {     //assign all the position to random letter to avoid null pointer exception. set start
  45.        
  46.         this.position[row][col] = 'S';
  47.     };
  48.  
  49. //  public void setFinish(int row, int col) {
  50. //      this.position[row][col] = 'F';
  51. //  };
  52.    
  53.     public void setFinish(int row, int col)    //set the finish point for the maze
  54.     {
  55.         this.position[row][col] = 'F';
  56.     };
  57.  
  58.     public void displayMaze(MazeRunnerStack path) {   //print out the maze
  59.    
  60.         if (true) { // change to !solved                  
  61.             int count = 0;
  62.             System.out.println("+---+---+---+");
  63.             for (int i = 0; i < mazeInfo.length; i++) {
  64.                 int[] temp = new int[3];
  65.                 for (int j = 0; j < mazeInfo[i].length; j++) {
  66.                     // System.out.print(j);
  67.                     if (mazeInfo[i][j] == 'L' || mazeInfo[i][j] == '.' || mazeInfo[i][j] == '_'
  68.                             || mazeInfo[i][j] == '|') {
  69.                         char ch = mazeInfo[i][j];    //initialize
  70.  
  71.            
  72.                        
  73.                         {
  74.  
  75.                             switch (ch) {      //check the wall type, print out depend on the wall
  76.  
  77.                             case 'L': // 1         //wall on the left side and botton
  78.  
  79.                                
  80.                                 if (position[i][j] == 'S')
  81.                                     System.out.print("| S ");
  82.                                 else if (position[i][j] == 'F')
  83.                                     System.out.print("| F ");
  84.                                 else if(path.contains(new Position(i, j)) )
  85.                                     System.out.print("| * ");
  86.                                 else
  87.                                     System.out.print("|   ");
  88.                                 temp[j] = 1;
  89.                                 break;
  90.                             case '.':// 2                  //no wall
  91.                                 if (position[i][j] == 'S')
  92.                                     System.out.print("  S ");
  93.                                 else if (position[i][j] == 'F')
  94.                                     System.out.print("  F ");
  95.                                 else if(path.contains(new Position(i, j)) )
  96.                                     System.out.print("  * ");
  97.                                 else
  98.                                     System.out.print("    ");
  99.                                 temp[j] = 2;
  100.                                 break;
  101.                             case '|': // 3                 //wall on the left
  102.                                 if (position[i][j] == 'S')
  103.                                     System.out.print("| S ");
  104.                                 else if (position[i][j] == 'F')
  105.                                     System.out.print("| F ");
  106.                                 else if(path.contains(new Position(i, j)))
  107.                                     System.out.print("| * ");
  108.                                 else
  109.                                     System.out.print("|   ");
  110.                                 temp[j] = 3;
  111.                                 break;
  112.                             case '_': // 4                //wall on the bottom
  113.                                 if (position[i][j] == 'S')
  114.                                     System.out.print("  S ");
  115.                                 else if (position[i][j] == 'F')
  116.                                     System.out.print("  F ");
  117.                                 else if(path.contains(new Position(i, j)))
  118.                                     System.out.print("  * ");
  119.                                 else
  120.                                     System.out.print("    ");
  121.                                 temp[j] = 4;
  122.                                 break;
  123.                             }
  124.                         }
  125.  
  126.                     }
  127.  
  128.                 }
  129.                 if (count % 2 == 0) // the last char of each line , eitehr "+" or "|"
  130.                     System.out.print("|");
  131.                 else if (count % 2 == 1)
  132.                     System.out.print("+");
  133.                 System.out.println();
  134.  
  135.                 for (int k = 0; k < temp.length; k++) {
  136.  
  137.                     if (temp[k] == 1 || temp[k] == 4)
  138.                         System.out.print("+---");
  139.                     else if (temp[k] == 2 || temp[k] == 3)
  140.                         System.out.print("+   ");
  141.                 }
  142.                 if (count % 2 == 0) // the last char of the very next line
  143.                     System.out.print("+");
  144.                 else if (count % 2 == 1)
  145.                     System.out.print("|");
  146.                 System.out.println();
  147.  
  148.             }
  149.             count++;
  150.         }
  151.     };
  152.  
  153.     public void solveMaze() {
  154.         int attempts = 0;  
  155.         path = new MazeRunnerStack();
  156.         shortenPath=new MazeRunnerStack();   //creating  a new path
  157.         char direction = 'E'; // Can be N, E, S, W to represent North West East and South
  158.         Position currPosition;
  159.         for (int i = 0; i < position.length; i++) {         //push the starting point in the stack
  160.             for (int j = 0; j < position[i].length; j++) {
  161.                 if (position[i][j] == 'S') {
  162.                     currPosition = new Position(i, j);
  163.                     path.push(currPosition);
  164.                     break;
  165.                 }
  166.                 // path.push(item);
  167.             }
  168.         }
  169.  
  170.         while (!solved ) {                          //keep running until its solved
  171.             Position topStack = path.peek();
  172.             if(mazeInfo==null|mazeInfo.length==0)
  173.                 break;
  174.             if(attempts>=maxCount)
  175.                 break;
  176.            
  177.  
  178.             if (position[topStack.row][topStack.col] == 'F') {
  179.                 this.solved = true;
  180.                 break;
  181.             }
  182.             //System.out.println(topStack.row + ", " + topStack.col + "     dir: " + direction);
  183.             switch (direction) {
  184.  
  185.             case 'w': // north
  186.             case 'W':
  187.                 if (topStack.row - 1 < 0) {
  188.                     direction = 's';
  189.                 } else if (mazeInfo[topStack.row - 1][topStack.col] == 'L'
  190.                         || mazeInfo[topStack.row - 1][topStack.col] == '_')
  191.                     direction = 's';//'e'
  192.                 else {
  193.                     currPosition = new Position(topStack.row - 1, topStack.col);
  194.                     path.push(currPosition);
  195.                     direction='n';
  196.                    
  197.  
  198.                 }
  199.                 attempts++;
  200.                 break;
  201.  
  202.             case 'n': // east
  203.             case 'N':
  204.                 if (topStack.col + 1 >= WIDTH) {
  205.                     direction = 'w';
  206.                 } else if (mazeInfo[topStack.row][topStack.col + 1] == 'L'
  207.                         || mazeInfo[topStack.row][topStack.col + 1] == '|') {
  208.                     direction = 'w';
  209.                 } else {
  210.                     currPosition = new Position(topStack.row, topStack.col + 1);
  211.                     path.push(currPosition);
  212.                     // System.out.print(Arrays.toString(path.peek()));
  213.                     direction='e';
  214.                 }
  215.                 attempts++;
  216.                 break;
  217.  
  218.             case 'e': // south
  219.             case 'E':
  220.                 if (topStack.row + 1 >= HEIGHT) {
  221.                     direction = 'n';
  222.                 } else if (mazeInfo[topStack.row][topStack.col] == 'L' || mazeInfo[topStack.row][topStack.col] == '_') {
  223.                     direction = 'n';
  224.                 } else {
  225.                     currPosition = new Position(topStack.row + 1, topStack.col);
  226.                     path.push(currPosition);
  227.                     direction='s';
  228.                 }
  229.                 attempts++;
  230.                 break;
  231.  
  232.             case 's': // west
  233.             case 'S':
  234.                 if (topStack.col - 1 < 0) {
  235.                     direction = 'e';
  236.                 } else if (mazeInfo[topStack.row][topStack.col] == 'L' || mazeInfo[topStack.row][topStack.col] == '|') {
  237.                     direction = 'e';
  238.                 } else {
  239.                     currPosition = new Position(topStack.row, topStack.col - 1);
  240.                     path.push(currPosition);
  241.                     direction='w';
  242.                 }
  243.                 attempts++;
  244.                 break;
  245.  
  246.             }
  247.            
  248.  
  249.         }if(solved) {
  250.             Position[] path1 = new Position[path.getSize()];
  251.             for (int i = path1.length - 1; i >= 0; i--) { //pop all value ,assgin to path1
  252.                 path1[i] = path.pop();
  253.             }
  254.             for (int i = 0; i < path1.length; i++) {
  255.                 path.push(path1[i]);                       //restore the stack
  256.             }
  257.             System.out.println("Solution is:");
  258.             for (int i = path1.length - 1; i >= 0; i--) {
  259.                 while (shortenPath.contains(path1[i])) {    //check whether contain,if contains, remove it until there is no same position
  260.                     shortenPath.pop();
  261.                 }
  262.                 shortenPath.push(path1[i]);
  263.             }
  264.            
  265.             displayMaze(shortenPath);                        //print out the maze
  266.             Position[] newPath = new Position[shortenPath.getSize()];  
  267.             for (int i = 0; i < newPath.length; i++) {       //pop out the stack, assign the value to newPath
  268.                 newPath[i] = shortenPath.pop();
  269.             }
  270.             for (int i = newPath.length - 1; i >= 0; i--) {     //restore the stack again
  271.                 shortenPath.push(newPath[i]);
  272.             }
  273.  
  274.             System.out.print("Path is: ");
  275.             for (int i = 0; i < newPath.length; i++) {         //print out the path
  276.                 System.out.print("[" + newPath[i].row + "," + newPath[i].col + "]");
  277.                 if (i != newPath.length - 1)
  278.                     System.out.print(" --> ");
  279.             }
  280.             System.out.println();
  281.         }else {
  282.             System.out.println("No Solution could be found.");
  283.             displayMaze(shortenPath);
  284.         }
  285.            
  286.  
  287.     };
  288.  
  289.     public static void main(String[] args) {
  290.  
  291.         Maze test = new Maze(mazeInfo);
  292.         test.setStart(0, 2);
  293.         test.setFinish(2,1);
  294.         test.solveMaze();
  295.        
  296.        
  297.  
  298.     }
  299.  
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement