/*********************************************
目的: 模擬魔術方塊公式
性質: 自行寫作
日期: 2009/8/4
*********************************************/
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <cstdlib>
#include <iomanip>
using std::setw;
#define a 6 //六個面
#define b 3 //一個面x3格
#define white '0'
#define red '1'
#define orange '2'
#define yellow '3'
#define green '4'
#define blue '5'
char arr[a][b][b];
void print();
void doL();
void doR();
void doU();
void doD();
void doF();
void doB();
bool check();
int main()
{
int counter = 0;
char buzz = 7;
for(int i = 0; i < a; i++)
for(int j = 0; j < b; j++)
for(int k = 0; k < b; k++)
arr[i][j][k] = '0' + i;
print();
do{
doR();
print();
doU();
print();
counter++;
}while(!check());
cout<<buzz<<endl;
cout<<"Do "<<counter<<" times."<<endl;
system("pause");
return 0;
}
void print()
{
cout<<"------------------------------------------------------------Top"<<endl;
for(int i = 0; i < 3; i++){
cout<<setw(8);
for(int j = 0; j < 3; j++){
cout<<arr[5][i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
for(int i = 0; i < 3; i++){
cout<<setw(8);
for(int j = 0; j < 3; j++){
cout<<arr[4][i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
cout<<arr[3][i][j]<<" ";
}
cout<<" ";
for(int j = 0; j < 3; j++){
cout<<arr[0][i][j]<<" ";
}
cout<<" ";
for(int j = 0; j < 3; j++){
cout<<arr[1][i][j]<<" ";
}
cout<<" ";
cout<<endl;
}
cout<<endl;
for(int i = 0; i < 3; i++){
cout<<setw(8);
for(int j = 0; j < 3; j++){
cout<<arr[2][i][j]<<" ";
}
cout<<endl;
}
cout<<"------------------------------------------------------------End"<<endl;
}
void doL()
{
int side = 3;
int line = 0;
char temp1[4][3];
char temp2[8];
//左側邊旋轉
for(int i = 0; i < 3; i++){
temp1[0][i] = arr[0][i][line];
temp1[1][i] = arr[4][i][line];
temp1[2][i] = arr[5][i][line];
temp1[3][i] = arr[2][i][line];
}
for(int i = 0; i < 3; i++){
arr[0][i][line] = temp1[3][i];
arr[4][i][line] = temp1[0][i];
arr[5][i][line] = temp1[1][i];
arr[2][i][line] = temp1[2][i];
}
//左側面旋轉
temp2[0] = arr[side][0][0];
temp2[1] = arr[side][0][1];
temp2[2] = arr[side][0][2];
temp2[3] = arr[side][1][2];
temp2[4] = arr[side][2][2];
temp2[5] = arr[side][2][1];
temp2[6] = arr[side][2][0];
temp2[7] = arr[side][1][0];
arr[side][0][0] = temp2[2];
arr[side][0][1] = temp2[3];
arr[side][0][2] = temp2[4];
arr[side][1][2] = temp2[5];
arr[side][2][2] = temp2[6];
arr[side][2][1] = temp2[7];
arr[side][2][0] = temp2[0];
arr[side][1][0] = temp2[1];
cout<<"do L"<<endl;
}
void doR()
{
int side = 1;
int line = 2;
char temp1[4][3];
char temp2[8];
//右側邊旋轉
for(int i = 0; i < 3; i++){
temp1[0][i] = arr[0][i][line];
temp1[1][i] = arr[4][i][line];
temp1[2][i] = arr[5][i][line];
temp1[3][i] = arr[2][i][line];
}
for(int i = 0; i < 3; i++){
arr[0][i][line] = temp1[3][i];
arr[4][i][line] = temp1[0][i];
arr[5][i][line] = temp1[1][i];
arr[2][i][line] = temp1[2][i];
}
//右側面旋轉
temp2[0] = arr[side][0][0];
temp2[1] = arr[side][0][1];
temp2[2] = arr[side][0][2];
temp2[3] = arr[side][1][2];
temp2[4] = arr[side][2][2];
temp2[5] = arr[side][2][1];
temp2[6] = arr[side][2][0];
temp2[7] = arr[side][1][0];
arr[side][0][0] = temp2[6];
arr[side][0][1] = temp2[7];
arr[side][0][2] = temp2[0];
arr[side][1][2] = temp2[1];
arr[side][2][2] = temp2[2];
arr[side][2][1] = temp2[3];
arr[side][2][0] = temp2[4];
arr[side][1][0] = temp2[5];
cout<<"do R"<<endl;
}
void doU()
{
int side = 0;
char temp1[4][3];
char temp2[8];
//上側邊旋轉
for(int i = 0; i < 3; i++){
temp1[0][i] = arr[3][i][2];
temp1[1][i] = arr[4][2][i];
temp1[2][i] = arr[1][i][0];
temp1[3][i] = arr[2][0][i];
}
for(int i = 0; i < 3; i++){
arr[3][i][2] = temp1[3][i];
arr[4][2][i] = temp1[0][2-i];
arr[1][i][0] = temp1[1][i];
arr[2][0][i] = temp1[2][2-i];
}
//上側面旋轉
temp2[0] = arr[side][0][0];
temp2[1] = arr[side][0][1];
temp2[2] = arr[side][0][2];
temp2[3] = arr[side][1][2];
temp2[4] = arr[side][2][2];
temp2[5] = arr[side][2][1];
temp2[6] = arr[side][2][0];
temp2[7] = arr[side][1][0];
arr[side][0][0] = temp2[6];
arr[side][0][1] = temp2[7];
arr[side][0][2] = temp2[0];
arr[side][1][2] = temp2[1];
arr[side][2][2] = temp2[2];
arr[side][2][1] = temp2[3];
arr[side][2][0] = temp2[4];
arr[side][1][0] = temp2[5];
cout<<"do U"<<endl;
}
void doD()
{
int side = 5;
char temp1[4][3];
char temp2[8];
//下側邊旋轉
for(int i = 0; i < 3; i++){
temp1[0][i] = arr[3][i][0];
temp1[1][i] = arr[4][0][i];
temp1[2][i] = arr[1][i][2];
temp1[3][i] = arr[2][2][i];
}
for(int i = 0; i < 3; i++){
arr[3][i][0] = temp1[3][i];
arr[4][0][i] = temp1[0][2-i];
arr[1][i][2] = temp1[1][i];
arr[2][2][i] = temp1[2][2-i];
}
//下側面旋轉
temp2[0] = arr[side][0][0];
temp2[1] = arr[side][0][1];
temp2[2] = arr[side][0][2];
temp2[3] = arr[side][1][2];
temp2[4] = arr[side][2][2];
temp2[5] = arr[side][2][1];
temp2[6] = arr[side][2][0];
temp2[7] = arr[side][1][0];
arr[side][0][0] = temp2[2];
arr[side][0][1] = temp2[3];
arr[side][0][2] = temp2[4];
arr[side][1][2] = temp2[5];
arr[side][2][2] = temp2[6];
arr[side][2][1] = temp2[7];
arr[side][2][0] = temp2[0];
arr[side][1][0] = temp2[1];
cout<<"do D"<<endl;
}
void doF()
{
int side = 4;
char temp1[4][3];
char temp2[8];
//前側邊旋轉
for(int i = 0; i < 3; i++){
temp1[0][i] = arr[3][0][i];
temp1[1][i] = arr[0][0][i];
temp1[2][i] = arr[1][0][i];
temp1[3][i] = arr[5][2][i];
}
for(int i = 0; i < 3; i++){
arr[3][0][i] = temp1[3][2-i];
arr[0][0][i] = temp1[0][i];
arr[1][0][i] = temp1[1][i];
arr[5][2][i] = temp1[2][2-i];
}
//前側面旋轉
temp2[0] = arr[side][0][0];
temp2[1] = arr[side][0][1];
temp2[2] = arr[side][0][2];
temp2[3] = arr[side][1][2];
temp2[4] = arr[side][2][2];
temp2[5] = arr[side][2][1];
temp2[6] = arr[side][2][0];
temp2[7] = arr[side][1][0];
arr[side][0][0] = temp2[2];
arr[side][0][1] = temp2[3];
arr[side][0][2] = temp2[4];
arr[side][1][2] = temp2[5];
arr[side][2][2] = temp2[6];
arr[side][2][1] = temp2[7];
arr[side][2][0] = temp2[0];
arr[side][1][0] = temp2[1];
cout<<"do F"<<endl;
}
void doB()
{
int side = 2;
char temp1[4][3];
char temp2[8];
//後側邊旋轉
for(int i = 0; i < 3; i++){
temp1[0][i] = arr[3][2][i];
temp1[1][i] = arr[0][2][i];
temp1[2][i] = arr[1][2][i];
temp1[3][i] = arr[5][0][i];
}
for(int i = 0; i < 3; i++){
arr[3][2][i] = temp1[3][2-i];
arr[0][2][i] = temp1[0][i];
arr[1][2][i] = temp1[1][i];
arr[5][0][i] = temp1[2][2-i];
}
//後側面旋轉
temp2[0] = arr[side][0][0];
temp2[1] = arr[side][0][1];
temp2[2] = arr[side][0][2];
temp2[3] = arr[side][1][2];
temp2[4] = arr[side][2][2];
temp2[5] = arr[side][2][1];
temp2[6] = arr[side][2][0];
temp2[7] = arr[side][1][0];
arr[side][0][0] = temp2[6];
arr[side][0][1] = temp2[7];
arr[side][0][2] = temp2[0];
arr[side][1][2] = temp2[1];
arr[side][2][2] = temp2[2];
arr[side][2][1] = temp2[3];
arr[side][2][0] = temp2[4];
arr[side][1][0] = temp2[5];
cout<<"do B"<<endl;
}
bool check()
{
int check = 0;
char temp;
for(int g = 0; g < a; g++){
temp = arr[g][0][0];
for(int i = 0; i < b; i++)
for(int j = 0; j < b; j++)
if(temp != arr[g][i][j])check++;
}
if(check==0)
return true;
else
return false;
}