SHARE
TWEET

Untitled

a guest Nov 19th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Your code here! */
  2. #include "maze.h"
  3.  
  4. SquareMaze::SquareMaze() {}
  5.  
  6. void SquareMaze::makeMaze(int width, int height) {
  7.     bottomWall = new std::vector<std::vector<bool>*>(height, new std::vector<bool>(width, true));
  8.     rightWall = new std::vector<std::vector<bool>*>(height, new std::vector<bool>(width, true));
  9.  
  10.     DisjointSets *djs = new DisjointSets();
  11.     djs -> addelements(width * height);
  12.  
  13.     // iterating until no more walls can be deleted
  14.     while (true) {
  15.         std::set<int> *visited_indexes = new std::set<int>();
  16.         // finding a wall does doesn't create a cycle
  17.         while (true) {
  18.             int dir = rand() % 1;
  19.             bool down = dir == 1;
  20.  
  21.             int index;
  22.             do {
  23.                 index = rand() % (width * height);
  24.             } while (visited_indexes -> count(index));
  25.             visited_indexes -> insert(index);
  26.             int row = index / width;
  27.             int col = index % width;
  28.             int index_set = djs -> find(index);
  29.  
  30.             if (down) {
  31.                 // check that it is not last row
  32.                 if (row != height - 1) {
  33.                     int down_index_set = djs -> find(index + width);
  34.                     if (index_set != down_index_set) {
  35.                         // delete bottom wall
  36.                         bottomWall -> at(row) -> at(col) = false;
  37.                         break;
  38.                     }
  39.                 }
  40.             } else {
  41.                 // check that it is not last col
  42.                 if (col != width - 1) {
  43.                     int right_index_set = djs -> find(index + 1); // check overflow case
  44.                     if (index_set != right_index_set) {
  45.                         rightWall -> at(row) -> at(col) = false;
  46.                         break;
  47.                     }
  48.                 }
  49.             }
  50.  
  51.             if ((int) visited_indexes -> size() == width * height) {
  52.                 break;
  53.             }
  54.         }
  55.  
  56.         if ((int) visited_indexes -> size() == width * height) {
  57.             break;
  58.         }
  59.     }
  60. }
  61.  
  62. bool SquareMaze::canTravel(int x, int y, int dir) const {
  63.   if (dir == 0) {
  64.     if (x == width - 1) {
  65.       return false;
  66.     } else if (rightWall->at(x)->at(y) == false) {
  67.       return true;
  68.     } else {
  69.       return false;
  70.     }
  71.   } else if (dir == 1) {
  72.     if (y == height - 1) {
  73.       return false;
  74.     } else if (rightWall->at(x)->at(y) == false) {
  75.       return true;
  76.     } else {
  77.       return false;
  78.     }
  79.   } else if (dir == 2) {
  80.     if (x == 0) {
  81.       return false;
  82.     } else if (rightWall->at(x-1)->at(y) == false) {
  83.       return true;
  84.     } else {
  85.       return false;
  86.     }
  87.   } else if (dir == 3) {
  88.     if (y == 0) {
  89.       return false;
  90.     } else if (rightWall->at(x)->at(y-1) == false) {
  91.       return true;
  92.     } else {
  93.       return false;
  94.     }
  95.   } else {
  96.     return false;
  97.   }
  98. }
  99. void SquareMaze::setWall(int x, int y, int dir, bool exists) {
  100.   if (dir == 0) {
  101.     if (exists) {
  102.       rightWall->at(x)->at(y) = true;
  103.     } else {
  104.       rightWall->at(x)->at(y) = false;
  105.     }
  106.   } else {
  107.     if (exists) {
  108.       bottomWall->at(x)->at(y) = true;
  109.     } else {
  110.       bottomWall->at(x)->at(y) = false;
  111.     }
  112.   }
  113. }
  114.  
  115. std::vector<int> SquareMaze::solveMaze() {
  116.   vector<int> blah;
  117.   blah.push_back(0);
  118.   return blah;
  119. }
  120.  
  121. cs225::PNG* SquareMaze::drawMaze() const {
  122.   cs225::PNG* maze = new cs225::PNG(width*10 + 1, height*10 + 1);
  123.   cs225::HSLAPixel black(0,0,0);
  124.   // shade in all of left and top
  125.   (*maze).getPixel(0,0) = black;
  126.  
  127.   for (int i = 10; i < width*10 + 1; i++) {
  128.     (*maze).getPixel(i,0) = black;
  129.   }
  130.  
  131.   for (int j = 0; j < width*10 + 1; j++) {
  132.     (*maze).getPixel(0,j) = black;
  133.   }
  134.  
  135.   for (int i = 0; i < width; i++) {
  136.     for (int j = 0; j < height; i++) {
  137.       if (!canTravel(i,j,0)) {
  138.         for (int k = 0; k < 11; k++) {
  139.           (*maze).getPixel(10*i + 10, 10*j + k) = black;
  140.         }
  141.       }
  142.       if (!canTravel(i,j,1)) {
  143.         for (int k = 0; k < 11; k++) {
  144.           (*maze).getPixel(10*i + k, 10*j + 10) = black;
  145.         }
  146.       }
  147.     }
  148.   }
  149.   return maze;
  150. }
  151. cs225::PNG* SquareMaze::drawMazeWithSolution() {
  152.  return NULL;
  153. }
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