Update functions.py
Browse files- functions.py +119 -0
functions.py
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
|
| 4 |
+
def flat_to_grid(flat_grid, w,h):
|
| 5 |
+
grid = [[] for i in range(h)]
|
| 6 |
+
i = 0
|
| 7 |
+
for idx, item in enumerate(flat_grid):
|
| 8 |
+
|
| 9 |
+
if idx%w == 0 and idx != 0:
|
| 10 |
+
i += 1
|
| 11 |
+
grid[i].append(item)
|
| 12 |
+
return grid
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def grid_to_image(grid, resolution=896):
|
| 19 |
+
"""
|
| 20 |
+
Convert a Sokoban grid to RGB matrices of size resolution x resolution.
|
| 21 |
+
"""
|
| 22 |
+
grid = np.array(grid)
|
| 23 |
+
h, w = grid.shape
|
| 24 |
+
cell_size = resolution // max(h, w)
|
| 25 |
+
|
| 26 |
+
# Initialize channels with white floor
|
| 27 |
+
R = np.full((resolution, resolution), 255, dtype=np.uint8)
|
| 28 |
+
G = np.full((resolution, resolution), 255, dtype=np.uint8)
|
| 29 |
+
B = np.full((resolution, resolution), 255, dtype=np.uint8)
|
| 30 |
+
|
| 31 |
+
for i in range(h):
|
| 32 |
+
for j in range(w):
|
| 33 |
+
code = grid[i, j]
|
| 34 |
+
y0, x0 = i * cell_size, j * cell_size
|
| 35 |
+
y1, x1 = y0 + cell_size, x0 + cell_size
|
| 36 |
+
|
| 37 |
+
# Wall: fill black
|
| 38 |
+
if code == 1:
|
| 39 |
+
R[y0:y1, x0:x1] = 0
|
| 40 |
+
G[y0:y1, x0:x1] = 0
|
| 41 |
+
B[y0:y1, x0:x1] = 0
|
| 42 |
+
continue
|
| 43 |
+
|
| 44 |
+
# Goal or overlapping shapes draw red circle
|
| 45 |
+
if code in (4, 5):
|
| 46 |
+
yy, xx = np.ogrid[y0:y1, x0:x1]
|
| 47 |
+
cy, cx = y0 + cell_size/2, x0 + cell_size/2
|
| 48 |
+
radius = cell_size/3
|
| 49 |
+
mask = (yy - cy)**2 + (xx - cx)**2 <= radius**2
|
| 50 |
+
R[y0:y1, x0:x1][mask] = 255
|
| 51 |
+
G[y0:y1, x0:x1][mask] = 0
|
| 52 |
+
B[y0:y1, x0:x1][mask] = 0
|
| 53 |
+
|
| 54 |
+
# Box: blue square or red square on goal
|
| 55 |
+
if code == 3:
|
| 56 |
+
R[y0:y1, x0:x1] = 0
|
| 57 |
+
G[y0:y1, x0:x1] = 0
|
| 58 |
+
B[y0:y1, x0:x1] = 255
|
| 59 |
+
elif code == 5:
|
| 60 |
+
R[y0:y1, x0:x1] = 255
|
| 61 |
+
G[y0:y1, x0:x1] = 0
|
| 62 |
+
B[y0:y1, x0:x1] = 0
|
| 63 |
+
|
| 64 |
+
# Player: green triangle
|
| 65 |
+
if code == 2:
|
| 66 |
+
for dy in range(cell_size):
|
| 67 |
+
frac = dy / (cell_size - 1)
|
| 68 |
+
x_left = int(x0 + (cell_size * (0.5 - 0.5 * frac)))
|
| 69 |
+
x_right = int(x0 + (cell_size * (0.5 + 0.5 * frac)))
|
| 70 |
+
y = y0 + dy
|
| 71 |
+
G[y, x_left:x_right] = 255
|
| 72 |
+
R[y, x_left:x_right] = 0
|
| 73 |
+
B[y, x_left:x_right] = 0
|
| 74 |
+
# Player on Goal: Red triangle
|
| 75 |
+
if code == 6:
|
| 76 |
+
for dy in range(cell_size):
|
| 77 |
+
frac = dy / (cell_size - 1)
|
| 78 |
+
x_left = int(x0 + (cell_size * (0.5 - 0.5 * frac)))
|
| 79 |
+
x_right = int(x0 + (cell_size * (0.5 + 0.5 * frac)))
|
| 80 |
+
y = y1 - dy - 1 # inverted triangle
|
| 81 |
+
G[y, x_left:x_right] = 0
|
| 82 |
+
R[y, x_left:x_right] = 255
|
| 83 |
+
B[y, x_left:x_right] = 255
|
| 84 |
+
return R, G, B
|
| 85 |
+
|
| 86 |
+
"""
|
| 87 |
+
Examples:
|
| 88 |
+
example_grid = [
|
| 89 |
+
[1,1,1,1,1,1,1,1],
|
| 90 |
+
[1,0,1,0,0,0,0,1],
|
| 91 |
+
[1,0,0,0,0,0,6,1],
|
| 92 |
+
[1,0,0,0,0,1,3,1],
|
| 93 |
+
[1,0,5,0,0,0,0,1],
|
| 94 |
+
[1,0,0,0,0,0,0,1],
|
| 95 |
+
[1,0,0,1,0,0,0,1],
|
| 96 |
+
[1,1,1,1,1,1,1,1],
|
| 97 |
+
]
|
| 98 |
+
|
| 99 |
+
# Generate image
|
| 100 |
+
R, G, B = grid_to_image(example_grid)
|
| 101 |
+
img = np.stack([R, G, B], axis=-1)
|
| 102 |
+
|
| 103 |
+
# Display
|
| 104 |
+
plt.figure(figsize=(6,6))
|
| 105 |
+
plt.imshow(img)
|
| 106 |
+
plt.axis('off')
|
| 107 |
+
plt.show()
|
| 108 |
+
---------------------------------------
|
| 109 |
+
flat = [1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,0,0,0,1,1,0,1,1,0,0,1,0,0,0,1,1,3,0,0,0,0,0,1,1,6,5,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1]
|
| 110 |
+
# Generate image
|
| 111 |
+
R, G, B = grid_to_image(flat_to_grid(flat, 8,8))
|
| 112 |
+
img = np.stack([R, G, B], axis=-1)
|
| 113 |
+
|
| 114 |
+
# Display
|
| 115 |
+
plt.figure(figsize=(6,6))
|
| 116 |
+
plt.imshow(img)
|
| 117 |
+
plt.axis('off')
|
| 118 |
+
plt.show()
|
| 119 |
+
"""
|