最后一步实际上有两个步骤,第一步是先还原顶部四个棱角,然后还原顶部四个棱块,然后魔方得以全部还原。
万事开头难,越到后面越简单。这两个步骤分别用两个公式即可完成,而且状态也特别简单,算法如下:
def 顶部4棱角到位():c1=(cube_list[3][5][0] == cube_list[4][4][0]) and (cube_list[3][6][0] == cube_list[4][7][0])c2 = (cube_list[3][3][0] == cube_list[4][4][0]) and (cube_list[3][2][0] == cube_list[4][1][0])c3 = (cube_list[3][0][0] == cube_list[4][1][0]) and (cube_list[3][11][0] == cube_list[4][10][0])c4 = (cube_list[3][8][0] == cube_list[4][7][0]) and (cube_list[3][9][0] == cube_list[4][10][0])if c1 and c2 and c3 and c4:return Trueelse:return Falsedef 当前棱角到位():if (cube_list[3][5][0] == cube_list[4][4][0]) and (cube_list[3][6][0] == cube_list[4][7][0]):return Trueelse:return Falsedef 还原顶部4棱角():while not 顶部4棱角到位():if not 当前棱角到位():formula_cmd(["R", "U","U","Rp","Up", "R", "U","U","Lp","U", "Rp", "Up", "L"])left()
def 顶部四棱块到位():passe1=cube_list[3][4][0]==cube_list[4][4][0]e2 = cube_list[3][1][0] == cube_list[4][1][0]e3 = cube_list[4][7][0] == cube_list[3][7][0]e4 = cube_list[4][10][0] == cube_list[3][10][0]if e1 and e2 and e3 and e4:return Trueelse:return Falsedef 最后一步():while not 顶部四棱块到位():if cube_list[3][4][0]!=cube_list[4][4][0] and cube_list[3][7][0]!=cube_list[4][7][0] and cube_list[3][1][0]!=cube_list[4][1][0]:formula_cmd(['F','F','U','L','Rp','F','F','R','Lp','U','F','F'])else:left()
运行结果如下:
先前状态,还原到顶部出现绿面:
还原顶部四个棱角
还原顶部四棱块
--end
如果觉得《Python练手项目:计算机自动还原魔方(5)最后一步》对你有帮助,请点赞、收藏,并留下你的观点哦!