-
Notifications
You must be signed in to change notification settings - Fork 5
/
cube.py
137 lines (122 loc) · 4.44 KB
/
cube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""Magic Cube"""
import array
import math
import tkintertools as tkt
from tkintertools import constants
from tkintertools import tools_3d as t3d
import definition
class Block(t3d.Cuboid):
"""One block of the magic cube"""
def __init__(
self,
canvas: t3d.Canvas3D | t3d.Space,
x: float,
y: float,
z: float,
length: float,
width: float,
height: float
) -> None:
t3d.Cuboid.__init__(
self, canvas, x, y, z, length, width, height,
color_fill_up="#DD0",
color_fill_down="#DDD",
color_fill_left="orange",
color_fill_right="#C00",
color_fill_front="dodgerblue",
color_fill_back="green",
boardwidth=2
)
class MagicCube:
"""powerful magic cube"""
def __init__(self, canvas: t3d.Canvas3D | t3d.Space) -> None:
s = 120
self.master = canvas
self.x = t3d.Line(canvas, (s*3/2, 0, 0), (999, 0, 0), fill='', width=3)
self.y = t3d.Line(canvas, (0, s*3/2, 0), (0, 999, 0), fill='', width=3)
self.z = t3d.Line(canvas, (0, 0, s*3/2), (0, 0, 999), fill='', width=3)
self.blocks: list[Block] = []
self.ind: list[t3d.Text3D] = []
self.data = list(range(27))
self.steps: list[definition.OP] = []
count = 0
for i in range(-1, 2):
for j in range(-1, 2):
for k in range(-1, 2):
self.ind.append(
t3d.Text3D(
canvas, [i*s*1.65, j*s*1.65, k*s*1.65], fill='', text=f'{count:02}\n{(i, j, k)}'))
self.blocks.append(
Block(canvas, i*s-s/2, j*s-s/2, k*s-s/2, s, s, s))
count += 1
canvas.space_sort()
@classmethod
def get(
cls,
way: definition.Ways,
reverse: bool = False
) -> list[int] | None:
"""get key information of operation"""
match way:
case 'R': ans = [16, 15, 24, 25, 26, 17, 8, 7, 6]
case 'L': ans = [10, 9, 18, 19, 20, 11, 2, 1, 0]
case 'U': ans = [14, 2, 5, 8, 17, 26, 23, 20, 11]
case 'D': ans = [12, 0, 3, 6, 15, 24, 21, 18, 9]
case 'F': ans = [22, 18, 19, 20, 23, 26, 25, 24, 21]
case 'B': ans = [4, 0, 1, 2, 5, 8, 7, 6, 3]
case 'LR': ans = [13, 12, 21, 22, 23, 14, 5, 4, 3]
case 'UD': ans = [13, 1, 4, 7, 16, 25, 22, 19, 10]
case 'FB': ans = [13, 9, 10, 11, 14, 17, 16, 15, 12]
case _: raise ValueError(way)
return ans[:1] + ans[-1:0:-1] if reverse else ans
@classmethod
def set(
cls,
data: array.array[int],
way: definition.Ways,
reverse: bool = False
) -> None:
"""set data with key information"""
keys = cls.get(way, reverse)
data[keys[1]], data[keys[3]], data[keys[5]], data[keys[7]] = \
data[keys[3]], data[keys[5]], data[keys[7]], data[keys[1]]
data[keys[2]], data[keys[4]], data[keys[6]], data[keys[8]] = \
data[keys[4]], data[keys[6]], data[keys[8]], data[keys[2]]
def _turn(
self,
blocks: list[Block],
delta: float,
axis: tuple[tuple[float, float, float], tuple[float, float, float]],
) -> None:
"""turn without any animation almost"""
for block in blocks:
block.rotate(delta, axis=axis)
block.update()
self.master.space_sort()
def turn(
self,
way: definition.Ways,
reverse: bool = False,
*,
ms: int = 250,
animate: bool = True
) -> None:
"""turn the Magic Cube"""
delta = -math.pi / 2 if reverse else math.pi / 2
if animate:
delta /= (ms * constants.FPS // 1000)
match way:
case 'L' | 'R' | 'LR': axis = self.y.coordinates
case 'U' | 'D' | 'UD': axis = self.z.coordinates
case 'F' | 'B' | 'FB': axis = self.x.coordinates
self.steps.append((way, reverse))
self.set(self.blocks, way, reverse)
self.set(self.data, way, reverse)
blocks = [self.blocks[ind] for ind in self.get(way, reverse)]
if animate:
tkt.Animation(
self.master, ms, controller=(math.sin, 0, math.pi),
callback=lambda _: self._turn(blocks, delta, axis)
).run()
else:
self._turn(blocks, delta, axis)