Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Your code here! */
- #include "maze.h"
- SquareMaze::SquareMaze() {}
- void SquareMaze::makeMaze(int width, int height) {
- bottomWall = new std::vector<std::vector<bool>*>(height, new std::vector<bool>(width, true));
- rightWall = new std::vector<std::vector<bool>*>(height, new std::vector<bool>(width, true));
- DisjointSets *djs = new DisjointSets();
- djs -> addelements(width * height);
- // iterating until no more walls can be deleted
- while (true) {
- std::set<int> *visited_indexes = new std::set<int>();
- // finding a wall does doesn't create a cycle
- while (true) {
- int dir = rand() % 1;
- bool down = dir == 1;
- int index;
- do {
- index = rand() % (width * height);
- } while (visited_indexes -> count(index));
- visited_indexes -> insert(index);
- int row = index / width;
- int col = index % width;
- int index_set = djs -> find(index);
- if (down) {
- // check that it is not last row
- if (row != height - 1) {
- int down_index_set = djs -> find(index + width);
- if (index_set != down_index_set) {
- // delete bottom wall
- bottomWall -> at(row) -> at(col) = false;
- break;
- }
- }
- } else {
- // check that it is not last col
- if (col != width - 1) {
- int right_index_set = djs -> find(index + 1); // check overflow case
- if (index_set != right_index_set) {
- rightWall -> at(row) -> at(col) = false;
- break;
- }
- }
- }
- if ((int) visited_indexes -> size() == width * height) {
- break;
- }
- }
- if ((int) visited_indexes -> size() == width * height) {
- break;
- }
- }
- }
- bool SquareMaze::canTravel(int x, int y, int dir) const {
- if (dir == 0) {
- if (x == width - 1) {
- return false;
- } else if (rightWall->at(x)->at(y) == false) {
- return true;
- } else {
- return false;
- }
- } else if (dir == 1) {
- if (y == height - 1) {
- return false;
- } else if (rightWall->at(x)->at(y) == false) {
- return true;
- } else {
- return false;
- }
- } else if (dir == 2) {
- if (x == 0) {
- return false;
- } else if (rightWall->at(x-1)->at(y) == false) {
- return true;
- } else {
- return false;
- }
- } else if (dir == 3) {
- if (y == 0) {
- return false;
- } else if (rightWall->at(x)->at(y-1) == false) {
- return true;
- } else {
- return false;
- }
- } else {
- return false;
- }
- }
- void SquareMaze::setWall(int x, int y, int dir, bool exists) {
- if (dir == 0) {
- if (exists) {
- rightWall->at(x)->at(y) = true;
- } else {
- rightWall->at(x)->at(y) = false;
- }
- } else {
- if (exists) {
- bottomWall->at(x)->at(y) = true;
- } else {
- bottomWall->at(x)->at(y) = false;
- }
- }
- }
- std::vector<int> SquareMaze::solveMaze() {
- vector<int> blah;
- blah.push_back(0);
- return blah;
- }
- cs225::PNG* SquareMaze::drawMaze() const {
- cs225::PNG* maze = new cs225::PNG(width*10 + 1, height*10 + 1);
- cs225::HSLAPixel black(0,0,0);
- // shade in all of left and top
- (*maze).getPixel(0,0) = black;
- for (int i = 10; i < width*10 + 1; i++) {
- (*maze).getPixel(i,0) = black;
- }
- for (int j = 0; j < width*10 + 1; j++) {
- (*maze).getPixel(0,j) = black;
- }
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < height; i++) {
- if (!canTravel(i,j,0)) {
- for (int k = 0; k < 11; k++) {
- (*maze).getPixel(10*i + 10, 10*j + k) = black;
- }
- }
- if (!canTravel(i,j,1)) {
- for (int k = 0; k < 11; k++) {
- (*maze).getPixel(10*i + k, 10*j + 10) = black;
- }
- }
- }
- }
- return maze;
- }
- cs225::PNG* SquareMaze::drawMazeWithSolution() {
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement