PDA

View Full Version : scrambling a rubik's cube using srand() C++ and opengl

AstroJazz
04-09-2003, 02:13 PM
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>SIZE) sz=1;
break;

case 'x': case 'X':
controlKey[5]=true;
sy++; sx=sz=0;
if(sy>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>0){
rotcounter=90; //for the clockwise rotation
if(unit->x1==sx-2) unit->Rotate(Forward);
else if(unit->y1==sy-2) unit->Rotate(CW);
else if(unit->z1==sz-2) unit->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

nexusone
04-09-2003, 04:30 PM
You need to have what ever key for random call a routine to scramble the cube.

void Cube_scramble(void)
{

// then loop X times until cube is randomize
// you could even record each move to have it unsolve.
for(pass = 0; pass < max_pass; pass++)
{
Rotate_cube_section( Random_section )
glutPostRedisplay(); // show cube changing positions as it is made random.
}

}

Originally posted by AstroJazz:
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>SIZE) sz=1;
break;

case 'x': case 'X':
controlKey[5]=true;
sy++; sx=sz=0;
if(sy>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>0){
rotcounter=90; //for the clockwise rotation
if(unit->x1==sx-2) unit->Rotate(Forward);
else if(unit->y1==sy-2) unit->Rotate(CW);
else if(unit->z1==sz-2) unit->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--);
????????????
}
}