PDA

View Full Version : scramble and opengl created rubik's cube using the C++ function srand()



AstroJazz
04-09-2003, 03:15 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--);
????????????
}
}


am I going about this the right way???

If anyone has any ideas, please let me know.

Thanks again
Astro

pkaler
04-09-2003, 03:55 PM
This is not an OpenGL question. It's definitely not an advanced question.
<slaps wrist>
Go read this: http://www.catb.org/~esr/faqs/smart-questions.html

Please use the code tag to make your code snippets more readable.

Now to answering your question.

You call srand() once in an init function at the start of your program. Call rand() after that every time you need a random number.

rgpc
04-09-2003, 08:27 PM
http://www.opengl.org/discussion_boards/ubb/rolleyes.gif

Boy, you missed April fools day by 8 days. Well done, that's got to be a record.