Featured post

A reason to exercise.

It's been two years since I started going to the gym and this post is just a summary of my experience. Before going to the gym. Iv...

Sunday, 2 April 2017

Code to solve a 3x3 Rubik's cube........Have fun!!

/* ###################################################################
**     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 A_1[3][3]={{1,1,1},{1,1,1},{1,1,1}};
int B_1[3][3]={{2,2,2},{2,2,2},{2,2,2}};
int C_1[3][3]={{3,3,3},{3,3,3},{3,3,3}};
int D_1[3][3]={{4,4,4},{4,4,4},{4,4,4}};
int E_1[3][3]={{5,5,5},{5,5,5},{5,5,5}};
int F_1[3][3]={{6,6,6},{6,6,6},{6,6,6}};


int A_2[3][3]={{1,1,1},{1,1,1},{1,1,1}};
int B_2[3][3]={{2,2,2},{2,2,2},{2,2,2}};
int C_2[3][3]={{3,3,3},{3,3,3},{3,3,3}};
int D_2[3][3]={{4,4,4},{4,4,4},{4,4,4}};
int E_2[3][3]={{5,5,5},{5,5,5},{5,5,5}};
int F_2[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;
int delay=0;
int seq=0;
int flag=0;//0 =>front clockwise and anticlock
           //1 =>back clock and anticlock
//counts used in stepper clock function
int count1=1;
int count2=1;
int count3=1;
int count4=1;
int count5=1;

//counts used in stepper anticlock function
int count6=1;
int count7=1;
int count8=1;
int count9=1;
int count10=1;

//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 calculate_edgepairs(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);

void fix_top_blue_edges(void);
void edge_swap(int);//0 => clockwise swap
                    //1 =>anticlockwise swap

void turn_top_layer_in_place(void);

void turn_stepper_clock(int);     //0 is passed as 2nd parameter in all cases except back_clk and
void turn_stepper_anticlock(int); //back_anticlk where 1 is passed as the value

void change_cube(int a);//if a==1,   A=A_1;
                        //if a==2,   A=A_2;

int soln1[300];int soln1_count=0;
int soln2[300];int soln2_count=0;

int cube_flag=0; // 1 implies cube1
                 // 2 implies cube2

void print_moves(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(;;) { } */

  //this code has now been modified to solve two rubik's cubes simultaneously
 
  //hope u work

  /*
  how i solve the cube
 
  1.form plus
  2.solve first layer
  3.solve for middle layer
  4.form top cross
  5.make sure corner pieces are in place
  6.put them in place
  7.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 \n");

  printf("Enter cube 1:\n");
  get_matrices(A_1,'A');
  get_matrices(B_1,'B');
  get_matrices(C_1,'C');
  get_matrices(D_1,'D');
  get_matrices(E_1,'E');
  get_matrices(F_1,'F');
 
  change_cube(1);
 
  check_cubes_correctness();
  while(count!=189)
  {
 check_cubes_correctness();
  }
 
  printf("Enter cube 2:\n");
  get_matrices(A_2,'A');
  get_matrices(B_2,'B');
  get_matrices(C_2,'C');
  get_matrices(D_2,'D');
  get_matrices(E_2,'E');
  get_matrices(F_2,'F');
 
  change_cube(2);
 
  check_cubes_correctness();
  while(count!=189)
  {
 check_cubes_correctness();
  }
 
  printf("\n Verify cube 1 once before proceeding with the solution. \n");
  printf("\n");
  change_cube(1);
  print_matrices();
 
  printf("\n\n");
 
  printf("\n Verify cube 2 once before proceeding with the solution. \n");
  printf("\n");
  change_cube(2);
  print_matrices();
 
  //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.

  change_cube(1);
 
  printf("solving cross for cube 1\n");
  calculate_edgepairs();
  if(edgepair[3]!=16 || edgepair[6]!=26 || edgepair[9]!=36 || edgepair[11]!=46)
  {
  solve_green_cross();
  }
  printf("Bottom Cross of cube 1 solved!!\n");

   
  printf("solving corners for cube 1\n");
  solve_green_corners();
  printf("Bottom Corners of cube 1 are solved!!\n");

  printf("solving middle layers of cube 1\n");
  solve_middle_layer();
  printf("Middle layer of cube 1 solved!!\n");
 
  printf("solving top cross of cube 1\n");
  solve_top_cross();
  printf("Top cross of cube 1 solved!!\n");
 
  printf("solving blue of cube 1\n");
  solve_top_blue();
  printf("Top Layer of cube 1 solved!!\n");

  printf("fixing blue corners of cube 1\n");
  fix_top_blue_corners();
  printf("Top Corners of cube 1 Fixed!!\n");
 
  printf("fixing blue edges of cube 1\n");
  fix_top_blue_edges();
  printf("Top Edges of cube 1 Fixed!!\n");

  printf("completing solving cube 1\n");
  turn_top_layer_in_place();
  print_matrices();
  printf("\n The Cube 1 is solved!!\n");


  printf("oooooooooooooooooooooooooooooooooooooooooooooooooooo\n");
 
  change_cube(2);
 
  printf("solving cross for cube 2\n");
  calculate_edgepairs();
  if(edgepair[3]!=16 || edgepair[6]!=26 || edgepair[9]!=36 || edgepair[11]!=46)
  {
  solve_green_cross();
  }
  printf("Bottom Cross of cube 2 solved!!\n");

   
  printf("solving corners for cube 2\n");
  solve_green_corners();
  printf("Bottom Corners of cube 2 are solved!!\n");

  printf("solving middle layers of cube 2\n");
  solve_middle_layer();
  printf("Middle layer of cube 2 solved!!\n");
 
  printf("solving top cross of cube 2\n");
  solve_top_cross();
  printf("Top cross of cube 2 solved!!\n");
 
  printf("solving blue of cube 2\n");
  solve_top_blue();
  printf("Top Layer of cube 2 solved!!\n");

  printf("fixing blue corners of cube 2\n");
  fix_top_blue_corners();
  printf("Top Corners of cube 2 Fixed!!\n");
 
  printf("fixing blue edges of cube 2\n");
  fix_top_blue_edges();
  printf("Top Edges of cube 2 Fixed!!\n");

  printf("completing solving cube 2\n");
  turn_top_layer_in_place();
  print_matrices();
  printf("\n The Cube 2 is solved!!\n");

  printf("\n priting moves\n\n\n");
  print_moves();
 
  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');

}

if(j==0)
{

if(cube_flag==1)
{
soln1[soln1_count++]=1;
}
if(cube_flag==2)
{
soln2[soln2_count++]=1;
}


}

if(j==2)
{
if(cube_flag==1)
{
soln1[soln1_count++]=2;
}
if(cube_flag==2)
{
soln2[soln2_count++]=2;
}

}

}

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


if(j==0)
{
if(cube_flag==1)
{
soln1[soln1_count++]=3;
}
if(cube_flag==2)
{
soln2[soln2_count++]=3;
}
//turn_stepper_anticlock(1);
}

if(j==2)
{
if(cube_flag==1)
{
soln1[soln1_count++]=4;
}
if(cube_flag==2)
{
soln2[soln2_count++]=4;
}
//turn_stepper_clock(2);
}

}

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

if(i==0)
{
if(cube_flag==1)
{
soln1[soln1_count++]=5;
}
if(cube_flag==2)
{
soln2[soln2_count++]=5;
}
//turn_stepper_clock(5);
}

if(i==2)
{
if(cube_flag==1)
{
soln1[soln1_count++]=6;
}
if(cube_flag==2)
{
soln2[soln2_count++]=6;
}
//turn_stepper_anticlock(6);
}

}

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

if(i==0)
{
if(cube_flag==1)
{
soln1[soln1_count++]=7;
}
if(cube_flag==2)
{
soln2[soln2_count++]=7;
}
//turn_stepper_anticlock(5);
}

if(i==2)
{
if(cube_flag==1)
{
soln1[soln1_count++]=8;
}
if(cube_flag==2)
{
soln2[soln2_count++]=8;
}
//turn_stepper_clock(6);
}

}

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');
if(flag==0)
{
//turn_stepper_anticlock(3);
}

if(cube_flag==1)
{
soln1[soln1_count++]=9;
}
if(cube_flag==2)
{
soln2[soln2_count++]=9;
}

}

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');
if(flag==0)
{
//turn_stepper_clock(3);
}

if(cube_flag==1)
{
soln1[soln1_count++]=10;
}
if(cube_flag==2)
{
soln2[soln2_count++]=10;
}

}

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 E clockwise 2 times

circle_clockwise('E');
circle_clockwise('E');

//Turn F clockwise 2 times

circle_clockwise('F');
circle_clockwise('F');

temp[0][0]=C[0][2];
temp[0][1]=C[1][2];
temp[0][2]=C[2][2];

C[0][2]=F[0][0];
C[1][2]=F[0][1];
C[2][2]=F[0][2];

F[0][0]=A[2][0];
F[0][1]=A[1][0];
F[0][2]=A[0][0];

A[2][0]=E[2][2];
A[1][0]=E[2][1];
A[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('D');

//Turn E clockwise 2 times

circle_clockwise('E');
circle_clockwise('E');

//Turn F clockwise 2 times

circle_clockwise('F');
circle_clockwise('F');


if(cube_flag==1)
{
soln1[soln1_count++]=11;
}
if(cube_flag==2)
{
soln2[soln2_count++]=11;
}

}

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 E clockwise 2 times

circle_clockwise('E');
circle_clockwise('E');

//Turn F clockwise 2 times

circle_clockwise('F');
circle_clockwise('F');


 temp[0][0]=C[0][2];
 temp[0][1]=C[1][2];
 temp[0][2]=C[2][2];
 
 C[0][2]=E[2][2];
 C[1][2]=E[2][1];
 C[2][2]=E[2][0];
 
 E[2][2]=A[2][0];
 E[2][1]=A[1][0];  
 E[2][0]=A[0][0];  
 
 
 A[2][0]=F[0][0];
 A[1][0]=F[0][1];
 A[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('D');

//Turn E clockwise 2 times

circle_clockwise('E');
circle_clockwise('E');

//Turn F clockwise 2 times

circle_clockwise('F');
circle_clockwise('F');


if(cube_flag==1)
{
soln1[soln1_count++]=12;
}
if(cube_flag==2)
{
soln2[soln2_count++]=12;
}

}

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();


}
}

void fix_top_blue_edges(void)
{
if(A[0][0]!=A[0][1] && B[0][0]!=B[0][1] && C[0][0]!=C[0][1] && D[0][0]!=D[0][1])
{
edge_swap(0);
}
if(A[0][0]==A[0][1] || B[0][0]==B[0][1] || C[0][0]==C[0][1] || D[0][0]==D[0][1])
{
while(D[0][0]!=D[0][1])
{
row_left(0);
}

if(B[0][0]==A[0][1] && C[0][0]==B[0][1] && A[0][0]==C[0][1])
{
edge_swap(1);
}

if(A[0][0]==B[0][1] && B[0][0]==C[0][1] && C[0][0]==A[0][1])
{
edge_swap(0);
}
}

}

void edge_swap(int a)
{
front_clockwise();
front_clockwise();
if(a==0)
{
row_left(0);
}
if(a==1)
{
row_right(0);
}
column_toward(0);
column_toward(2);
front_clockwise();
front_clockwise();
column_away(0);
column_away(2);
if(a==0)
{
row_left(0);
}
if(a==1)
{
row_right(0);
}
front_clockwise();
front_clockwise();
}

void turn_top_layer_in_place(void)
{

while(A[0][1]!=A[1][1] && B[0][1]!=B[1][1] && C[0][1]!=C[1][1] && D[0][1]!=D[1][1])
{
row_left(0);
}

}


void change_cube(int a)
{
if(a==1)
{
cube_flag=1;

for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
A[i][j]=A_1[i][j];
B[i][j]=B_1[i][j];
C[i][j]=C_1[i][j];
D[i][j]=D_1[i][j];
E[i][j]=E_1[i][j];
F[i][j]=F_1[i][j];
}
}
}

else if(a==2)
{
cube_flag=2;

for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{

A[i][j]=A_2[i][j];
B[i][j]=B_2[i][j];
C[i][j]=C_2[i][j];
D[i][j]=D_2[i][j];
E[i][j]=E_2[i][j];
F[i][j]=F_2[i][j];
}
}
}

else { cube_flag=0; }
}

void print_moves(void)
{
printf("CUBE 1  \n\n");
for(soln1_count=0;soln1_count<=300;soln1_count++)
{
if(soln1[soln1_count]==1) {printf("column_away(0)\n");}
if(soln1[soln1_count]==2) {printf("column_away(2)\n");}
if(soln1[soln1_count]==3) {printf("column_toward(0)\n");}
if(soln1[soln1_count]==4) {printf("column_toward(2)\n");}
if(soln1[soln1_count]==5) {printf("row_right(0)\n");}
if(soln1[soln1_count]==6) {printf("row_right(2)\n");}
if(soln1[soln1_count]==7) {printf("row_left(0)\n");}
if(soln1[soln1_count]==8) {printf("row_left(2)\n");}
if(soln1[soln1_count]==9) {printf("front clockwise\n");}
if(soln1[soln1_count]==10) {printf("front anticlockwise\n");}
if(soln1[soln1_count]==11) {printf("back clockwise\n");}
if(soln1[soln1_count]==12) {printf("back anticlockwise\n");}

}

for(soln2_count=0;soln2_count<=300;soln2_count++)
{
if(soln2[soln2_count]==1) {printf("column_away(0)\n");}
if(soln2[soln2_count]==2) {printf("column_away(2)\n");}
if(soln2[soln2_count]==3) {printf("column_toward(0)\n");}
if(soln2[soln2_count]==4) {printf("column_toward(2)\n");}
if(soln2[soln2_count]==5) {printf("row_right(0)\n");}
if(soln2[soln2_count]==6) {printf("row_right(2)\n");}
if(soln2[soln2_count]==7) {printf("row_left(0)\n");}
if(soln2[soln2_count]==8) {printf("row_left(2)\n");}
if(soln2[soln2_count]==9) {printf("front clockwise\n");}
if(soln2[soln2_count]==10) {printf("front anticlockwise\n");}
if(soln2[soln2_count]==11) {printf("back clockwise\n");}
if(soln2[soln2_count]==12) {printf("back anticlockwise\n");}
}
}

/* 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