首先简述一下三阶魔方,魔方可以通过打乱公式进行状态的转变,
(F,B,U,D,L,R)六个字母分别表示:前面顺时针旋转90°、背面顺时针旋转90°、上面顺时针旋转90°、下面顺时针旋转90°、左面顺时针旋转90°、右面顺时针旋转90°。
(F',B',U',D',L',R')六个字母分别表示:前面逆时针旋转90°、背面逆时针旋转90°、上面逆时针旋转90°、下面逆时针旋转90°、左面逆时针旋转90°、右面逆时针旋转90°。
(F2,B2,U2,D2,L2,R2)六个字母分别表示:前面顺时针旋转180°、背面顺时针旋转180°、上面顺时针旋转180°、下面顺时针旋转180°、左面顺时针旋转180°、右面顺时针旋转180°。
在做这项功能之初我使用Python进行编码测试
首先定义了六个列表存储魔方状态,前9位我称之为基态、后九位我称之为缓冲位。由于魔方变化多样在每一次旋转变化时影响的是其本身和与之相邻的四个面。对魔方每个面上的每个块进行编码,从上至下、从左至右编码0-8(基态)、9-17(缓冲位)。
bai = ["白","白","白","白","白","白","白","白","白","白","白","白","白","白","白","白","白","白"]
huang = ["黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄"]
lan = ["蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝"]
lv = ["绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿"]
hong = ["红","红","红","红","红","红","红","红","红","红","红","红","红","红","红","红","红","红"]
cheng = ["橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙"]
如下代码为生成打乱公式,内容较为简单就不做过多赘述。
dlstr = ""
dllist = []
dlzb = ["L","R","U","D","F","B","L'","R'","U'","D'","F'","B'","L2","R2","U2","D2","F2","B2"]
for i in range(21):
j = random.randint(0,17)
dlstr = dlstr + dlzb[j] + " "
dllist.append(dlzb[j])
print(dlstr)
当白面朝上,绿面朝自己时我们以R打乱为例当R转动时红色面会顺时针转动90°所以红色面除中心块外都会变换位置白色面的2、5、8位置的块会被绿色面2、5、8取代此时我采用缓冲位实现替代,其他四个方向均为此种方式替代。最后应将缓冲位归位即让缓冲位与基态相同再进行下一次旋转。
def turnR():
bai[2] = lv[11]
bai[5] = lv[14]
bai[8] = lv[17]
lan[0] = bai[17]
lan[3] = bai[14]
lan[6] = bai[11]
huang[2] = lan[15]
huang[5] = lan[12]
huang[8] = lan[9]
lv[2] = huang[11]
lv[5] = huang[14]
lv[8] = huang[17]
hong[0] = hong[15]
hong[1] = hong[12]
hong[2] = hong[9]
hong[3] = hong[16]
hong[5] = hong[10]
hong[6] = hong[17]
hong[7] = hong[14]
hong[8] = hong[11]
hcgw()
def hcgw():
for i in range(9,18):
bai[i] = bai[i - 9]
lv[i] = lv[i - 9]
huang[i] = huang[i - 9]
lan[i] = lan[i - 9]
cheng[i] = cheng[i - 9]
hong[i] = hong[i - 9]
至此打乱三阶魔方的逻辑思路已经清晰完整代码如下:
import random
def turnL():
lv[0] = bai[9]
lv[3] = bai[12]
lv[6] = bai[15]
bai[0] = lan[17]
bai[3] = lan[14]
bai[6] = lan[11]
lan[2] = huang[15]
lan[5] = huang[12]
lan[8] = huang[9]
huang[0] = lv[9]
huang[3] = lv[12]
huang[6] = lv[15]
cheng[0] = cheng[15]
cheng[1] = cheng[12]
cheng[2] = cheng[9]
cheng[3] = cheng[16]
cheng[5] = cheng[10]
cheng[6] = cheng[17]
cheng[7] = cheng[14]
cheng[8] = cheng[11]
hcgw()
def turnR():
bai[2] = lv[11]
bai[5] = lv[14]
bai[8] = lv[17]
lan[0] = bai[17]
lan[3] = bai[14]
lan[6] = bai[11]
huang[2] = lan[15]
huang[5] = lan[12]
huang[8] = lan[9]
lv[2] = huang[11]
lv[5] = huang[14]
lv[8] = huang[17]
hong[0] = hong[15]
hong[1] = hong[12]
hong[2] = hong[9]
hong[3] = hong[16]
hong[5] = hong[10]
hong[6] = hong[17]
hong[7] = hong[14]
hong[8] = hong[11]
hcgw()
def turnU():
lv[0] = hong[9]
lv[1] = hong[10]
lv[2] = hong[11]
cheng[0] = lv[9]
cheng[1] = lv[10]
cheng[2] = lv[11]
lan[0] = cheng[9]
lan[1] = cheng[10]
lan[2] = cheng[11]
hong[0] = lan[9]
hong[1] = lan[10]
hong[2] = lan[11]
bai[0] = bai[15]
bai[1] = bai[12]
bai[2] = bai[9]
bai[3] = bai[16]
bai[5] = bai[10]
bai[6] = bai[17]
bai[7] = bai[14]
bai[8] = bai[11]
hcgw()
def turnD():
lv[6] = cheng[15]
lv[7] = cheng[16]
lv[8] = cheng[17]
cheng[6] = lan[15]
cheng[7] = lan[16]
cheng[8] = lan[17]
lan[6] = hong[15]
lan[7] = hong[16]
lan[8] = hong[17]
hong[6] = lv[15]
hong[7] = lv[16]
hong[8] = lv[17]
huang[0] = huang[15]
huang[1] = huang[12]
huang[2] = huang[9]
huang[3] = huang[16]
huang[5] = huang[10]
huang[6] = huang[17]
huang[7] = huang[14]
huang[8] = huang[11]
hcgw()
def turnF():
bai[6] = cheng[17]
bai[7] = cheng[14]
bai[8] = cheng[11]
cheng[2] = huang[9]
cheng[5] = huang[10]
cheng[8] = huang[11]
huang[0] = hong[15]
huang[1] = hong[12]
huang[2] = hong[9]
hong[0] = bai[15]
hong[3] = bai[16]
hong[6] = bai[17]
lv[0] = lv[15]
lv[1] = lv[12]
lv[2] = lv[9]
lv[3] = lv[16]
lv[5] = lv[10]
lv[6] = lv[17]
lv[7] = lv[14]
lv[8] = lv[11]
hcgw()
def turnB():
bai[0] = hong[11]
bai[1] = hong[14]
bai[2] = hong[17]
hong[2] = huang[17]
hong[5] = huang[16]
hong[8] = huang[15]
huang[6] = cheng[9]
huang[7] = cheng[12]
huang[8] = cheng[15]
cheng[0] = bai[11]
cheng[3] = bai[10]
cheng[6] = bai[9]
lan[0] = lan[15]
lan[1] = lan[12]
lan[2] = lan[9]
lan[3] = lan[16]
lan[5] = lan[10]
lan[6] = lan[17]
lan[7] = lan[14]
lan[8] = lan[11]
hcgw()
def hcgw():
for i in range(9,18):
bai[i] = bai[i - 9]
lv[i] = lv[i - 9]
huang[i] = huang[i - 9]
lan[i] = lan[i - 9]
cheng[i] = cheng[i - 9]
hong[i] = hong[i - 9]
# 后九位缓冲
bai = ["白","白","白","白","白","白","白","白","白","白","白","白","白","白","白","白","白","白"]
huang = ["黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄"]
lan = ["蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝"]
lv = ["绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿"]
hong = ["红","红","红","红","红","红","红","红","红","红","红","红","红","红","红","红","红","红"]
cheng = ["橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙"]
dlstr = ""
dllist = []
dlzb = ["L","R","U","D","F","B","L'","R'","U'","D'","F'","B'","L2","R2","U2","D2","F2","B2"]
for i in range(21):
j = random.randint(0,17)
dlstr = dlstr + dlzb[j] + " "
dllist.append(dlzb[j])
print(dlstr)
for i in range(21):
if (dllist[i] == "L"):
turnL()
elif(dllist[i] == "R"):
turnR()
elif(dllist[i] == "U"):
turnU()
elif(dllist[i] == "D"):
turnD()
elif(dllist[i] == "F"):
turnF()
elif(dllist[i] == "B"):
turnB()
elif(dllist[i] == "L'"):
for j in range(3):
turnL()
elif(dllist[i] == "R'"):
for j in range(3):
turnR()
elif(dllist[i] == "U'"):
for j in range(3):
turnU()
elif(dllist[i] == "D'"):
for j in range(3):
turnD()
elif(dllist[i] == "F'"):
for j in range(3):
turnF()
elif(dllist[i] == "B'"):
for j in range(3):
turnB()
elif(dllist[i] == "L2"):
for j in range(2):
turnL()
elif(dllist[i] == "R2"):
for j in range(2):
turnR()
elif(dllist[i] == "U2"):
for j in range(2):
turnU()
elif(dllist[i] == "D2"):
for j in range(2):
turnD()
elif(dllist[i] == "F2"):
for j in range(2):
turnF()
elif(dllist[i] == "B2"):
for j in range(2):
turnB()
print("白: ", bai)
print("绿: ", lv)
print("黄: ", huang)
print("蓝: ", lan)
print("橙: ", cheng)
print("红: ", hong)
笔者能力有限,当时代码较为混乱。
运行结果:
此时每个列表前九位为各个面的转态。
接下来Android中实现!
转换为Java代码!
public class CubeUpSetUtil {
// 前九位标准位后九位缓冲位
String [] bai = {"白","白","白","白","白","白","白","白","白","白","白","白","白","白","白","白","白","白"};
String [] huang = {"黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄","黄"};
String [] lan = {"蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝","蓝"};
String [] lv = {"绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿","绿"};
String [] hong = {"红","红","红","红","红","红","红","红","红","红","红","红","红","红","红","红","红","红"};
String [] cheng = {"橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙","橙"};
// 缓冲归位
public void hcgw(){
for(int i = 9; i < 18; i++){
bai[i] = bai[i - 9];
lv[i] = lv[i - 9];
huang[i] = huang[i - 9];
lan[i] = lan[i - 9];
cheng[i] = cheng[i - 9];
hong[i] = hong[i - 9];
}
}
// 顺时针左转
public void turnL(){
lv[0] = bai[9];
lv[3] = bai[12];
lv[6] = bai[15];
bai[0] = lan[17];
bai[3] = lan[14];
bai[6] = lan[11];
lan[2] = huang[15];
lan[5] = huang[12];
lan[8] = huang[9];
huang[0] = lv[9];
huang[3] = lv[12];
huang[6] = lv[15];
cheng[0] = cheng[15];
cheng[1] = cheng[12];
cheng[2] = cheng[9];
cheng[3] = cheng[16];
cheng[5] = cheng[10];
cheng[6] = cheng[17];
cheng[7] = cheng[14];
cheng[8] = cheng[11];
hcgw();
}
// 顺时针右转
public void turnR(){
bai[2] = lv[11];
bai[5] = lv[14];
bai[8] = lv[17];
lan[0] = bai[17];
lan[3] = bai[14];
lan[6] = bai[11];
huang[2] = lan[15];
huang[5] = lan[12];
huang[8] = lan[9];
lv[2] = huang[11];
lv[5] = huang[14];
lv[8] = huang[17];
hong[0] = hong[15];
hong[1] = hong[12];
hong[2] = hong[9];
hong[3] = hong[16];
hong[5] = hong[10];
hong[6] = hong[17];
hong[7] = hong[14];
hong[8] = hong[11];
hcgw();
}
// 顺时针上转
public void turnU(){
lv[0] = hong[9];
lv[1] = hong[10];
lv[2] = hong[11];
cheng[0] = lv[9];
cheng[1] = lv[10];
cheng[2] = lv[11];
lan[0] = cheng[9];
lan[1] = cheng[10];
lan[2] = cheng[11];
hong[0] = lan[9];
hong[1] = lan[10];
hong[2] = lan[11];
bai[0] = bai[15];
bai[1] = bai[12];
bai[2] = bai[9];
bai[3] = bai[16];
bai[5] = bai[10];
bai[6] = bai[17];
bai[7] = bai[14];
bai[8] = bai[11];
hcgw();
}
// 顺时针下转
public void turnD(){
lv[6] = cheng[15];
lv[7] = cheng[16];
lv[8] = cheng[17];
cheng[6] = lan[15];
cheng[7] = lan[16];
cheng[8] = lan[17];
lan[6] = hong[15];
lan[7] = hong[16];
lan[8] = hong[17];
hong[6] = lv[15];
hong[7] = lv[16];
hong[8] = lv[17];
huang[0] = huang[15];
huang[1] = huang[12];
huang[2] = huang[9];
huang[3] = huang[16];
huang[5] = huang[10];
huang[6] = huang[17];
huang[7] = huang[14];
huang[8] = huang[11];
hcgw();
}
// 顺时针前转
public void turnF(){
bai[6] = cheng[17];
bai[7] = cheng[14];
bai[8] = cheng[11];
cheng[2] = huang[9];
cheng[5] = huang[10];
cheng[8] = huang[11];
huang[0] = hong[15];
huang[1] = hong[12];
huang[2] = hong[9];
hong[0] = bai[15];
hong[3] = bai[16];
hong[6] = bai[17];
lv[0] = lv[15];
lv[1] = lv[12];
lv[2] = lv[9];
lv[3] = lv[16];
lv[5] = lv[10];
lv[6] = lv[17];
lv[7] = lv[14];
lv[8] = lv[11];
hcgw();
}
// 顺时针后转
public void turnB(){
bai[0] = hong[11];
bai[1] = hong[14];
bai[2] = hong[17];
hong[2] = huang[17];
hong[5] = huang[16];
hong[8] = huang[15];
huang[6] = cheng[9];
huang[7] = cheng[12];
huang[8] = cheng[15];
cheng[0] = bai[11];
cheng[3] = bai[10];
cheng[6] = bai[9];
lan[0] = lan[15];
lan[1] = lan[12];
lan[2] = lan[9];
lan[3] = lan[16];
lan[5] = lan[10];
lan[6] = lan[17];
lan[7] = lan[14];
lan[8] = lan[11];
hcgw();
}
public List<String[]> getUpSet(String [] dllist) {
List<String[]> list = new ArrayList<String[]>();
for (int i = 0; i < dllist.length; i++) {
if (dllist[i] == "L") {
turnL();
} else if (dllist[i] == "R") {
turnR();
} else if (dllist[i] == "U") {
turnU();
} else if (dllist[i] == "D") {
turnD();
} else if (dllist[i] == "F") {
turnF();
} else if (dllist[i] == "B") {
turnB();
} else if (dllist[i] == "L'") {
for (int j = 0; j < 3; j++) {
turnL();
}
} else if (dllist[i] == "R'") {
for (int j = 0; j < 3; j++) {
turnR();
}
} else if (dllist[i] == "U'") {
for (int j = 0; j < 3; j++) {
turnU();
}
} else if (dllist[i] == "D'") {
for (int j = 0; j < 3; j++) {
turnD();
}
} else if (dllist[i] == "F'") {
for (int j = 0; j < 3; j++) {
turnF();
}
} else if (dllist[i] == "B'") {
for (int j = 0; j < 3; j++) {
turnB();
}
} else if (dllist[i] == "L2") {
for (int j = 0; j < 2; j++) {
turnL();
}
} else if (dllist[i] == "R2") {
for (int j = 0; j < 2; j++) {
turnR();
}
} else if (dllist[i] == "U2") {
for (int j = 0; j < 2; j++) {
turnU();
}
} else if (dllist[i] == "D2") {
for (int j = 0; j < 2; j++) {
turnD();
}
} else if (dllist[i] == "F2") {
for (int j = 0; j < 2; j++) {
turnF();
}
} else if (dllist[i] == "B2") {
for (int j = 0; j < 2; j++) {
turnB();
}
}
}
list.add(bai);
list.add(huang);
list.add(lan);
list.add(lv);
list.add(hong);
list.add(cheng);
return list;
}
}
获取打乱后的转态并为其上色
public void setDlView(List<String[]> list){
bai1.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(0)[0]).toString()));
bai2.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(0)[1]).toString()));
bai3.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(0)[2]).toString()));
bai4.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(0)[3]).toString()));
bai5.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(0)[4]).toString()));
bai6.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(0)[5]).toString()));
bai7.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(0)[6]).toString()));
bai8.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(0)[7]).toString()));
bai9.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(0)[8]).toString()));
//
huang1.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(1)[0])));
huang2.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(1)[1])));
huang3.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(1)[2])));
huang4.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(1)[3])));
huang5.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(1)[4])));
huang6.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(1)[5])));
huang7.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(1)[6])));
huang8.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(1)[7])));
huang9.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(1)[8])));
//
lan1.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(2)[0])));
lan2.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(2)[1])));
lan3.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(2)[2])));
lan4.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(2)[3])));
lan5.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(2)[4])));
lan6.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(2)[5])));
lan7.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(2)[6])));
lan8.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(2)[7])));
lan9.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(2)[8])));
//
lv1.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(3)[0])));
lv2.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(3)[1])));
lv3.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(3)[2])));
lv4.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(3)[3])));
lv5.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(3)[4])));
lv6.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(3)[5])));
lv7.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(3)[6])));
lv8.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(3)[7])));
lv9.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(3)[8])));
//
hong1.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(4)[0])));
hong2.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(4)[1])));
hong3.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(4)[2])));
hong4.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(4)[3])));
hong5.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(4)[4])));
hong6.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(4)[5])));
hong7.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(4)[6])));
hong8.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(4)[7])));
hong9.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(4)[8])));
//
cheng1.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(5)[0])));
cheng2.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(5)[1])));
cheng3.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(5)[2])));
cheng4.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(5)[3])));
cheng5.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(5)[4])));
cheng6.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(5)[5])));
cheng7.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(5)[6])));
cheng8.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(5)[7])));
cheng9.setBackgroundColor(android.graphics.Color.parseColor(ColorChange(list.get(5)[8])));
}
public String ColorChange(String color){
if(color.equals("白")){
return "#ffffff";
}else if(color.equals("黄")){
return "#ffff00";
}else if(color.equals("蓝")){
return "#3333ff";
}else if(color.equals("绿")){
return "#33cc33";
}else if(color.equals("橙")){
return "#ff9900";
}else if(color.equals("红")){
return "#ff0000";
}else {
return "#ffffff";
}
}
好了成品图
嘿嘿嘿完成个人记录一下!
附上一组练习视频。
手残老年人终于avg14!!!