Tuesday, February 17, 2015

Possible combination two dimensional array

Here is how to find possible combination on one side of 5 by 5 Rubik’s cube with two colours. Mathematically two colours in 5 x 5 = 25 places comes to 225 = 3,35,54,432. To visualise this combination we can look from 1 to possible number of combination but challenge come on representing on Cube UI.

To achieve programmatically we need two dimension array representing 5 x 5 places on one face on cube. Imaging below face with yellow and white colour. Colour can be presented with numeric value 0 and 1. So, White = 0 and Yellow = 1




For example, to represent below face we need following values in 5 by 5 array

Index
0
1
2
3
4
0
0
0
0
0
0
1
0
0
0
0
0
2
0
0
0
0
0
3
0
0
0
0
0
4
0
0
0
0
1

Single number can be represented as 00000 00000 00000 00000 00001 which is nothing but 1

To achieve this using binary representation of each combination and transform into colour representation of each number.

The simple code will look like this, a represent binary string of each number starting from 225 to 1

for (long i = (long)Math.Pow(2, 25) - 1; i > 0; i--)
{
                a = Convert.ToString(i, 2).PadLeft(25, '0');
}

Full code to transform binary string to 5 by 5 and further into colour is attached in window application in this blog. This code is using TableLayoutPanel control to paint cube face.


Enjoy!

No comments: