# creating random rotations for a rubik's cube

Hi all

I am trying to scramble my rubik’s cube using the srand function in C++. I’m not sure how exactly to go about it. All my rotation of the cube itself is done using the keyboard keys, do I have to link the keyboard keys with the srand()??

Here is my code below that deals with the rotations

Code:
switch(key) {

case ‘z’: case ‘Z’:
controlKey[4]=true;
sx++; sy=sz=0;
if(sx>SIZE) sx=1;
break;

case 'c': case 'C':
controlKey[6]=true;
sz++; sx=sy=0;
if(sz&gt;SIZE) sz=1;
break;

case 'x': case 'X':
controlKey[5]=true;
sy++; sx=sz=0;
if(sy&gt;SIZE) sy=1;
break;

}

void rotate_selected(int direction){
int i;
Rubik *unit;
for( i=0; i<26; i++){
unit=faces[i];

if(direction<0) {
rotcounter=-90; //for the anti-clockwise rotation
if(unit->x1==sx-2) unit->Rotate(Back);
else if(unit->y1==sy-2) unit->Rotate(CCW);
else if(unit->z1==sz-2) unit->Rotate(Left);

else if(direction&gt;0){
rotcounter=90;  //for the clockwise rotation
if(unit-&gt;x1==sx-2) unit-&gt;Rotate(Forward);
else if(unit-&gt;y1==sy-2) unit-&gt;Rotate(CW);
else if(unit-&gt;z1==sz-2) unit-&gt;Rotate(Right);
}
}

}
}

/* the input for the application*/
void inputUpdate(void) {
float step = 3.0;
int size=3;

if(controlKey[0]) rotate_x -= step; /* up /
if(controlKey[1]) rotate_x += step; /
down /
if(controlKey[2]) rotate_y -= step; /
left /
if(controlKey[3]) rotate_y += step; /
right */

if(rotcounter==0){
if(controlKey[7]) { /* counter-clockwise rotation - A selected /
rotate_selected(1);
} else if(controlKey[8]) { /
clockwise rotation - S selected */
rotate_selected(-1);
}
}
}

static void KeyboardUp( unsigned char key, int x, int y )
{
(void) x;
(void) y;
switch(key) {
case ‘z’: case ‘Z’: controlKey[4]=false; break;
case ‘x’: case ‘X’: controlKey[5]=false; break;
case ‘c’: case ‘C’: controlKey[6]=false; break;
case ‘a’: case ‘A’: controlKey[7]=false; break;
case ‘s’: case ‘S’: controlKey[8]=false; break;
}
}

I was thinking of doing somethin like this to generate the scramble???

void scramble_cube() {
GLint i, rotationcounter;
char *axis;
GLint new_state, st;

/* srand() causes the computer to read its clock to obtain a value,
therefore creates a different random number each execution
of the program*/

srand( time( 0 ) );

for (i = 0; i < 10; i++) {
new_state = rand() % 3;
switch (new_state) {
case 0:
axis = “x1”;
break;
case 1:
axis = “y1”;
break;
case 2:
axis = “z1”;
break;
}
st = rand() % 3;
rotationcounter = rand() % 4;
for (; rotationcounter > 0; rotationcounter–);
???
}
}

If anyone has any ideas, please let me know.

Thanks again
AstroJ

See my post in the beginners forum to this.

Originally posted by AstroJazz:
[b]Hi all

I am trying to scramble my rubik’s cube using the srand function in C++. I’m not sure how exactly to go about it. All my rotation of the cube itself is done using the keyboard keys, do I have to link the keyboard keys with the srand()??

Here is my code below that deals with the rotations

Code:
switch(key) {

case ‘z’: case ‘Z’:
controlKey[4]=true;
sx++; sy=sz=0;
if(sx>SIZE) sx=1;
break;

case 'c': case 'C':
controlKey[6]=true;
sz++; sx=sy=0;
if(sz&gt;SIZE) sz=1;
break;
case 'x': case 'X':
controlKey[5]=true;
sy++; sx=sz=0;
if(sy&gt;SIZE) sy=1;
break;

}

void rotate_selected(int direction){
int i;
Rubik *unit;
for( i=0; i<26; i++){
unit=faces[i];

if(direction<0) {
rotcounter=-90; //for the anti-clockwise rotation
if(unit->x1==sx-2) unit->Rotate(Back);
else if(unit->y1==sy-2) unit->Rotate(CCW);
else if(unit->z1==sz-2) unit->Rotate(Left);

else if(direction&gt;0){
rotcounter=90;  //for the clockwise rotation
if(unit-&gt;x1==sx-2) unit-&gt;Rotate(Forward);
else if(unit-&gt;y1==sy-2) unit-&gt;Rotate(CW);
else if(unit-&gt;z1==sz-2) unit-&gt;Rotate(Right);
}
}

}
}

/* the input for the application*/
void inputUpdate(void) {
float step = 3.0;
int size=3;

if(controlKey[0]) rotate_x -= step; /* up /
if(controlKey[1]) rotate_x += step; /
down /
if(controlKey[2]) rotate_y -= step; /
left /
if(controlKey[3]) rotate_y += step; /
right */

if(rotcounter==0){
if(controlKey[7]) { /* counter-clockwise rotation - A selected /
rotate_selected(1);
} else if(controlKey[8]) { /
clockwise rotation - S selected */
rotate_selected(-1);
}
}
}

static void KeyboardUp( unsigned char key, int x, int y )
{
(void) x;
(void) y;
switch(key) {
case ‘z’: case ‘Z’: controlKey[4]=false; break;
case ‘x’: case ‘X’: controlKey[5]=false; break;
case ‘c’: case ‘C’: controlKey[6]=false; break;
case ‘a’: case ‘A’: controlKey[7]=false; break;
case ‘s’: case ‘S’: controlKey[8]=false; break;
}
}

I was thinking of doing somethin like this to generate the scramble???

void scramble_cube() {
GLint i, rotationcounter;
char *axis;
GLint new_state, st;

/* srand() causes the computer to read its clock to obtain a value,
therefore creates a different random number each execution
of the program*/

srand( time( 0 ) );

for (i = 0; i < 10; i++) {
new_state = rand() % 3;
switch (new_state) {
case 0:
axis = “x1”;
break;
case 1:
axis = “y1”;
break;
case 2:
axis = “z1”;
break;
}
st = rand() % 3;
rotationcounter = rand() % 4;
for (; rotationcounter > 0; rotationcounter–);
???
}
}