Creating A Random Maze - Step 2

◀ Creating A Random Maze - Step 1▶ Creating A Random Maze - Step 3
Amazon Let’s apply the 2nd step of the Four-Step Programming Model to generate a random maze!

Four Step Programming Model: Step 2
As we already discussed, we need to keep track of attributes of each cell somehow. I use vector to store them. I need double to store the horizontal and vertical offsets when displaying the maze.

Next, I need to store the width, height, and number of cells in int variables; I need to store the names of the files in char*; I need 2 vector: one stores the solution to the maze and the other stores the available cells to randomly pick while knocking down walls.


Finally, I probably need an int as an index for a loop. Given these decisions, I update my skeleton as follows:
void remove(int);
void findSol(int, int);
void displaySol();

class Maze{
public:
Maze(int);
int find_root(int);	
	void union_cell(int, int);

vector<int> s;  /* parent's id */
	vector<double> xcoord;  /* right down coordinate x */
	vector<double> ycoord;  /* right down coordinate y */
/* for the next two vectors, -1 means down, 0 means must be up 1 means up */ vector<int> down; /* lower wall; knocked down or there */ vector<int> right; /* right wall; knocked down or there */ vector<int> visited; /* 1 means visited and 0 means not visited yet while finding solution */ }; /* precondition: n must be a positive integer postcondition: s, xcoord, ycoord, down, right, visited are assigned values */ Maze::Maze(int n){ - same } /*
precondition: n must be >= 0 and < s.size() postcondition: return the root of n */ int Maze::find_root(int n){ - same } /* precondition: root1 and root2 both must be >= 0 and < s.size() postcondition: root1 and root2 belong to the same set */ void Maze::union_cell(int root1, int root2){ - same } int main(int argc, char** argv){ - exit if something is missing in the command line double xOffset, yOffset;
int width, height, numOfCells, i; char *file, *file2; ofstream fout, fout2; vector<int> lottery, sol; - exit if the user provides unacceptable information - initialize the random number generator - store critical data in variables - push all elements into a vector except the last one because it has no walls to knock down - use a while loop to construct the maze - inside the while loop randomly choose a cell deal with the case when the cell has 2 neighbors deal with the case when the cell has only 1 neighbor
- generate code for Matlab to display the maze - find and put solution code in the solution file } /* precondition: victim should, but not must, be an element in the vector that holds all cells postcondition: victim is erased from the vector */ void remove(int victim){ - same } /* precondition: from and to must be >= 0 and < numOfCells postcondition: find the solution */ void findSol(int from, int to){ - same }
/* precondition: none postcondition: put code for displaying the maze in file2 */ void displaySol(){ - same }
Let's look at the next step!
◀ Creating A Random Maze - Step 1▶ Creating A Random Maze - Step 3

fShare
Questions? Let me know!