Simple2012

Marcus_Maze_Generation

May 27th, 2013
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.99 KB | None | 0 0
  1. #include <iostream>
  2. #include "Maze.h"
  3. #include <stack>
  4. #include <stdlib.h>
  5. #include <algorithm> // random_shuffle
  6. #include <ctime>
  7. #include <sstream>
  8. typedef unsigned int Uint;
  9. enum { north, east, south, west };
  10. const char Maze::WALL = '#';
  11. const char Maze::PATH = ' ';
  12. //const char Maze::VISITED = '_';
  13. //const unsigned int Maze::SIZE = 100;
  14.  
  15. unsigned int VisitedCells, TotalCells, x, y;
  16. std::pair <unsigned int, unsigned int> CurrentCell;
  17. std::vector< std::pair<unsigned int, unsigned int> > NeighboursToVisit;
  18. std::vector<unsigned int> Direction;
  19. std::stack< std::pair<Uint, Uint> > CellStack;
  20.  
  21.  
  22. void Maze::Create_maze(Uint row, Uint col){
  23.     // Fill the vector<vector> with astrix
  24.     Map = std::vector< std::vector<char> >(row, std::vector<char>(col, WALL));
  25.     // CellStack to hold list of all cell locations
  26.     // Number of cells in the grid/map
  27.     TotalCells = row*col;
  28.     // Mark first CurrentCell as (1,1)
  29.     CurrentCell = std::make_pair(1,1);
  30.     // Set visited cells to 1
  31.     VisitedCells = 1;
  32.     Generate_maze(CurrentCell);
  33. }
  34. void Maze::Generate_maze(std::pair<Uint, Uint> CurrentCell){
  35.     srand (time(NULL));
  36.     int RandElement;
  37.     //while (VisitedCells < TotalCells){
  38.     for(int i = 0;i<15;i++){
  39.         // To check the direction of neighbour
  40.         x = CurrentCell.first, y = CurrentCell.second;
  41.         std::string s;
  42.         std::stringstream out;
  43.         out << i;
  44.         s = out.str();
  45.         Map[x][y]=s[0];
  46.         // NeighboursToVisit is an vector<pair<Uint, Uint> >
  47.         NeighboursToVisit.clear();
  48.         NeighboursToVisit = Find_neighbours(CurrentCell);
  49.         RandElement = rand()%NeighboursToVisit.size();
  50.         if (!NeighboursToVisit.empty()){
  51.             // Shuffle the order to make it random
  52.             //std::random_shuffle ( NeighboursToVisit.begin(), NeighboursToVisit.end() );
  53.             // Keep track of the direction
  54.             Direction = Check_direction(x, y, NeighboursToVisit);
  55.             // Knock down the wall between this random cell and CurrentCell
  56.             Crush_wall(NeighboursToVisit[RandElement], Direction[RandElement]);
  57.             // Push CurrentCell to the cellStack
  58.             CellStack.push(std::make_pair(CurrentCell.first, CurrentCell.second));
  59.             CurrentCell = std::make_pair(NeighboursToVisit[RandElement].first, NeighboursToVisit[RandElement].second);
  60.             VisitedCells++;
  61.         }
  62.         else{
  63.             if(!CellStack.empty()){
  64.                 CurrentCell = CellStack.top();
  65.                 CellStack.pop();
  66.             }
  67.         }
  68.     }  
  69. }
  70. // x, y is the previous value of CurrentCell
  71. std::vector<Uint> Maze::Check_direction(Uint x, Uint y, std::vector<std::pair <Uint, Uint> > v){
  72.     std::vector<Uint> dir;
  73.     int dx, dy;
  74.     dir.clear();
  75.     for (int i = 0; i < v.size(); i++)
  76.     {
  77.         dx = (v[i].first - x); // Computing the direction
  78.         dy = (v[i].second - y); // Computing the direction
  79.         std::cout << dx << " " << dy << std::endl;
  80.  
  81.         if (dx == 2)            // If east
  82.             dir.push_back(1); // Push 1 (east by the enumeration)
  83.         else if (dx == -2)  // if west
  84.             dir.push_back(3); // Push 3 (west by the enumeration)
  85.         else if (dy == -2)  // if north
  86.             dir.push_back(0); // push 0 (north by the enumeration)
  87.         else if (dy == 2)   // if south
  88.             dir.push_back(2); // push 2 (south by the enumeration)
  89.         std::cout << dir.back() << std::endl;
  90.     }
  91.     return dir;
  92. }
  93.  
  94. std::vector< std::pair<Uint, Uint> > Maze::Find_neighbours(std::pair<Uint, Uint> CurrentCell){
  95.     std::vector<std::pair <Uint, Uint> > v;
  96.     CurrentCell.first+=2;   // East of original Cell
  97.     if (Walls_Intact(CurrentCell))
  98.         v.push_back(CurrentCell);
  99.  
  100.     CurrentCell.first-=4;   // West of original cell
  101.     if (Walls_Intact(CurrentCell))
  102.         v.push_back(CurrentCell);
  103.  
  104.     CurrentCell.first+=2;   // Back to original cell
  105.     CurrentCell.second+=2;  // South of original cell
  106.     if (Walls_Intact(CurrentCell))
  107.         v.push_back(CurrentCell);
  108.  
  109.     CurrentCell.second-=4;  // North of original cell
  110.     if (Walls_Intact(CurrentCell))
  111.         v.push_back(CurrentCell);
  112.  
  113.     //CurrentCell.second-=2;    // Back to original cell
  114.     return v;
  115. }
  116. bool Maze::Walls_Intact(std::pair<Uint, Uint> CurrentCell){ // Returns wether all walls intact or not
  117.     int row = CurrentCell.first;
  118.     int col = CurrentCell.second;
  119.     if (col-1 >= Map.size() || row-1 >= Map[col-1].size())
  120.         return false;
  121.     if(col < Map.size() && row < Map[row].size()){ 
  122.         if(
  123.             (Map[row+1][col] == WALL) &&    // check east wall
  124.             (Map[row-1][col] == WALL) &&    // check west wall
  125.             (Map[row][col-1] == WALL) &&    // check north wall
  126.             (Map[row][col+1] == WALL))      // check south wall
  127.         {
  128.             return true;
  129.         }else{
  130.             return false;
  131.         }
  132.         return false;
  133.     }
  134.     return false;
  135. }
  136.  
  137.  
  138. void Maze::Crush_wall(std::pair<Uint, Uint> CurrentCell, int dir){
  139.     if (dir == north){
  140.         Map[CurrentCell.first][CurrentCell.second+1] = 'w';
  141.     }
  142.     else if (dir == east){
  143.         Map[CurrentCell.first-1][CurrentCell.second] = 's';
  144.     }
  145.     else if (dir == south){
  146.         Map[CurrentCell.first][CurrentCell.second-1] = 'e';
  147.     }
  148.     else if (dir == west){
  149.         Map[CurrentCell.first+1][CurrentCell.second] = 'n';
  150.     }
  151. }
  152.  
  153.  
  154. void Maze::Print_maze() const{
  155.     for (int i = 0; i < Map.size(); i++)
  156.     {
  157.         for (int j = 0; j < Map[i].size(); j++)
  158.         {
  159.             std::cout << Map[i][j];
  160.         }
  161.         std::cout << std::endl;
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment