cube-solver/cube_struct_gen.py
2022-08-01 19:16:24 -05:00

126 lines
4.1 KiB
Python

def make_transition(old_face, old_index, new_face, new_index):
return "(struct CubeTransition){{ {}, {}, {}, {} }}".format(
old_face, old_index, new_face, new_index
)
def get_clockwise_transitions(face):
return [
make_transition(face, 0, face, 2),
make_transition(face, 1, face, 5),
make_transition(face, 2, face, 8),
make_transition(face, 3, face, 1),
make_transition(face, 5, face, 7),
make_transition(face, 6, face, 0),
make_transition(face, 7, face, 3),
make_transition(face, 8, face, 6),
]
def get_counterclockwise_transitions(face):
return [
make_transition(face, 2, face, 0),
make_transition(face, 5, face, 1),
make_transition(face, 8, face, 2),
make_transition(face, 1, face, 3),
make_transition(face, 7, face, 5),
make_transition(face, 0, face, 6),
make_transition(face, 3, face, 7),
make_transition(face, 6, face, 8),
]
def get_double_transitions(face):
return [
make_transition(face, 0, face, 8),
make_transition(face, 1, face, 7),
make_transition(face, 2, face, 6),
make_transition(face, 3, face, 5),
make_transition(face, 5, face, 3),
make_transition(face, 6, face, 2),
make_transition(face, 7, face, 1),
make_transition(face, 8, face, 0),
]
def get_rot_transitions(faces, faces_indices, n):
l = len(faces_indices[0])
assert len(faces) == len(faces_indices), "faces and faces_indices not same length"
for face_indices in faces_indices:
assert len(face_indices) == l, "Indices not all same length"
transitions = []
for i in range(len(faces)):
j = (i + n) % len(faces)
for k in range(len(faces_indices[i])):
transitions.append(make_transition(faces[i], faces_indices[i][k], faces[j], faces_indices[j][k]))
return transitions
def make_struct_code(face, rot_faces, rot_faces_indices, rot_n):
transitions = []
if rot_n == 1:
transitions += get_clockwise_transitions(face)
elif rot_n == -1:
transitions += get_counterclockwise_transitions(face)
elif rot_n == 2:
transitions += get_double_transitions(face)
transitions += get_rot_transitions(rot_faces, rot_faces_indices, rot_n)
return "{{\n {}\n }}".format(",\n ".join(transitions))
def make_func(x, n):
name = x["name"]
if n == -1:
name += "p"
elif n == 2:
name += "2"
return (
"void {}(struct Cube* cube) {{\n" +\
" static struct CubeTransition transitions[TRANSITIONS_PER_ROTATION] = {};\n" +\
" transition_cube(cube, transitions);\n" +\
"}}\n"
).format(name, make_struct_code(x["face"], x["rot_faces"], x["rot_faces_indices"], n))
u = {
"name": "u",
"face": "FACE_TOP",
"rot_faces": ["FACE_FRONT", "FACE_LEFT", "FACE_BACK", "FACE_RIGHT"],
"rot_faces_indices": [[0, 1, 2], [0, 1, 2], [8, 7, 6], [0, 1, 2]]
}
d = {
"name": "d",
"face": "FACE_TOP",
"rot_faces": ["FACE_FRONT", "FACE_RIGHT", "FACE_BACK", "FACE_LEFT"],
"rot_faces_indices": [[6, 7, 8], [6, 7, 8], [0, 1, 2], [6, 7, 8]]
}
f = {
"name": "f",
"face": "FACE_FRONT",
"rot_faces": ["FACE_LEFT", "FACE_TOP", "FACE_RIGHT", "FACE_BOTTOM"],
"rot_faces_indices": [[8, 5, 2], [6, 7, 8], [0, 3, 6], [2, 1, 0]]
}
b = {
"name": "b",
"face": "FACE_BACK",
"rot_faces": ["FACE_RIGHT", "FACE_TOP", "FACE_LEFT", "FACE_BOTTOM"],
"rot_faces_indices": [[8, 5, 2], [0, 1, 2], [0, 3, 6], [8, 7, 6]]
}
l = {
"name": "l",
"face": "FACE_LEFT",
"rot_faces": ["FACE_TOP", "FACE_FRONT", "FACE_BOTTOM", "FACE_BACK"],
"rot_faces_indices": [[0, 3, 6], [0, 3, 6], [0, 3, 6], [0, 3, 6]]
}
r = {
"name": "r",
"face": "FACE_RIGHT",
"rot_faces": ["FACE_BACK", "FACE_BOTTOM", "FACE_FRONT", "FACE_TOP"],
"rot_faces_indices": [[2, 5, 8], [2, 5, 8], [2, 5, 8], [2, 5, 8]]
}
for n in [1, -1, 2]:
for x in [u, d, f, b, l, r]:
print(make_func(x, n))