forked from raulc03/Ubongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiece.py
112 lines (95 loc) · 3.25 KB
/
Piece.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
class Piece:
def __init__(self, matrix):
self.matrix = matrix
self.rotations = []
self.flipped = False
self.width = len(matrix[0])
self.height = len(matrix)
self.biggest = max(self.width, self.height)
def produce_variants(self):
work = self.matrix
if work not in self.rotations:
self.rotations.append(work)
count = 0
while count < 5 and True:
temp = self.rotate(work)
if temp in self.rotations:
break
self.rotations.append(temp)
work = temp
count += 1
work = self.flip_x(self.matrix)
if work not in self.rotations:
self.flipped = True
self.rotations.append(work)
count = 0
while count < 5 and True:
temp = self.rotate(work)
if temp in self.rotations:
break
self.rotations.append(temp)
work = temp
count += 1
work = self.flip_y(self.matrix)
if work not in self.rotations:
self.flipped = True
self.rotations.append(work)
count = 0
while count < 5 and True:
temp = self.rotate(work)
if temp in self.rotations:
break
self.rotations.append(temp)
work = temp
count += 1
def flip_x(self, mat):
out = []
s = len(mat[0]) - 1
for row in mat:
out.append([row[s - x] for x, _ in enumerate(row)])
return out
def flip_y(self, mat):
out = []
s = len(mat) - 1
for i in range(len(mat)):
out.append([])
for y, row in enumerate(mat):
out[s - y] = list(row)
return out
def rotate(self, mat):
width = len(mat[0])
height = len(mat)
out = []
for _ in range(width):
out.append([0 for _ in range(height)])
try:
for y, row in enumerate(mat):
for x, value in enumerate(row):
out[x][height - 1 - y] = value
except Exception as e:
print(f"x={x}, y={y}, h={len(out)}, w={len(out[x])}")
raise e
return out
def output(self, name=""):
if name:
print(f"Name={name}")
print(f"Rotation count {len(self.rotations)}")
# print(self.rotations)
temp = []
for i in range(self.biggest):
temp.append([" " for _ in range((1 + self.biggest) * len(self.rotations))])
for i, mat in enumerate(self.rotations):
start = (1 + self.biggest) * i
if start - 1 > 0:
for y in range(self.biggest):
temp[y][start - 1] = "|"
for y, row in enumerate(mat):
for x, value in enumerate(row):
try:
if 1 == value:
temp[y][start + x] = "X"
except Exception as e:
print(f"x={x}, y={y}, start={start}, h={len(temp)}, w={len(temp[y])}")
raise e
for text_matrix in temp:
print("".join(text_matrix))