SHARE
TWEET

mazegen function

a guest Mar 24th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void mazegenerator::Generate(int rowCount, int colCount) //Generates 'maze' using vectors.
  2. {
  3.  
  4.     // Resize origin and inner vectors to desired sizes.
  5.     maze.resize(rowCount);
  6.     for (int i = 0; i < rowCount; i++)
  7.     {
  8.         maze[i].resize(colCount);
  9.     }
  10.    
  11.     int row = 1;
  12.     int col = 1;
  13.     Tile& tile = maze[row][col];
  14.    
  15.     // Set default values for Tile attributes iterating through every 'Tile'
  16.     for (int r = 0; r < maze.size(); r++)
  17.     {
  18.         for (int c = 0; c < maze[0].size(); c++)
  19.         {
  20.             tile.row = r;
  21.             tile.col = c;
  22.             tile.visited = false;
  23.             tile.blocked = true;
  24.         }
  25.     }
  26.  
  27.     tile.blocked = false;
  28.     tile.visited= false;
  29.     /*for (int r = 0; r < getRowCount(); r++) // Iterate through rows
  30.     {
  31.         for (int c = 0; c < getColCount(); c++) // Iterate through cols
  32.         {
  33.             Tile& tile = maze[r][c]; // tile = maze position r, c E.g. 0, 1
  34.  
  35.             for (int i = 0; i < neighbours.size(); i++) // i = 0; If i is less than size of neighbours then i++
  36.             {
  37.                 cout << "Neighbour " << i << ": row:" << neighbours[i].row << " col: " << neighbours[i].col << endl;
  38.             }
  39.            
  40.         }
  41.     }*/
  42.  
  43.     /*cout << "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB" << endl;
  44.     for (int r = 0; r < getRowCount(); r++)\
  45.     {
  46.         cout << row << "," << col << endl;
  47.         for (int c = 0; c < getColCount(); c++)
  48.         {
  49.             cout << "\tRow: " << r << " Col: " << c << endl;
  50.            
  51.         }
  52.         cout << "\n";
  53.  
  54.         if(row != getRowCount())
  55.             row = row + 1;
  56.         if (col != getColCount())
  57.             col = col + 1;
  58.     }
  59.     cout << "\n";*/
  60.     //cout << "Number of Neighbours for [" << row << "," << col << "]: " << *neighbours.size() << "\n" << endl;
  61.     //cout << "Neighbour\tRow\tCol\tVisited" << endl;
  62.    
  63.    
  64.  
  65.     while (!maze.empty())
  66.     {
  67.         //Get current tile
  68.         Tile thisTile = maze.front();
  69.         //Get unvisited neighbours
  70.         vector <Tile*> neighbours = getUnvisitedNeighbours(&tile, row, col);
  71.  
  72.         if (!neighbours.empty())
  73.         {
  74.             Tile nexttile = *neighbours[rand() % neighbours.size()];
  75.  
  76.             // Get wall between tiles
  77.             int midRow = tile.row + (nexttile.row = tile.row) / 2;
  78.             int midCol = tile.col + (nexttile.col = tile.col) / 2;
  79.             Tile tileWall = maze[midRow][midCol];
  80.  
  81.             // Mark neighbour and wall cells as unblocked
  82.             nexttile.blocked = false;
  83.             tileWall.blocked = false;
  84.  
  85.             //Add neighbour to top of stack and mark visited
  86.             maze.push_back(nexttile);
  87.             nexttile.visited = true;
  88.         }
  89.         else
  90.         {
  91.             maze.pop_back();
  92.         }
  93.     }
  94.    
  95.  
  96.    
  97. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top