Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "PointList.h"
- class Maze {
- public:
- boolean maze[4][4][4];
- boolean bmap[8][8];
- byte bitmap[8];
- Maze(int w, int h) {
- generateNewMaze();
- }
- boolean checkUsed(boolean cell[]) {
- for (int i = 0; i < 4; i++) {
- if (!cell[i]) {
- return true;
- }
- }
- return false;
- }
- boolean checkRange(int a, int b) {
- if (a < 0 || a > 3 || b < 0 || b > 3) {
- return false;
- }
- return true;
- }
- void generateNewMaze(){
- for (int a = 0; a < 4; a++) { //initialize maze array
- for (int b = 0; b < 4; b++) {
- for (int c = 0; c < 4; c++) {
- maze[a][b][c] = true;
- }
- }
- }
- int refTab[4][2] = {
- {0, -1},
- {1, 0},
- {0, 1},
- {-1, 0}
- };
- PointList l = PointList(); //generate the actual maze
- l.add(new int[2] {0, 0});
- while (l.len > 0) {
- int r = random(0, l.len);
- int c = 0;
- boolean cells[] = {false, false, false, false};
- for (int i = 0; i < 4; i++) {
- 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 &&
- cells[i] = true;
- c++;
- }
- }
- if (c == 0) {
- l.rem(r);
- } else {
- int choice = random(0, c);
- int spin = -1;
- for (int i = 0; i < 4; i++) {
- if (cells[i]) {
- spin++;
- }
- if (spin == choice) {
- maze[l.list[r][0]][l.list[r][1]][i] = false;
- maze[l.list[r][0]+refTab[i][0]][l.list[r][1]+refTab[i][1]][(i+2)%4] = false;
- if (!(l.list[r][0]+refTab[i][0] == 3 && l.list[r][1]+refTab[i][1] == 3)) {
- l.add(new int[2] {l.list[r][0]+refTab[i][0], l.list[r][1]+refTab[i][1]});
- }
- break;
- }
- }
- }
- }
- for (int x = 0; x < 8; x++) { //convert the maze to a boolean map
- for (int y = 0; y < 8; y++) {
- bmap[x][y] = false;
- if (x==0 || y==0 || (x%2==0 && y%2==0)) {
- bmap[x][y] = true;
- } else if ((x+y)%2 == 1) {
- if (y%2 == 1) {
- bmap[x][y] = maze[(x/2)-1][(y-1)/2][1];
- } else if (x%2 == 1) {
- bmap[x][y] = maze[(x-1)/2][(y/2)-1][2];
- }
- }
- }
- }
- for (int x = 0; x < 8; x++) { //convert boolean map to bitmap
- bitmap[x] = B00000000;
- for (int y = 0; y < 8; y++) {
- if (bmap[x][y]) {
- bitSet(bitmap[x], y);
- }
- }
- }
- }
- };
Add Comment
Please, Sign In to add comment