Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Maze Generation

Discussion in 'Scripting' started by fumertue, Sep 22, 2015.

  1. fumertue

    fumertue

    Joined:
    Sep 21, 2015
    Posts:
    2
    Hi,
    I'm creating a small random maze with an algorithm, which in a way works, but it seems i must be doing something very inneficiently or illegal in unity for whenever i try to create a maze that is "large", as in bigger than 6x6 unity will stop responding.
    What is weird is that if i create a 5x5 maze then create a 6x6 then a 7x7 (all in their own different game sessions, never together) unity doesn't crash, and i tried this up to 13*13. But if i open up unity and create a large maze directly it crashes. Does anyone know any reason this could be? Any other criticism of the code is greatly appreciated :) (this is all based on js)
    Code (JavaScript):
    1. var CellFloor : Transform;
    2. var Wall : Transform;
    3.  
    4.  
    5. var SizeX2 = 2;                //X and Y size of maze
    6. var SizeY2 = 2;
    7. var Xmaze = 0;                //location of current cell we are at
    8. var Ymaze = 0;
    9. var counter = 1;            //counting how many different cells we have visited
    10. var update = false;            //should we update lastX and lastY
    11. var lastX = new Array();    //array of cells where there was another possible path
    12. var lastY = new Array();  
    13. var walls = new Array(SizeX2);
    14. for (var x =0; x<SizeX2; x++) {
    15.     walls[x] = new Array(SizeY2);
    16.     for (var y=0; y<SizeY2; y++){
    17.     walls[x][y]= new Array();
    18.     walls[x][y].push(0,"East","South","West","North");
    19.     }
    20. }
    21.  
    22.    
    23. function Start () {
    24.     while (counter <SizeX2*SizeY2){
    25.            print("counter :" +counter);
    26.         print("X :" +Xmaze);
    27.         print("Y :" +Ymaze);
    28.        
    29.         if (walls[Xmaze][Ymaze][0]!=1){
    30.             walls[Xmaze][Ymaze][0]=1;
    31.             print(" should be x  y : "+Xmaze+" "+Ymaze);
    32.             counter++;
    33.             }
    34.          var neighbour = find_neighbour(Xmaze,Ymaze);
    35.         next_neighbour(neighbour);
    36.     }
    37.     draw();
    38. }
    39.  
    40.  
    41. function Update () {
    42.  
    43. }
    44.  
    45.  
    46. function next_neighbour(neighbour){
    47.        if (neighbour!= null){
    48.             walls[Xmaze][Ymaze][0]=1;
    49.             if (update == true){
    50.                 lastX.push(Xmaze);
    51.                 lastY.push(Ymaze);
    52.                 update = false;
    53.                   }
    54.             if (neighbour=="East"){Xmaze++; walls[Xmaze][Ymaze].remove("West");}
    55.             else if (neighbour=="South"){Ymaze--; walls[Xmaze][Ymaze].remove("North");}
    56.             else if (neighbour=="West"){Xmaze--; walls[Xmaze][Ymaze].remove("East");}      
    57.             else if (neighbour=="North"){Ymaze++; walls[Xmaze][Ymaze].remove("South");}
    58.         }
    59.         else{  
    60.                 print("XXXXX "+ lastX);
    61.                 print("XXXXX "+ lastY);
    62.                 Xmaze = lastX[lastX.length-1];
    63.                 Ymaze = lastY[lastY.length-1];
    64.                 lastX.RemoveAt(lastX.length-1);
    65.                 lastY.RemoveAt(lastY.length-1);
    66.         }          
    67. }
    68. function find_neighbour(x2, y2) {
    69.        w = new Array();
    70.        if (hasEast(x2,y2)) w.push("East");
    71.        if (hasSouth(x2,y2)) w.push("South");
    72.        if (hasWest(x2,y2))w.push("West");
    73.        if (hasNorth(x2,y2))w.push("North");
    74.      
    75.        if (w.length==0) return null;
    76.        if (w.length>1) update = true;
    77.        var indexOfW = parseInt(Random.Range(0,w.length));
    78.        var whichWall = w[indexOfW];
    79.        walls[x2][y2].remove(whichWall);
    80.        return whichWall;
    81. }
    82.  
    83.  
    84. function hasNorth(x2, y2) {
    85.         if ( y2+1 > SizeY2-1) return false;
    86.         if (walls[x2][y2+1][0]!=1)return true;
    87.         return false;
    88. }
    89. function hasSouth(x2, y2) {
    90.         if ( y2-1 < 0 ) return false;
    91.         if (walls[x2][y2-1][0]!=1)return true;
    92.         return false;
    93. }
    94. function hasEast( x2, y2) {
    95.         if ( x2+1 > SizeX2-1 ) return false;
    96.         if (walls[x2+1][y2][0]!=1) return true;
    97.         return false;
    98. }
    99. function hasWest( x2, y2) {
    100.         if ( x2-1 < 0 ) return false;
    101.         if (walls[x2-1][y2][0]!=1) return true;
    102.         return false;
    103. }
    104. function draw(){
    105.         for (var i =0; i<SizeX2; i++) {
    106.             for (var j =0; j<SizeY2; j++) {
    107.                 for(var s =1; s<walls[i][j].length;s++){
    108.                     if(walls[i][j][s]=="East") Instantiate (Wall , Vector3((i * 6)+3, 0, j * 6),  Quaternion.Euler(90, -90, 0));
    109.                     else if(walls[i][j][s]=="South") Instantiate (Wall , Vector3(i * 6, 0, (j * 6)-3), Quaternion.Euler(90, 0, 0));
    110.                     else if(walls[i][j][s]=="West")  Instantiate (Wall , Vector3((i * 6)-3, 0, j * 6), Quaternion.Euler(90, 90, 0));
    111.                     else if(walls[i][j][s]=="North") Instantiate (Wall , Vector3(i * 6, 0, (j * 6)+3), Quaternion.Euler(90, 180, 0));
    112.                 }
    113.                    Instantiate (CellFloor , Vector3(i * 6, 0, j * 6), Quaternion.identity);
    114.             }
    115.         }
    116. }
     
  2. fumertue

    fumertue

    Joined:
    Sep 21, 2015
    Posts:
    2
    I'm not sure my message was very clear so if you need any other info, tell me
     
  3. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Kiwasi likes this.