Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: creating random rotations for a rubik's cube

  1. #1
    Junior Member Newbie
    Join Date
    Apr 2003
    Location
    Ireland
    Posts
    5

    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>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
    AstroJ

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: creating random rotations for a rubik's cube

    See my post in the beginners forum to this.


    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--);
    ????????????
    }
    }


    am I going about this the right way???

    If anyone has any ideas, please let me know.

    Thanks again
    AstroJ


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •