2x2 Rubiks Cube Operation Program in C

138 views Asked by At

I recently tried to code a simple program, which can do the 6 basic operations, such as "Turn the Top", "Turn the Bottom", "Turn the Left Side", of a 2x2 Rubik's Cube. (All of these operations are meant to be 90° clockwise!

I began to try to create a function which can rotate the top of the 2x2 Rubik's cube by 90°. In theory I thought the code should work, but when I tried to print out the Basic Rubik's cube I got many issues with GCC such as "Stack Smashing". I really don't know where my mistake is, I thought the hardest part was the thinking process (To understand how the cube works, and which parts of the array are moving) but no I came across problems with the coding/compiling.

If I fix this problem, the implementation of the other 5 operation functions won't be that hard, because I follow a simple thought (Which is explained in the ASCII-Drawn-Comment).

I would be very thankful if anyone can help me by locating and fixing my problem. Thank you :)

In the following there is my code:

#include <stdio.h>
#include <stdlib.h>

typedef char rubik_t[6][2][2];

/*
Rotation in General:

           top1 top2                                                            left1 left2
                                             90° Turn                   
left2   sideTopL sideTopR   right1      -------------------->          bottom2   sideBotL sideTopL      top1    
left1   sideBotL sideBotR   right2      -------------------->          bottom1   sideBotR sideTopR      top2     

         bottom2 bottom1                                                        right2 right1


*/

//Copy the Input Cube into a new Array
char *copyRubik(rubik_t rubik, rubik_t *copied){
    int i, j, k;
    for(i = 0; i < 6; i++){
        for(j = 0; j < 2; j++){
            for(k = 0; k < 2; k++){
                *copied[i][j][k] = rubik[i][j][k];
            }
        }
    }
    return 0;
}

//Function to turn the Top of the Cube
void turnTop(rubik_t *copied){
    char temp1, temp2, temp3, temp4, temp5;
    char *left1 = copied[0][0][1];
    char *left2 = copied[0][1][1];
    char *right1 = copied[2][1][1];
    char *right2 = copied[2][0][1];
    char *top1 = copied[3][0][1];
    char *top2 = copied[3][1][1];
    char *bottom1 = copied[1][1][1];
    char *bottom2 = copied[1][0][1];
    char *sideTopL = copied[4][0][1];
    char *sideBotL = copied[4][0][0];
    char *sideTopR = copied[4][1][1];
    char *sideBotR = copied[4][1][0];

    temp1 = *top1;
    temp2 = *top2;
    temp3 = *sideBotL;
    temp4 = *sideTopL;
    temp5 = *sideTopR;
    *left1 = *bottom1;
    *left2 = *bottom2;
    *sideBotL = *sideBotR;
    *right1 = temp1;
    *right2 = temp2;
    *sideTopL = temp3;
    *sideTopR = temp4;
    *sideBotR = temp5;
    *top1 = *left1;
    *top2 = *left2;
    *bottom1 = *right1;
    *bottom2 = *right2;
}

int main(){

//Basic (solved) Rubik's cube
rubik_t cube_ordered = {
  {{'B','B'},{'B','B'}},       
  {{'O','O'},{'O','O'}},       
  {{'G','G'},{'G','G'}},       
  {{'R','R'},{'R','R'}},       
  {{'W','W'},{'W','W'}},       
  {{'Y','Y'},{'Y','Y'}}   
};
     rubik_t copiedCube;
     char *copied = malloc(25);
     copyRubik(cube_ordered, &copiedCube);
     turnTop(&copiedCube);
     
//  Print the Cube  
     int i, j, k;
     for(i = 0; i < 6; i++){
        for(j = 0; j < 2; j++){
            for(k = 0; k < 2; k++){
                printf("%c", copiedCube[i][j][k]);
                if(k == 1){
                    printf(" ");
                    if(j == 1){
                        printf("\n");
                        if(i == 5){
                            printf("\n");
                        }
                    }
                }
            }
        }
     }
     
     free(copied);
     return 0;
}

I had a whole thinking process in how to solve the problem, tried to implement it into Code, but came across many issues which seem (for me) impossible to solve. I tried to fix the memory allocation, but it didn't work (I don't understand that much of memory in C ;( )

1

There are 1 answers

0
hko On

You don't need to pass a pointer to your 3d array. You can just pass the array itself:

//Copy the Input Cube into a new Array
int copyRubik(rubik_t rubik, rubik_t copied){
    int i, j, k;
    for(i = 0; i < 6; i++){
        for(j = 0; j < 2; j++){
            for(k = 0; k < 2; k++){
                copied[i][j][k] = rubik[i][j][k];
            }
        }
    }
    return 0;
}

//Function to turn the Top of the Cube
void turnTop(rubik_t copied){
    char temp1, temp2, temp3, temp4, temp5;
    char *left1 = &copied[0][0][1];
    char *left2 = &copied[0][1][1];
    char *right1 = &copied[2][1][1];
    char *right2 = &copied[2][0][1];
    char *top1 = &copied[3][0][1];
    char *top2 = &copied[3][1][1];
    char *bottom1 = &copied[1][1][1];
    char *bottom2 = &copied[1][0][1];
    char *sideTopL = &copied[4][0][1];
    char *sideBotL = &copied[4][0][0];
    char *sideTopR = &copied[4][1][1];
    char *sideBotR = &copied[4][1][0];

    temp1 = *top1;
    temp2 = *top2;
    temp3 = *sideBotL;
    temp4 = *sideTopL;
    temp5 = *sideTopR;
    *left1 = *bottom1;
    *left2 = *bottom2;
    *sideBotL = *sideBotR;
    *right1 = temp1;
    *right2 = temp2;
    *sideTopL = temp3;
    *sideTopR = temp4;
    *sideBotR = temp5;
    *top1 = *left1;
    *top2 = *left2;
    *bottom1 = *right1;
    *bottom2 = *right2;
}


     copyRubik(cube_ordered, copiedCube);
     turnTop(copiedCube);

This works in C99 and above.