#include #define UP 0 #define DOWN 1 #define LEFT 2 #define RIGHT 3 #define FRONT 4 #define BACK 5 typedef struct Cube { int x, y, z; char sticker[6]; } Cube; void initialize(Cube* array, int pos[3][3][3]) { for (int i=0; i<27; i++) { // Calcolo le coordinate in base agli indici e le assegno ai cubetti int x=i%3; int y=(i/9); int z=(i/3)%3; array[i].x=x; array[i].y=y; array[i].z=z; // Inizializzo tutti gli sticker e associo i colori ai cubetti array[i].sticker[DOWN] = 'W'; array[i].sticker[UP] = 'Y'; array[i].sticker[LEFT] = 'B'; array[i].sticker[RIGHT] = 'G'; array[i].sticker[BACK] = 'O'; array[i].sticker[FRONT] = 'R'; // Mi salvo che cubetto ho per ogni coordinata pos[x][y][z]=i; } } void print(Cube* array, int pos[3][3][3]) { printf(" %c %c %c\n %c %c %c\n %c %c %c", array[pos[0][2][2]].sticker[BACK], array[pos[1][2][2]].sticker[BACK], array[pos[2][2][2]].sticker[BACK], array[pos[0][1][2]].sticker[BACK], array[pos[1][1][2]].sticker[BACK], array[pos[2][1][2]].sticker[BACK], array[pos[0][0][2]].sticker[BACK], array[pos[1][0][2]].sticker[BACK], array[pos[2][0][2]].sticker[BACK]); printf("\n\n %c %c %c\n %c %c %c\n %c %c %c", array[pos[0][0][2]].sticker[UP], array[pos[1][0][2]].sticker[UP], array[pos[2][0][2]].sticker[UP], array[pos[0][0][1]].sticker[UP], array[pos[1][0][1]].sticker[UP], array[pos[2][0][1]].sticker[UP], array[pos[0][0][0]].sticker[UP], array[pos[1][0][0]].sticker[UP], array[pos[2][0][0]].sticker[UP]); printf("\n\n%c %c %c %c %c %c %c %c %c", array[pos[0][0][2]].sticker[LEFT], array[pos[0][0][1]].sticker[LEFT], array[pos[0][0][0]].sticker[LEFT], array[pos[0][0][0]].sticker[FRONT], array[pos[1][0][0]].sticker[FRONT], array[pos[2][0][0]].sticker[FRONT], array[pos[2][0][0]].sticker[RIGHT], array[pos[2][0][1]].sticker[RIGHT], array[pos[2][0][2]].sticker[RIGHT]); printf("\n%c %c %c %c %c %c %c %c %c", array[pos[0][1][2]].sticker[LEFT], array[pos[0][1][1]].sticker[LEFT], array[pos[0][1][0]].sticker[LEFT], array[pos[0][1][0]].sticker[FRONT], array[pos[1][1][0]].sticker[FRONT], array[pos[2][1][0]].sticker[FRONT], array[pos[2][1][0]].sticker[RIGHT], array[pos[2][1][1]].sticker[RIGHT], array[pos[2][1][2]].sticker[RIGHT]); printf("\n%c %c %c %c %c %c %c %c %c", array[pos[0][2][2]].sticker[LEFT], array[pos[0][2][1]].sticker[LEFT], array[pos[0][2][0]].sticker[LEFT], array[pos[0][2][0]].sticker[FRONT], array[pos[1][2][0]].sticker[FRONT], array[pos[2][2][0]].sticker[FRONT], array[pos[2][2][0]].sticker[RIGHT], array[pos[2][2][1]].sticker[RIGHT], array[pos[2][2][2]].sticker[RIGHT]); printf("\n\n %c %c %c\n %c %c %c\n %c %c %c\n\n\n\n", array[pos[0][2][0]].sticker[DOWN], array[pos[1][2][0]].sticker[DOWN], array[pos[2][2][0]].sticker[DOWN], array[pos[0][2][1]].sticker[DOWN], array[pos[1][2][1]].sticker[DOWN], array[pos[2][2][1]].sticker[DOWN], array[pos[0][2][2]].sticker[DOWN], array[pos[1][2][2]].sticker[DOWN], array[pos[2][2][2]].sticker[DOWN]); } void rotate(Cube* array, int pos[3][3][3], char rotazione) { int involved_cubes[9]; int index=0; int tmp_x, tmp_y, tmp_z; // z=0 if (rotazione == 'F') { for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { involved_cubes[index]=pos[i][j][0]; index++; } } for (index=0; index<9; index++) { int i=involved_cubes[index]; tmp_x = array[i].x; tmp_y = array[i].y; array[i].x=2-tmp_y; array[i].y=tmp_x; pos[array[i].x][array[i].y][0]=i; char tmp = array[i].sticker[UP]; array[i].sticker[UP] = array[i].sticker[LEFT]; array[i].sticker[LEFT] = array[i].sticker[DOWN]; array[i].sticker[DOWN] = array[i].sticker[RIGHT]; array[i].sticker[RIGHT] = tmp; } } // z=2 if (rotazione == 'B') { for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { involved_cubes[index]=pos[i][j][2]; index++; } } for (index=0; index<9; index++) { int i=involved_cubes[index]; tmp_x = array[i].x; tmp_y = array[i].y; array[i].x=tmp_y; array[i].y=2-tmp_x; pos[array[i].x][array[i].y][2]=i; char tmp = array[i].sticker[DOWN]; array[i].sticker[DOWN] = array[i].sticker[LEFT]; array[i].sticker[LEFT] = array[i].sticker[UP]; array[i].sticker[UP] = array[i].sticker[RIGHT]; array[i].sticker[RIGHT] = tmp; } } // y=0 if (rotazione == 'U') { for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { involved_cubes[index]=pos[i][0][j]; index++; } } for (index=0; index<9; index++) { int i=involved_cubes[index]; tmp_x = array[i].x; tmp_z = array[i].z; array[i].x=tmp_z; array[i].z=2-tmp_x; pos[array[i].x][0][array[i].z]=i; char tmp = array[i].sticker[FRONT]; array[i].sticker[FRONT] = array[i].sticker[RIGHT]; array[i].sticker[RIGHT] = array[i].sticker[BACK]; array[i].sticker[BACK] = array[i].sticker[LEFT]; array[i].sticker[LEFT] = tmp; } } // y=2 if (rotazione == 'D') { for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { involved_cubes[index]=pos[i][2][j]; index++; } } for (index=0; index<9; index++) { int i=involved_cubes[index]; tmp_x = array[i].x; tmp_z = array[i].z; array[i].x=2-tmp_z; array[i].z=tmp_x; pos[array[i].x][2][array[i].z]=i; char tmp = array[i].sticker[FRONT]; array[i].sticker[FRONT] = array[i].sticker[LEFT]; array[i].sticker[LEFT] = array[i].sticker[BACK]; array[i].sticker[BACK] = array[i].sticker[RIGHT]; array[i].sticker[RIGHT] = tmp; } } // x=0 if (rotazione == 'L') { for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { involved_cubes[index]=pos[0][i][j]; index++; } } for (index=0; index<9; index++) { int i=involved_cubes[index]; tmp_y = array[i].y; tmp_z = array[i].z; array[i].y=2-tmp_z; array[i].z=tmp_y; pos[0][array[i].y][array[i].z]=i; char tmp = array[i].sticker[UP]; array[i].sticker[UP] = array[i].sticker[BACK]; array[i].sticker[BACK] = array[i].sticker[DOWN]; array[i].sticker[DOWN] = array[i].sticker[FRONT]; array[i].sticker[FRONT] = tmp; } } // x=2 if (rotazione == 'R') { for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { involved_cubes[index]=pos[2][i][j]; index++; } } for (index=0; index<9; index++) { int i=involved_cubes[index]; tmp_y = array[i].y; tmp_z = array[i].z; array[i].y=tmp_z; array[i].z=2-tmp_y; pos[2][array[i].y][array[i].z]=i; char tmp = array[i].sticker[UP]; array[i].sticker[UP] = array[i].sticker[FRONT]; array[i].sticker[FRONT] = array[i].sticker[DOWN]; array[i].sticker[DOWN] = array[i].sticker[BACK]; array[i].sticker[BACK] = tmp; } } } int main() { Cube array[27]; int pos[3][3][3]; initialize(array, pos); rotate(array, pos, 'F'); print(array, pos); rotate(array, pos, 'U'); print(array, pos); }