I want to create a pool of numbers for an Mastermind algorithm (https://www.swtestacademy.com/algorithms-mastermind/). The pool should consist of every possible combination of an Mastermind code with size n. So for example when n = 4 the pool should look like this:
[0000][0001][0002] .... [5555]
with n = 5:
[00000] .... [55555]
where every number represents a color from the mastermind solution. So for Example [3101] would be red,blue,white,blue.
I made a function to create a pool with n = 4:
vector<string> createPool4()
{
vector<string> pool;
for (int i = 0; i < colours; i++)
for (int j = 0; j < colours; j++)
for (int k = 0; k < colours; k++)
for (int l = 0; l < colours; l++)
pool.push_back(to_string(i) + to_string(j) + to_string(k) + to_string(l));
return pool;
}
What i tried then, was to transform this function into some kind of recursive nested for-loop but, well look for yourself:
vector<string> fillPool(int n, vector<string> pool, const string& s) {
if (n == 0) {
pool.push_back(s);
s.clear();
return pool;
}
for (int i = 0; i < n; i++) {
s += to_string(i);
pool = fillPool(n-1,pool,s);
}
}
This Code doesn't work, it should just show in what directions I was going.
So to conclude, I need a function that can take a dimension n and then create a list of possible codes. so far i was going with a vector of strings but I'm happy to hear alternative possibilities.
Maybe someone can help me with this, because I know that somewhere out there, there is a really good solution for this.
Thanks!