pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

epitaph64 private pastebin - collaborative debugging tool What's a private pastebin?


Posted by Epitaph64 on Mon 25 May 17:27
report abuse | download | new post

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package mazetime;
  7.  
  8. /**
  9.  *
  10.  * @author Epitaph64
  11.  */
  12. public class MazeGenerator {
  13.  
  14.     private boolean working;
  15.  
  16.     public Cell[][] maze;
  17.  
  18.     public int width, height;
  19.    
  20.     public class Cell
  21.     {
  22.         boolean flooded = false;
  23.         public boolean[] connections = new boolean[4];
  24.  
  25.         public boolean hasConnections()
  26.         {
  27.             boolean r = false;
  28.             for (int i = 0; i < 4; i++)
  29.             {
  30.                 if (connections[i])
  31.                 {
  32.                     r = true;
  33.                 }
  34.             }
  35.             return r;
  36.         }
  37.  
  38.         public int hasNumberConnections()
  39.         {
  40.             int x = 0;
  41.             for (int i = 0; i < 4; i++)
  42.             {
  43.                 if (connections[i])
  44.                 {
  45.                     x ++;
  46.                 }
  47.             }
  48.             return x;
  49.         }
  50.     }
  51.  
  52.     public Cell[][] generateMazeRecursive(int width, int height)
  53.     {
  54.         this.width = width;
  55.         this.height = height;    
  56.         maze = new Cell[width][height];
  57.  
  58.         for (int x = 0; x < width; x++)
  59.         {
  60.             for (int y = 0; y < height; y++)
  61.             {
  62.                 maze[x][y] = new Cell();
  63.             }
  64.         }
  65.  
  66.         int cursorX = (int) (Math.random() * width);
  67.         int cursorY = (int) (Math.random() * height);
  68.         branchOff(cursorX, cursorY);
  69.         return maze;
  70.     }
  71.  
  72.     public void branchOff(int x, int y)
  73.     {
  74.         if (maze[x][y].hasNumberConnections() <= 1)
  75.         {
  76.             if (Math.random() * 1000 >= 500)
  77.             {
  78.                 if (y - 1 >= 0)
  79.                 {
  80.                     if (! maze[x][y-1].hasConnections())
  81.                     {
  82.                         maze[x][y].connections[0] = true;
  83.                         maze[x][y-1].connections[2] = true;
  84.                         branchOff(x, y-1);
  85.                     }
  86.                 }
  87.             }
  88.             else
  89.             {
  90.                 if (y + 1 < height)
  91.                 {
  92.                     if (! maze[x][y+1].hasConnections())
  93.                     {
  94.                         maze[x][y].connections[2] = true;
  95.                         maze[x][y+1].connections[0] = true;
  96.                         branchOff(x, y+1);
  97.                     }
  98.                 }
  99.             }
  100.             if (x + 1 < width)
  101.             {
  102.                 if (! maze[x+1][y].hasConnections())
  103.                 {
  104.                     maze[x][y].connections[1] = true;
  105.                     maze[x+1][y].connections[3] = true;
  106.                     branchOff(x+1, y);
  107.                 }
  108.             }
  109.             if (x - 1 >= 0)
  110.             {
  111.                 if (! maze[x-1][y].hasConnections())
  112.                 {
  113.                     maze[x][y].connections[3] = true;
  114.                     maze[x-1][y].connections[1] = true;
  115.                     branchOff(x-1, y);
  116.                 }
  117.             }
  118.         }
  119.     }
  120. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post