Phew.
My project has now finally reached a stage where I can take a deep breath and relax.
After tons of coding and checking,I have finally been able to almost solve the cube.The only part left is to align the top edges,rotate the steppers,build the frame and I'm done.
Please patiently go through the code,and feel free to use or modify it.
Note: 1.There are cases where there are a lot of redundant moves.
2.I forgot to mention this but the algorithm used is the beginners algorithm,not the advanced one's like Kociemba. So,a huge solution is guaranteed,but it's a solution nonetheless!!
/* ###################################################################
** Filename : main.c
** Project : RUBIKS CUBE SOLVER
** Processor : MKL25Z128VLK4
** Version : Driver 01.01
** Compiler : GNU C Compiler
** Date/Time : 2016-01-11, 09:39, # CodeGen: 0
** Abstract :
** Main module.
** This module contains user's application code.
** Settings :
** Contents :
** No public methods
**
** ###################################################################*/
/*!
** @file main.c
** @version 01.01
** @brief
** Main module.
** This module contains user's application code.
*/
/*!
** @addtogroup main_module main module documentation
** @{
*/
/* MODULE main */
/*
* array format
* E
* A B C D
* F
* */
/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "Events.h"
#include "CsIO1.h"
#include "IO1.h"
/* Including shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
//colour - 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
//the values are just initially assigned.they WILL change later on when the colours are manually input.
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}};
int matrix_name[3][3]; //only to be used in change_matrix_name();
int temp[3][3]; //temp 3x3 matrix to store the values of the 6 matrices
int temp1;
int temp2;
int i; //used in the row count for the 6 matrices
int j; //used in the column count of the 6 matrices
int d=0; //temp variable to be used
int t=0;
int y=2; //used to solve the "D" matrix problem in column_away(); and column_toward();
char c=0; //used in print_matrix() function;
char f=0; //used to get Y or N in check_cubes_correctness();
int count=0; //holds the count in check_cubes_correctness();
int cross_value[4][3]; //holds the locations of the cross forming edges of B matrix
int edgepair[12]; //holds the values of all the 12 edges in the cube
int green_edges=0;
int cornerpair[8];
int green_corners=0;
int middle_layer_count=0;
int top_cross_count=0;
int blue_count=0;
int move_count=0;
//ALL THE FUNCTION DEFINED BELOW DOES EXACTLY WHAT ITS NAME TELLS!!
void get_matrices(int *,char);
void check_cubes_correctness(void);
void print_matrix(int *);//prints out a single matrix
void print_matrices(void);//prints out all 6 matrices
void column_away(int);
void column_toward(int);
void row_right(int);
void row_left(int);
void front_clockwise(void);
void front_anticlockwise(void);
void turn_the_cube_virtually_twice(void);
void back_clk(void);
void back_anticlk(void);
void circle_clockwise(char);
void circle_anticlockwise(char);
void change_matrix_name(int,char);//0=>matrix to matrix_name
//1=>matrix_name to matrix
//void search_cross_location(void);
void calculate_edgepairs(void);
//void solve_for_cross(void);
void count_green(void);
void empty(int);
void solve_green_cross(void);
void calculate_cornerpairs(void);
void calculate_green_corners_on_top(void);
void place_correct_green_corners_on_top(void);
void put_green_corners_in_place(int);
void solve_green_corners(void);
void get_middle_layer_count(void);
void solve_middle_piece(int);
void put_middle_piece_in_place(void);
void solve_middle_layer(void);
void solve_top_cross(void);
void get_blue_count(void);
void repeat_pattern(void);
void solve_top_blue(void);
void fix_top_blue_corners(void);
/* User includes (#include below this line is not maintained by Processor Expert) */
/*lint -save -e970 Disable MISRA rule (6.3) checking. */
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
/* Write your local variable definition here */
/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
PE_low_level_init();
/*** End of Processor Expert internal initialization. ***/
/* Write your code here */
/* For example: for(;;) { } */
//hope u work
/*
how i solve the cube(Beginners method)
1.form plus
2.solve first layer
3.solve for middle layer
4.form top cross
5.Solve top layer
6.orient top corners in place
7.orient top edges
8.end up with solved cube
*/
printf("\n Color - number code\n");
printf("\n yellow 1 \n orange 2 \n white 3 \n red 4 \n blue 5 \n green 6");
get_matrices(A,'A');
get_matrices(B,'B');
get_matrices(C,'C');
get_matrices(D,'D');
get_matrices(E,'E');
get_matrices(F,'F');
check_cubes_correctness();
while(count!=189)
{
check_cubes_correctness();
}
//this condition is added because in the solve green cross function,all the green edges are brought on top
//and then solved at the bottom.the problem is when the green cross is solved,then too,they will be brought
//above and solved.so,this case is to bypass that situation.
calculate_edgepairs();
if(edgepair[3]!=16 || edgepair[6]!=26 || edgepair[9]!=36 || edgepair[11]!=46)
{
solve_green_cross();
}
printf("Bottom Cross solved!!\n");
solve_green_corners();
printf("Bottom Corners are solved!!\n");
solve_middle_layer();
printf("Middle layer solved!!\n");
solve_top_cross();
printf("Top cross solved!!\n");
solve_top_blue();
printf("Top Layer solved!!");
fix_top_blue_corners();
printf("Top Blue Corners Fixed!!");
print_matrices();
while(1)
{
}
/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
/*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
#ifdef PEX_RTOS_START
PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of RTOS startup code. ***/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;){}
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
void get_matrices(int *z,char b)
{
printf("\n Enter matrix %c: \n",b);
//empty the temp matrix
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
temp[i][j]=0;
}
}
//take input and store in temp matrix as characters
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%c",&temp[i][j]);
printf("%c",temp[i][j]);
}
}
//copy from temp to appropriate matrix
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
*z=temp[i][j]-48;
z++;
}
}
}
//here,the values 123456 are correspondingly hard-coded with the colours YOWRBG
//because this is how the colours are arranged in my cube.It is not the case with all the cubes and if a cube with
//a different colour orientation is used,make sure to change these hard-coded values.
void print_matrix(int *a)
{
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(*a==1) c='Y';
if(*a==2) c='O';
if(*a==3) c='W';
if(*a==4) c='R';
if(*a==5) c='B';
if(*a==6) c='G';
printf("%c\t\t",c);
a++;
}
printf("\n");
}
printf("\n\n");
}
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 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 of the numbers.
//if the value ends up to be 189,the cube is valid
count=0;
printf("\n\n 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("\n count = %d \n", count);
if(count==189)
{
printf("\n valid cube\n\n");
}
else
{
printf("\n invalid cube.re-enter values.\n\n");
//code to rescan all 6 sides again
printf("\n Enter Y or N in \"Caps\" \n");
printf("\n Matrix A \n");
print_matrix(A);
printf(" \n Enter Y for correct N for not correct \n");
scanf("%c",&f);
if(f=='Y')
{
}
if(f=='N')
{
get_matrices(A,'A');
}
printf("\n Matrix B \n");
print_matrix(B);
printf(" \n Enter Y for correct N for not correct \n");
scanf("%c",&f);
if(f=='Y')
{
}
if(f=='N')
{
get_matrices(B,'B');
}
printf("\n Matrix C \n");
print_matrix(C);
printf(" \n Enter Y for correct N for not correct \n");
scanf("%c",&f);
if(f=='Y')
{
}
if(f=='N')
{
get_matrices(C,'C');
}
printf("\n Matrix D \n");
print_matrix(D);
printf(" \n Enter Y for correct N for not correct \n");
scanf("%c",&f);
if(f=='Y')
{
}
if(f=='N')
{
get_matrices(D,'D');
}
printf("\n Matrix E \n");
print_matrix(E);
printf(" \n Enter Y for correct N for not correct \n");
scanf("%c",&f);
if(f=='Y')
{
}
if(f=='N')
{
get_matrices(E,'E');
}
printf("\n Matrix F \n");
print_matrix(F);
printf(" \n Enter Y for correct N for not correct \n");
scanf("%c",&f);
if(f=='Y')
{
}
if(f=='N')
{
get_matrices(F,'F');
}
}
}
void column_away(int j)
{
//stepper turn mechanism
printf("\n");
printf("%d ",move_count++);
printf("\n Column Away %d \n \n",j);
y=2;
for(i=0;i<=2;i++)
{
temp[i][j]=E[i][j];
E[i][j]=B[i][j];
B[i][j]=F[i][j];
}
for(i=0;i<=2;i++)
{
if(j==0)
{
F[y][j]=D[i][j+2];
D[i][j+2]=temp[y][j];
}
if(j==2)
{
F[i][j]=D[y][j-2];
D[y][j-2]=temp[i][j];
}
y=y-1;
}
if(j==2)
{
circle_clockwise('C');
}
if(j==0)
{
circle_anticlockwise('A');
}
}
void column_toward(int j)
{
//stepper turn mechanism
printf("\n");
printf("%d ",move_count++);
printf("\n Column toward %d \n \n",j);
y=2;
for(i=0;i<=2;i++)
{
if(j==0)
{
temp[i][j]=D[y][j+2];
D[y][j+2]=F[i][j];
}
if(j==2)
{
temp[i][j]=D[y][j-2];
D[y][j-2]=F[i][j];
}
y=y-1;
}
for(i=0;i<=2;i++)
{
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
printf("\n");
printf("%d ",move_count++);
printf("\n Row Right %d \n \n",i);
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
printf("\n");
printf("%d ",move_count++);
printf("\n Row Left %d \n \n",i);
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 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
printf("\n");
printf("%d ",move_count++);
printf("\n Front Clockwise \n \n");
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];
circle_clockwise('B');
}
void front_anticlockwise(void)
{
//stepper turn mechanism
printf("\n");
printf("%d ",move_count++);
printf("\n Front Anticlockwise \n \n");
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];
circle_anticlockwise('B');
}
void calculate_edgepairs(void)
{
edgepair[0]=A[1][0]*10+D[1][2];
edgepair[1]=A[0][1]*10+E[1][0];
edgepair[2]=A[1][2]*10+B[1][0];
edgepair[3]=A[2][1]*10+F[1][0];
edgepair[4]=B[0][1]*10+E[2][1];
edgepair[5]=B[1][2]*10+C[1][0];
edgepair[6]=B[2][1]*10+F[0][1];
edgepair[7]=C[0][1]*10+E[1][2];
edgepair[8]=C[1][2]*10+D[1][0];
edgepair[9]=C[2][1]*10+F[1][2];
edgepair[10]=D[0][1]*10+E[0][1];
edgepair[11]=D[2][1]*10+F[2][1];
/*
for(d=0;d<=11;d++)
{
printf("\nEdge pair %d : %d\n",d,edgepair[d]);
}
*/
}
void turn_the_cube_virtually_twice(void)
{
//interchange A and C
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
temp[i][j]=0;
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
temp[i][j]=A[i][j];
A[i][j]=C[i][j];
C[i][j]=temp[i][j];
}
}
//interchange B and D
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
temp[i][j]=0;
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
temp[i][j]=B[i][j];
B[i][j]=D[i][j];
D[i][j]=temp[i][j];
}
}
//Turn E clockwise 2 times
circle_clockwise('E');
circle_clockwise('E');
//Turn F clockwise 2 times
circle_clockwise('F');
circle_clockwise('F');
}
void back_clk(void)
{
//stepper turn mechanism
printf("\n");
printf("%d ",move_count++);
printf("\n Back Clockwise \n \n");
printf("\n IMP: AVOID THE front clockwise PRINTED BELOW \n");
turn_the_cube_virtually_twice();
//now perform front clockwise rotation
front_clockwise();
turn_the_cube_virtually_twice();
}
void back_anticlk(void)
{
//stepper turn mechanism
printf("\n");
printf("%d ",move_count++);
printf("\n Back Anticlockwise \n \n");
printf("\n IMP: AVOID THE front anticlockwise PRINTED BELOW \n");
turn_the_cube_virtually_twice();
//now perform front anticlockwise rotation
front_anticlockwise();
turn_the_cube_virtually_twice();
}
void count_green(void)
{
calculate_edgepairs();
green_edges=0;
if(E[0][1]==6) green_edges+=1;
if(E[1][0]==6) green_edges+=1;
if(E[1][2]==6) green_edges+=1;
if(E[2][1]==6) green_edges+=1;
}
void empty(int a)
{
if(a==1)
{
while(E[1][0]==6)
{
row_left(0);
}
}
if(a==2)
{
while(E[2][1]==6)
{
row_left(0);
}
}
if(a==3)
{
while(E[1][2]==6)
{
row_left(0);
}
}
if(a==4)
{
while(E[0][1]==6)
{
row_left(0);
}
}
}
void solve_green_cross(void)
{
green_edges=0;
while(green_edges<4)
{
count_green();
if(edgepair[0]==16 || edgepair[0]==26 || edgepair[0]==36 || edgepair[0]==46)
{
empty(1);
column_toward(0);
calculate_edgepairs();
printf("\n");
}
if(edgepair[2]==16 || edgepair[2]==26 || edgepair[2]==36 || edgepair[2]==46)
{
empty(1);
column_away(0);
calculate_edgepairs();
printf("\n");
}
if(edgepair[3]==16 || edgepair[3]==26 || edgepair[3]==36 || edgepair[3]==46)
{
empty(1);
column_toward(0);
column_toward(0);
calculate_edgepairs();
printf("\n");
}
if(edgepair[5]==16 || edgepair[5]==26 || edgepair[5]==36 || edgepair[5]==46)
{
empty(2);
front_anticlockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[6]==16 || edgepair[6]==26 || edgepair[6]==36 || edgepair[6]==46)
{
empty(2);
front_clockwise();
front_clockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[2]==61 || edgepair[2]==62 || edgepair[2]==63 || edgepair[2]==64)
{
empty(2);
front_clockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[5]==61 || edgepair[5]==62 || edgepair[5]==63 || edgepair[5]==64)
{
empty(3);
column_away(2);
calculate_edgepairs();
printf("\n");
}
if(edgepair[8]==16 || edgepair[8]==26 || edgepair[8]==36 || edgepair[8]==46)
{
empty(3);
column_toward(2);
calculate_edgepairs();
printf("\n");
}
if(edgepair[9]==16 || edgepair[9]==26 || edgepair[9]==36 || edgepair[9]==46)
{
empty(3);
column_toward(2);
column_toward(2);
calculate_edgepairs();
printf("\n");
}
if(edgepair[8]==61 || edgepair[8]==62 || edgepair[8]==63 || edgepair[8]==64)
{
empty(4);
back_clk();
calculate_edgepairs();
printf("\n");
}
if(edgepair[0]==61 || edgepair[0]==62 || edgepair[0]==63 || edgepair[0]==64)
{
empty(4);
back_anticlk();
calculate_edgepairs();
printf("\n");
}
if(edgepair[11]==16 || edgepair[11]==26 || edgepair[11]==36 || edgepair[11]==46)
{
empty(4);
back_clk();
back_clk();
calculate_edgepairs();
printf("\n");
}
if(edgepair[1]==61 || edgepair[1]==62 || edgepair[1]==63 || edgepair[1]==64)
{
column_toward(0);
empty(2);
front_clockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[4]==61 || edgepair[4]==62 || edgepair[4]==63 || edgepair[4]==64)
{
front_clockwise();
empty(3);
column_away(2);
calculate_edgepairs();
printf("\n");
}
if(edgepair[3]==61 || edgepair[3]==62 || edgepair[3]==63 || edgepair[3]==64)
{
empty(1);
column_away(0);
empty(2);
front_clockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[6]==61 || edgepair[6]==62 || edgepair[6]==63 || edgepair[6]==64)
{
empty(2);
front_anticlockwise();
empty(3);
column_away(2);
calculate_edgepairs();
printf("\n");
}
if(edgepair[7]==61 || edgepair[7]==62 || edgepair[7]==63 || edgepair[7]==64)
{
column_toward(2);
empty(2);
front_anticlockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[10]==61 || edgepair[10]==62 || edgepair[10]==63 || edgepair[10]==64)
{
back_anticlk();
empty(3);
column_toward(2);
calculate_edgepairs();
printf("\n");
}
if(edgepair[9]==61 || edgepair[9]==62 || edgepair[9]==63 || edgepair[9]==64)
{
empty(3);
column_away(2);
empty(2);
front_anticlockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[11]==61 || edgepair[11]==62 || edgepair[11]==63 || edgepair[11]==64)
{
empty(4);
back_clk();
empty(3);
column_toward(2);
calculate_edgepairs();
printf("\n");
}
count_green();
}
//now all greens are on top(blue side)
for(t=0;t<=3;t++)
{
calculate_edgepairs();
if(edgepair[1]==16)
{
column_toward(0);
column_toward(0);
}
if(edgepair[7]==36)
{
column_toward(2);
column_toward(2);
}
if(edgepair[4]==26)
{
front_clockwise();
front_clockwise();
}
if(edgepair[10]==46)
{
back_clk();
back_clk();
}
row_left(0);
}
}
void calculate_cornerpairs(void)
{
cornerpair[0]=D[0][2]*100+A[0][0]*10+E[0][0];
cornerpair[1]=A[0][2]*100+B[0][0]*10+E[2][0];
cornerpair[5]=A[2][2]*100+B[2][0]*10+F[0][0];
cornerpair[4]=D[2][2]*100+A[2][0]*10+F[2][0];
cornerpair[2]=B[0][2]*100+C[0][0]*10+E[2][2];
cornerpair[6]=B[2][2]*100+C[2][0]*10+F[0][2];
cornerpair[3]=C[0][2]*100+D[0][0]*10+E[0][2];
cornerpair[7]=C[2][2]*100+D[2][0]*10+F[2][2];
/*
for(d=0;d<=7;d++)
{
printf("\n Cornerpair[%d] = %d \n",d,cornerpair[d]);
}
*/
}
void calculate_green_corners_on_top(void)
{
green_corners=0;
if(A[0][0]==6) green_corners+=1;
if(A[0][2]==6) green_corners+=1;
if(B[0][0]==6) green_corners+=1;
if(B[0][2]==6) green_corners+=1;
if(C[0][0]==6) green_corners+=1;
if(C[0][2]==6) green_corners+=1;
if(D[0][0]==6) green_corners+=1;
if(D[0][2]==6) green_corners+=1;
if(E[0][0]==6) green_corners+=1;
if(E[0][2]==6) green_corners+=1;
if(E[2][0]==6) green_corners+=1;
if(E[2][2]==6) green_corners+=1;
//if green_corners=0,it means there is no green on top layer and we have to bring a green corner to top;
if(green_corners==0)
{
if(cornerpair[4]!=416)
{
column_toward(0);
row_left(0);
column_away(0);
}
else if(cornerpair[5]!=126)
{
column_away(0);
row_right(0);
column_toward(0);
}
else if(cornerpair[6]!=236)
{
column_away(2);
row_left(0);
column_toward(2);
}
else if(cornerpair[7]!=346)
{
column_toward(2);
row_right(0);
column_away(2);
}
else
{
t=1;//helps break from the while loop of solve_green_cornes();
}
calculate_cornerpairs();
}
}
void put_green_corners_in_place(int a)
{
if(a==0)
{
if(cornerpair[0]==461)
{
back_clk();
column_away(0);
back_anticlk();
column_toward(0);
}
if(cornerpair[0]==614)
{
column_away(0);
back_clk();
column_toward(0);
back_anticlk();
}
if(cornerpair[0]==146)
{
column_toward(0);
row_right(0);
column_away(0);
row_right(0);
row_right(0);
back_clk();
column_away(0);
back_anticlk();
column_toward(0);
}
}
if(a==1)
{
if(cornerpair[1]==621)
{
front_anticlockwise();
column_toward(0);
front_clockwise();
column_away(0);
}
if(cornerpair[1]==162)
{
column_toward(0);
front_anticlockwise();
column_away(0);
front_clockwise();
}
if(cornerpair[1]==216)
{
column_away(0);
row_left(0);
column_toward(0);
row_left(0);
row_left(0);
front_anticlockwise();
column_toward(0);
front_clockwise();
column_away(0);
}
}
if(a==2)
{
if(cornerpair[2]==326)
{
column_away(2);
row_right(0);
column_toward(2);
row_left(0);
row_left(0);
front_clockwise();
column_toward(2);
front_anticlockwise();
column_away(2);
}
if(cornerpair[2]==632)
{
column_toward(2);
front_clockwise();
column_away(2);
front_anticlockwise();
}
if(cornerpair[2]==263)
{
front_clockwise();
column_toward(2);
front_anticlockwise();
column_away(2);
}
}
if(a==3)
{
if(cornerpair[3]==436)
{
column_toward(2);
row_left(0);
column_away(2);
row_right(0);
row_right(0);
back_anticlk();
column_away(2);
back_clk();
column_toward(2);
}
if(cornerpair[3]==364)
{
column_away(2);
back_anticlk();
column_toward(2);
back_clk();
}
if(cornerpair[3]==643)
{
back_anticlk();
column_away(2);
back_clk();
column_toward(2);
}
}
calculate_cornerpairs();
}
void place_correct_green_corners_on_top(void)
{
if(cornerpair[0]==461 || cornerpair[0]==614 || cornerpair[0]==146 )
{
//its in the right place
put_green_corners_in_place(0);
}
if(cornerpair[0]==621 || cornerpair[0]==216 || cornerpair[0]==162 )
{
row_right(0);
put_green_corners_in_place(1);
}
if(cornerpair[0]==263 || cornerpair[0]==326 || cornerpair[0]==632 )
{
row_right(0);
row_right(0);
put_green_corners_in_place(2);
}
if(cornerpair[0]==643 || cornerpair[0]==364 || cornerpair[0]==436 )
{
row_left(0);
put_green_corners_in_place(3);
}
if(cornerpair[1]==621 || cornerpair[1]==216 || cornerpair[1]==162 )
{
//its in the right place
put_green_corners_in_place(1);
}
if(cornerpair[1]==461 || cornerpair[1]==614 || cornerpair[1]==146 )
{
row_left(0);
put_green_corners_in_place(0);
}
if(cornerpair[1]==263 || cornerpair[1]==326 || cornerpair[1]==632 )
{
row_right(0);
put_green_corners_in_place(2);
}
if(cornerpair[1]==643 || cornerpair[1]==364 || cornerpair[1]==436 )
{
row_left(0);
row_left(0);
put_green_corners_in_place(3);
}
if(cornerpair[2]==263 || cornerpair[2]==326 || cornerpair[2]==632 )
{
//its in the right place
put_green_corners_in_place(2);
}
if(cornerpair[2]==621 || cornerpair[2]==216 || cornerpair[2]==162 )
{
row_left(0);
put_green_corners_in_place(1);
}
if(cornerpair[2]==461 || cornerpair[2]==614 || cornerpair[2]==146 )
{
row_left(0);
row_left(0);
put_green_corners_in_place(0);
}
if(cornerpair[2]==643 || cornerpair[2]==364 || cornerpair[2]==436 )
{
row_right(0);
put_green_corners_in_place(3);
}
if(cornerpair[3]==643 || cornerpair[3]==364 || cornerpair[3]==436 )
{
//its in the right place
put_green_corners_in_place(3);
}
if(cornerpair[3]==263 || cornerpair[3]==326 || cornerpair[3]==632 )
{
row_left(0);
put_green_corners_in_place(2);
}
if(cornerpair[3]==621 || cornerpair[3]==216 || cornerpair[3]==162 )
{
row_left(0);
row_left(0);
put_green_corners_in_place(1);
}
if(cornerpair[3]==461 || cornerpair[3]==614 || cornerpair[3]==146 )
{
row_right(0);
put_green_corners_in_place(0);
}
}
void solve_green_corners(void)
{
t=0;
while(t==0)
{
calculate_cornerpairs();
calculate_green_corners_on_top();
place_correct_green_corners_on_top();
// called by above function internally: put_green_corners_in_place();
}
}
void get_middle_layer_count(void)
{
middle_layer_count=0;
if(A[0][1]==5) middle_layer_count+=1;
if(B[0][1]==5) middle_layer_count+=1;
if(C[0][1]==5) middle_layer_count+=1;
if(D[0][1]==5) middle_layer_count+=1;
if(E[0][1]==5) middle_layer_count+=1;
if(E[1][0]==5) middle_layer_count+=1;
if(E[1][2]==5) middle_layer_count+=1;
if(E[2][1]==5) middle_layer_count+=1;
if(middle_layer_count==4)//it means all top edges contain blue.so bring one unsolved middle edge up
{
if(edgepair[2]!=12)
{
column_away(0);
row_left(0);
column_toward(0);
row_left(0);
front_clockwise();
row_right(0);
front_anticlockwise();
}
else if(edgepair[5]!=23)
{
column_away(2);
row_right(0);
column_toward(2);
row_right(0);
front_anticlockwise();
row_left(0);
front_clockwise();
}
else if(edgepair[8]!=34)
{
column_toward(2);
row_left(0);
column_away(2);
row_left(0);
back_clk();
row_right(0);
back_anticlk();
}
else if(edgepair[0]!=14)
{
column_toward(0);
row_right(0);
column_away(0);
row_right(0);
back_anticlk();
row_left(0);
back_clk();
}
else
{
t=1;//middle layer is solved.we have to break from while loop
}
calculate_edgepairs();
}
}
void put_middle_piece_in_place(void)
{
if(edgepair[1]==12)
{
row_left(0);
solve_middle_piece(2);
}
if(edgepair[1]==21)
{
row_right(0);
row_right(0);
solve_middle_piece(3);
}
if(edgepair[1]==23)
{
//no movements
solve_middle_piece(4);
}
if(edgepair[1]==32)
{
row_left(0);
solve_middle_piece(5);
}
if(edgepair[1]==34)
{
row_right(0);
solve_middle_piece(6);
}
if(edgepair[1]==43)
{
//no movements
solve_middle_piece(7);
}
if(edgepair[1]==41)
{
row_right(0);
row_right(0);
solve_middle_piece(0);
}
if(edgepair[1]==14)
{
row_right(0);
solve_middle_piece(1);
}
if(edgepair[4]==12)
{
row_right(0);
row_right(0);
solve_middle_piece(2);
}
if(edgepair[4]==21)
{
row_right(0);
solve_middle_piece(3);
}
if(edgepair[4]==23)
{
row_left(0);
solve_middle_piece(4);
}
if(edgepair[4]==32)
{
row_right(0);
row_right(0);
solve_middle_piece(5);
}
if(edgepair[4]==34)
{
//no movements
solve_middle_piece(6);
}
if(edgepair[4]==43)
{
row_left(0);
solve_middle_piece(7);
}
if(edgepair[4]==41)
{
row_right(0);
solve_middle_piece(0);
}
if(edgepair[4]==14)
{
//no movements
solve_middle_piece(1);
}
if(edgepair[7]==12)
{
row_right(0);
solve_middle_piece(2);
}
if(edgepair[7]==21)
{
//no movements
solve_middle_piece(3);
}
if(edgepair[7]==23)
{
row_right(0);
row_right(0);
solve_middle_piece(4);
}
if(edgepair[7]==32)
{
row_right(0);
solve_middle_piece(5);
}
if(edgepair[7]==34)
{
row_left(0);
solve_middle_piece(6);
}
if(edgepair[7]==43)
{
row_right(0);
row_right(0);
solve_middle_piece(7);
}
if(edgepair[7]==41)
{
//no movements
solve_middle_piece(0);
}
if(edgepair[7]==14)
{
row_left(0);
solve_middle_piece(1);
}
if(edgepair[10]==12)
{
//no movements
solve_middle_piece(2);
}
if(edgepair[10]==21)
{
row_left(0);
solve_middle_piece(3);
}
if(edgepair[10]==23)
{
row_right(0);
solve_middle_piece(4);
}
if(edgepair[10]==32)
{
//no movements
solve_middle_piece(5);
}
if(edgepair[10]==34)
{
row_right(0);
row_right(0);
solve_middle_piece(6);
}
if(edgepair[10]==43)
{
row_right(0);
solve_middle_piece(7);
}
if(edgepair[10]==41)
{
row_left(0);
solve_middle_piece(0);
}
if(edgepair[10]==14)
{
row_right(0);
row_right(0);
solve_middle_piece(1);
}
}
void solve_middle_piece(int a)
{
if(a==0)
{
column_toward(0);
row_right(0);
column_away(0);
row_right(0);
back_anticlk();
row_left(0);
back_clk();
}
if(a==1)
{
back_anticlk();
row_left(0);
back_clk();
row_left(0);
column_toward(0);
row_right(0);
column_away(0);
}
if(a==2)
{
front_clockwise();
row_right(0);
front_anticlockwise();
row_right(0);
column_away(0);
row_left(0);
column_toward(0);
}
if(a==3)
{
column_away(0);
row_left(0);
column_toward(0);
row_left(0);
front_clockwise();
row_right(0);
front_anticlockwise();
}
if(a==4)
{
column_away(2);
row_right(0);
column_toward(2);
row_right(0);
front_anticlockwise();
row_left(0);
front_clockwise();
}
if(a==5)
{
front_anticlockwise();
row_left(0);
front_clockwise();
row_left(0);
column_away(2);
row_right(0);
column_toward(2);
}
if(a==6)
{
back_clk();
row_right(0);
back_anticlk();
row_right(0);
column_toward(2);
row_left(0);
column_away(2);
}
if(a==7)
{
column_toward(2);
row_left(0);
column_away(2);
row_left(0);
back_clk();
row_right(0);
back_anticlk();
}
}
void solve_middle_layer(void)
{
t=0;
while(t==0)
{
calculate_edgepairs();
get_middle_layer_count();
put_middle_piece_in_place();
//solve middle piece is called inside put middle piece in place
}
}
void solve_top_cross(void)
{
top_cross_count=0;
if(E[0][1]==5) top_cross_count+=1;
if(E[1][0]==5) top_cross_count+=1;
if(E[1][1]==5) top_cross_count+=1;
if(E[1][2]==5) top_cross_count+=1;
if(E[2][1]==5) top_cross_count+=1;
if(top_cross_count==1) //it is a dot
{
front_clockwise();
column_away(2);
row_left(0);
column_toward(2);
row_right(0);
front_anticlockwise();
row_left(0);
row_left(0);
//////////////////////////////////
top_cross_count=3;
//at end,we get inverted L
}
if(top_cross_count==3)
{
if(E[0][1]+E[2][1]==10 || E[1][0]+E[1][2]==10)
{
if(E[0][1]==5 && E[1][1]==5 && E[2][1]==5)
{
row_left(0);
}
if(E[1][0]==5 && E[1][1]==5 && E[1][2]==5)
{
front_clockwise();
column_away(2);
row_left(0);
column_toward(2);
row_right(0);
front_anticlockwise();
}
}
else
{
while(E[0][1]+E[1][0]!=10)
{
row_left(0);
}
front_clockwise();
column_away(2);
row_left(0);
column_toward(2);
row_right(0);
column_away(2);
row_left(0);
column_toward(2);
row_right(0);
front_anticlockwise();
}
//at the end,cross is solved
}
}
void get_blue_count(void)
{
blue_count=0;
if(E[0][0]==5) blue_count+=1;
if(E[0][2]==5) blue_count+=1;
if(E[2][0]==5) blue_count+=1;
if(E[2][2]==5) blue_count+=1;
if(blue_count==4)
{
t=1;
}
}
void repeat_pattern(void)
{
column_away(2);
row_left(0);
column_toward(2);
row_left(0);
column_away(2);
row_left(0);
row_left(0);
column_toward(2);
}
void solve_top_blue(void)
{
t=0;
while(t==0)
{
get_blue_count();
if(blue_count==0)
{
repeat_pattern();
}
get_blue_count();
if(blue_count==2)
{
while(E[0][2]+E[2][0]!=10 && E[0][2]+E[2][2]!=10)
{
row_left(0);
}
if(E[0][2]+E[2][0]==10)
{
repeat_pattern();
}
repeat_pattern();
}
get_blue_count();
if(blue_count==1)
{
while(E[2][0]!=5)
{
row_left(0);
}
repeat_pattern();
}
}
}
void fix_top_blue_corners(void)
{
calculate_cornerpairs();
while(cornerpair[0]!=415 || cornerpair[1]!=125 || cornerpair[2]!=235 || cornerpair[3]!=345)
{
if(A[0][0]!=A[0][2] && B[0][0]!=B[0][2] && C[0][0]!=C[0][2] && D[0][0]!=D[0][2])
{
column_toward(2);
front_clockwise();
column_toward(2);
back_clk();
back_clk();
column_away(2);
front_anticlockwise();
column_toward(2);
back_clk();
back_clk();
column_toward(2);
column_toward(2);
}
if(A[0][0]==A[0][2] || B[0][0]==B[0][2] || C[0][0]==C[0][2] || D[0][0]==D[0][2])
{
while(D[0][0]!=D[0][2])
{
row_left(0);
}
if(A[0][0]!=A[0][2] && B[0][0]!=B[0][2] && C[0][0]!=C[0][2] && D[0][0]==D[0][2])
{
column_toward(2);
front_clockwise();
column_toward(2);
back_clk();
back_clk();
column_away(2);
front_anticlockwise();
column_toward(2);
back_clk();
back_clk();
column_toward(2);
column_toward(2);
}
else
{
row_left(0);
}
}
calculate_cornerpairs();
}
}
/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
** This file was created by Processor Expert 10.3 [05.09]
** for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/
My project has now finally reached a stage where I can take a deep breath and relax.
After tons of coding and checking,I have finally been able to almost solve the cube.The only part left is to align the top edges,rotate the steppers,build the frame and I'm done.
Please patiently go through the code,and feel free to use or modify it.
Note: 1.There are cases where there are a lot of redundant moves.
2.I forgot to mention this but the algorithm used is the beginners algorithm,not the advanced one's like Kociemba. So,a huge solution is guaranteed,but it's a solution nonetheless!!
/* ###################################################################
** Filename : main.c
** Project : RUBIKS CUBE SOLVER
** Processor : MKL25Z128VLK4
** Version : Driver 01.01
** Compiler : GNU C Compiler
** Date/Time : 2016-01-11, 09:39, # CodeGen: 0
** Abstract :
** Main module.
** This module contains user's application code.
** Settings :
** Contents :
** No public methods
**
** ###################################################################*/
/*!
** @file main.c
** @version 01.01
** @brief
** Main module.
** This module contains user's application code.
*/
/*!
** @addtogroup main_module main module documentation
** @{
*/
/* MODULE main */
/*
* array format
* E
* A B C D
* F
* */
/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "Events.h"
#include "CsIO1.h"
#include "IO1.h"
/* Including shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
//colour - 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
//the values are just initially assigned.they WILL change later on when the colours are manually input.
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}};
int matrix_name[3][3]; //only to be used in change_matrix_name();
int temp[3][3]; //temp 3x3 matrix to store the values of the 6 matrices
int temp1;
int temp2;
int i; //used in the row count for the 6 matrices
int j; //used in the column count of the 6 matrices
int d=0; //temp variable to be used
int t=0;
int y=2; //used to solve the "D" matrix problem in column_away(); and column_toward();
char c=0; //used in print_matrix() function;
char f=0; //used to get Y or N in check_cubes_correctness();
int count=0; //holds the count in check_cubes_correctness();
int cross_value[4][3]; //holds the locations of the cross forming edges of B matrix
int edgepair[12]; //holds the values of all the 12 edges in the cube
int green_edges=0;
int cornerpair[8];
int green_corners=0;
int middle_layer_count=0;
int top_cross_count=0;
int blue_count=0;
int move_count=0;
//ALL THE FUNCTION DEFINED BELOW DOES EXACTLY WHAT ITS NAME TELLS!!
void get_matrices(int *,char);
void check_cubes_correctness(void);
void print_matrix(int *);//prints out a single matrix
void print_matrices(void);//prints out all 6 matrices
void column_away(int);
void column_toward(int);
void row_right(int);
void row_left(int);
void front_clockwise(void);
void front_anticlockwise(void);
void turn_the_cube_virtually_twice(void);
void back_clk(void);
void back_anticlk(void);
void circle_clockwise(char);
void circle_anticlockwise(char);
void change_matrix_name(int,char);//0=>matrix to matrix_name
//1=>matrix_name to matrix
//void search_cross_location(void);
void calculate_edgepairs(void);
//void solve_for_cross(void);
void count_green(void);
void empty(int);
void solve_green_cross(void);
void calculate_cornerpairs(void);
void calculate_green_corners_on_top(void);
void place_correct_green_corners_on_top(void);
void put_green_corners_in_place(int);
void solve_green_corners(void);
void get_middle_layer_count(void);
void solve_middle_piece(int);
void put_middle_piece_in_place(void);
void solve_middle_layer(void);
void solve_top_cross(void);
void get_blue_count(void);
void repeat_pattern(void);
void solve_top_blue(void);
void fix_top_blue_corners(void);
/* User includes (#include below this line is not maintained by Processor Expert) */
/*lint -save -e970 Disable MISRA rule (6.3) checking. */
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
/* Write your local variable definition here */
/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
PE_low_level_init();
/*** End of Processor Expert internal initialization. ***/
/* Write your code here */
/* For example: for(;;) { } */
//hope u work
/*
how i solve the cube(Beginners method)
1.form plus
2.solve first layer
3.solve for middle layer
4.form top cross
5.Solve top layer
6.orient top corners in place
7.orient top edges
8.end up with solved cube
*/
printf("\n Color - number code\n");
printf("\n yellow 1 \n orange 2 \n white 3 \n red 4 \n blue 5 \n green 6");
get_matrices(A,'A');
get_matrices(B,'B');
get_matrices(C,'C');
get_matrices(D,'D');
get_matrices(E,'E');
get_matrices(F,'F');
check_cubes_correctness();
while(count!=189)
{
check_cubes_correctness();
}
//this condition is added because in the solve green cross function,all the green edges are brought on top
//and then solved at the bottom.the problem is when the green cross is solved,then too,they will be brought
//above and solved.so,this case is to bypass that situation.
calculate_edgepairs();
if(edgepair[3]!=16 || edgepair[6]!=26 || edgepair[9]!=36 || edgepair[11]!=46)
{
solve_green_cross();
}
printf("Bottom Cross solved!!\n");
solve_green_corners();
printf("Bottom Corners are solved!!\n");
solve_middle_layer();
printf("Middle layer solved!!\n");
solve_top_cross();
printf("Top cross solved!!\n");
solve_top_blue();
printf("Top Layer solved!!");
fix_top_blue_corners();
printf("Top Blue Corners Fixed!!");
print_matrices();
while(1)
{
}
/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
/*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
#ifdef PEX_RTOS_START
PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of RTOS startup code. ***/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;){}
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
void get_matrices(int *z,char b)
{
printf("\n Enter matrix %c: \n",b);
//empty the temp matrix
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
temp[i][j]=0;
}
}
//take input and store in temp matrix as characters
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%c",&temp[i][j]);
printf("%c",temp[i][j]);
}
}
//copy from temp to appropriate matrix
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
*z=temp[i][j]-48;
z++;
}
}
}
//here,the values 123456 are correspondingly hard-coded with the colours YOWRBG
//because this is how the colours are arranged in my cube.It is not the case with all the cubes and if a cube with
//a different colour orientation is used,make sure to change these hard-coded values.
void print_matrix(int *a)
{
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(*a==1) c='Y';
if(*a==2) c='O';
if(*a==3) c='W';
if(*a==4) c='R';
if(*a==5) c='B';
if(*a==6) c='G';
printf("%c\t\t",c);
a++;
}
printf("\n");
}
printf("\n\n");
}
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 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 of the numbers.
//if the value ends up to be 189,the cube is valid
count=0;
printf("\n\n 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("\n count = %d \n", count);
if(count==189)
{
printf("\n valid cube\n\n");
}
else
{
printf("\n invalid cube.re-enter values.\n\n");
//code to rescan all 6 sides again
printf("\n Enter Y or N in \"Caps\" \n");
printf("\n Matrix A \n");
print_matrix(A);
printf(" \n Enter Y for correct N for not correct \n");
scanf("%c",&f);
if(f=='Y')
{
}
if(f=='N')
{
get_matrices(A,'A');
}
printf("\n Matrix B \n");
print_matrix(B);
printf(" \n Enter Y for correct N for not correct \n");
scanf("%c",&f);
if(f=='Y')
{
}
if(f=='N')
{
get_matrices(B,'B');
}
printf("\n Matrix C \n");
print_matrix(C);
printf(" \n Enter Y for correct N for not correct \n");
scanf("%c",&f);
if(f=='Y')
{
}
if(f=='N')
{
get_matrices(C,'C');
}
printf("\n Matrix D \n");
print_matrix(D);
printf(" \n Enter Y for correct N for not correct \n");
scanf("%c",&f);
if(f=='Y')
{
}
if(f=='N')
{
get_matrices(D,'D');
}
printf("\n Matrix E \n");
print_matrix(E);
printf(" \n Enter Y for correct N for not correct \n");
scanf("%c",&f);
if(f=='Y')
{
}
if(f=='N')
{
get_matrices(E,'E');
}
printf("\n Matrix F \n");
print_matrix(F);
printf(" \n Enter Y for correct N for not correct \n");
scanf("%c",&f);
if(f=='Y')
{
}
if(f=='N')
{
get_matrices(F,'F');
}
}
}
void column_away(int j)
{
//stepper turn mechanism
printf("\n");
printf("%d ",move_count++);
printf("\n Column Away %d \n \n",j);
y=2;
for(i=0;i<=2;i++)
{
temp[i][j]=E[i][j];
E[i][j]=B[i][j];
B[i][j]=F[i][j];
}
for(i=0;i<=2;i++)
{
if(j==0)
{
F[y][j]=D[i][j+2];
D[i][j+2]=temp[y][j];
}
if(j==2)
{
F[i][j]=D[y][j-2];
D[y][j-2]=temp[i][j];
}
y=y-1;
}
if(j==2)
{
circle_clockwise('C');
}
if(j==0)
{
circle_anticlockwise('A');
}
}
void column_toward(int j)
{
//stepper turn mechanism
printf("\n");
printf("%d ",move_count++);
printf("\n Column toward %d \n \n",j);
y=2;
for(i=0;i<=2;i++)
{
if(j==0)
{
temp[i][j]=D[y][j+2];
D[y][j+2]=F[i][j];
}
if(j==2)
{
temp[i][j]=D[y][j-2];
D[y][j-2]=F[i][j];
}
y=y-1;
}
for(i=0;i<=2;i++)
{
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
printf("\n");
printf("%d ",move_count++);
printf("\n Row Right %d \n \n",i);
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
printf("\n");
printf("%d ",move_count++);
printf("\n Row Left %d \n \n",i);
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 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
printf("\n");
printf("%d ",move_count++);
printf("\n Front Clockwise \n \n");
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];
circle_clockwise('B');
}
void front_anticlockwise(void)
{
//stepper turn mechanism
printf("\n");
printf("%d ",move_count++);
printf("\n Front Anticlockwise \n \n");
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];
circle_anticlockwise('B');
}
void calculate_edgepairs(void)
{
edgepair[0]=A[1][0]*10+D[1][2];
edgepair[1]=A[0][1]*10+E[1][0];
edgepair[2]=A[1][2]*10+B[1][0];
edgepair[3]=A[2][1]*10+F[1][0];
edgepair[4]=B[0][1]*10+E[2][1];
edgepair[5]=B[1][2]*10+C[1][0];
edgepair[6]=B[2][1]*10+F[0][1];
edgepair[7]=C[0][1]*10+E[1][2];
edgepair[8]=C[1][2]*10+D[1][0];
edgepair[9]=C[2][1]*10+F[1][2];
edgepair[10]=D[0][1]*10+E[0][1];
edgepair[11]=D[2][1]*10+F[2][1];
/*
for(d=0;d<=11;d++)
{
printf("\nEdge pair %d : %d\n",d,edgepair[d]);
}
*/
}
void turn_the_cube_virtually_twice(void)
{
//interchange A and C
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
temp[i][j]=0;
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
temp[i][j]=A[i][j];
A[i][j]=C[i][j];
C[i][j]=temp[i][j];
}
}
//interchange B and D
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
temp[i][j]=0;
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
temp[i][j]=B[i][j];
B[i][j]=D[i][j];
D[i][j]=temp[i][j];
}
}
//Turn E clockwise 2 times
circle_clockwise('E');
circle_clockwise('E');
//Turn F clockwise 2 times
circle_clockwise('F');
circle_clockwise('F');
}
void back_clk(void)
{
//stepper turn mechanism
printf("\n");
printf("%d ",move_count++);
printf("\n Back Clockwise \n \n");
printf("\n IMP: AVOID THE front clockwise PRINTED BELOW \n");
turn_the_cube_virtually_twice();
//now perform front clockwise rotation
front_clockwise();
turn_the_cube_virtually_twice();
}
void back_anticlk(void)
{
//stepper turn mechanism
printf("\n");
printf("%d ",move_count++);
printf("\n Back Anticlockwise \n \n");
printf("\n IMP: AVOID THE front anticlockwise PRINTED BELOW \n");
turn_the_cube_virtually_twice();
//now perform front anticlockwise rotation
front_anticlockwise();
turn_the_cube_virtually_twice();
}
void count_green(void)
{
calculate_edgepairs();
green_edges=0;
if(E[0][1]==6) green_edges+=1;
if(E[1][0]==6) green_edges+=1;
if(E[1][2]==6) green_edges+=1;
if(E[2][1]==6) green_edges+=1;
}
void empty(int a)
{
if(a==1)
{
while(E[1][0]==6)
{
row_left(0);
}
}
if(a==2)
{
while(E[2][1]==6)
{
row_left(0);
}
}
if(a==3)
{
while(E[1][2]==6)
{
row_left(0);
}
}
if(a==4)
{
while(E[0][1]==6)
{
row_left(0);
}
}
}
void solve_green_cross(void)
{
green_edges=0;
while(green_edges<4)
{
count_green();
if(edgepair[0]==16 || edgepair[0]==26 || edgepair[0]==36 || edgepair[0]==46)
{
empty(1);
column_toward(0);
calculate_edgepairs();
printf("\n");
}
if(edgepair[2]==16 || edgepair[2]==26 || edgepair[2]==36 || edgepair[2]==46)
{
empty(1);
column_away(0);
calculate_edgepairs();
printf("\n");
}
if(edgepair[3]==16 || edgepair[3]==26 || edgepair[3]==36 || edgepair[3]==46)
{
empty(1);
column_toward(0);
column_toward(0);
calculate_edgepairs();
printf("\n");
}
if(edgepair[5]==16 || edgepair[5]==26 || edgepair[5]==36 || edgepair[5]==46)
{
empty(2);
front_anticlockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[6]==16 || edgepair[6]==26 || edgepair[6]==36 || edgepair[6]==46)
{
empty(2);
front_clockwise();
front_clockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[2]==61 || edgepair[2]==62 || edgepair[2]==63 || edgepair[2]==64)
{
empty(2);
front_clockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[5]==61 || edgepair[5]==62 || edgepair[5]==63 || edgepair[5]==64)
{
empty(3);
column_away(2);
calculate_edgepairs();
printf("\n");
}
if(edgepair[8]==16 || edgepair[8]==26 || edgepair[8]==36 || edgepair[8]==46)
{
empty(3);
column_toward(2);
calculate_edgepairs();
printf("\n");
}
if(edgepair[9]==16 || edgepair[9]==26 || edgepair[9]==36 || edgepair[9]==46)
{
empty(3);
column_toward(2);
column_toward(2);
calculate_edgepairs();
printf("\n");
}
if(edgepair[8]==61 || edgepair[8]==62 || edgepair[8]==63 || edgepair[8]==64)
{
empty(4);
back_clk();
calculate_edgepairs();
printf("\n");
}
if(edgepair[0]==61 || edgepair[0]==62 || edgepair[0]==63 || edgepair[0]==64)
{
empty(4);
back_anticlk();
calculate_edgepairs();
printf("\n");
}
if(edgepair[11]==16 || edgepair[11]==26 || edgepair[11]==36 || edgepair[11]==46)
{
empty(4);
back_clk();
back_clk();
calculate_edgepairs();
printf("\n");
}
if(edgepair[1]==61 || edgepair[1]==62 || edgepair[1]==63 || edgepair[1]==64)
{
column_toward(0);
empty(2);
front_clockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[4]==61 || edgepair[4]==62 || edgepair[4]==63 || edgepair[4]==64)
{
front_clockwise();
empty(3);
column_away(2);
calculate_edgepairs();
printf("\n");
}
if(edgepair[3]==61 || edgepair[3]==62 || edgepair[3]==63 || edgepair[3]==64)
{
empty(1);
column_away(0);
empty(2);
front_clockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[6]==61 || edgepair[6]==62 || edgepair[6]==63 || edgepair[6]==64)
{
empty(2);
front_anticlockwise();
empty(3);
column_away(2);
calculate_edgepairs();
printf("\n");
}
if(edgepair[7]==61 || edgepair[7]==62 || edgepair[7]==63 || edgepair[7]==64)
{
column_toward(2);
empty(2);
front_anticlockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[10]==61 || edgepair[10]==62 || edgepair[10]==63 || edgepair[10]==64)
{
back_anticlk();
empty(3);
column_toward(2);
calculate_edgepairs();
printf("\n");
}
if(edgepair[9]==61 || edgepair[9]==62 || edgepair[9]==63 || edgepair[9]==64)
{
empty(3);
column_away(2);
empty(2);
front_anticlockwise();
calculate_edgepairs();
printf("\n");
}
if(edgepair[11]==61 || edgepair[11]==62 || edgepair[11]==63 || edgepair[11]==64)
{
empty(4);
back_clk();
empty(3);
column_toward(2);
calculate_edgepairs();
printf("\n");
}
count_green();
}
//now all greens are on top(blue side)
for(t=0;t<=3;t++)
{
calculate_edgepairs();
if(edgepair[1]==16)
{
column_toward(0);
column_toward(0);
}
if(edgepair[7]==36)
{
column_toward(2);
column_toward(2);
}
if(edgepair[4]==26)
{
front_clockwise();
front_clockwise();
}
if(edgepair[10]==46)
{
back_clk();
back_clk();
}
row_left(0);
}
}
void calculate_cornerpairs(void)
{
cornerpair[0]=D[0][2]*100+A[0][0]*10+E[0][0];
cornerpair[1]=A[0][2]*100+B[0][0]*10+E[2][0];
cornerpair[5]=A[2][2]*100+B[2][0]*10+F[0][0];
cornerpair[4]=D[2][2]*100+A[2][0]*10+F[2][0];
cornerpair[2]=B[0][2]*100+C[0][0]*10+E[2][2];
cornerpair[6]=B[2][2]*100+C[2][0]*10+F[0][2];
cornerpair[3]=C[0][2]*100+D[0][0]*10+E[0][2];
cornerpair[7]=C[2][2]*100+D[2][0]*10+F[2][2];
/*
for(d=0;d<=7;d++)
{
printf("\n Cornerpair[%d] = %d \n",d,cornerpair[d]);
}
*/
}
void calculate_green_corners_on_top(void)
{
green_corners=0;
if(A[0][0]==6) green_corners+=1;
if(A[0][2]==6) green_corners+=1;
if(B[0][0]==6) green_corners+=1;
if(B[0][2]==6) green_corners+=1;
if(C[0][0]==6) green_corners+=1;
if(C[0][2]==6) green_corners+=1;
if(D[0][0]==6) green_corners+=1;
if(D[0][2]==6) green_corners+=1;
if(E[0][0]==6) green_corners+=1;
if(E[0][2]==6) green_corners+=1;
if(E[2][0]==6) green_corners+=1;
if(E[2][2]==6) green_corners+=1;
//if green_corners=0,it means there is no green on top layer and we have to bring a green corner to top;
if(green_corners==0)
{
if(cornerpair[4]!=416)
{
column_toward(0);
row_left(0);
column_away(0);
}
else if(cornerpair[5]!=126)
{
column_away(0);
row_right(0);
column_toward(0);
}
else if(cornerpair[6]!=236)
{
column_away(2);
row_left(0);
column_toward(2);
}
else if(cornerpair[7]!=346)
{
column_toward(2);
row_right(0);
column_away(2);
}
else
{
t=1;//helps break from the while loop of solve_green_cornes();
}
calculate_cornerpairs();
}
}
void put_green_corners_in_place(int a)
{
if(a==0)
{
if(cornerpair[0]==461)
{
back_clk();
column_away(0);
back_anticlk();
column_toward(0);
}
if(cornerpair[0]==614)
{
column_away(0);
back_clk();
column_toward(0);
back_anticlk();
}
if(cornerpair[0]==146)
{
column_toward(0);
row_right(0);
column_away(0);
row_right(0);
row_right(0);
back_clk();
column_away(0);
back_anticlk();
column_toward(0);
}
}
if(a==1)
{
if(cornerpair[1]==621)
{
front_anticlockwise();
column_toward(0);
front_clockwise();
column_away(0);
}
if(cornerpair[1]==162)
{
column_toward(0);
front_anticlockwise();
column_away(0);
front_clockwise();
}
if(cornerpair[1]==216)
{
column_away(0);
row_left(0);
column_toward(0);
row_left(0);
row_left(0);
front_anticlockwise();
column_toward(0);
front_clockwise();
column_away(0);
}
}
if(a==2)
{
if(cornerpair[2]==326)
{
column_away(2);
row_right(0);
column_toward(2);
row_left(0);
row_left(0);
front_clockwise();
column_toward(2);
front_anticlockwise();
column_away(2);
}
if(cornerpair[2]==632)
{
column_toward(2);
front_clockwise();
column_away(2);
front_anticlockwise();
}
if(cornerpair[2]==263)
{
front_clockwise();
column_toward(2);
front_anticlockwise();
column_away(2);
}
}
if(a==3)
{
if(cornerpair[3]==436)
{
column_toward(2);
row_left(0);
column_away(2);
row_right(0);
row_right(0);
back_anticlk();
column_away(2);
back_clk();
column_toward(2);
}
if(cornerpair[3]==364)
{
column_away(2);
back_anticlk();
column_toward(2);
back_clk();
}
if(cornerpair[3]==643)
{
back_anticlk();
column_away(2);
back_clk();
column_toward(2);
}
}
calculate_cornerpairs();
}
void place_correct_green_corners_on_top(void)
{
if(cornerpair[0]==461 || cornerpair[0]==614 || cornerpair[0]==146 )
{
//its in the right place
put_green_corners_in_place(0);
}
if(cornerpair[0]==621 || cornerpair[0]==216 || cornerpair[0]==162 )
{
row_right(0);
put_green_corners_in_place(1);
}
if(cornerpair[0]==263 || cornerpair[0]==326 || cornerpair[0]==632 )
{
row_right(0);
row_right(0);
put_green_corners_in_place(2);
}
if(cornerpair[0]==643 || cornerpair[0]==364 || cornerpair[0]==436 )
{
row_left(0);
put_green_corners_in_place(3);
}
if(cornerpair[1]==621 || cornerpair[1]==216 || cornerpair[1]==162 )
{
//its in the right place
put_green_corners_in_place(1);
}
if(cornerpair[1]==461 || cornerpair[1]==614 || cornerpair[1]==146 )
{
row_left(0);
put_green_corners_in_place(0);
}
if(cornerpair[1]==263 || cornerpair[1]==326 || cornerpair[1]==632 )
{
row_right(0);
put_green_corners_in_place(2);
}
if(cornerpair[1]==643 || cornerpair[1]==364 || cornerpair[1]==436 )
{
row_left(0);
row_left(0);
put_green_corners_in_place(3);
}
if(cornerpair[2]==263 || cornerpair[2]==326 || cornerpair[2]==632 )
{
//its in the right place
put_green_corners_in_place(2);
}
if(cornerpair[2]==621 || cornerpair[2]==216 || cornerpair[2]==162 )
{
row_left(0);
put_green_corners_in_place(1);
}
if(cornerpair[2]==461 || cornerpair[2]==614 || cornerpair[2]==146 )
{
row_left(0);
row_left(0);
put_green_corners_in_place(0);
}
if(cornerpair[2]==643 || cornerpair[2]==364 || cornerpair[2]==436 )
{
row_right(0);
put_green_corners_in_place(3);
}
if(cornerpair[3]==643 || cornerpair[3]==364 || cornerpair[3]==436 )
{
//its in the right place
put_green_corners_in_place(3);
}
if(cornerpair[3]==263 || cornerpair[3]==326 || cornerpair[3]==632 )
{
row_left(0);
put_green_corners_in_place(2);
}
if(cornerpair[3]==621 || cornerpair[3]==216 || cornerpair[3]==162 )
{
row_left(0);
row_left(0);
put_green_corners_in_place(1);
}
if(cornerpair[3]==461 || cornerpair[3]==614 || cornerpair[3]==146 )
{
row_right(0);
put_green_corners_in_place(0);
}
}
void solve_green_corners(void)
{
t=0;
while(t==0)
{
calculate_cornerpairs();
calculate_green_corners_on_top();
place_correct_green_corners_on_top();
// called by above function internally: put_green_corners_in_place();
}
}
void get_middle_layer_count(void)
{
middle_layer_count=0;
if(A[0][1]==5) middle_layer_count+=1;
if(B[0][1]==5) middle_layer_count+=1;
if(C[0][1]==5) middle_layer_count+=1;
if(D[0][1]==5) middle_layer_count+=1;
if(E[0][1]==5) middle_layer_count+=1;
if(E[1][0]==5) middle_layer_count+=1;
if(E[1][2]==5) middle_layer_count+=1;
if(E[2][1]==5) middle_layer_count+=1;
if(middle_layer_count==4)//it means all top edges contain blue.so bring one unsolved middle edge up
{
if(edgepair[2]!=12)
{
column_away(0);
row_left(0);
column_toward(0);
row_left(0);
front_clockwise();
row_right(0);
front_anticlockwise();
}
else if(edgepair[5]!=23)
{
column_away(2);
row_right(0);
column_toward(2);
row_right(0);
front_anticlockwise();
row_left(0);
front_clockwise();
}
else if(edgepair[8]!=34)
{
column_toward(2);
row_left(0);
column_away(2);
row_left(0);
back_clk();
row_right(0);
back_anticlk();
}
else if(edgepair[0]!=14)
{
column_toward(0);
row_right(0);
column_away(0);
row_right(0);
back_anticlk();
row_left(0);
back_clk();
}
else
{
t=1;//middle layer is solved.we have to break from while loop
}
calculate_edgepairs();
}
}
void put_middle_piece_in_place(void)
{
if(edgepair[1]==12)
{
row_left(0);
solve_middle_piece(2);
}
if(edgepair[1]==21)
{
row_right(0);
row_right(0);
solve_middle_piece(3);
}
if(edgepair[1]==23)
{
//no movements
solve_middle_piece(4);
}
if(edgepair[1]==32)
{
row_left(0);
solve_middle_piece(5);
}
if(edgepair[1]==34)
{
row_right(0);
solve_middle_piece(6);
}
if(edgepair[1]==43)
{
//no movements
solve_middle_piece(7);
}
if(edgepair[1]==41)
{
row_right(0);
row_right(0);
solve_middle_piece(0);
}
if(edgepair[1]==14)
{
row_right(0);
solve_middle_piece(1);
}
if(edgepair[4]==12)
{
row_right(0);
row_right(0);
solve_middle_piece(2);
}
if(edgepair[4]==21)
{
row_right(0);
solve_middle_piece(3);
}
if(edgepair[4]==23)
{
row_left(0);
solve_middle_piece(4);
}
if(edgepair[4]==32)
{
row_right(0);
row_right(0);
solve_middle_piece(5);
}
if(edgepair[4]==34)
{
//no movements
solve_middle_piece(6);
}
if(edgepair[4]==43)
{
row_left(0);
solve_middle_piece(7);
}
if(edgepair[4]==41)
{
row_right(0);
solve_middle_piece(0);
}
if(edgepair[4]==14)
{
//no movements
solve_middle_piece(1);
}
if(edgepair[7]==12)
{
row_right(0);
solve_middle_piece(2);
}
if(edgepair[7]==21)
{
//no movements
solve_middle_piece(3);
}
if(edgepair[7]==23)
{
row_right(0);
row_right(0);
solve_middle_piece(4);
}
if(edgepair[7]==32)
{
row_right(0);
solve_middle_piece(5);
}
if(edgepair[7]==34)
{
row_left(0);
solve_middle_piece(6);
}
if(edgepair[7]==43)
{
row_right(0);
row_right(0);
solve_middle_piece(7);
}
if(edgepair[7]==41)
{
//no movements
solve_middle_piece(0);
}
if(edgepair[7]==14)
{
row_left(0);
solve_middle_piece(1);
}
if(edgepair[10]==12)
{
//no movements
solve_middle_piece(2);
}
if(edgepair[10]==21)
{
row_left(0);
solve_middle_piece(3);
}
if(edgepair[10]==23)
{
row_right(0);
solve_middle_piece(4);
}
if(edgepair[10]==32)
{
//no movements
solve_middle_piece(5);
}
if(edgepair[10]==34)
{
row_right(0);
row_right(0);
solve_middle_piece(6);
}
if(edgepair[10]==43)
{
row_right(0);
solve_middle_piece(7);
}
if(edgepair[10]==41)
{
row_left(0);
solve_middle_piece(0);
}
if(edgepair[10]==14)
{
row_right(0);
row_right(0);
solve_middle_piece(1);
}
}
void solve_middle_piece(int a)
{
if(a==0)
{
column_toward(0);
row_right(0);
column_away(0);
row_right(0);
back_anticlk();
row_left(0);
back_clk();
}
if(a==1)
{
back_anticlk();
row_left(0);
back_clk();
row_left(0);
column_toward(0);
row_right(0);
column_away(0);
}
if(a==2)
{
front_clockwise();
row_right(0);
front_anticlockwise();
row_right(0);
column_away(0);
row_left(0);
column_toward(0);
}
if(a==3)
{
column_away(0);
row_left(0);
column_toward(0);
row_left(0);
front_clockwise();
row_right(0);
front_anticlockwise();
}
if(a==4)
{
column_away(2);
row_right(0);
column_toward(2);
row_right(0);
front_anticlockwise();
row_left(0);
front_clockwise();
}
if(a==5)
{
front_anticlockwise();
row_left(0);
front_clockwise();
row_left(0);
column_away(2);
row_right(0);
column_toward(2);
}
if(a==6)
{
back_clk();
row_right(0);
back_anticlk();
row_right(0);
column_toward(2);
row_left(0);
column_away(2);
}
if(a==7)
{
column_toward(2);
row_left(0);
column_away(2);
row_left(0);
back_clk();
row_right(0);
back_anticlk();
}
}
void solve_middle_layer(void)
{
t=0;
while(t==0)
{
calculate_edgepairs();
get_middle_layer_count();
put_middle_piece_in_place();
//solve middle piece is called inside put middle piece in place
}
}
void solve_top_cross(void)
{
top_cross_count=0;
if(E[0][1]==5) top_cross_count+=1;
if(E[1][0]==5) top_cross_count+=1;
if(E[1][1]==5) top_cross_count+=1;
if(E[1][2]==5) top_cross_count+=1;
if(E[2][1]==5) top_cross_count+=1;
if(top_cross_count==1) //it is a dot
{
front_clockwise();
column_away(2);
row_left(0);
column_toward(2);
row_right(0);
front_anticlockwise();
row_left(0);
row_left(0);
//////////////////////////////////
top_cross_count=3;
//at end,we get inverted L
}
if(top_cross_count==3)
{
if(E[0][1]+E[2][1]==10 || E[1][0]+E[1][2]==10)
{
if(E[0][1]==5 && E[1][1]==5 && E[2][1]==5)
{
row_left(0);
}
if(E[1][0]==5 && E[1][1]==5 && E[1][2]==5)
{
front_clockwise();
column_away(2);
row_left(0);
column_toward(2);
row_right(0);
front_anticlockwise();
}
}
else
{
while(E[0][1]+E[1][0]!=10)
{
row_left(0);
}
front_clockwise();
column_away(2);
row_left(0);
column_toward(2);
row_right(0);
column_away(2);
row_left(0);
column_toward(2);
row_right(0);
front_anticlockwise();
}
//at the end,cross is solved
}
}
void get_blue_count(void)
{
blue_count=0;
if(E[0][0]==5) blue_count+=1;
if(E[0][2]==5) blue_count+=1;
if(E[2][0]==5) blue_count+=1;
if(E[2][2]==5) blue_count+=1;
if(blue_count==4)
{
t=1;
}
}
void repeat_pattern(void)
{
column_away(2);
row_left(0);
column_toward(2);
row_left(0);
column_away(2);
row_left(0);
row_left(0);
column_toward(2);
}
void solve_top_blue(void)
{
t=0;
while(t==0)
{
get_blue_count();
if(blue_count==0)
{
repeat_pattern();
}
get_blue_count();
if(blue_count==2)
{
while(E[0][2]+E[2][0]!=10 && E[0][2]+E[2][2]!=10)
{
row_left(0);
}
if(E[0][2]+E[2][0]==10)
{
repeat_pattern();
}
repeat_pattern();
}
get_blue_count();
if(blue_count==1)
{
while(E[2][0]!=5)
{
row_left(0);
}
repeat_pattern();
}
}
}
void fix_top_blue_corners(void)
{
calculate_cornerpairs();
while(cornerpair[0]!=415 || cornerpair[1]!=125 || cornerpair[2]!=235 || cornerpair[3]!=345)
{
if(A[0][0]!=A[0][2] && B[0][0]!=B[0][2] && C[0][0]!=C[0][2] && D[0][0]!=D[0][2])
{
column_toward(2);
front_clockwise();
column_toward(2);
back_clk();
back_clk();
column_away(2);
front_anticlockwise();
column_toward(2);
back_clk();
back_clk();
column_toward(2);
column_toward(2);
}
if(A[0][0]==A[0][2] || B[0][0]==B[0][2] || C[0][0]==C[0][2] || D[0][0]==D[0][2])
{
while(D[0][0]!=D[0][2])
{
row_left(0);
}
if(A[0][0]!=A[0][2] && B[0][0]!=B[0][2] && C[0][0]!=C[0][2] && D[0][0]==D[0][2])
{
column_toward(2);
front_clockwise();
column_toward(2);
back_clk();
back_clk();
column_away(2);
front_anticlockwise();
column_toward(2);
back_clk();
back_clk();
column_toward(2);
column_toward(2);
}
else
{
row_left(0);
}
}
calculate_cornerpairs();
}
}
/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
** This file was created by Processor Expert 10.3 [05.09]
** for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/
No comments:
Post a Comment