maze

.cpp

School

Stanford University *

*We aren’t endorsed by this school

Course

120

Subject

Computer Science

Date

Oct 30, 2023

Type

cpp

Pages

8

Uploaded by JusticeKouprey15684

Report
/* Your code here! */ #include "maze.h" #include <random> #include <stack> #include <queue> #include <vector> #include <algorithm> #include <iostream> SquareMaze::SquareMaze() { std::random_device rd; // Taken from cpp reference gen = std::mt19937(rd()); // mersenne_twister eneigne seeding with rd() mazeHeight =0; mazeWidth = 0; size_ = 0; return; } void SquareMaze::makeMaze(int width, int height) { // Width is Rows, each corresponding Columns has pair of right down walls boolean // First Bool is right, Second is Down // if maze dimension does not match given, resize and make all wall true // if (mazeHeight!= height || mazeWidth!= width) // { mazeHeight = height; mazeWidth = width; size_ = mazeHeight * mazeWidth; cellData.addelements((height*width) ); // All height*width index 1 to height*width, avoiding 0 // row# * TotalWidth + col# wallData.resize(width, std::vector<std::pair<bool, bool>>(height)); for(int i = 0; i < width; i++) { for(int j = 0; j< height; j++) { // Mark both Right & Down Wall True wallData[j][i] = std::make_pair(true, true); } } std::uniform_int_distribution<int> xGen(0, width-1); // all possible x std::uniform_int_distribution<int> yGen(0, height-1);// all possible y std::uniform_int_distribution<int> wallGen(0,1); // While the beginning cell has no cycle // int beginX = xGen(gen); // int beginY = yGen(gen); int currentIndex = 0; int checkIndex = 0; while(cellData.size(0) != (height*width) ){ int currentX =xGen(gen); int currentY = yGen(gen); int removeWall = wallGen(gen); switch(removeWall) { case 0: // Want to remove the right wall
// Check if Perimeter if(currentX >= width-1) { break; } else if(wallData[currentY][currentX].first == true && currentX <width-1) { // hasNoCycle between Current, and Right Cell currentIndex = calculateIndex(currentX, currentY); checkIndex = calculateIndex(currentX+1, currentY); if(hasNoCycle(currentIndex, checkIndex)==true) { wallData[currentY][currentX].first = false; // mark Right Wall as False cellData.setunion(currentIndex, checkIndex); } } break; case 1: // Want to remove the Down Wall if(currentY >= height-1) { break; } else if(wallData[currentY][currentX].second == true && currentY < height-1) { currentIndex = calculateIndex(currentX, currentY); checkIndex = calculateIndex(currentX, currentY+1); if(hasNoCycle(currentIndex, checkIndex)==true) { wallData[currentY][currentX].second = false; cellData.setunion(currentIndex, checkIndex); } } break; } } return; // int xRandom = xGen(gen); // int yRandom = yGen(gen); // Generate from 0 to (width*height) -1 has index for all // Choose random from width and height in range } bool SquareMaze::hasNoCycle(int current, int peek) { return cellData.find(current) != cellData.find(peek); } int SquareMaze::calculateIndex(int x, int y) const { return (y * mazeWidth) + x;
} bool SquareMaze::canTravel(int x, int y, int dir) const { // Edge Case current Point Valid && Dir input is 0-3 inclusive switch (dir) { case 0: // Right & Check its Right Wall // Check Valid Neighbor if(x < mazeWidth-1 && wallData[y][x].first == false) { return true; } break; // Else break and Return false case 1: // Down & Check its Wall if(y < mazeHeight-1 && wallData[y][x].second == false) { return true; } break; case 2: // Left & Check Left Neighbor Right if(x>0 && wallData[y][x-1].first == false) { return true; } break; case 3: // Up & Check Up Neighbor Down if(y>0 && wallData[y-1][x].second == false) { return true; } break; default: break; } return false; } // // My Own Function // bool SquareMaze::checkPoint(int x, int y) const // { // // Check if neighbor is in range x, y are index, has to be smaller than width and height // if (x> 0 && x < mazeWidth && y> 0 && y< mazeHeight ) // { // return true; // } // else{ // return false; // } // } void SquareMaze::setWall(int x, int y, int dir, bool exists) { // Edge Case x,y checks check dir switch(dir) {
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help

Browse Popular Homework Q&A

Q: A space vehicle is coasting at a constant velocity of 15.4 m/s in the +y direction relative to a…
Q: What is the velocity of a 1000 kg car with 480,000 J of kinetic energy? Can you determine its…
Q: Select all the curved arrows that are correct C
Q: the conditional statement. (a v b) ⇒ c
Q: Recognize and explain some of the challenges associated with developing an information system…
Q: Find a potential function for F(x, y, z) = :yi+xj+xzk or prove none exists.
Q: Let X₁, X2, ..., XK-1 have a multinomial distribution. (a) Find the mgf of X₁, X₂, ..., XK-2. (b)…
Q: Question5. Design a social network model. We will have users with a mailbox and connections to other…
Q: A friend of mine is giving a dinner party. His current wine supply includes 9 bottles of zinfandel,…
Q: 3. List all product categories and number of products sold for each category PRODUCT_CATEGORY…
Q: 6 Write each of the following complex numbers in the form a + bi. (a)* 3 cis120°
Q: Let x have a uniform distribution on the interval 0 to 10. Find the probability. P(3.8 < X < 8.1)
Q: What is the crossover point in miles between the hybrid vehicle and the alternative vehicle from a…
Q: Graphing Inequalities Graph the following inequality. Note: To graph the inequality: 1. Select the…
Q: Draw the product of the reaction shown below. Ignore inorganic byproducts. H₂O, heat - CO2 OH
Q: Question 2 Use Newton's second law of motion to determine an expression for the acceleration of the…
Q: Define the following: personal selling; relationship selling (consultative selling); sales process…
Q: Identify the vertex, axis of symmetry, y-intercept, x-intercepts, and opening of the parabola, then…
Q: Hydrogen-3 has a half-life of 12.3 years. How many years will it take for 502.0 mg ^3H to decay to…
Q: Determine the molarity of a solution formed by dissolving 97.7 g LiBr in enough water to yield 750.0…
Q: How much would you need to deposit in an account now in order to have $6000 in the account in 5…
Q: 1. Let (i) Dr (ii) Fi