CaptainSpaceCat

Maze.h

Apr 2nd, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. #include "PointList.h"
  2.  
  3. class Maze {
  4.    
  5.     public:
  6.       boolean maze[4][4][4];
  7.     boolean bmap[8][8];
  8.       byte bitmap[8];
  9.    
  10.       Maze(int w, int h) {
  11.         generateNewMaze();
  12.       }
  13.  
  14.  
  15.     boolean checkUsed(boolean cell[]) {
  16.         for (int i = 0; i < 4; i++) {
  17.           if (!cell[i]) {
  18.             return true;
  19.           }
  20.         }
  21.         return false;
  22.     }
  23.  
  24.     boolean checkRange(int a, int b) {
  25.       if (a < 0 || a > 3 || b < 0 || b > 3) {
  26.         return false;
  27.       }
  28.       return true;
  29.     }
  30.      
  31.       void generateNewMaze(){
  32.           for (int a = 0; a < 4; a++) { //initialize maze array
  33.         for (int b = 0; b < 4; b++) {
  34.           for (int c = 0; c < 4; c++) {
  35.             maze[a][b][c] = true;
  36.           }
  37.         }
  38.       }
  39.      
  40.       int refTab[4][2] = {
  41.         {0, -1},
  42.         {1, 0},
  43.         {0, 1},
  44.         {-1, 0}
  45.       };
  46.       PointList l = PointList(); //generate the actual maze
  47.       l.add(new int[2] {0, 0});
  48.       while (l.len > 0) {
  49.         int r = random(0, l.len);
  50.         int c = 0;
  51.         boolean cells[] = {false, false, false, false};
  52.        
  53.         for (int i = 0; i < 4; i++) {
  54.           if (checkRange(l.list[r][0]+refTab[i][0], l.list[r][1]+refTab[i][1]) && !checkUsed(maze[l.list[r][0]+refTab[i][0]][l.list[r][1]+refTab[i][1]])) { //l.list[r][0]+refTab[i][0] > 0 && l.list[r][0]+refTab[i][0] < 4 && l.list[r][1]+refTab[i][1] > 0 && l.list[r][0]+refTab[i][0] < 4 &&
  55.             cells[i] = true;
  56.             c++;
  57.           }
  58.         }
  59.         if (c == 0) {
  60.           l.rem(r);
  61.         } else {
  62.           int choice = random(0, c);
  63.          
  64.           int spin = -1;
  65.           for (int i = 0; i < 4; i++) {
  66.             if (cells[i]) {
  67.               spin++;
  68.             }
  69.             if (spin == choice) {
  70.               maze[l.list[r][0]][l.list[r][1]][i] = false;
  71.               maze[l.list[r][0]+refTab[i][0]][l.list[r][1]+refTab[i][1]][(i+2)%4] = false;
  72.               if (!(l.list[r][0]+refTab[i][0] == 3 && l.list[r][1]+refTab[i][1] == 3)) {
  73.                 l.add(new int[2] {l.list[r][0]+refTab[i][0], l.list[r][1]+refTab[i][1]});
  74.               }
  75.               break;
  76.             }
  77.           }
  78.         }
  79.       }
  80.      
  81.  
  82.       for (int x = 0; x < 8; x++) { //convert the maze to a boolean map
  83.         for (int y = 0; y < 8; y++) {
  84.           bmap[x][y] = false;
  85.           if (x==0 || y==0 || (x%2==0 && y%2==0)) {
  86.             bmap[x][y] = true;
  87.           } else if ((x+y)%2 == 1) {
  88.             if (y%2 == 1) {
  89.               bmap[x][y] = maze[(x/2)-1][(y-1)/2][1];
  90.             } else if (x%2 == 1) {
  91.               bmap[x][y] = maze[(x-1)/2][(y/2)-1][2];
  92.             }
  93.           }
  94.         }
  95.       }
  96.      
  97.           for (int x = 0; x < 8; x++) { //convert boolean map to bitmap
  98.         bitmap[x] = B00000000;
  99.         for (int y = 0; y < 8; y++) {
  100.                 if (bmap[x][y]) {
  101.             bitSet(bitmap[x], y);
  102.                 }
  103.         }
  104.           }
  105.       }
  106. };
Add Comment
Please, Sign In to add comment