Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1. #ifndef MAZEGEN_H
  2. #define MAZEGEN_H
  3.  
  4. #include "mazegen.h"
  5.  
  6. #include <stdlib.h>
  7. #include <iostream>
  8. #include <ctime>
  9. #include <time.h>
  10. //#include <windows.h>
  11. //#include <conio.h>
  12. #include <stack>
  13. #include <fstream>
  14. //#include "stdafx.h" //http://sourceforge.net/p/wpbdc/website/ci/master/tree/Judge/StdAfx.h
  15.  
  16. using namespace std;
  17.  
  18. #define MSIZE 11
  19.  
  20. void ClearScreen(){
  21. printf("\033[2J\033[1;1H"); // clear screen
  22. }
  23.  
  24. // CELL STRUCTURE
  25. struct Cell
  26. {
  27. bool visited;
  28. bool top_wall;
  29. bool bot_wall;
  30. bool left_wall;
  31. bool right_wall;
  32. char display;
  33. };
  34.  
  35. // INITIALIZE MAZE
  36. void Initialize(Cell Level[][MSIZE]) {
  37. for(int i=0; i<MSIZE; i++) {
  38. for(int j=0; j<MSIZE; j++) {
  39. Level[i][j].display = '*';
  40. Level[i][j].visited = false;
  41. Level[i][j].top_wall = true;
  42. Level[i][j].bot_wall = true;
  43. Level[i][j].left_wall = true;
  44. Level[i][j].right_wall = true;
  45. }
  46. }
  47. for(int i=1; i<MSIZE-1; i++) {
  48. for(int j=1; j<MSIZE-1; j++) {
  49. // Border Cells have fewer accessible walls
  50. Level[1][j].top_wall = false;
  51. Level[MSIZE-2][j].bot_wall = false;
  52. Level[i][1].left_wall = false;
  53. Level[i][MSIZE-2].right_wall = false;
  54. }
  55. }
  56. }
  57.  
  58. // REDRAW MAZE
  59. void Redraw(Cell Level[][MSIZE]) {
  60. for(int i=0; i<MSIZE; i++) {
  61. cout << endl;
  62. for(int j=0; j< MSIZE; j++)
  63. cout << " " << Level[i][j].display;
  64. }
  65. }
  66.  
  67. // GENERATE MAZE
  68. void GenerateMaze(Cell Level[][MSIZE], int &posX, int &posY, int &goalX, int &goalY) {
  69. srand((unsigned)time(NULL)); // Pick random start cell
  70. int random = 0;
  71. int randomX = ((2*rand())+1)%(MSIZE-1); // Generate a random odd number between 1 and MSIZE
  72. int randomY = ((2*rand())+1)%(MSIZE-1); // Generate a random odd number between 1 and MSIZE
  73. posX = randomX; // Save player's initial X position
  74. posY = randomY; // Save player's initial Y position
  75. int visitedCells = 1;
  76. int totalCells = ((MSIZE-1)/2)*((MSIZE-1)/2);
  77. int percent = 0;
  78. stack<int> back_trackX, back_trackY; // Stack is used to trace the reverse path
  79.  
  80. Level[randomY][randomX].display = 'S'; // Set S as the start cell
  81. Level[randomY][randomX].visited = true; // Set start cell as visited;
  82.  
  83. while(visitedCells < totalCells)
  84. {
  85. if(((Level[randomY-2][randomX].visited == false) && (Level[randomY][randomX].top_wall == true && Level[randomY-2][randomX].bot_wall == true)) ||
  86. ((Level[randomY+2][randomX].visited == false) && (Level[randomY][randomX].bot_wall == true && Level[randomY+2][randomX].top_wall == true)) ||
  87. ((Level[randomY][randomX-2].visited == false) && (Level[randomY][randomX].left_wall == true && Level[randomY][randomX-2].right_wall == true)) ||
  88. ((Level[randomY][randomX+2].visited == false) && (Level[randomY][randomX].right_wall == true && Level[randomY][randomX+2].left_wall == true)))
  89. {
  90. random = (rand() % 4) + 1; // Pick a random wall 1-4 to knock down
  91.  
  92. // GO UP
  93. if((random == 1) && (randomY > 1)) {
  94. if(Level[randomY-2][randomX].visited == false) { // If not visited
  95. Level[randomY-1][randomX].display = ' '; // Delete display
  96. Level[randomY-1][randomX].visited = true; // Mark cell as visited
  97. Level[randomY][randomX].top_wall = false; // Knock down wall
  98.  
  99. back_trackX.push(randomX); // Push X for back track
  100. back_trackY.push(randomY); // Push Y for back track
  101.  
  102. randomY -= 2; // Move to next cell
  103. Level[randomY][randomX].visited = true; // Mark cell moved to as visited
  104. Level[randomY][randomX].display = ' '; // Update path
  105. Level[randomY][randomX].bot_wall = false; // Knock down wall
  106. visitedCells++; // Increase visitedCells counter
  107. }
  108. else
  109. continue;
  110. }
  111.  
  112. // GO DOWN
  113. else if((random == 2) && (randomY < MSIZE-2)) {
  114. if(Level[randomY+2][randomX].visited == false) { // If not visited
  115. Level[randomY+1][randomX].display = ' '; // Delete display
  116. Level[randomY+1][randomX].visited = true; // Mark cell as visited
  117. Level[randomY][randomX].bot_wall = false; // Knock down wall
  118.  
  119. back_trackX.push(randomX); // Push X for back track
  120. back_trackY.push(randomY); // Push Y for back track
  121.  
  122. randomY += 2; // Move to next cell
  123. Level[randomY][randomX].visited = true; // Mark cell moved to as visited
  124. Level[randomY][randomX].display = ' '; // Update path
  125. Level[randomY][randomX].top_wall = false; // Knock down wall
  126. visitedCells++; // Increase visitedCells counter
  127. }
  128. else
  129. continue;
  130. }
  131.  
  132. // GO LEFT
  133. else if((random == 3) && (randomX > 1)) {
  134. if(Level[randomY][randomX-2].visited == false) { // If not visited
  135. Level[randomY][randomX-1].display = ' '; // Delete display
  136. Level[randomY][randomX-1].visited = true; // Mark cell as visited
  137. Level[randomY][randomX].left_wall = false; // Knock down wall
  138.  
  139. back_trackX.push(randomX); // Push X for back track
  140. back_trackY.push(randomY); // Push Y for back track
  141.  
  142. randomX -= 2; // Move to next cell
  143. Level[randomY][randomX].visited = true; // Mark cell moved to as visited
  144. Level[randomY][randomX].display = ' '; // Update path
  145. Level[randomY][randomX].right_wall = false; // Knock down wall
  146. visitedCells++; // Increase visitedCells counter
  147. }
  148. else
  149. continue;
  150. }
  151.  
  152. // GO RIGHT
  153. else if((random == 4) && (randomX < MSIZE-2)) {
  154. if(Level[randomY][randomX+2].visited == false) { // If not visited
  155. Level[randomY][randomX+1].display = ' '; // Delete display
  156. Level[randomY][randomX+1].visited = true; // Mark cell as visited
  157. Level[randomY][randomX].right_wall = false; // Knock down wall
  158.  
  159. back_trackX.push(randomX); // Push X for back track
  160. back_trackY.push(randomY); // Push Y for back track
  161.  
  162. randomX += 2; // Move to next cell
  163. Level[randomY][randomX].visited = true; // Mark cell moved to as visited
  164. Level[randomY][randomX].display = ' '; // Update path
  165. Level[randomY][randomX].left_wall = false; // Knock down wall
  166. visitedCells++; // Increase visitedCells counter
  167. }
  168. else
  169. continue;
  170. }
  171.  
  172. percent = (visitedCells*100/totalCells*100)/100; // Progress in percentage
  173. cout << endl << " Generating a Random Maze... " << percent << "%" << endl;
  174. }
  175. else {
  176. randomX = back_trackX.top();
  177. back_trackX.pop();
  178.  
  179. randomY = back_trackY.top();
  180. back_trackY.pop();
  181. }
  182.  
  183. ClearScreen();
  184. Redraw(Level);
  185. }
  186.  
  187. goalX = randomX;
  188. goalY = randomY;
  189. Level[goalY][goalX].display = 'E';
  190. ClearScreen();
  191. Redraw(Level);
  192. cout << endl << "\a\t Complete!" << endl;
  193. }
  194.  
  195. #endif // MAZEGEN_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement