Reputation: 31
I have coded a random maze generator and it totally works. However I want to make it so that if the user types in the same seed (third command line parameter) it generates the same maze. I was wondering what the best way to do that is, should I output stream the maze and the seed into a separate file. When a seed is called compare the 2 seeds and if they are the same, call the corresponding maze in the file. Or how would you guys suggest I do it?
Thanks in advance!
EDIT: Here is the code that uses my randomizer
std::mt19937 rng(std::random_device{}());
Grid[yPos][xPos] = " ";
std::vector<std::pair<int, int>> Directions{{
{North, 0},
{South, 0},
{0, East},
{0, West},
}};
// Shuffle vector
for (int i = 0; i < 4; i++) {
int j = i + rng() % (4 - i);
std::swap(Directions[i], Directions[j]);
}
// Loop over a range of directions
for(std::pair<int, int>& pair : Directions) {
int toY = pair.first;
int toX = pair.second;
int y2 = yPos + toY * 2;
int x2 = xPos + toX * 2;
if(checkValid(y2, x2) && Grid[y2][x2] == "#") {
Grid[yPos + toY][xPos + toX] = " ";
carvePath(y2, x2);
}
}
}
Upvotes: 0
Views: 173
Reputation: 59263
You don't need to store anything.
std::mt19937
is a pseudo-random number generator. You provide a seed when you construct it, and the seed completely determines the sequence of random numbers that it generates.
You should just hash your user-entered seed and pass the hash to this constructor instead of using random_device
.
Then the user-entered seed will completely determine which maze you get.
You can see an example of seeding this generated from a string here: https://www.cplusplus.com/reference/random/mersenne_twister_engine/mersenne_twister_engine/
Upvotes: 3