step search(void){ push(start); getVisited(start) = 1; while (queueIsNotEmpty){ step curr = pop(); if (isEnd(curr)) return curr; int i, j; for (i = 0; i < MOVENUM; i++){ step next = curr; next.parent = qs-1; // popped index next.move = i+1; for (j = 0; j < CLOCKNUM; j++){ next.clocks[j] += moves[i][j]; next.clocks[j] %= STATENUM; } if (!getVisited(next)){ push(next); getVisited(next) = 1; } } } }
BMP MazeGen::Generate() { // Create a vertex with coordinates within (1, 1) and (Width-1, Height-1), // and set it's visited member to true. Vertex current; current.x = (rand() % (Width / 2)) * 2 + 1; current.y = (rand() % (Height/ 2)) * 2 + 1; setVisited(current); // Push the starting cell to the stack stk.push(current); while(!AllCellsVisited()) { // Get all neighbors of the current cell std::vector<Vertex> neighbors = GetNeighbors(current); // Get a random unvisited neighbor of the current cell, or discover that there are no unvisited neighbors int randomNeighbor = rand() % neighbors.size(); // While neighbors[randomNeighbor] is a cell that has been visited already: while( getVisited(mazeArray[neighbors[randomNeighbor].x][neighbors[randomNeighbor].y]) == true) { // Remove that random neighbor from the vector of neighbors neighbors.erase(neighbors.begin() + randomNeighbor); // If there are no neighbors left, break the loop if(neighbors.empty()) break; // Pick a new random neighbor randomNeighbor = rand() % neighbors.size(); } // If there are no unvisited neighbors of the current cell if(neighbors.empty()) { // Go back a step, popping the last cell off the stack and setting it to the current cell, // then repeating the whole process current = stk.top(); stk.pop(); } // If there was an unvisited neighbor of of the current cell else { // Remove the 'wall' between the current cell and it's neighbor, drawing a line between the two cells RemoveWall(current, neighbors[randomNeighbor]); // Set the current vertex to the randomly selected neighbor current = neighbors[randomNeighbor]; // Mark it as visted setVisited(mazeArray[current.x][current.y]); // Push it to the stack stk.push(current); } } return maze; }