我们不仅销售产品,我们还传播知识
商品分类

arduino自动还原魔方人工智能源代码

arduino自动还原魔方人工智能源代码

arduino自动还原魔方人工智能源代码 评论

 1.主程序模块相关代码
#include<iostream>
#include<fstream>
using namespace std;
#ifndef CUBE
#define CUBE
class cube{
public:
	cube() {}  //类的构造函数
	void input_cube(char *a);  //输入魔方颜色
	void RUN();
private:
	int numb = 0;
	char center[6];  //魔方六个面的中心
	char edge[12][2];  //魔方的12条棱
	char vertex[8][3];  //魔方的8个顶点
	ofstream file;
	void fileout1(char a);  //向txt文档输出顺时针旋转
	void fileout2(char a);  //向txt文档输出180度旋转
	void fileout3(char a);  //向txt文档输出逆时针旋转
	void U_CW();  //魔方的上面顺时针转动
	void U2_CW();  //魔方的上两层顺时针转动
	void U3_CW();  //魔方的上三层顺时针转动
	void UD_CW();  //魔方的上下面中间层顺时针转动
	void D_CW();  //魔方的下面顺时针转动
	void D2_CW();  //魔方的下两层顺时针转动
	void F_CW();  //魔方的前面顺时针转动
	void F2_CW();  //魔方的前两层顺时针转动
	void F3_CW();  //魔方的前三层顺时针转动
	void B_CW();  //魔方的后面顺时针转动
	void B2_CW();  //魔方的后两层顺时针转动
	void L_CW();  //魔方的左面顺时针转动
	void LR_CW();  //魔方的左右面中间层顺时针转动
	void R_CW();  //魔方的右面顺时针转动
	void R2_CW();  //魔方的右两层顺时针转动
	void R3_CW();  //魔方的右三层顺时针转动
	//以下为转动魔方的执行函数
	void U_cw1();  //魔方的上面顺时针转动90度
	void U_cw2();  //魔方的上面转动180度
	void U_cw3();  //魔方的上面逆时针转动90度
	void U2_cw1();  //魔方的上两层顺时针转动90度
	void U2_cw2();  //魔方的上两层转动180度
	void U2_cw3();  //魔方的上两层逆时针转动90度
	void U3_cw1();  //魔方的上三层顺时针转动90度
	void U3_cw2();  //魔方的上三层转动180度
	void U3_cw3();  //魔方的上三层逆时针转动90度
	void UD_cw1();  //魔方的上下面中间层顺时针转动90度
	void UD_cw2();  //魔方的上下面中间层转动180度
	void UD_cw3();  //魔方的上下面中间层逆时针转动90度
	void D_cw1();  //魔方的下面顺时针转动90度
	void D_cw2();  //魔方的下面转动180度
	void D_cw3();  //魔方的下面逆时针转动90度
	void F_cw1();  //魔方的前面顺时针转动90度
	void F_cw2();  //魔方的前面转动180度
	void F_cw3();  //魔方的前面逆时针转动90度
	void F2_cw1();  //魔方的前两层顺时针转动90度
	void F2_cw2();  //魔方的前两层转动180度
	void F2_cw3();  //魔方的前两层逆时针转动90度
	void F3_cw1();  //魔方的前三层顺时针转动90度
	void F3_cw2();  //魔方的前三层转动180度
	void F3_cw3();  //魔方的前三层逆时针转动90度
	void B_cw1();  //魔方的后面顺时针转动90度
	void B_cw2();  //魔方的后面转动180度
	void B_cw3();  //魔方的后面逆时针转动90度
	void L_cw1();  //魔方的左面顺时针转动90度
	void L_cw2();  //魔方的左面转动180度
	void L_cw3();  //魔方的左面逆时针转动90度
	void LR_cw1();  //魔方的左右面中间层顺时针转动90度
	void LR_cw2();  //魔方的左右面中间层转动180度
	void LR_cw3();  //魔方的左右面中间层逆时针转动90度
	void R_cw1();  //魔方的右面顺时针转动90度
	void R_cw2();  //魔方的右面转动180度
	void R_cw3();  //魔方的右面逆时针转动90度
	void R2_cw1();  //魔方的右两层顺时针转动90度
	void R2_cw2();  //魔方的右两层转动180度
	void R2_cw3();  //魔方的右两层逆时针转动90度
	void R3_cw1();  //魔方的右三层顺时针转动90度
	void R3_cw2();  //魔方的右三层转动180度
	void R3_cw3();  //魔方的右三层逆时针转动90度
	void bottom_cross();  //还原魔方的上面十字
	void bottom_vertex();  //还原魔方的上面
	void middlelayer();  //还原魔方的上下面中间层
	void top_cross();  //还原魔方的下面十字
	void top_face();  //还原魔方的下面棱
	void top_vertex();  //还原魔方的下面角
	void top_edge();  //还原魔方的下面
};
void cube::input_cube(char *a){  //输入魔方颜色
	//输入顶面色块:
	vertex[0][0] = a[0];
	edge[0][0] = a[1];
	vertex[1][0] = a[2];
	edge[3][0] = a[3];
	center[0] = a[4];
	edge[1][0] = a[5];
	vertex[3][0] = a[6];
	edge[2][0] = a[7];
	vertex[2][0] = a[8];
	//输入侧面第一层色块:
	vertex[3][1] = a[9];
	edge[2][1] = a[10];
	vertex[2][2] = a[11];
	vertex[2][1] = a[18];
	edge[1][1] = a[19];
	vertex[1][2] = a[20];
	vertex[1][1] = a[27];
	edge[0][1] = a[28];
	vertex[0][2] = a[29];
	vertex[0][1] = a[36];
	edge[3][1] = a[37];
	vertex[3][2] = a[38];
	//输入侧面第二层色块:
	edge[7][0] = a[12];
	center[4] = a[13];
	edge[6][1] = a[14];
	edge[6][0] = a[21];
	center[3] = a[22];
	edge[5][1] = a[23];
	edge[5][0] = a[30];
	center[2] = a[31];
	edge[4][1] = a[32];
	edge[4][0] = a[39];
	center[1] = a[40];
	edge[7][1] = a[41];
	//输入侧面第三层色块:
	vertex[7][1] = a[15];
	edge[10][1] = a[16];
	vertex[6][2] = a[17];
	vertex[6][1] = a[24];
	edge[9][1] = a[25];
	vertex[5][2] = a[26];
	vertex[5][1] = a[33];
	edge[8][1] = a[34];
	vertex[4][2] = a[35];
	vertex[4][1] = a[42];
	edge[11][1] = a[43];
	vertex[7][2] = a[44];
	//输入底面色块:
	vertex[7][0] = a[45];
	edge[10][0] = a[46];
	vertex[6][0] = a[47];
	edge[11][0] = a[48];
	center[5] = a[49];
	edge[9][0] = a[50];
	vertex[4][0] = a[51];
	edge[8][0] = a[52];
	vertex[5][0] = a[53];
}
void cube::fileout1(char a) {  //向txt文档输出顺时针旋转
	if (numb == 45) {  //txt文档换行函数
		file << endl;
		numb = 0;
	}
	file << char(a);
	numb = numb + 1;
}
void cube::fileout2(char a) {  //向txt文档输出180度旋转
	if (numb == 45) {  //txt文档换行函数
		file << endl;
		numb = 0;
	}
	file << char(a + 33);
	numb = numb + 1;
}
void cube::fileout3(char a) {  //向txt文档输出逆时针旋转
	if (numb == 45) {  //txt文档换行函数
		file << endl;
		numb = 0;
	}
	file << char(a + 32);
	numb = numb + 1;
}
void cube::U_CW() {  //魔方的上面顺时针转动
	char vertex3[3] = {};  //魔方顶点3
	for (int i = 0; i<3; i++)
		vertex3[i] = vertex[3][i];
	char edge3[2] = {};  //魔方棱3
	edge3[0] = edge[3][0]; edge3[1] = edge[3][1];
	for (int i = 3; i>0; i--) {
		vertex[i][2] = vertex[i - 1][2];
		vertex[i][1] = vertex[i - 1][1];
		vertex[i][0] = vertex[i - 1][0];
		edge[i][1] = edge[i - 1][1];
		edge[i][0] = edge[i - 1][0];
	}
	vertex[0][2] = vertex3[2];
	vertex[0][1] = vertex3[1];
	vertex[0][0] = vertex3[0];
	edge[0][1] = edge3[1];
	edge[0][0] = edge3[0];
}
void cube::U2_CW() {  //魔方的下面顺时针转动
	U_CW();
	UD_CW();  //魔方的上两层顺时针转动
}
void cube::U3_CW() {  //魔方的上三层顺时针转动
	U_CW();
	UD_CW();
	D_CW(); D_CW(); D_CW();
}
void cube::UD_CW() {  //魔方的上下面中间层顺时针转动
	char c1 = edge[7][0], c2 = edge[7][1], c3 = center[4];
	for (int i = 3; i>0; i--) {
		edge[i + 4][0] = edge[i + 3][0];
		edge[i + 4][1] = edge[i + 3][1];
		center[i + 1] = center[i];
	}
	edge[4][0] = c1;
	edge[4][1] = c2;
	center[1] = c3;  //魔方的上面逆时针转动,魔方的下面顺时针转动
}
void cube::D_CW() {  //魔方的下面顺时针转动
	char vertex4[3] = {};  //魔方顶点4
	for (int i = 0; i < 3; i++)
		vertex4[i] = vertex[4][i];
	char edge8[2] = {};  //魔方棱8
	edge8[0] = edge[8][0];
	edge8[1] = edge[8][1];
	for (int i = 4; i < 7; i++) {
		vertex[i][2] = vertex[i + 1][2];
		vertex[i][1] = vertex[i + 1][1];
		vertex[i][0] = vertex[i + 1][0];
		edge[i + 4][1] = edge[i + 5][1];
		edge[i + 4][0] = edge[i + 5][0];
	}
	vertex[7][2] = vertex4[2];
	vertex[7][1] = vertex4[1];
	vertex[7][0] = vertex4[0];
	edge[11][1] = edge8[1];
	edge[11][0] = edge8[0];
}
void cube::D2_CW() {  //魔方的上面顺时针转动
	D_CW(); D_CW(); D_CW();
	UD_CW();  //魔方的下两层顺时针转动
}
void cube::F_CW() {  //魔方的前面顺时针转动
	R3_CW(); U_CW();
	R3_CW(); R3_CW(); R3_CW();
}
void cube::F2_CW() {  //魔方的后面顺时针转动
	R3_CW(); U2_CW();
	R3_CW(); R3_CW(); R3_CW();  //魔方的前两层顺时针转动
}
void cube::F3_CW() {  //魔方的前三层顺时针转动
	F2_CW();
	B_CW(); B_CW(); B_CW();
}
void cube::B_CW() {  //魔方的后面顺时针转动
	R3_CW(); R3_CW();
	F_CW();
	R3_CW(); R3_CW();
}
void cube::B2_CW() {  //魔方的前面顺时针转动
	R3_CW(); R3_CW();
	F2_CW();
	R3_CW(); R3_CW();  //魔方的后两层顺时针转动
}
void cube::L_CW() {  //魔方左面顺时针转动
	char vertex3[3] = {};  //魔方顶点3
	for (int i = 0; i<3; i++)
		vertex3[i] = vertex[3][i];
	//魔方顶点的变化:
	vertex[3][0] = vertex[0][2];
	vertex[3][1] = vertex[0][0];
	vertex[3][2] = vertex[0][1];
	vertex[0][0] = vertex[4][2];
	vertex[0][1] = vertex[4][1];
	vertex[0][2] = vertex[4][0];
	vertex[4][0] = vertex[7][1];
	vertex[4][1] = vertex[7][2];
	vertex[4][2] = vertex[7][0];
	vertex[7][0] = vertex3[1];
	vertex[7][1] = vertex3[0];
	vertex[7][2] = vertex3[2];
	char edge3[2] = {};  //魔方棱3
	//魔方棱的变化:
	edge3[0] = edge[3][0];
	edge3[1] = edge[3][1];
	edge[3][0] = edge[4][1];
	edge[3][1] = edge[4][0];
	edge[4][0] = edge[11][1];
	edge[4][1] = edge[11][0];
	edge[11][0] = edge[7][0];
	edge[11][1] = edge[7][1];
	edge[7][0] = edge3[0];
	edge[7][1] = edge3[1];
}
void cube::LR_CW() {  //魔方的左右面中间层顺时针转动
	char c1 = edge[2][1], c2 = center[4], c3 = edge[10][1];
	edge[2][1] = edge[10][0];
	center[4] = center[5];
	edge[10][1] = edge[8][0];
	edge[10][0] = edge[8][1];
	center[5] = center[2];
	edge[8][0] = edge[0][1];
	edge[8][1] = edge[0][0];
	center[2] = center[0];
	edge[0][1] = edge[2][0];
	edge[0][0] = c1;
	center[0] = c2;
	edge[2][0] = c3;  //魔方的左面顺时针转动,魔方的右面逆时针转动
}
void cube::R_CW(){  //魔方右面顺时针转动
	char vertex1[3] = {};  //魔方顶点1
	for (int i = 0; i<3; i++)
		vertex1[i] = vertex[1][i];
	//魔方顶点的变化:
	vertex[1][0] = vertex[2][2];
	vertex[1][1] = vertex[2][0];
	vertex[1][2] = vertex[2][1];
	vertex[2][0] = vertex[6][2];
	vertex[2][1] = vertex[6][1];
	vertex[2][2] = vertex[6][0];
	vertex[6][0] = vertex[5][1];
	vertex[6][1] = vertex[5][2];
	vertex[6][2] = vertex[5][0];
	vertex[5][0] = vertex1[1];
	vertex[5][1] = vertex1[0];
	vertex[5][2] = vertex1[2];
	char edge1[2] = {};  //魔方棱1
	//魔方棱的变化:
	edge1[0] = edge[1][0];
	edge1[1] = edge[1][1];
	edge[1][0] = edge[6][1];
	edge[1][1] = edge[6][0];
	edge[6][0] = edge[9][1];
	edge[6][1] = edge[9][0];
	edge[9][0] = edge[5][0];
	edge[9][1] = edge[5][1];
	edge[5][0] = edge1[0];
	edge[5][1] = edge1[1];
}
void cube::R2_CW(){  //魔方的左面顺时针转动
	R_CW();
	LR_CW();  //魔方的右两层顺时针转动
}
void cube::R3_CW(){  //魔方的右三层顺时针转动
	LR_CW();
	R_CW();
	L_CW(); L_CW(); L_CW();
}
void cube::U_cw1() {  //执行:魔方的上面顺时针转动90度
	getchar();
	cout << center[0] << "面顺时针旋转90度" << endl;
	U_CW();
	fileout1(center[0]);
}
void cube::U_cw2() {  //执行:魔方的上面转动180度
	getchar();
	cout << center[0] << "面旋转180度" << endl;
	U_CW(); U_CW();
	fileout2(center[0]);
}
void cube::U_cw3() {  //执行:魔方的上面逆时针转动90度
	getchar();
	cout << center[0] << "面逆时针旋转90度" << endl;
	U_CW(); U_CW(); U_CW();
	fileout3(center[0]);
}
void cube::U2_cw1() {  //执行:魔方的上两层顺时针转动90度
	getchar();
	cout << center[5] << "顺时针旋转90度" << endl;
	U2_CW();
	fileout1(center[5]);
}
void cube::U2_cw2() {  //执行:魔方的上两层转动180度
	getchar();
	cout << center[5] << "旋转180度" << endl;
	U2_CW(); U2_CW();
	fileout2(center[5]);
}
void cube::U2_cw3() {  //执行:魔方的上两层逆时针转动90度
	getchar();
	cout << center[5] << "逆时针旋转90度" << endl;
	U2_CW(); U2_CW(); U2_CW();
	fileout3(center[5]);
}
void cube::U3_cw1() {  //执行:魔方的上三层顺时针转动90度
	U3_CW();
}
void cube::U3_cw2() {  //执行:魔方的上三层转动180度
	U3_CW(); U3_CW();
}
void cube::U3_cw3() {  //执行:魔方的上三层逆时针转动90度
	U3_CW(); U3_CW(); U3_CW();
}
void cube::UD_cw1() {  //执行:魔方的上下面中间层顺时针转动90度
	getchar();
	cout << center[0] << "面逆时针旋转90度; " << endl << center[5] << "面顺时针旋转90度" << endl;
	UD_CW();
	fileout3(center[0]);
	fileout1(center[5]);
}
void cube::UD_cw2() {  //执行:魔方的上下面中间层转动180度
	getchar();
	cout << center[0] << "面旋转180度; " << endl << center[5] << "面旋转180度" << endl;
	UD_CW(); UD_CW();
	fileout2(center[0]);
	fileout2(center[5]);
}
void cube::UD_cw3() {  //执行:魔方的上下面中间层逆时针转动90度
	getchar();
	cout << center[0] << "面顺时针旋转90度; " << endl << center[5] << "面逆时针旋转90度" << endl;
	UD_CW(); UD_CW(); UD_CW();
	fileout1(center[0]);
	fileout3(center[5]);
}
void cube::D_cw1() {  //执行:魔方的下面顺时针转动90度
	getchar();
	cout << center[5] << "面顺时针旋转90度" << endl;
	D_CW();
	fileout1(center[5]);
}
void cube::D_cw2() {  //执行:魔方的下面转动180度
	getchar();
	cout << center[5] << "面旋转180度" << endl;
	D_CW(); D_CW();
	fileout2(center[5]);
}
void cube::D_cw3() {  //执行:魔方的下面逆时针转动90度
	getchar();
	cout << center[5] << "面逆时针旋转90度" << endl;
	D_CW(); D_CW(); D_CW();
	fileout3(center[5]);
}
void cube::F_cw1() {  //执行:魔方的前面顺时针转动90度
	getchar();
	cout << center[4] << "面顺时针旋转90度" << endl;
	F_CW();
	fileout1(center[4]);
}
void cube::F_cw2() {  //执行:魔方的前面转动180度
	getchar();
	cout << center[4] << "面旋转180度" << endl;
	F_CW(); F_CW();
	fileout2(center[4]);
}
void cube::F_cw3() {  //执行:魔方的前面逆时针转动90度
	getchar();
	cout << center[4] << "面逆时针旋转90度" << endl;
	F_CW(); F_CW(); F_CW();
	fileout3(center[4]);
}
void cube::F2_cw1() {  //执行:魔方的前两层顺时针转动90度
	getchar();
	cout << center[2] << "面顺时针旋转90度" << endl;
	F_CW();
	fileout1(center[2]);
}
void cube::F2_cw2() {  //执行:魔方的前两层转动180度
	getchar();
	cout << center[2] << "面旋转180度" << endl;
	F_CW(); F_CW();
	fileout2(center[2]);
}
void cube::F2_cw3() {  //执行:魔方的前两层逆时针转动90度
	getchar();
	cout << center[2] << "面逆时针旋转90度" << endl;
	F_CW(); F_CW(); F_CW();
	fileout3(center[2]);
}
void cube::F3_cw1() {  //执行:魔方的前三层顺时针转动90度
	F3_CW();
}
void cube::F3_cw2() {  //执行:魔方的前三层转动180度
	F3_CW(); F3_CW();
}
void cube::F3_cw3() {  //执行:魔方的前三层逆时针转动90度
	F3_CW(); F3_CW(); F3_CW();
}
void cube::B_cw1() {  //执行:魔方的后面顺时针转动90度
	getchar();
	cout << center[2] << "面顺时针旋转90度" << endl;
	B_CW();
	fileout1(center[2]);
}
void cube::B_cw2() {  //执行:魔方的后面转动180度
	getchar();
	cout << center[2] << "面旋转180度" << endl;
	B_CW(); B_CW();
	fileout2(center[2]);
}
void cube::B_cw3() {  //执行:魔方的后面逆时针转动90度
	getchar();
	cout << center[2] << "面逆时针旋转90度" << endl;
	B_CW(); B_CW(); B_CW();
	fileout3(center[2]);
}
void cube::L_cw1() {  //执行:魔方的左面顺时针转动90度
	getchar();
	cout << center[1] << "面顺时针旋转90度" << endl;
	L_CW();
	fileout1(center[1]);
}
void cube::L_cw2() {  //执行:魔方的左面转动180度
	getchar();
	cout << center[1] << "面旋转180度" << endl;
	L_CW(); L_CW();
	fileout2(center[1]);
}
void cube::L_cw3() {  //执行:魔方的左面逆时针转动90度
	getchar();
	cout << center[1] << "面逆时针旋转90度" << endl;
	L_CW(); L_CW(); L_CW();
	fileout3(center[1]);
}
void cube::LR_cw1() {  //执行:魔方的左右面中间层顺时针转动90度
	getchar();
	cout << center[1] << "面顺时针旋转90度; " << endl << center[3] << "面逆时针旋转90度" << endl;
	LR_CW();
	fileout1(center[1]);
	fileout3(center[3]);
}
void cube::LR_cw2() {  //执行:魔方的左右面中间层转动180度
	getchar();
	cout << center[1] << "面旋转180度; " << endl << center[3] << "面旋转180度" << endl;
	LR_CW(); LR_CW();
	fileout2(center[1]);
	fileout2(center[3]);
}
void cube::LR_cw3() {  //执行:魔方的左右面中间层逆时针转动90度
	getchar();
	cout << center[1] << "面逆时针旋转90度; " << endl << center[3] << "面顺时针旋转90度" << endl;
	LR_CW(); LR_CW(); LR_CW();
	fileout3(center[1]);
	fileout1(center[3]);
}
void cube::R_cw1(){  //执行:魔方的右面顺时针转动90度
	getchar();
	cout << center[3] << "面顺时针旋转90度" << endl;
	R_CW();
	fileout1(center[3]);
}
void cube::R_cw2(){  //执行:魔方的右面转动180度
	getchar();
	cout << center[3] << "面旋转180度" << endl;
	R_CW(); R_CW();
	fileout2(center[3]);
}
void cube::R_cw3(){  //执行:魔方的右面逆时针转动90度
	getchar();
	cout << center[3] << "面逆时针旋转90度" << endl;
	R_CW(); R_CW(); R_CW();
	fileout3(center[3]);
}
void cube::R2_cw1(){  //执行:魔方的右两层顺时针转动90度
	getchar();
	cout << center[1] << "面顺时针旋转90度" << endl;
	R2_CW();
	fileout1(center[1]);
}
void cube::R2_cw2(){  //执行:魔方的右两层转动180度
	getchar();
	cout << center[1] << "面旋转180度" << endl;
	R2_CW(); R2_CW();
	fileout2(center[1]);
}
void cube::R2_cw3(){  //执行:魔方的右两层逆时针转动90度
	getchar();
	cout << center[1] << "面逆时针旋转90度" << endl;
	R2_CW(); R2_CW(); R2_CW();
	fileout3(center[1]);
}
void cube::R3_cw1(){  //执行:魔方的右三层顺时针转动90度
	R3_CW();
}
void cube::R3_cw2(){  //执行:魔方的右三层转动180度
	R3_CW(); R3_CW();
}
void cube::R3_cw3(){  //执行:魔方的右三层逆时针转动90度
	R3_CW(); R3_CW(); R3_CW();
}
void cube::bottom_cross(){  //还原魔方的上面十字
	for (int count = 0; count<4; count++){
		int loc; char c1 = center[4], c2 = center[0];
		for (loc = 0; loc<12; loc++){  //查找魔方上面棱的位置
			if (edge[loc][0] == c1 || edge[loc][0] == c2)
			if (edge[loc][1] == c1 || edge[loc][1] == c2)
					break;
		}
		if (loc >= 0 && loc <= 3){//使用R' U F' U'公式还原魔方上面棱
			if (edge[loc][0] == center[0]){
				if (count == 0){
					switch (loc){
					case 0:U_cw2();break;
					case 1:U_cw1();break;
					case 2:break;
					case 3:U_cw3(); break;
					}
					U3_cw1();continue;
				}
				else
					if (loc == 2){
						U3_cw1(); continue;
					}
			}
			switch (loc){
			case 0:B_cw2();break;
			case 1:R_cw2();break;
			case 2:F_cw2();break;
			case 3:L_cw2();break;
			}
			count--; continue;
		}
		else if (loc >= 4 && loc <= 7){
			switch (loc){
			case 4:UD_cw3();F_cw3();UD_cw1();break;
			case 5:UD_cw1();F_cw1();UD_cw3();break;
			case 6:F_cw1();break;
			case 7:F_cw3();break;
			}
			count--; continue;
		}
		else if (loc >= 8 && loc <= 11){
			if (edge[loc][0] == center[0]){
				switch (loc){
				case 8:D_cw2();break;
				case 9:D_cw3();break;
				case 10:break;
				case 11:D_cw1();break;
				}
				F_cw2();
			}
			else{
				switch (loc){
				case 8:D_cw1();break;
				case 9:D_cw2();break;
				case 10:D_cw3();break;
				case 11:; break;
				}
				L_cw3();F_cw1();L_cw1();
			}
		}
		U3_cw1();
	}
}
void cube::bottom_vertex(){  //还原魔方的上面
	for (int count = 0; count<4; count++){
		int loc; char c1 = center[0], c2 = edge[2][1], c3 = edge[1][1];
		for (loc = 0; loc<8; loc++){  //查找魔方上面角的位置
			if (vertex[loc][0] == c1 || vertex[loc][0] == c2 || vertex[loc][0] == c3)
				if (vertex[loc][1] == c1 || vertex[loc][1] == c2 || vertex[loc][1] == c3)
					if (vertex[loc][2] == c1 || vertex[loc][2] == c2 || vertex[loc][2] == c3)
						break;
		}
		if (loc >= 0 && loc <= 3){//不断使用R' D' R D公式还原魔方上面角
			if (loc == 2 && vertex[loc][1] == edge[1][1] && vertex[loc][2] == edge[2][1] && vertex[loc][0] == center[0]){
				U3_cw1();continue;
			}
			else{
				switch (loc){
				case 0:{
					if (vertex[0][0] == center[0] || vertex[0][2] == center[0]){
						B_cw1();D_cw1();B_cw3();
					}
					else {
						L_cw3(); D_cw3(); L_cw1();
					}
				}break;
				case 1:{
					if (vertex[1][0] == center[0] || vertex[1][1] == center[0]){
						B_cw3();D_cw3();B_cw1();
					}
					else{
						R_cw1();D_cw1();R_cw3();
					}
				}break;
				case 2:{
					if (vertex[2][0] == center[0] || vertex[2][1] == center[0]){
						R_cw3();D_cw3();R_cw1();
					}
					else{
						F_cw1(); D_cw1(); F_cw3();
					}
				}break;
				case 3:{
					if (vertex[3][0] == center[0] || vertex[3][2] == center[0]){
						L_cw1();D_cw1();L_cw3();
					}
					else{
						F_cw3();D_cw3();F_cw1();
					}
				}break;
				}
				count--; continue;
			}
		}
		else if (loc >= 4 && loc <= 7){
			if (vertex[loc][0] == center[0]){
				switch (loc){
				case 4:D_cw2();break;
				case 5:D_cw3();break;
				case 6:break;
				case 7:D_cw1();break;
				}
				F_cw1();D_cw3();F_cw3();
				count--; continue;
			}
			else if (vertex[loc][1] == center[0]){
				switch (loc){
				case 4:D_cw2();break;
				case 5:D_cw3();break;
				case 6:break;
				case 7:D_cw1();break;
				}
				R_cw3();D_cw3();R_cw1();
			}
			else{
				switch (loc){
				case 4:D_cw1();break;
				case 5:D_cw2();break;
				case 6:D_cw3();break;
				case 7:break;
				}
				R_cw3();D_cw1();R_cw1();
			}
		}
		U3_cw1();
	}
	R3_cw2();
}
void cube::middlelayer(){  //还原魔方的上下面中间层
	for (int count = 0; count<4; count++){//查找魔方上下面中间层棱的位置
		int loc;
		for (loc = 0; loc<7; loc++){
			if (edge[loc][0] == center[4] || edge[loc][0] == center[3])
				if (edge[loc][1] == center[4] || edge[loc][1] == center[3])
					break;
		}
		if (loc >= 0 && loc <= 3){  //不断使用U R U' R' U' F' U F和U' F' U F U R U' R'公式还原魔方上下面中间层棱
			if (edge[loc][1] == center[4]){
				switch (loc){
				case 0:U_cw3();break;
				case 1:U_cw2();break;
				case 2:U_cw1();break;
				case 3:break;
				}
				R_cw1(); U_cw3(); R_cw3();
				U_cw3(); F_cw3(); U_cw1();
				F_cw1();
			}
			else if (edge[loc][1] = center[3]){
				switch (loc){
				case 0:; break;
				case 1:U_cw3(); break;
				case 2:U_cw2(); break;
				case 3:U_cw1(); break;
				}
				F_cw3(); U_cw1(); F_cw1();
				U_cw1(); R_cw1(); U_cw3();
				R_cw3();
			}
		}
		else if (loc >= 4 && loc <= 7){
			if (edge[loc][0] == center[3] && edge[loc][1] == center[4] && loc == 6){
				U3_cw1(); continue;
			}
			switch (loc){
			case 4:U3_cw2(); break;
			case 5:U3_cw1(); break;
			case 6:break;
			case 7:U3_cw3(); break;
			}
			int loc1;
			for (loc1 = 0; loc1<4; loc1++){
				if (edge[loc1][0] == center[0] || edge[loc1][1] == center[0])
					break;
			}
			loc1 = (loc1 == 4) ? 2 : loc1;
			switch (loc1){
			case 0:U_cw2(); break;
			case 1:U_cw1(); break;
			case 2:break;
			case 3:U_cw3(); break;
			}
			U_cw1(); R_cw1(); U_cw3();
			R_cw3(); U_cw3(); F_cw3();
			U_cw1(); F_cw1();
			switch (loc){
			case 4:U3_cw2(); break;
			case 5:U3_cw3(); break;
			case 6:break;
			case 7:U3_cw1(); break;
			}
			count--; continue;
		}
		U3_cw1();
	}
}
void cube::top_cross(){  //还原魔方的下面十字
	int num = 0;
	for (int i = 0; i<4; i++){  //查找魔方下面十字的位置
		if (edge[i][0] == center[0])
			num++;
	}
	if (num == 0){  //不断使用F R U R' U' F'公式还原魔方下面十字
		F_cw1(); R_cw1(); U_cw1();
		R_cw3(); U_cw3(); F_cw3();
		num = 2;
	}
	if (num == 2){
		int loc[2] = { 0,0 };
		for (int i = 0, j = 0; i<4; i++){
			if (edge[i][0] == center[0]){
				loc[j] = i; j++;
			}
		}
		if (loc[1] - loc[0] == 2){
			switch (loc[0]){
			case 0:U_cw1(); break;
			case 1:break;
			}
			F_cw1(); R_cw1(); U_cw1();
			R_cw3(); U_cw3(); F_cw3();
		}
		else{
			switch (loc[0]){
			case 0: {
				if (loc[1] == 3)
					U_cw2();
				else
					U_cw1();
			}break;
			case 1:break;
			case 2:U_cw3(); break;
			}
			F2_cw1(); R_cw1(); U_cw1();
			R_cw3(); U_cw3(); F2_cw3();
		}
	}
}
void cube::top_face(){  //还原魔方下面角的位置
	int num = 0;
	for (int i = 0; i<4; i++){  //查找魔方下面角的位置
		if (vertex[i][0] == center[0])
			num++;
	}
	if (num == 0){  //不断使用L U L' U L U2 L'和R' U' R U' R' U2 R公式还原魔方下面角的位置
		int loc1 = 0, loc2 = 0;
		loc1 = (vertex[0][1] == center[0]) ? 1 : 2;
		loc2 = (vertex[2][1] == center[0]) ? 1 : 2;
		if (loc1 == loc2){
			switch (loc1){
			case 1:U_cw1(); break;
			case 2:break;
			}
			R_cw1(); U_cw2(); R_cw3();
			U_cw3(); R_cw1(); U_cw1();
			R_cw3(); U_cw3(); R_cw1();
			U_cw3(); R_cw3();
		}
		else{
			switch (loc1){
			case 1: {
				if (vertex[3][1] == center[0])
					U_cw1();
			} break;
			case 2: {
				if (vertex[3][1] == center[0])
					U_cw2();
				else
					U_cw3();
			}break;
			}
			R_cw1(); U_cw2(); R_cw2();
			U_cw3(); R_cw2(); U_cw3();
			R_cw2(); U_cw2(); R_cw1();
		}
	}
	else if (num == 1){
		int loc1, loc2;
		for (int i = 0; i<4; i++){
			if (vertex[i][0] != center[0])
				loc1 = (vertex[i][1] == center[0]) ? 1 : 2;
			else
				loc2 = i;
		}
		if (loc1 == 1){
			switch (loc2){
			case 0:U_cw1(); break;
			case 1:break;
			case 2:U_cw3(); break;
			case 3:U_cw2(); break;
			}
			R_cw1(); U_cw2(); R_cw3();
			U_cw3(); R_cw1(); U_cw3();
			R_cw3();
		}
		else{
			switch (loc2){
			case 0:U_cw2(); break;
			case 1:U_cw1(); break;
			case 2:break;
			case 3:U_cw3(); break;
			}
			R_cw3(); U_cw2(); R_cw1();
			U_cw1(); R_cw3(); U_cw1();
			R_cw1();
		}
	}
	else if (num == 2){
		int loc[2];
		for (int i = 0, j = 0; i<4; i++){
			if (vertex[i][0] != center[0]){
				loc[j] = i;
				j++;
			}
		}
		if (loc[1] - loc[0] == 2){
			int loc1;
			loc1 = (vertex[loc[0]][1] == center[0]) ? loc[1] : loc[0];
			switch (loc1){
			case 0:U_cw2(); break;
			case 1:U_cw1(); break;
			case 2:break;
			case 3:U_cw3(); break;
			}
			F_cw3(); R2_cw1(); U_cw1();
			R_cw3(); U_cw3(); R2_cw3();
			F_cw1(); R_cw1();
		}
		else{
			switch (loc[0]){
			case 0: {
				if (loc[1] == 3)
					U_cw3();
				else
					U_cw2();
			}break;
			case 1:U_cw1(); break;
			case 2:break;
			}
			if (vertex[2][1] == center[0]){
				U_cw1(); R2_cw1(); U_cw1();
				R_cw3(); U_cw3(); R2_cw3();
				F_cw1(); R_cw1(); F_cw3();
			}
			else{
				R_cw1(); U_cw1(); R_cw3();
				U_cw1(); R_cw1(); U_cw2();
				R_cw3(); U_cw1(); R_cw1();
				U_cw2(); R_cw3(); U_cw3();
				R_cw1(); U_cw3(); R_cw3();
			}
		}
	}
}
void cube::top_vertex(){  //还原魔方的下面角
	int loc[2] = { -1,-1 }; int num = 0;
x:	for (int i = 0; i<4; i++){  //查找魔方下面角的位置
		if (vertex[i][2] == vertex[(i + 1) % 4][1]){
			num++;
			if (num == 1){
				loc[0] = i;
				loc[1] = (i + 1) % 4;
			}
		}
	}
	if (num == 0){//使用R U2 R' U' R U2 L' U R' U' L公式还原魔方下面角
		R3_cw3(); R_cw2(); D_cw2();
		R_cw3(); U_cw3(); R_cw1();
		D_cw2(); R_cw3(); U_cw1();
		R_cw3(); R3_cw1();
		goto x;
	}
	else if (num != 4){
		switch (loc[0]){
		case 0:U_cw1(); break;
		case 1:break;
		case 2:U_cw3(); break;
		case 3:U_cw2(); break;
		}
		R3_cw3(); R_cw2(); D_cw2();
		R_cw3(); U_cw3(); R_cw1();
		D_cw2(); R_cw3(); U_cw1();
		R_cw3(); R3_cw1();
	}
	int loc1;
	for (loc1 = 1; loc1<5; loc1++){
		if (vertex[3][2] == center[loc1])
			break;
	}
	switch (loc1){
	case 1:break;
	case 2:U_cw1(); break;
	case 3:U_cw2(); break;
	case 4:U_cw3(); break;
	}
}
void cube::top_edge(){  //还原魔方的下面
	int num = 0;
	for (int i = 0; i<3; i++){  //查找魔方下面棱的位置
		if (edge[i][1] != center[i + 2])
			num++;
	}
	if (edge[3][1] != center[1])//不断使用F2 U L R' F2 R L' U F2公式还原魔方下面棱
		num++;
	if (num == 3){
		int loc;
		for (loc = 0; loc<3; loc++){
			if (edge[loc][1] == center[loc + 2])
				break;
		}
		if (edge[3][1] == center[0])
			loc = 3;
		switch (loc){
		case 0:break;
		case 1:U3_cw3(); break;
		case 2:U3_cw2(); break;
		case 3:U3_cw1(); break;
		}
		if (edge[1][1] == center[4]){
			R_cw2(); U_cw1(); R_cw1();
			U_cw1(); R_cw3(); U_cw3();
			R_cw3(); U_cw3(); R_cw3();
			U_cw1(); R_cw3();
		}
		else{
			R_cw1(); U_cw3(); R_cw1();
			U_cw1(); R_cw1(); U_cw1();
			R_cw1(); U_cw3(); R_cw3();
			U_cw3(); R_cw2();
		}
	}
	else if (num == 4){
		int loc;
		for (loc = 1; loc<5; loc++){
			if (edge[0][1] == center[loc])
				break;
		}
		if (loc == 4){
			LR_cw2(); U_cw1(); LR_cw2();
			U_cw2(); LR_cw2(); U_cw1();
			LR_cw2();
		}
		else{
			if (loc == 3)
				U_cw3();
			U_cw1(); R_cw3(); U_cw3();
			R_cw1(); U_cw3(); R_cw1();
			U_cw1(); R_cw1(); U_cw3();
			R_cw3(); U_cw1(); R_cw1();
			U_cw1(); R_cw2(); U_cw3();
			R_cw3(); U_cw1();
		}
	}
	int loc1;
	for (loc1 = 1; loc1<5; loc1++){
		if (vertex[3][2] == center[loc1])
			break;
	}
	switch (loc1){
	case 1:break;
	case 2:U_cw1(); break;
	case 3:U_cw2(); break;
	case 4:U_cw3(); break;
	}
}
void cube::RUN(){
	file.open("D:\\1.txt");  //打开txt文档
	bottom_cross();  //还原魔方的上面十字
	bottom_vertex();  //还原魔方的上面
	middlelayer();  //还原魔方的上下面中间层
	top_cross();  //还原魔方的下面十字
	top_face();  //还原魔方下面角的位置
	top_vertex();  //还原魔方的下面角
	top_edge();  //还原魔方的下面
	file << "|";  //向txt文档输出还原魔方成功
	file.close();  //关闭txt文档
}
#endif 
void main(){
	int width, height, x, y;  //图片像素
	unsigned short bitCount;  //图片色深
	unsigned char data1, data2, data3;  //RGB数值
	char colour[54];  //魔方色块的颜色
	int offbits;
	int bitPerLine;  //比特每行
	FILE* bmp = fopen("D:\\1.bmp", "rb");  //读写打开一个二进制文件,允许读写数据
	fseek(bmp, 18, SEEK_SET);  //把bmp指针移动到开头第18个字节
	fread(&width, sizeof(int), 1, bmp);  //读取一个数据项的字节数
	fread(&height, sizeof(int), 1, bmp);
	fseek(bmp, 2, SEEK_CUR);  //把bmp指针后移2个字节
	fread(&bitCount, sizeof(bitCount), 1, bmp);
	fseek(bmp, 10, SEEK_SET); //把bmp指针移动到开头第10个字节
	fread(&offbits, sizeof(int), 1, bmp);
	if (bitCount == 24) {
		if (width * 3 % 4 == 0)
			bitPerLine = width * 3;
		else
			bitPerLine = (width * 3 / 4 * 4 + 4);  //计算比特每行
		y = height * 7 / 8;
		for (int i = 0; i < 3; i++) {
			x = width / 6;
			for (int j = 0; j < 3; j++) {
				fseek(bmp, 18 + offbits + bitPerLine*y + 3 * x, SEEK_SET);  //把bmp指针移动到指定像素
				fread(&data1, sizeof(data1), 1, bmp);
				fread(&data2, sizeof(data2), 1, bmp);
				fread(&data3, sizeof(data3), 1, bmp);
				if (data1 < 160 && data2 > 50 && data2 <150 && data3 > 210)
					colour[3 * i + j] = 'O';
				else if (data1 < 160 && data2 > 90 && data3 < 40)
					colour[3 * i + j] = 'G';
				else if (data1 < 140 && data2 > 130 && data3 > 100)
					colour[3 * i + j] = 'Y';
				else if (data1 > 140 && data2 > 120 && data3 > 100)
					colour[3 * i + j] = 'W';
				else if (data1 < 100 && data2 < 30 && data3 > 100)
					colour[3 * i + j] = 'R';
				else if (data1 > 90 && data2 < 170 && data3 < 40)
					colour[3 * i + j] = 'B';
				else
					cout << endl << "B:" << (int)data1 << " G:" << (int)data2 << " R:" << (int)data3 << endl;
				cout << colour[3 * i + j] << " ";
				x = x + width / 3;
			}
			y = y - height * 3 / 8;
		}
		cout << endl;
	}
	else
		cout << "非真彩位图!" << endl;
	fclose(bmp);  //关闭二进制文件
	bmp = fopen("D:\\2.bmp", "rb");//读写打开一个二进制文件,允许读写数据
	fseek(bmp, 18, SEEK_SET);  //把bmp指针移动到开头第18个字节
	fread(&width, sizeof(int), 1, bmp);  //读取一个数据项的字节数
	fread(&height, sizeof(int), 1, bmp);
	fseek(bmp, 2, SEEK_CUR);  //把bmp指针后移2个字节
	fread(&bitCount, sizeof(bitCount), 1, bmp);
	fseek(bmp, 10, SEEK_SET); //把bmp指针移动到开头第10个字节
	fread(&offbits, sizeof(int), 1, bmp);
	if (bitCount == 24) {
		if (width * 3 % 4 == 0)
			bitPerLine = width * 3;
		else
			bitPerLine = (width * 3 / 4 * 4 + 4);  //计算比特每行
		y = height * 7 / 8;
		for (int i = 0; i < 3; i++) {
			x = width / 6;
			for (int j = 0; j < 3; j++) {
				fseek(bmp, 18 + offbits + bitPerLine*y + 3 * x, SEEK_SET);  //把bmp指针移动到指定像素
				fread(&data1, sizeof(data1), 1, bmp);
				fread(&data2, sizeof(data2), 1, bmp);
				fread(&data3, sizeof(data3), 1, bmp);
				if (data1 < 160 && data2 > 50 && data2 <150 && data3 > 210)
					colour[3 * i + j + 9] = 'O';
				else if (data1 < 160 && data2 > 90 && data3 < 40)
					colour[3 * i + j + 9] = 'G';
				else if (data1 < 140 && data2 > 130 && data3 > 100)
					colour[3 * i + j + 9] = 'Y';
				else if (data1 > 140 && data2 > 120 && data3 > 100)
					colour[3 * i + j + 9] = 'W';
				else if (data1 < 100 && data2 < 30 && data3 > 100)
					colour[3 * i + j + 9] = 'R';
				else if (data1 > 90 && data2 < 170 && data3 < 40)
					colour[3 * i + j + 9] = 'B';
				else
					cout << endl << "B:" << (int)data1 << " G:" << (int)data2 << " R:" << (int)data3 << endl;
				cout << colour[3 * i + j + 9] << " ";
				x = x + width / 3;
			}
			y = y - height * 3 / 8;
		}
		cout << endl;
	}
	else
		cout << "非真彩位图!" << endl;
	fclose(bmp);
	bmp = fopen("D:\\3.bmp", "rb");//读写打开一个二进制文件,允许读写数据
	fseek(bmp, 18, SEEK_SET);  //把bmp指针移动到开头第18个字节
	fread(&width, sizeof(int), 1, bmp);  //读取一个数据项的字节数
	fread(&height, sizeof(int), 1, bmp);
	fseek(bmp, 2, SEEK_CUR);  //把bmp指针后移2个字节
	fread(&bitCount, sizeof(bitCount), 1, bmp);
	fseek(bmp, 10, SEEK_SET); //把bmp指针移动到开头第10个字节
	fread(&offbits, sizeof(int), 1, bmp);
	if (bitCount == 24) {
		if (width * 3 % 4 == 0)
			bitPerLine = width * 3;
		else
			bitPerLine = (width * 3 / 4 * 4 + 4);  //计算比特每行
		y = height * 7 / 8;
		for (int i = 0; i < 3; i++) {
			x = width / 6;
			for (int j = 0; j < 3; j++) {
				fseek(bmp, 18 + offbits + bitPerLine*y + 3 * x, SEEK_SET);  //把bmp指针移动到指定像素
				fread(&data1, sizeof(data1), 1, bmp);
				fread(&data2, sizeof(data2), 1, bmp);
				fread(&data3, sizeof(data3), 1, bmp);
				if (data1 < 160 && data2 > 50 && data2 <150 && data3 > 210)
					colour[3 * i + j + 18] = 'O';
				else if (data1 < 160 && data2 > 90 && data3 < 40)
					colour[3 * i + j + 18] = 'G';
				else if (data1 < 140 && data2 > 130 && data3 > 100)
					colour[3 * i + j + 18] = 'Y';
				else if (data1 > 140 && data2 > 120 && data3 > 100)
					colour[3 * i + j + 18] = 'W';
				else if (data1 < 100 && data2 < 30 && data3 > 100)
					colour[3 * i + j + 18] = 'R';
				else if (data1 > 90 && data2 < 170 && data3 < 40)
					colour[3 * i + j + 18] = 'B';
				else
					cout << endl << "B:" << (int)data1 << " G:" << (int)data2 << " R:" << (int)data3 << endl;
				cout << colour[3 * i + j + 18] << " ";
				x = x + width / 3;
			}
			y = y - height * 3 / 8;
		}
		cout << endl;
	}
	else
		cout << "非真彩位图!" << endl;
	fclose(bmp);
	bmp = fopen("D:\\4.bmp", "rb");//读写打开一个二进制文件,允许读写数据
	fseek(bmp, 18, SEEK_SET);  //把bmp指针移动到开头第18个字节
	fread(&width, sizeof(int), 1, bmp);  //读取一个数据项的字节数
	fread(&height, sizeof(int), 1, bmp);
	fseek(bmp, 2, SEEK_CUR);  //把bmp指针后移2个字节
	fread(&bitCount, sizeof(bitCount), 1, bmp);
	fseek(bmp, 10, SEEK_SET); //把bmp指针移动到开头第10个字节
	fread(&offbits, sizeof(int), 1, bmp);
	if (bitCount == 24) {
		if (width * 3 % 4 == 0)
			bitPerLine = width * 3;
		else
			bitPerLine = (width * 3 / 4 * 4 + 4);  //计算比特每行
		y = height * 7 / 8;
		for (int i = 0; i < 3; i++) {
			x = width / 6;
			for (int j = 0; j < 3; j++) {
				fseek(bmp, 18 + offbits + bitPerLine*y + 3 * x, SEEK_SET);  //把bmp指针移动到指定像素
				fread(&data1, sizeof(data1), 1, bmp);
				fread(&data2, sizeof(data2), 1, bmp);
				fread(&data3, sizeof(data3), 1, bmp);
				if (data1 < 160 && data2 > 50 && data2 <150 && data3 > 210)
					colour[3 * i + j + 27] = 'O';
				else if (data1 < 160 && data2 > 90 && data3 < 40)
					colour[3 * i + j + 27] = 'G';
				else if (data1 < 140 && data2 > 130 && data3 > 100)
					colour[3 * i + j + 27] = 'Y';
				else if (data1 > 140 && data2 > 120 && data3 > 100)
					colour[3 * i + j + 27] = 'W';
				else if (data1 < 100 && data2 < 30 && data3 > 100)
					colour[3 * i + j + 27] = 'R';
				else if (data1 > 90 && data2 < 170 && data3 < 40)
					colour[3 * i + j + 27] = 'B';
				else
					cout << endl << "B:" << (int)data1 << " G:" << (int)data2 << " R:" << (int)data3 << endl;
				cout << colour[3 * i + j + 27] << " ";
				x = x + width / 3;
			}
			y = y - height * 3 / 8;
		}
		cout << endl;
	}
	else
		cout << "非真彩位图!" << endl;
	fclose(bmp);
	bmp = fopen("D:\\5.bmp", "rb");//读写打开一个二进制文件,允许读写数据
	fseek(bmp, 18, SEEK_SET);  //把bmp指针移动到开头第18个字节
	fread(&width, sizeof(int), 1, bmp);  //读取一个数据项的字节数
	fread(&height, sizeof(int), 1, bmp);
	fseek(bmp, 2, SEEK_CUR);  //把bmp指针后移2个字节
	fread(&bitCount, sizeof(bitCount), 1, bmp);
	fseek(bmp, 10, SEEK_SET); //把bmp指针移动到开头第10个字节
	fread(&offbits, sizeof(int), 1, bmp);
	if (bitCount == 24) {
		if (width * 3 % 4 == 0)
			bitPerLine = width * 3;
		else
			bitPerLine = (width * 3 / 4 * 4 + 4);  //计算比特每行
		y = height * 7 / 8;
		for (int i = 0; i < 3; i++) {
			x = width / 6;
			for (int j = 0; j < 3; j++) {
				fseek(bmp, 18 + offbits + bitPerLine*y + 3 * x, SEEK_SET);  //把bmp指针移动到指定像素
				fread(&data1, sizeof(data1), 1, bmp);
				fread(&data2, sizeof(data2), 1, bmp);
				fread(&data3, sizeof(data3), 1, bmp);
				if (data1 < 160 && data2 > 50 && data2 <150 && data3 > 210)
					colour[3 * i + j + 36] = 'O';
				else if (data1 < 160 && data2 > 90 && data3 < 40)
					colour[3 * i + j + 36] = 'G';
				else if (data1 < 140 && data2 > 130 && data3 > 100)
					colour[3 * i + j + 36] = 'Y';
				else if (data1 > 140 && data2 > 120 && data3 > 100)
					colour[3 * i + j + 36] = 'W';
				else if (data1 < 100 && data2 < 30 && data3 > 100)
					colour[3 * i + j + 36] = 'R';
				else if (data1 > 90 && data2 < 170 && data3 < 40)
					colour[3 * i + j + 36] = 'B';
				else
					cout << endl << "B:" << (int)data1 << " G:" << (int)data2 << " R:" << (int)data3 << endl;
				cout << colour[3 * i + j + 36] << " ";
				x = x + width / 3;
			}
			y = y - height * 3 / 8;
		}
		cout << endl;
	}
	else
		cout << "非真彩位图!" << endl;
	fclose(bmp);
	bmp = fopen("D:\\6.bmp", "rb");//读写打开一个二进制文件,允许读写数据
	fseek(bmp, 18, SEEK_SET);  //把bmp指针移动到开头第18个字节
	fread(&width, sizeof(int), 1, bmp);  //读取一个数据项的字节数
	fread(&height, sizeof(int), 1, bmp);
	fseek(bmp, 2, SEEK_CUR);  //把bmp指针后移2个字节
	fread(&bitCount, sizeof(bitCount), 1, bmp);
	fseek(bmp, 10, SEEK_SET); //把bmp指针移动到开头第10个字节
	fread(&offbits, sizeof(int), 1, bmp);
	if (bitCount == 24) {
		if (width * 3 % 4 == 0)
			bitPerLine = width * 3;
		else
			bitPerLine = (width * 3 / 4 * 4 + 4);  //计算比特每行
		y = height * 7 / 8;
		for (int i = 0; i < 3; i++) {
			x = width / 6;
			for (int j = 0; j < 3; j++) {
				fseek(bmp, 18 + offbits + bitPerLine*y + 3 * x, SEEK_SET);  //把bmp指针移动到指定像素
				fread(&data1, sizeof(data1), 1, bmp);
				fread(&data2, sizeof(data2), 1, bmp);
				fread(&data3, sizeof(data3), 1, bmp);
				if (data1 < 160 && data2 > 50 && data2 <150 && data3 > 210)
					colour[3 * i + j + 45] = 'O';
				else if (data1 < 160 && data2 > 90 && data3 < 40)
					colour[3 * i + j + 45] = 'G';
				else if (data1 < 140 && data2 > 130 && data3 > 100)
					colour[3 * i + j + 45] = 'Y';
				else if (data1 > 140 && data2 > 120 && data3 > 100)
					colour[3 * i + j + 45] = 'W';
				else if (data1 < 100 && data2 < 30 && data3 > 100)
					colour[3 * i + j + 45] = 'R';
				else if (data1 > 90 && data2 < 170 && data3 < 40)
					colour[3 * i + j + 45] = 'B';
				else
					cout << endl << "B:" << (int)data1 << " G:" << (int)data2 << " R:" << (int)data3 << endl;
				cout << colour[3 * i + j + 45] << " ";
				x = x + width / 3;
			}
			y = y - height * 3 / 8;
		}
		cout << endl;
	}
	else
		cout << "非真彩位图!" << endl;
	fclose(bmp);
	cube c;
	c.input_cube(colour);  //输入魔方颜色
	c.RUN();  //还原魔方
	getchar();
	cout << "魔方还原成功" << endl;
	while (getchar() == 10);
}
  2.ESP8266模块相关代码
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2a, 3);  // 定义ESP8266的接口RX,TX
void setup(){
  Serial.begin(9600);  //初始化串口通信,并将波特率设置为9600
  while (!Serial);  //等待串口通信
  Serial.println("O");
  mySerial.begin(4800);  //初始化网络通信,并将波特率设置为4800
  mySerial.println("K");
}
void loop(){
  if (mySerial.available()){  //将ESP8266收到的信号发送到Arduino UNO开发板
    Serial.write(mySerial.read());
  }
   if (Serial.available()) {  //将Arduino UNO开发板收到的信号发送到ESP8266
    mySerial.write(Serial.read()); 
  }
}
/*1,设置模块的工作模式为AP模式AT+CWMODE=2
2,复位AT+RST
3,发送AT+CIPMUX=0启动单连接
4,AT+CIPSTART="TCP","192.168.4.2",8080
以TCP协议连接电脑的IP地址及端口号*/
  3.输出模块相关代码
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);  // 定义ESP8266的接口RX,TX
int LDW=4,LDR=5,LDY=6,LDO=7,LDG=8,LDB=9;  //定义LED代表魔方的六个面
int cw1=10,cw2=11,cw3=12;  //定义LED代表魔方的转动方向
int button=13;  //定义按钮
char data;
void setup(){
  Serial.begin(9600);  //初始化串口通信,并将波特率设置为9600
  while (!Serial);  //等待串口通信
  mySerial.begin(4800);  //初始化网络通信,并将波特率设置为4800
  pinMode(LDW,OUTPUT);digitalWrite(LDW,LOW);  //初始化LED
  pinMode(LDR,OUTPUT);digitalWrite(LDR,LOW);
  pinMode(LDY,OUTPUT);digitalWrite(LDY,LOW);
  pinMode(LDO,OUTPUT);digitalWrite(LDO,LOW);
  pinMode(LDG,OUTPUT);digitalWrite(LDG,LOW);
  pinMode(LDB,OUTPUT);digitalWrite(LDB,LOW);
  pinMode(cw1,OUTPUT);digitalWrite(cw1,LOW);
  pinMode(cw2,OUTPUT);digitalWrite(cw2,LOW);
  pinMode(cw3,OUTPUT);digitalWrite(cw3,LOW);
  pinMode(button,INPUT);  //初始化开关
}
void loop(){
  if (mySerial.available()){  //ESP8266收到信号
    data=mySerial.read();  //读取信号
    Serial.write(data);  //将ESP8266收到的信号发送到Arduino UNO开发板
    if(data=='W'){  //当开关为低电平时输出魔方的白色面顺时针转动90度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDW,HIGH);
      digitalWrite(cw1,HIGH);
    }
    else if(data=='x'){  //当开关为低电平时输出魔方的白色面转动180度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDW,HIGH);
      digitalWrite(cw2,HIGH);
    }
    else if(data=='w'){   //当开关为低电平时输出魔方的白色面逆时针转动90度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDW,HIGH);
      digitalWrite(cw3,HIGH);
    }
    else if(data=='R'){  //当开关为低电平时输出魔方的红色面顺时针转动90度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDR,HIGH);
      digitalWrite(cw1,HIGH);
    }
    else if(data=='s'){  //当开关为低电平时输出魔方的红色面转动180度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDR,HIGH);
      digitalWrite(cw2,HIGH);
    }
    else if(data=='r'){  //当开关为低电平时输出魔方的红色面逆时针转动90度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDR,HIGH);
      digitalWrite(cw3,HIGH);
    }
    else if(data=='Y'){  //当开关为低电平时输出魔方的黄色面顺时针转动90度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDY,HIGH);
      digitalWrite(cw1,HIGH);
    }
    else if(data=='z'){  //当开关为低电平时输出魔方的黄色面转动180度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDY,HIGH);
      digitalWrite(cw2,HIGH);
    }
    else if(data=='y'){  //当开关为低电平时输出魔方的黄色面逆时针转动90度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDY,HIGH);
      digitalWrite(cw3,HIGH);
    }
    else if(data=='O'){   //当开关为低电平时输出魔方的橙色面顺时针转动90度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDO,HIGH);
      digitalWrite(cw1,HIGH);
    }
    else if(data=='p'){  //当开关为低电平时输出魔方的橙色面转动180度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDO,HIGH);
      digitalWrite(cw2,HIGH);
    }
    else if(data=='o'){  //当开关为低电平时输出魔方的橙色面逆时针转动90度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDO,HIGH);
      digitalWrite(cw3,HIGH);
    }
    else if(data=='G'){  //当开关为低电平时输出魔方的绿色面顺时针转动90度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDG,HIGH);
      digitalWrite(cw1,HIGH);
    }
    else if(data=='h'){  //当开关为低电平时输出魔方的绿色面转动180度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDG,HIGH);
      digitalWrite(cw2,HIGH);
    }
    else if(data=='g'){  //当开关为低电平时输出魔方的绿色面逆时针转动90度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDG,HIGH);
      digitalWrite(cw3,HIGH);
    }
    else if(data=='B'){  //当开关为低电平时输出魔方的蓝色面顺时针转动90度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDB,HIGH);
      digitalWrite(cw1,HIGH);
    }
    else if(data=='c'){  //当开关为低电平时输出魔方的蓝色面转动180度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDB,HIGH);
      digitalWrite(cw2,HIGH);
    }
    else if(data=='b'){  //当开关为低电平时输出魔方的蓝色面逆时针转动90度
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(600);
      digitalWrite(LDB,HIGH);
      digitalWrite(cw3,HIGH);
    }
    else if(data=='|'){//当开关为低电平时输出全部LED灯常亮,还原魔方完成
      while(digitalRead(button)==HIGH);
      digitalWrite(LDW,LOW);
      digitalWrite(LDR,LOW);
      digitalWrite(LDY,LOW);
      digitalWrite(LDO,LOW);
      digitalWrite(LDG,LOW);
      digitalWrite(LDB,LOW);
      digitalWrite(cw1,LOW);
      digitalWrite(cw2,LOW);
      digitalWrite(cw3,LOW);
      delay(800);
      digitalWrite(LDW,HIGH);
      digitalWrite(LDR,HIGH);
      digitalWrite(LDY,HIGH);
      digitalWrite(LDO,HIGH);
      digitalWrite(LDG,HIGH);
      digitalWrite(LDB,HIGH);
      digitalWrite(cw1,HIGH);
      digitalWrite(cw2,HIGH);
      digitalWrite(cw3,HIGH);
    }
  }
  if (Serial.available()) {{  //将Arduino UNO开发板收到的信号发送到ESP8266
    mySerial.write(Serial.read()); 
  }
}

发表评论

登录后才能评论
联系我们

联系我们

微信:13823392571

在线咨询:点击这里给我发消息

工作时间:周一至周五,9:30-18:30,节假日休息

微信客服
微信客服
分享本页
返回顶部