这段代码中的报错是因为 UPCLOCKWISE 没有被定义。UPCLOCKWISE 是枚举类型中的成员,需要在代码中定义它们,或者使用枚举类型的全名来表示它们。例如,可以改为 cube.rotate(Color::UP, Rotation::CLOCKWISE)

#include <iostream>
#include <vector>
#include <string>
using namespace std;

// 定义魔方的颜色
enum Color { RED, BLUE, GREEN, WHITE, YELLOW, ORANGE };

// 定义魔方的转动方式
enum Rotation { CLOCKWISE, COUNTERCLOCKWISE, DOUBLE };

// 定义魔方的状态
struct CubeState {
	Color up[3][3];      // 上层
	Color down[3][3];    // 下层
	Color left[3][3];    // 左层
	Color right[3][3];   // 右层
	Color front[3][3];   // 前层
	Color back[3][3];    // 后层
};

// 定义魔方操作
class Cube {
	public:
		void rotate(Color face, Rotation rotation) {
			// 定义一个临时数组,用于存储需要旋转的面的颜色
			Color temp[3][3];

			// 根据旋转方式调整魔方状态
			switch (face) {
				case RED:
					// 根据旋转方式调整 red 面的状态
					
					break;
				case BLUE:
					// 根据旋转方式调整 blue 面的状态
					
					break;

				case GREEN:
					// 根据旋转方式调整 green 面的状态
					
					break;
				//补全之后的代码,这段代码是使用Thistlethwaite实现魔方的复原
				case WHITE:
					// 根据旋转方式调整 white 面的状态
					
					
					break;
				case YELLOW:
					// 根据旋转方式调整 yellow 面的状态
					
					break;
				case ORANGE:
					// 根据旋转方式调整 orange 面的状态
					
						break;
					
					break; // 退出switch语句



				default:
					cout << '无效的面颜色' << endl;
					break;
			}
		}

		CubeState getState() const {
			// 创建一个新的 CubeState 对象,用于存储当前魔方状态
			CubeState state;

			// 将当前魔方状态复制到新的 CubeState 对象中
			for (int i = 0; i < 3; i++) {
				for (int j = 0; j < 3; j++) {
					state.up[i][j] = up[i][j];
					state.down[i][j] = down[i][j];
					state.left[i][j] = left[i][j];
					state.right[i][j] = right[i][j];
					state.front[i][j] = front[i][j];
					state.back[i][j] = back[i][j];
				}
			}

			// 返回新的 CubeState 对象
			return state;
		}

	private:
		Color up[3][3] = {{YELLOW, YELLOW, YELLOW}, {YELLOW, YELLOW, YELLOW}, {YELLOW, YELLOW, YELLOW}};      // 上层
		Color down[3][3] = {{WHITE, WHITE, WHITE}, {WHITE, WHITE, WHITE}, {WHITE, WHITE, WHITE}};    // 下层
		Color left[3][3] = {{ORANGE, ORANGE, ORANGE}, {ORANGE, ORANGE, ORANGE}, {ORANGE, ORANGE, ORANGE}};    // 左层
		Color right[3][3] = {{RED, RED, RED}, {RED, RED, RED}, {RED, RED, RED}};   // 右层
		Color front[3][3] = {{GREEN, GREEN, GREEN}, {GREEN, GREEN, GREEN}, {GREEN, GREEN, GREEN}};   // 前层
		Color back[3][3] = {{BLUE, BLUE, BLUE}, {BLUE, BLUE, BLUE}, {BLUE, BLUE, BLUE}};    // 后层
};
// Thistlethwaite 算法类
class ThistlethwaiteSolver {
	public:
	void solve(CubeState state) {
		// 使用 Thistlethwaite 算法解决魔方
		// TODO: 实现 Thistlethwaite 算法的解决逻辑
	
		// 第一步:完成顶层十字
		// 找到顶层中心颜色
		Color upCenter = state.up[1][1];
		// 找到与顶层中心颜色相同的边块
		vector<pair<Color, Color>> edges = {{state.front[0][1], state.right[1][0]}, {state.right[0][1], state.back[0][1]}, {state.back[0][1], state.left[1][0]}, {state.left[0][1], state.front[0][1]}};
		pair<Color, Color> targetEdge;
		for (auto edge : edges) {
			if (edge.first == upCenter || edge.second == upCenter) {
				targetEdge = edge;
				break;
			}
		}
		// 如果目标边块已经在顶层,则跳过这一步
		if (targetEdge.first != upCenter && targetEdge.second != upCenter) {
			// 将目标边块移到顶层
			while (targetEdge.first != upCenter) {
				cube.rotate(Color::UP, Rotation::CLOCKWISE);
				targetEdge = make_pair(cube.getState().front[0][1], cube.getState().right[1][0]);
			}
			// 调整边块位置
			if (targetEdge.second == cube.getState().right[1][0]) {
				cube.rotate(Color::UP, Rotation::CLOCKWISE);
				cube.rotate(Color::RIGHT, Rotation::CLOCKWISE);
				cube.rotate(Color::UP, Rotation::COUNTERCLOCKWISE);
				cube.rotate(Color::RIGHT, Rotation::COUNTERCLOCKWISE);
			} else {
				cube.rotate(Color::UP, Rotation::COUNTERCLOCKWISE);
				cube.rotate(Color::LEFT, Rotation::COUNTERCLOCKWISE);
				cube.rotate(Color::UP, Rotation::CLOCKWISE);
				cube.rotate(Color::LEFT, Rotation::CLOCKWISE);
			}
			// 调整顶层边块朝向
			targetEdge = make_pair(cube.getState().front[0][1], cube.getState().right[1][0]);
			if (targetEdge.first != upCenter) {
				cube.rotate(Color::UP, Rotation::CLOCKWISE);
				cube.rotate(Color::RIGHT, Rotation::CLOCKWISE);
				cube.rotate(Color::UP, Rotation::COUNTERCLOCKWISE);
				cube.rotate(Color::RIGHT, Rotation::COUNTERCLOCKWISE);
				cube.rotate(Color::UP, Rotation::COUNTERCLOCKWISE);
				cube.rotate(Color::FRONT, Rotation::COUNTERCLOCKWISE);
				cube.rotate(Color::UP, Rotation::CLOCKWISE);
				cube.rotate(Color::FRONT, Rotation::CLOCKWISE);
			}
		}
		// 第二步:完成顶层角块
		// TODO: 实现完成顶层角块的逻辑
	
		// 第三步:完成中层
		// TODO: 实现完成中层的逻辑
	
		// 第四步:完成底层十字
		// TODO: 实现完成底层十字的逻辑
	
		// 第五步:完成底层角块
		// TODO: 实现完成底层角块的逻辑
	
		// 第六步:完成魔方
		// TODO: 实现完成魔方的逻辑
	}
		
		void solveTopCross(Cube& cube) {
			// 定义一个数组,存储需要旋转的面和旋转方式
			pair<Color, Rotation> moves[] = {
				{GREEN, CLOCKWISE},
				{GREEN, DOUBLE},
				{GREEN, COUNTERCLOCKWISE},
				{BLUE, CLOCKWISE},
				{BLUE, DOUBLE},
				{BLUE, COUNTERCLOCKWISE},
				{ORANGE, CLOCKWISE},
				{ORANGE, DOUBLE},
				{ORANGE, COUNTERCLOCKWISE},
				{RED, CLOCKWISE},
				{RED, DOUBLE},
				{RED, COUNTERCLOCKWISE}
			};
		
			// 遍历所有可能的旋转方式,直到顶层十字完成
			for (int i = 0; i < 12; i++) {
				// 获取当前魔方状态
				CubeState state = cube.getState();
		
				// 获取需要旋转的面和旋转方式
				Color face = moves[i].first;
				Rotation rotation = moves[i].second;
		
				// 执行旋转操作
				cube.rotate(face, rotation);
		
				// 判断是否完成顶层十字
				if (state.up[0][1] == YELLOW && state.up[1][0] == YELLOW && state.up[1][2] == YELLOW && state.up[2][1] == YELLOW) {
					break;
				}
			}
		}
};

int main() {
	// 创建魔方对象
	Cube cube;

	// 获取初始魔方状态
	CubeState initialState = cube.getState();

	// 创建 ThistlethwaiteSolver 对象
	ThistlethwaiteSolver solver;

	// 使用 Thistlethwaite 算法解决魔方
	solver.solve(initialState);

	return 0;
}
C++ 魔方代码报错:cube.rotate(UP, CLOCKWISE) 错误原因及解决方案

原文地址: https://www.cveoy.top/t/topic/nlXL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录