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

04-09-2003, 02: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

switch(key) {

case 'z': case 'Z':
sx++; sy=sz=0;
if(sx>SIZE) sx=1;

case 'c': case 'C':
sz++; sx=sy=0;
if(sz>SIZE) sz=1;

case 'x': case 'X':
sy++; sx=sz=0;
if(sy>SIZE) sy=1;

void rotate_selected(int direction){
int i;
Rubik *unit;
for( i=0; i<26; 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(controlKey[7]) { /* counter-clockwise rotation - A selected */
} else if(controlKey[8]) { /* clockwise rotation - S selected */

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";
case 1:
axis = "y1";
case 2:
axis = "z1";
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

04-09-2003, 02: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.

04-09-2003, 07:27 PM

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