- #1
W3bbo
- 31
- 0
I'm writing a program that generates every possible valid Mastermind code.
That itself is easy. There are 6 colors in 4 possible positions. Cycling through them all is done like so:
where each color is represented by a number ( 0 = red, 1 = blue, etc )
which gives combinations like so:
(begin)
0000
0001
0002
...
3005
3010
...
5554
5555
(end)
So that's all perfectly fine.
Now, how do I do the same but for cases where there cannot be duplicate colors?
I know the size of the set is 6*5*4*3 = 360 elements
So I want my loop to be something like this:
So how can I convert a number between 1 and 360 into a permutation of colors?
Thanks!
That itself is easy. There are 6 colors in 4 possible positions. Cycling through them all is done like so:
Code:
for(int i = 0 to 1295 ) { // 1295 == 6^4 - 1, there are 1296 possible permutations of colors
color1 = ( i / 6 ^ 0 ) % 6;
color2 = ( i / 6 ^ 1 ) % 6;
color3 = ( i / 6 ^ 2 ) % 6;
color4 = ( i / 6 ^ 3 ) % 6;
}
where each color is represented by a number ( 0 = red, 1 = blue, etc )
which gives combinations like so:
(begin)
0000
0001
0002
...
3005
3010
...
5554
5555
(end)
So that's all perfectly fine.
Now, how do I do the same but for cases where there cannot be duplicate colors?
I know the size of the set is 6*5*4*3 = 360 elements
So I want my loop to be something like this:
Code:
for(int i=1;i<=360;i++) {
color1 = // what, exactly?
}
So how can I convert a number between 1 and 360 into a permutation of colors?
Thanks!