There are a few things to consider before starting off with the code.
Let's perform some rotations with the cube and note down the changes undergone by each side.
A B C D
F
color - number code
yellow 1
orange 2
white 3
red 4
blue 5
green 6
matrix name - number code
A 7
B 8
C 9
D 10
E 11
F 12
This is where the 6 faces of the cube is initialized.As the camera module is not interfaced, every location is manually entered before solving.
This initialization represents a solved cube
int A[3][3]={{1,1,1},{1,1,1},{1,1,1}};
int B[3][3]={{2,2,2},{2,2,2},{2,2,2}};
int C[3][3]={{3,3,3},{3,3,3},{3,3,3}};
int D[3][3]={{4,4,4},{4,4,4},{4,4,4}};
int E[3][3]={{5,5,5},{5,5,5},{5,5,5}};
int F[3][3]={{6,6,6},{6,6,6},{6,6,6}};
These were not initialized in one shot but as the code progressed.Use of each variable will be seen in the appropriate function definitions.
int matrix_name[3][3];
int temp[3][3];
int temp1;
int temp2;
int i;
int j;
int d=0;
int count=0;
int cross_value[4][3];
void print_matrix(int *); //prints out a single matrix
void print_matrices(void); //prints out all 6 matrices
//this function performs the R,R' and L,L'
void column_away(int);
void column_toward(int);
//this function performs U,U' and D,D'
void row_right(int);
void row_left(int);
//this function performs F and F'
void front_clockwise(void);
void front_anticlockwise(void);
void circle_clockwise(char);
void circle_anticlockwise(char);
void change_matrix_name(int,char);
void check_cubes_correctness(void);
The entire section of code above are the global declarations.
//prints a single matrix
void print_matrix(int *a)
{
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t\t",*a);
a++;
}
printf("\n");
}
printf("\n\n");
}
//prints all the 6 matrices
void print_matrices(void)
{
printf("Matrix A\n");
print_matrix(A);
printf("Matrix B\n");
print_matrix(B);
printf("Matrix C\n");
print_matrix(C);
printf("Matrix D\n");
print_matrix(D);
printf("Matrix E\n");
print_matrix(E);
printf("Matrix F\n");
print_matrix(F);
}
void column_away(int j)
{
//stepper turn mechanism
for(i=0;i<=2;i++)
{
temp[i][j]=E[i][j];
E[i][j]=B[i][j];
B[i][j]=F[i][j];
if(j==0)
{
F[i][j]=D[i][j+2];
D[i][j+2]=temp[i][j];
}
if(j==2)
{
F[i][j]=D[i][j-2];
D[i][j-2]=temp[i][j];
}
}
if(j==2)
{
circle_clockwise('C');
}
if(j==0)
{
circle_anticlockwise('A');
}
}
void column_toward(int j)
{
//stepper turn mechanism
for(i=0;i<=2;i++)
{
if(j==0)
{
temp[i][j]=D[i][j+2];
D[i][j+2]=F[i][j];
}
if(j==2)
{
temp[i][j]=D[i][j-2];
D[i][j-2]=F[i][j];
}
F[i][j]=B[i][j];
B[i][j]=E[i][j];
E[i][j]=temp[i][j];
}
if(j==2)
{
circle_anticlockwise('C');
}
if(j==0)
{
circle_clockwise('A');
}
}
void row_right(int i)
{
//stepper turn mechanism
for(j=0;j<=2;j++)
{
temp[i][j]=A[i][j];
A[i][j]=D[i][j];
D[i][j]=C[i][j];
C[i][j]=B[i][j];
B[i][j]=temp[i][j];
}
if(i==0)
{
circle_anticlockwise('E');
}
if(i==2)
{
circle_clockwise('F');
}
}
void row_left(int i)
{
//stepper turn mechanism
for(j=0;j<=2;j++)
{
temp[i][j]=A[i][j];
A[i][j]=B[i][j];
B[i][j]=C[i][j];
C[i][j]=D[i][j];
D[i][j]=temp[i][j];
}
if(i==0)
{
circle_clockwise('E');
}
if(i==2)
{
circle_anticlockwise('F');
}
}
Let's perform some rotations with the cube and note down the changes undergone by each side.
Decoding from my notes above we see that when column_toward(0) is performed, A rotates clockwise and column 0 of B,D, E and F rotate one step downwards.Same with column_toward(2),wherein C rotates anticlockwise and column 2 of B,D,E and F rotate one step downwards.
D has a strange characteristic and that is it is not like other matrices.
column[0] of other matrices = D[2];
column[1] of other matrices = D[1];
column[2] of other matrices = D[0];
This is a very important point to notice without which huge problems will appear in our code.Whenever there is anything weird near D,it is to correct this "change of columns".
row_right(0) is equivalent to U'.row_right(2) is equivalent to D and vice versa with row_left.
array format
EA B C D
F
color - number code
yellow 1
orange 2
white 3
red 4
blue 5
green 6
matrix name - number code
A 7
B 8
C 9
D 10
E 11
F 12
This is where the 6 faces of the cube is initialized.As the camera module is not interfaced, every location is manually entered before solving.
This initialization represents a solved cube
int A[3][3]={{1,1,1},{1,1,1},{1,1,1}};
int B[3][3]={{2,2,2},{2,2,2},{2,2,2}};
int C[3][3]={{3,3,3},{3,3,3},{3,3,3}};
int D[3][3]={{4,4,4},{4,4,4},{4,4,4}};
int E[3][3]={{5,5,5},{5,5,5},{5,5,5}};
int F[3][3]={{6,6,6},{6,6,6},{6,6,6}};
These were not initialized in one shot but as the code progressed.Use of each variable will be seen in the appropriate function definitions.
int matrix_name[3][3];
int temp[3][3];
int temp1;
int temp2;
int i;
int j;
int d=0;
int count=0;
int cross_value[4][3];
void print_matrix(int *); //prints out a single matrix
void print_matrices(void); //prints out all 6 matrices
//this function performs the R,R' and L,L'
void column_away(int);
void column_toward(int);
//this function performs U,U' and D,D'
void row_right(int);
void row_left(int);
//this function performs F and F'
void front_clockwise(void);
void front_anticlockwise(void);
void circle_clockwise(char);
void circle_anticlockwise(char);
void change_matrix_name(int,char);
void check_cubes_correctness(void);
The entire section of code above are the global declarations.
//prints a single matrix
void print_matrix(int *a)
{
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("%d\t\t",*a);
a++;
}
printf("\n");
}
printf("\n\n");
}
//prints all the 6 matrices
void print_matrices(void)
{
printf("Matrix A\n");
print_matrix(A);
printf("Matrix B\n");
print_matrix(B);
printf("Matrix C\n");
print_matrix(C);
printf("Matrix D\n");
print_matrix(D);
printf("Matrix E\n");
print_matrix(E);
printf("Matrix F\n");
print_matrix(F);
}
void column_away(int j)
{
//stepper turn mechanism
for(i=0;i<=2;i++)
{
temp[i][j]=E[i][j];
E[i][j]=B[i][j];
B[i][j]=F[i][j];
if(j==0)
{
F[i][j]=D[i][j+2];
D[i][j+2]=temp[i][j];
}
if(j==2)
{
F[i][j]=D[i][j-2];
D[i][j-2]=temp[i][j];
}
}
if(j==2)
{
circle_clockwise('C');
}
if(j==0)
{
circle_anticlockwise('A');
}
}
void column_toward(int j)
{
//stepper turn mechanism
for(i=0;i<=2;i++)
{
if(j==0)
{
temp[i][j]=D[i][j+2];
D[i][j+2]=F[i][j];
}
if(j==2)
{
temp[i][j]=D[i][j-2];
D[i][j-2]=F[i][j];
}
F[i][j]=B[i][j];
B[i][j]=E[i][j];
E[i][j]=temp[i][j];
}
if(j==2)
{
circle_anticlockwise('C');
}
if(j==0)
{
circle_clockwise('A');
}
}
void row_right(int i)
{
//stepper turn mechanism
for(j=0;j<=2;j++)
{
temp[i][j]=A[i][j];
A[i][j]=D[i][j];
D[i][j]=C[i][j];
C[i][j]=B[i][j];
B[i][j]=temp[i][j];
}
if(i==0)
{
circle_anticlockwise('E');
}
if(i==2)
{
circle_clockwise('F');
}
}
void row_left(int i)
{
//stepper turn mechanism
for(j=0;j<=2;j++)
{
temp[i][j]=A[i][j];
A[i][j]=B[i][j];
B[i][j]=C[i][j];
C[i][j]=D[i][j];
D[i][j]=temp[i][j];
}
if(i==0)
{
circle_clockwise('E');
}
if(i==2)
{
circle_anticlockwise('F');
}
}
void circle_clockwise(char a)
{
change_matrix_name(0,a);
temp1=matrix_name[0][0];
matrix_name[0][0]=matrix_name[2][0];
matrix_name[2][0]=matrix_name[2][2];
matrix_name[2][2]=matrix_name[0][2];
matrix_name[0][2]=temp1;
temp2=matrix_name[0][1];
matrix_name[0][1]=matrix_name[1][0];
matrix_name[1][0]=matrix_name[2][1];
matrix_name[2][1]=matrix_name[1][2];
matrix_name[1][2]=temp2;
change_matrix_name(1,a);
}
void circle_anticlockwise(char a)
{
change_matrix_name(0,a);
temp1=matrix_name[0][0];
matrix_name[0][0]=matrix_name[0][2];
matrix_name[0][2]=matrix_name[2][2];
matrix_name[2][2]=matrix_name[2][0];
matrix_name[2][0]=temp1;
temp2=matrix_name[0][1];
matrix_name[0][1]=matrix_name[1][2];
matrix_name[1][2]=matrix_name[2][1];
matrix_name[2][1]=matrix_name[1][0];
matrix_name[1][0]=temp2;
change_matrix_name(1,a);
}
void check_cubes_correctness(void)
{
//The logic of this function is that when you consider a solved cube in my arrangement,
//side A is full of 1's,
//-||- B ----||-----2's
//-||- C ----||-----3's and so on.
//this function just sums up each od the numbers.
//if the value ends up to be 189,the cube is valid
printf("count = %d \n\n", count);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
count=count+A[i][j]+B[i][j]+C[i][j]+D[i][j]+E[i][j]+F[i][j];
}
}
printf("count = %d \n", count);
if(count==189)
{
printf("\n valid cube\n\n");
}
else
{
printf("\n invalid cube\n\n");
//add code to rescan all 6 sides again
}
}
void change_matrix_name(int b,char a)
{
int *c;
if(a=='A')
{
c=A;
}
if(a=='B')
{
c=B;
}
if(a=='C')
{
c=C;
}
if(a=='D')
{
c=D;
}
if(a=='E')
{
c=E;
}
if(a=='F')
{
c=F;
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(b==0)
{
matrix_name[i][j]=*c;
c++;
}
if(b==1)
{
*c=matrix_name[i][j];
c++;
}
}
}
}
void front_clockwise(void)
{
//stepper turn mechanism
circle_clockwise('B');
temp[0][0]=A[0][2];
temp[0][1]=A[1][2];
temp[0][2]=A[2][2];
A[0][2]=F[0][0];
A[1][2]=F[0][1];
A[2][2]=F[0][2];
F[0][0]=C[2][0];
F[0][1]=C[1][0];
F[0][2]=C[0][0];
C[2][0]=E[2][2];
C[1][0]=E[2][1];
C[0][0]=E[2][0];
E[2][2]=temp[0][0];
E[2][1]=temp[0][1];
E[2][0]=temp[0][2];
}
void front_anticlockwise(void)
{
//stepper turn mechanism
circle_anticlockwise('B');
temp[0][0]=A[0][2];
temp[0][1]=A[1][2];
temp[0][2]=A[2][2];
A[0][2]=E[2][2];
A[1][2]=E[2][1];
A[2][2]=E[2][0];
E[2][2]=C[2][0];
E[2][1]=C[1][0];
E[2][0]=C[0][0];
C[2][0]=F[0][0];
C[1][0]=F[0][1];
C[0][0]=F[0][2];
F[0][0]=temp[0][0];
F[0][1]=temp[0][1];
F[0][2]=temp[0][2];
}
No comments:
Post a Comment