Advertisement
Guest User

Untitled

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