1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
class theMapClass{
public:
theMapClass(){//constructor
srand(time(0));
}//ends constructor
char themaze[25][25]; //must be odd in each dimension
vector<int> x;
vector<int> y;
void generateTheMaze(){
int startRowroll = 1+rand()%25;
int startColroll = 1+rand()%25;
while(startRowroll%2 == 1){ //the starting numbers must be odd for it to work right
startRowroll = 1+rand()%25;
}
while(startColroll%2 == 1){
startColroll = 1+rand()%25;
}
int r = startRowroll;
int c = startColroll;
/////delete later
r = 13;
c = 13;
/////////////////
x.push_back(r);
y.push_back(c);
themaze[r][c] = ' ';
while(x.size() != 0 || y.size() != 0){
r = x.back();
c = y.back();
recursion(r, c, themaze);
x.pop_back();
y.pop_back();
}//ends while loop
}//ends maze generation
void printTheMaze(){
for(int r=0; r<25; r++){
for(int c=0; c<25; c++){
cout << themaze[r][c] << " ";
}
cout << endl;
}
}
void makeTheWallsForTheMaze(){
for(int r=0; r<25; r++){
for(int c=0; c<25; c++){ //making a 25 by 25 "# wall" grid
themaze[r][c] = '#';
}
}
}
void recursion(int row, int column, char maze[25][25]){
// 1 is up, 2 is left, 3 is down, 4 is right //
int r1, r2, r3, r4;
r1 = 1+rand()%4;
if(r1 == 1){
if(maze[row-2][column] == '#'){
maze[row-1][column] = ' ';
maze[row-2][column] = ' ';
row -= 2;
x.push_back(row);
y.push_back(column);
recursion(row, column, maze);
}
}//ends 1 or up
if(r1 == 2){
if(maze[row][column-2] == '#'){
maze[row][column-1] = ' ';
maze[row][column-2] = ' ';
column -= 2;
x.push_back(row);
y.push_back(column);
recursion(row, column, maze);
}
}//ends 2 or left
if(r1 == 3){
if(maze[row+2][column] == '#'){
maze[row+1][column] = ' ';
maze[row+2][column] = ' ';
row += 2;
x.push_back(row);
y.push_back(column);
recursion(row, column, maze);
}
}//ends 3 or down
if(r1 == 4){
if(maze[row][column+2] == '#'){
maze[row][column+1] = ' ';
maze[row][column+2] = ' ';
column += 2;
x.push_back(row);
y.push_back(column);
recursion(row, column, maze);
}
}//ends 4 or right
}//ends recursion
void MAZECREAION(){
makeTheWallsForTheMaze();
generateTheMaze();
printTheMaze();
}
};
int main(){
theMapClass mapobject;
mapobject.MAZECREAION();
return 0;
}
|