Advertisement
Guest User

Maze

a guest
Dec 23rd, 2013
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 KB | None | 0 0
  1. /*
  2.  * Mark Klingberg
  3.  * m2615919
  4.  * 11/13/2013
  5.  *
  6.  * This program generates and solves a maze
  7.  */
  8.  
  9. public class Maze {
  10.    
  11.     private static boolean endFound;
  12.    
  13.     public static char [][] create(int width, int height){
  14.        
  15.         int numCells = width*height, unionedCount = 0, rand, temp, i, j=0, k=0,
  16.                 setx, sety, x, y, numWalls = 2*(width-1)*(height-1)+width+height-2;
  17.         int[] walls = new int[numWalls];
  18.         int[] parent = new int[numCells];
  19.         int[] rank = new int[numCells];
  20.         boolean[] wallExsits = new boolean[numWalls];
  21.         char[][] maze = new char[2*height+1][2*width+1];
  22.                
  23.         //make set
  24.         for(i=0; i<parent.length; i++){
  25.             parent[i] = i;
  26.             rank[i] = 0;
  27.         }
  28.        
  29.         //initialize walls
  30.         for(i=0; i<walls.length; i++){
  31.             walls[i] = i;
  32.             wallExsits[i] = true;
  33.         }
  34.        
  35.         //shuffle walls
  36.         for(i=0; i<walls.length; i++){
  37.        
  38.             rand = (int)((Math.random() * 1000000) % walls.length);
  39.            
  40.             temp = walls[i];
  41.             walls[i] = walls[rand];
  42.             walls[rand] = temp;
  43.            
  44.         }
  45.        
  46.         i=0;
  47.         while((unionedCount < numCells) && (i<walls.length)){
  48.            
  49.             //find x and y
  50.             temp = walls[i] % (2*width-1);
  51.             if(temp < width-1){
  52.                 x = temp + (walls[i]/(2*width-1)*width);
  53.                 y = x+1;
  54.             }
  55.             else{
  56.                 x = temp + (walls[i]/(2*width-1)*width) - (width-1);
  57.                 y = x + width;
  58.             }
  59.            
  60.             //if w divides two cells, x and y, that are in disjoint sets
  61.             if(findset(x, parent)!=findset(y, parent)){
  62.                
  63.                 //remove w
  64.                 wallExsits[walls[i]] = false;
  65.                
  66.                 //union(x, y)
  67.                 setx = findset(x, parent);
  68.                 sety = findset(y, parent);
  69.  
  70.                 if (rank[setx] < rank[sety])
  71.                     parent[setx] = sety;
  72.                 else if (rank[sety] < rank[setx])
  73.                     parent[sety] = setx;
  74.                 else
  75.                 {
  76.                     parent[sety] = setx;
  77.                     rank[setx]++;
  78.                 }
  79.                
  80.                 unionedCount++;
  81.             }
  82.            
  83.             i++;
  84.            
  85.         }
  86.        
  87.         //translate from bool array to 2d char array
  88.         //filling in outer boundries
  89.         for(i=0; i<2*width+1; i++){
  90.             maze[0][i] = '#';
  91.             maze[2*height][i] = '#';
  92.         }
  93.         for(i=0; i<2*height+1; i++){
  94.             maze[i][0] = '#';
  95.             maze[i][2*width] = '#';
  96.         }
  97.        
  98.         //filling in the middle
  99.         for(j=0; j<(2*height-1); j++){
  100.             for(i=0; i<(2*width-1); i++){
  101.                
  102.                 if((j%2)==0){
  103.                     //even row, even column
  104.                     if((i%2)==0){
  105.                         maze[j+1][i+1]=' ';
  106.                     }
  107.                     //even row, odd column
  108.                     else{
  109.                         if(wallExsits[k])
  110.                             maze[j+1][i+1]='#';
  111.                         else
  112.                             maze[j+1][i+1]=' ';
  113.                         k++;
  114.                     }
  115.                 }
  116.                 else{
  117.                     //odd row, even column
  118.                     if((i%2)==0){
  119.                         if(wallExsits[k])
  120.                             maze[j+1][i+1]='#';
  121.                         else
  122.                             maze[j+1][i+1]=' ';
  123.                         k++;
  124.                     }
  125.                     //odd row, odd column
  126.                     else{
  127.                         maze[j+1][i+1]='#';
  128.                     }
  129.                 }
  130.                
  131.             }
  132.         }
  133.            
  134.         //add start and exit markers
  135.         maze[1][1] = 's';
  136.         maze[2*height-1][2*width-1] = 'e';
  137.        
  138.         return maze;
  139.     }
  140.    
  141.     private static int findset(int x, int[] parent){
  142.            
  143.        if (parent[x] == x)
  144.            return x;
  145.  
  146.         parent[x] = findset(parent[x], parent);
  147.         return parent[x];
  148.            
  149.     }
  150.    
  151.     public static char[][] solve(char [][] maze){
  152.        
  153.         endFound = false;
  154.        
  155.         maze = solver(maze, 1, 1);
  156.        
  157.         //fix the s
  158.         if(maze[1][1] != 'e')
  159.             maze[1][1] = 's';
  160.        
  161.         return maze;
  162.        
  163.     }
  164.    
  165.     //function is recursively called in the order up, right, down, left. only recurses if it sees a ' ' or an 'e'.
  166.     private static char[][] solver(char [][] maze, int i, int j){
  167.        
  168.         //end found, set flag
  169.         if(maze[i][j] == 'e'){
  170.             endFound = true;
  171.             return maze;
  172.         }
  173.        
  174.         //recurse up
  175.         if(maze[i][j-1]==' ' || maze[i][j-1]=='e'){
  176.             maze[i][j]='.';
  177.             maze = solver(maze, i, j-1);
  178.             //return without backtracking and removing period
  179.             if(endFound==true)
  180.                 return maze;
  181.         }
  182.        
  183.         //recurse right
  184.         if(maze[i+1][j]==' ' || maze[i+1][j]=='e'){
  185.             maze[i][j]='.';
  186.             maze = solver(maze, i+1, j);
  187.             //return without backtracking and removing period
  188.             if(endFound==true)
  189.                 return maze;
  190.         }
  191.        
  192.         //recurse down
  193.         if(maze[i][j+1]==' ' || maze[i][j+1]=='e'){
  194.             maze[i][j]='.';
  195.             maze = solver(maze, i, j+1);
  196.             //return without backtracking and removing period
  197.             if(endFound==true)
  198.                 return maze;
  199.         }
  200.        
  201.         //recurse left
  202.         if(maze[i-1][j]==' ' || maze[i-1][j]=='e'){
  203.             maze[i][j]='.';
  204.             maze = solver(maze, i-1, j);
  205.             //return without backtracking and removing period
  206.             if(endFound==true)
  207.                 return maze;
  208.         }
  209.        
  210.         //if backtracking, remove period
  211.         maze[i][j] = ' ';
  212.         return maze;
  213.     }
  214.    
  215.     public static double difficultyRating(){
  216.        
  217.         return 2.0;
  218.     }
  219.    
  220.     public static double hoursSpent(){
  221.        
  222.         return 10.0;
  223.     }
  224.    
  225.     public static void print(char[][] maze){
  226.        
  227.         for(int i=0; i<maze.length; i++){
  228.             for(int j=0; j<maze[i].length; j++){
  229.                 System.out.printf("%c", maze[i][j]);
  230.             }
  231.             System.out.println();
  232.         }
  233.        
  234.     }
  235.    
  236.     public static void main(String args[]){
  237.        
  238.         print(solve(create(10, 10)));
  239.        
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement