Cube.c

Akhirnya kesampaian juga buat program untuk solving Rubik’s Cube. Dibuat untuk UAS EL5004 – Sistem Intelijen, diselesaikan dengan cara Sangkuriang dan Bandung Bandawasa (sistem kebut semalam).:mrgreen:

Metode yang dipakai untuk menyelesaikan Rubik’s Cube di program ini adalah Classic Pochmann. Metode ini biasanya dipakai oleh para pemula untuk solving blindfolded.

Inputnya adalah sebuah file yang terdiri dari 6 baris, yang masing-masing mendeskripsikan warna dari sisi atas, depan, kanan, bawah, belakang, dan kiri dari file.

Misalnya scramble dari cube adalah:

L U2 L D’ L’ B R’ F2 D’ B L2 F’ U2 B L2 F R2 L2 B2

Maka input file, diberi nama scramble4.in misalnya adalah:

yowowoggo
wybrggwrw
yyrwrrgby
ogowywygg
bbbbbyoog
rwrboyrrb

Symbol “r” adalah untuk red, “b” untuk blue, dan seterusnya. Setelah itu program dijalankan dengan cara:

cube scamble4.in

Nah, nanti akan keluar langkah-langkah untuk penyelesaiannya. Sayangnya move-nya memang super banyak. Mungkin bisa mengalahkan Gaël Dusser untuk event fewest moves di urutan terbawah.

Keluaran program akan seperti ini:

Running program
Running program

Setelah itu output program dicobakan di cube simulator buatan Lucas Garron. Dan ternyata, cube solved!

Hasil simulator
Hasil simulator

Nah untuk listing code program, ini dia:

/*************************************************************/
/** Program for Solving Rubik's Cube using Pochmann method  **/
/** Ardianto Satriawan 23213079                             **/
/*************************************************************/

#include <stdio.h>
#include <math.h>
#include <string.h>

/** definition of a "cube" data type **/
typedef struct cube {
	char up[9];
	char front[9];
	char right[9];
	char down[9];
	char back[9];
	char left[9];
} cube;

/** main function prototypes **/

/** for file input **/
void ReadInput(cube *Cube, char *filename);

/** for edges **/
void GetArrayOfUnsolvedEdges(cube *Cube, int UnsolvedEdges[12]);
int EdgeTarget(cube *Cube, int UnsolvedEdges[12]);
void ShootEdges(cube *Cube, int target);

/** for parity **/
void SolveParity(cube *Cube);

/** for corners **/
void GetArrayOfUnsolvedCorners(cube *Cube, int UnsolvedCorners[8]);
int CornerTarget(cube *Cube, int UnsolvedCorners[8]);
void ShootCorners(cube *Cube, int target);

/** helping function prototypes **/
void SwapColour(char *a, char *b);

int main(int argc, char* argv[]) {
	if(argc == 2) {
		/* Data type declarations*/
		cube Cube;
		int UnsolvedEdges[12]; /* element contains 1 if unsolved, 0 if solved */
		int UnsolvedCorners[8]; /* element contains 1 if unsolved, 0 if solved */
		int TargetEdge;
		int TargetCorner;
		int ParityCounter = 0; /* parity counter */
		
		/* read cube colours from file */
		ReadInput(&Cube, argv[1]);
		
		/* solve edges */
		GetArrayOfUnsolvedEdges(&Cube, UnsolvedEdges);
		while (	UnsolvedEdges[0] != 0 || UnsolvedEdges[1] != 0 || UnsolvedEdges[2] != 0 ||
				UnsolvedEdges[3] != 0 || UnsolvedEdges[4] != 0 || UnsolvedEdges[5] != 0 ||
				UnsolvedEdges[6] != 0 || UnsolvedEdges[7] != 0 || UnsolvedEdges[8] != 0 ||
				UnsolvedEdges[9] != 0 || UnsolvedEdges[10] != 0 || UnsolvedEdges[11] != 0)
		{
			//1. get target
			TargetEdge = EdgeTarget(&Cube, UnsolvedEdges);
			// 2. shoot target
			ShootEdges(&Cube, TargetEdge);
			// 3. Count for parity
			ParityCounter++;
			// 4. Renew Unsolved Edges Array
			GetArrayOfUnsolvedEdges(&Cube, UnsolvedEdges);
		}
		
		/* solve parity, if necessary */
		if (ParityCounter%2 == 1) {
			SolveParity(&Cube);
		}
		
		/* solve corners*/
		GetArrayOfUnsolvedCorners(&Cube, UnsolvedCorners);
		
		while (	UnsolvedCorners[0] != 0 || UnsolvedCorners[1] != 0 || UnsolvedCorners[2] != 0 || UnsolvedCorners[3] != 0 ||
				UnsolvedCorners[4] != 0 || UnsolvedCorners[5] != 0 || UnsolvedCorners[6] != 0 || UnsolvedCorners[7] != 0)
		{
			// 1. get target
			TargetCorner = CornerTarget(&Cube, UnsolvedCorners);
			
			// 2. shoot target
			ShootCorners(&Cube, TargetCorner);
			// 3. Renew Unsolved Corners Array
			GetArrayOfUnsolvedCorners(&Cube, UnsolvedCorners);
		}
	}
	else {
		printf("Error, usage: \"%s filename\"", argv[0]);
	}
	return 0;
}

void ReadInput(cube *Cube, char *filename) {
	FILE *in;
	in = fopen(filename, "r");
	char temp, junk;
	
	int i=0;
	int j=0; /* calculating row */
	while(fscanf(in,"%c", &temp) != EOF) {
		if (i == 9) {
			j++;
			i = 0;
		}
		else {
			if (j == 0) {
				Cube->up[i] = temp;
				i++;
			}
			else if (j == 1) {
				Cube->front[i] = temp;
				i++;
			}
			else if (j == 2) {
				Cube->right[i] = temp;
				i++;
			}
			else if (j == 3) {
				Cube->down[i] = temp;
				i++;
			}
			else if (j == 4) {
				Cube->back[i] = temp;
				i++;
			}
			else {
				Cube->left[i] = temp;
				i++;
			}
		}
	}
	fclose(in);
}

void GetArrayOfUnsolvedEdges(cube *Cube, int UnsolvedEdges[12]) {
	/* UR */
	if (Cube->up[5] == 'w' && Cube->right[1] == 'r') {
		UnsolvedEdges[0] = 0;
	}
	else {
		UnsolvedEdges[0] = 1;
	}
	
	/* UL */
	if (Cube->up[3] == 'w' && Cube->left[1] == 'o') {
		UnsolvedEdges[1] = 0;
	}
	else {
		UnsolvedEdges[1] = 1;
	}
	
	/* UB */
	if (Cube->up[1] == 'w' && Cube->back[1] == 'b') {
		UnsolvedEdges[2] = 0;
	}
	else {
		UnsolvedEdges[2] = 1;
	}
	
	/* UF */
	if (Cube->up[7] == 'w' && Cube->front[1] == 'g') {
		UnsolvedEdges[3] = 0;
	}
	else {
		UnsolvedEdges[3] = 1;
	}
	
	/* FL */
	if (Cube->front[3] == 'g' && Cube->left[5] == 'o') {
		UnsolvedEdges[4] = 0;
	}
	else {
		UnsolvedEdges[4] = 1;
	}
	
	/* LB */
	if (Cube->left[3] == 'o' && Cube->back[5] == 'b') {
		UnsolvedEdges[5] = 0;
	}
	else {
		UnsolvedEdges[5] = 1;
	}
	
	/* BR */
	if (Cube->back[3] == 'b' && Cube->right[5] == 'r') {
		UnsolvedEdges[6] = 0;
	}
	else {
		UnsolvedEdges[6] = 1;
	}
	
	/* RF */
	if (Cube->right[3] == 'r' && Cube->front[5] == 'g') {
		UnsolvedEdges[7] = 0;
	}
	else {
		UnsolvedEdges[7] = 1;
	}
	
	/* FD */
	if (Cube->front[7] == 'g' && Cube->down[1] == 'y') {
		UnsolvedEdges[8] = 0;
	}
	else {
		UnsolvedEdges[8] = 1;
	}
	
	/* LD */
	if (Cube->left[7] == 'o' && Cube->down[3] == 'y') {
		UnsolvedEdges[9] = 0;
	}
	else {
		UnsolvedEdges[9] = 1;
	}
	
	/* BD */
	if (Cube->back[7] == 'b' && Cube->down[7] == 'y') {
		UnsolvedEdges[10] = 0;
	}
	else {
		UnsolvedEdges[10] = 1;
	}
	
	/* RD */
	if (Cube->right[7] == 'r' && Cube->down[5] == 'y') {
		UnsolvedEdges[11] = 0;
	}
	else {
		UnsolvedEdges[11] = 1;
	}
}

int EdgeTarget(cube *Cube, int UnsolvedEdges[12]) {
	if (UnsolvedEdges[0] == 1) { /* buffer is not in place*/
		
		/* UL target */
		if (Cube->up[5] == 'w' && Cube->right[1] == 'o') {
			return 1;
		}
		
		/* LU target */
		else if (Cube->up[5] == 'o' && Cube->right[1] == 'w') {
			return 2;
		}
		
		/* UB target */
		else if (Cube->up[5] == 'w' && Cube->right[1] == 'b') {
			return 3;
		}
		
		/* BU target */
		else if (Cube->up[5] == 'b' && Cube->right[1] == 'w') {
			return 4;
		}
		
		/* UF target */
		else if (Cube->up[5] == 'w' && Cube->right[1] == 'g') {
			return 5;
		}
		
		/* FU target */
		else if (Cube->up[5] == 'g' && Cube->right[1] == 'w') {
			return 6;
		}
		
		/* FL target */
		else if (Cube->up[5] == 'g' && Cube->right[1] == 'o') {
			return 7;
		}
		
		/* LF target */
		else if (Cube->up[5] == 'o' && Cube->right[1] == 'g') {
			return 8;
		}
		
		/* LB target */
		else if (Cube->up[5] == 'o' && Cube->right[1] == 'b') {
			return 9;
		}
		
		/* BL target */
		else if (Cube->up[5] == 'b' && Cube->right[1] == 'o') {
			return 10;
		}
		
		/* BR target */
		else if (Cube->up[5] == 'b' && Cube->right[1] == 'r') {
			return 11;
		}
		
		/* RB target */
		else if (Cube->up[5] == 'r' && Cube->right[1] == 'b') {
			return 12;
		}
		
		/* RF target */
		else if (Cube->up[5] == 'r' && Cube->right[1] == 'g') {
			return 13;
		}
		
		/* FR target */
		else if (Cube->up[5] == 'g' && Cube->right[1] == 'r') {
			return 14;
		}
		
		/* FD target */
		else if (Cube->up[5] == 'g' && Cube->right[1] == 'y') {
			return 15;
		}
		
		/* DF target */
		else if (Cube->up[5] == 'y' && Cube->right[1] == 'g') {
			return 16;
		}
		
		/* LD target */
		else if (Cube->up[5] == 'o' && Cube->right[1] == 'y') {
			return 17;
		}
		
		/* DL target */
		else if (Cube->up[5] == 'y' && Cube->right[1] == 'o') {
			return 18;
		}
		
		/* BD target */
		else if (Cube->up[5] == 'b' && Cube->right[1] == 'y') {
			return 19;
		}
		
		/* DB target */
		else if (Cube->up[5] == 'y' && Cube->right[1] == 'b') {
			return 20;
		}
		
		/* RD target */
		else if (Cube->up[5] == 'r' && Cube->right[1] == 'y') {
			return 21;
		}
		
		/* DR target */
		else if (Cube->up[5] == 'y' && Cube->right[1] == 'r') {
			return 22;
		}
		
		/* special case, buffer in place, flipped */
		else if (Cube->up[5] == 'r' && Cube->right[1] == 'w') {
			int found=0;
			int i, idx;
			for (i=1;i<=11;i++) {
				if (UnsolvedEdges[i] == 1) {
					idx = i;
					found = 1;
				}
			}
			if (found==1)
				return idx*2;
			else
				return 0;
			}
	}
	else { /* buffer is in place, UnsolvedEdges[0] == 0 */
		int found=0;
		int i, idx;
		for (i=1;i<=11;i++) {
			if (UnsolvedEdges[i] == 1) {
				idx = i;
				found = 1;
			}
		}
		if (found==1)
			return idx*2;
		else
			return 0;
	}
}

void ShootEdges(cube *Cube, int target) {
	if (target==1) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->up[3]);
		SwapColour(&Cube->right[1], &Cube->left[1]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("R U R' U' R' F R2 U' R' U' R U R' F'\n");
	}
	else if (target==2) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->left[1]);
		SwapColour(&Cube->right[1], &Cube->up[3]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("L Dw' L R U R' U' R' F R2 U' R' U' R U R' F' L' Dw L'\n");
	}
	else if (target==3) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->up[1]);
		SwapColour(&Cube->right[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("R' U2 R U R' U2 L U' R U L'\n");
	}
	else if (target==4) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->back[1]);
		SwapColour(&Cube->right[1], &Cube->up[1]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("Lw R U2 R' U' R U2 L' U R' U' L Lw'\n");
	}
	else if (target==5) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->up[7]);
		SwapColour(&Cube->right[1], &Cube->front[1]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("R U2 R' U' R U2 L' U R' U' L\n");
	}
	else if (target==6) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->front[1]);
		SwapColour(&Cube->right[1], &Cube->up[7]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("Lw' R' U2 R U R' U2 L U' R U L' Lw\n");
	}
	else if (target==7) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->front[3]);
		SwapColour(&Cube->right[1], &Cube->left[5]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("L' R U R' U' R' F R2 U' R' U' R U R' F' L\n");
	}
	else if (target==8) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->left[5]);
		SwapColour(&Cube->right[1], &Cube->front[3]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("Dw' L R U R' U' R' F R2 U' R' U' R U R' F' L' Dw\n");
	}
	else if (target==9) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->left[3]);
		SwapColour(&Cube->right[1], &Cube->back[5]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("Dw L' R U R' U' R' F R2 U' R' U' R U R' F' L Dw'\n");
	}
	else if (target==10) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->back[5]);
		SwapColour(&Cube->right[1], &Cube->left[3]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("L R U R' U' R' F R2 U' R' U' R U R' F' L'\n");
	}
	else if (target==11) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->back[3]);
		SwapColour(&Cube->right[1], &Cube->right[5]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("Dw2 L' R U R' U' R' F R2 U' R' U' R U R' F' L Dw2\n");
	}
	else if (target==12) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->right[5]);
		SwapColour(&Cube->right[1], &Cube->back[3]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("Dw L R U R' U' R' F R2 U' R' U' R U R' F' L' Dw'\n");
	}
	else if (target==13) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->right[3]);
		SwapColour(&Cube->right[1], &Cube->front[5]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("Dw' L' R U R' U' R' F R2 U' R' U' R U R' F' L Dw\n");
	}
	else if (target==14) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->front[5]);
		SwapColour(&Cube->right[1], &Cube->right[3]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("Dw2 L R U R' U' R' F R2 U' R' U' R U R' F' L' Dw2\n");
	}
	else if (target==15) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->front[7]);
		SwapColour(&Cube->right[1], &Cube->down[1]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("Lw' R U2 R' U' R U2 L' U R' U' L Lw\n");
	}
	else if (target==16) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->down[1]);
		SwapColour(&Cube->right[1], &Cube->front[7]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("Lw2 R' U2 R U R' U2 L U' R U L' Lw2\n");
	}
	else if (target==17) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->left[7]);
		SwapColour(&Cube->right[1], &Cube->down[3]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("D Lw' R U2 R' U' R U2 L' U R' U' L Lw D'\n");
	}
	else if (target==18) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->down[3]);
		SwapColour(&Cube->right[1], &Cube->left[7]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("L2 R U R' U' R' F R2 U' R' U' R U R' F' L2\n");
	}
	else if (target==19) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->back[7]);
		SwapColour(&Cube->right[1], &Cube->down[7]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("Lw R' U2 R U R' U2 L U' R U L' Lw'\n");
	}
	else if (target==20) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->down[7]);
		SwapColour(&Cube->right[1], &Cube->back[7]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("Lw2 R U2 R' U' R U2 L' U R' U' L Lw2\n");
	}
	else if (target==21) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->right[7]);
		SwapColour(&Cube->right[1], &Cube->down[5]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("D' Lw' R U2 R' U' R U2 L' U R' U' L Lw D\n");
	}
	else if (target==22) {
		//swap edge colours
		SwapColour(&Cube->up[5], &Cube->down[5]);
		SwapColour(&Cube->right[1], &Cube->right[7]);
		//swap corner colours
		SwapColour(&Cube->up[2], &Cube->up[8]);
		SwapColour(&Cube->right[0], &Cube->back[0]);
		SwapColour(&Cube->right[2], &Cube->front[2]);
		//print
		printf("D2 L2 R U R' U' R' F R2 U' R' U' R U R' F' L2 D2\n");
	}
}

void SolveParity(cube *Cube) {
	//swap edge colours
	SwapColour(&Cube->up[1], &Cube->up[3]);
	SwapColour(&Cube->left[1], &Cube->back[1]);
	//swap corner colours
	SwapColour(&Cube->up[2], &Cube->up[8]);
	SwapColour(&Cube->right[0], &Cube->back[0]);
	SwapColour(&Cube->right[2], &Cube->front[2]);
	printf("U2 L2 F' L' U' L' U L F L' U2 L U2 L' U\n");
}

void GetArrayOfUnsolvedCorners(cube *Cube, int UnsolvedCorners[8]) {
	/* ULB */
	if (Cube->up[0] == 'w' && Cube->left[0] == 'o' && Cube->back[2] == 'b') {
		UnsolvedCorners[0] = 0;
	}
	else {
		UnsolvedCorners[0] = 1;
	}
	
	/* UBR */
	if (Cube->up[2] == 'w' && Cube->back[0] == 'b' && Cube->right[2] == 'r') {
		UnsolvedCorners[1] = 0;
	}
	else {
		UnsolvedCorners[1] = 1;
	}
	
	/* URF */
	if (Cube->up[8] == 'w' && Cube->right[0] == 'r' && Cube->front[2] == 'g') {
		UnsolvedCorners[2] = 0;
	}
	else {
		UnsolvedCorners[2] = 1;
	}
	
	/* UFL */
	if (Cube->up[6] == 'w' && Cube->front[0] == 'g' && Cube->left[2] == 'o') {
		UnsolvedCorners[3] = 0;
	}
	else {
		UnsolvedCorners[3] = 1;
	}
	
	/* FRD */
	if (Cube->front[8] == 'g' && Cube->right[6] == 'r' && Cube->down[2] == 'y') {
		UnsolvedCorners[4] = 0;
	}
	else {
		UnsolvedCorners[4] = 1;
	}
	
	/* DRB */
	if (Cube->down[8] == 'y' && Cube->right[8] == 'r' && Cube->back[6] == 'b') {
		UnsolvedCorners[5] = 0;
	}
	else {
		UnsolvedCorners[5] = 1;
	}
	
	/* DBL */
	if (Cube->down[6] == 'y' && Cube->back[8] == 'b' && Cube->left[6] == 'o') {
		UnsolvedCorners[6] = 0;
	}
	else {
		UnsolvedCorners[6] = 1;
	}
	
	/* FDL */
	if (Cube->front[6] == 'g' && Cube->down[0] == 'y' && Cube->left[8] == 'o') {
		UnsolvedCorners[7] = 0;
	}
	else {
		UnsolvedCorners[7] = 1;
	}
}

int CornerTarget(cube *Cube, int UnsolvedCorners[8]) {
	if (UnsolvedCorners[0] == 1) { /* buffer is not in place*/
		
		/* UBR target */
		if (Cube->up[0] == 'w' && Cube->left[0] == 'b' && Cube->back[2] == 'r') {
			return 1;
		}
		
		/* BRU target */
		else if (Cube->up[0] == 'b' && Cube->left[0] == 'r' && Cube->back[2] == 'w') {
			return 2;
		}
		
		/* RUB target */
		else if (Cube->up[0] == 'r' && Cube->left[0] == 'w' && Cube->back[2] == 'b') {
			return 3;
		}
		
		/* URF target */
		else if (Cube->up[0] == 'w' && Cube->left[0] == 'r' && Cube->back[2] == 'g') {
			return 4;
		}
		
		/* RFU target */
		else if (Cube->up[0] == 'r' && Cube->left[0] == 'g' && Cube->back[2] == 'w') {
			return 5;
		}
		
		/* FUR target */
		else if (Cube->up[0] == 'g' && Cube->left[0] == 'w' && Cube->back[2] == 'r') {
			return 6;
		}
		
		/* UFL target */
		else if (Cube->up[0] == 'w' && Cube->left[0] == 'g' && Cube->back[2] == 'o') {
			return 7;
		}
		
		/* FLU target */
		else if (Cube->up[0] == 'g' && Cube->left[0] == 'o' && Cube->back[2] == 'w') {
			return 8;
		}
		
		/* LUF target */
		else if (Cube->up[0] == 'o' && Cube->left[0] == 'w' && Cube->back[2] == 'g') {
			return 9;
		}
		
		/* FRD target */
		else if (Cube->up[0] == 'g' && Cube->left[0] == 'r' && Cube->back[2] == 'y') {
			return 10;
		}
		
		/* RDF target */
		else if (Cube->up[0] == 'r' && Cube->left[0] == 'y' && Cube->back[2] == 'g') {
			return 11;
		}
		
		/* DFR target */
		else if (Cube->up[0] == 'y' && Cube->left[0] == 'g' && Cube->back[2] == 'r') {
			return 12;
		}
		
		/* DRB target */
		else if (Cube->up[0] == 'y' && Cube->left[0] == 'r' && Cube->back[2] == 'b') {
			return 13;
		}
		
		/* RBD target */
		else if (Cube->up[0] == 'r' && Cube->left[0] == 'b' && Cube->back[2] == 'y') {
			return 14;
		}
		
		/* BDR target */
		else if (Cube->up[0] == 'b' && Cube->left[0] == 'y' && Cube->back[2] == 'r') {
			return 15;
		}
		
		/* DBL target */
		else if (Cube->up[0] == 'y' && Cube->left[0] == 'b' && Cube->back[2] == 'o') {
			return 16;
		}
		
		/* BLD target */
		else if (Cube->up[0] == 'b' && Cube->left[0] == 'o' && Cube->back[2] == 'y') {
			return 17;
		}
		
		/* LDB target */
		else if (Cube->up[0] == 'o' && Cube->left[0] == 'y' && Cube->back[2] == 'b') {
			return 18;
		}
		
		/* FDL target */
		else if (Cube->up[0] == 'g' && Cube->left[0] == 'y' && Cube->back[2] == 'o') {
			return 19;
		}
		
		/* DLF target */
		else if (Cube->up[0] == 'y' && Cube->left[0] == 'o' && Cube->back[2] == 'g') {
			return 20;
		}
		
		/* LFD target */
		else if (Cube->up[0] == 'o' && Cube->left[0] == 'g' && Cube->back[2] == 'y') {
			return 21;
		}
		
		/* special case, buffer in place, twisted */
		else if ((Cube->up[0] == 'b' && Cube->left[0] == 'w' && Cube->back[2] == 'o') || (Cube->up[0] == 'o' && Cube->left[0] == 'b' && Cube->back[2] == 'w')){
			int found=0;
			int i, idx;
			for (i=1;i<=7;i++) {
				if (UnsolvedCorners[i] == 1) {
					idx = i;
					found = 1;
				}
			}
			if (found==1)
				return idx*3;
			else
				return 0;
			}
	}
	else { /* buffer is in place, UnsolvedEdges[0] == 0 */
		int found=0;
		int i, idx;
		for (i=1;i<=7;i++) {
			if (UnsolvedCorners[i] == 1) {
				idx = i;
				found = 1;
			}
		}
		if (found==1)
			return idx*3;
		else
			return 0;
	}
}

void ShootCorners(cube *Cube, int target) {
	if (target==1) { /* UBR */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->up[2]);
		SwapColour(&Cube->left[0], &Cube->back[0]);
		SwapColour(&Cube->back[2], &Cube->right[2]);
		//print
		printf("R2 D' F2 F R U' R' U' R U R' F' R U R' U' R' F R F' F2 D R2\n");
	}
	
	else if (target==2) { /* BRU */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->back[0]);
		SwapColour(&Cube->left[0], &Cube->right[2]);
		SwapColour(&Cube->back[2], &Cube->up[2]);
		//print
		printf("R' F R U' R' U' R U R' F' R U R' U' R' F R F' R\n");
	}
	
	else if (target==3) { /* RUB */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->right[2]);
		SwapColour(&Cube->left[0], &Cube->up[2]);
		SwapColour(&Cube->back[2], &Cube->back[0]);
		//print
		printf("R2 F' F R U' R' U' R U R' F' R U R' U' R' F R F' F R2\n");
	}
	
	else if (target==4) { /* URF */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->up[8]);
		SwapColour(&Cube->left[0], &Cube->right[0]);
		SwapColour(&Cube->back[2], &Cube->front[2]);
		//print
		printf("F R U' R' U' R U R' F' R U R' U' R' F R F'\n");
	}
	
	else if (target==5) { /* RFU */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->right[0]);
		SwapColour(&Cube->left[0], &Cube->front[2]);
		SwapColour(&Cube->back[2], &Cube->up[8]);
		//print
		printf("R' F' F R U' R' U' R U R' F' R U R' U' R' F R F' F R\n");
	}
	
	else if (target==6) { /* FUR */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->front[2]);
		SwapColour(&Cube->left[0], &Cube->up[8]);
		SwapColour(&Cube->back[2], &Cube->right[0]);
		//print
		printf("F R F R U' R' U' R U R' F' R U R' U' R' F R F' R' F'\n");
	}
	
	else if (target==7) { /* UFL */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->up[6]);
		SwapColour(&Cube->left[0], &Cube->front[0]);
		SwapColour(&Cube->back[2], &Cube->left[2]);
		//print
		printf("F2 D' F2 F R U' R' U' R U R' F' R U R' U' R' F R F' F2 D F2\n");
	}
	
	else if (target==8) { /* FLU */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->front[0]);
		SwapColour(&Cube->left[0], &Cube->left[2]);
		SwapColour(&Cube->back[2], &Cube->up[6]);
		//print
		printf("F2 R F R U' R' U' R U R' F' R U R' U' R' F R F' R' F2\n");
	}
	
	else if (target==9) { /* LUF */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->left[2]);
		SwapColour(&Cube->left[0], &Cube->up[6]);
		SwapColour(&Cube->back[2], &Cube->front[0]);
		//print
		printf("F F R U' R' U' R U R' F' R U R' U' R' F R F' F'\n");
	}
	
	else if (target==10) { /* FRD */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->front[8]);
		SwapColour(&Cube->left[0], &Cube->right[6]);
		SwapColour(&Cube->back[2], &Cube->down[2]);
		//print
		printf("R F R U' R' U' R U R' F' R U R' U' R' F R F' R'\n");
	}
	
	else if (target==11) { /* RDF */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->right[6]);
		SwapColour(&Cube->left[0], &Cube->down[2]);
		SwapColour(&Cube->back[2], &Cube->front[8]);
		//print
		printf("F' F R U' R' U' R U R' F' R U R' U' R' F R F' F\n");
	}
	
	else if (target==12) { /* DFR */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->down[2]);
		SwapColour(&Cube->left[0], &Cube->front[8]);
		SwapColour(&Cube->back[2], &Cube->right[6]);
		//print
		printf("D' F2 F R U' R' U' R U R' F' R U R' U' R' F R F' F2 D\n");
	}
	
	else if (target==13) { /* DRB */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->down[8]);
		SwapColour(&Cube->left[0], &Cube->right[8]);
		SwapColour(&Cube->back[2], &Cube->back[6]);
		//print
		printf("R2 F R U' R' U' R U R' F' R U R' U' R' F R F' R2\n");
	}
	
	else if (target==14) { /* RBD */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->right[8]);
		SwapColour(&Cube->left[0], &Cube->back[6]);
		SwapColour(&Cube->back[2], &Cube->down[8]);
		//print
		printf("D' R F R U' R' U' R U R' F' R U R' U' R' F R F' R' D\n");
	}
	
	else if (target==15) { /* BDR */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->back[6]);
		SwapColour(&Cube->left[0], &Cube->down[8]);
		SwapColour(&Cube->back[2], &Cube->right[8]);
		//print
		printf("D' F' F R U' R' U' R U R' F' R U R' U' R' F R F' F D\n");
	}
	
	else if (target==16) { /* DBL */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->down[6]);
		SwapColour(&Cube->left[0], &Cube->back[8]);
		SwapColour(&Cube->back[2], &Cube->left[6]);
		//print
		printf("D F2 F R U' R' U' R U R' F' R U R' U' R' F R F' F2 D'\n");
	}
	
	else if (target==17) { /* BLD */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->back[8]);
		SwapColour(&Cube->left[0], &Cube->left[6]);
		SwapColour(&Cube->back[2], &Cube->down[6]);
		//print
		printf("D2 R F R U' R' U' R U R' F' R U R' U' R' F R F' R' D2\n");
	}
	
	else if (target==18) { /* LDB */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->left[6]);
		SwapColour(&Cube->left[0], &Cube->down[6]);
		SwapColour(&Cube->back[2], &Cube->back[8]);
		//print
		printf("D2 F' F R U' R' U' R U R' F' R U R' U' R' F R F' F D2\n");
	}
	
	else if (target==19) { /* FDL */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->front[6]);
		SwapColour(&Cube->left[0], &Cube->down[0]);
		SwapColour(&Cube->back[2], &Cube->left[8]);
		//print
		printf("D F' F R U' R' U' R U R' F' R U R' U' R' F R F' F D'\n");
	}
	
	else if (target==20) { /* DLF */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->down[0]);
		SwapColour(&Cube->left[0], &Cube->left[8]);
		SwapColour(&Cube->back[2], &Cube->front[6]);
		//print
		printf("F2 F R U' R' U' R U R' F' R U R' U' R' F R F' F2\n");
	}
	
	else if (target==21) { /* LFD */
		//swap edge colours
		SwapColour(&Cube->up[1], &Cube->up[3]);
		SwapColour(&Cube->left[1], &Cube->back[1]);
		//swap corner colours
		SwapColour(&Cube->up[0], &Cube->left[8]);
		SwapColour(&Cube->left[0], &Cube->front[6]);
		SwapColour(&Cube->back[2], &Cube->down[0]);
		//print
		printf("D R F R U' R' U' R U R' F' R U R' U' R' F R F' R' D'\n");
	}
}

void SwapColour(char *a, char *b) {
	char temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

One thought on “Cube.c

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s