-
Notifications
You must be signed in to change notification settings - Fork 2
/
grid.py
137 lines (115 loc) · 5.08 KB
/
grid.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
from cell import Cell
import random
import svgwrite
from PIL import Image, ImageDraw
class Grid:
def __init__(self, rows, columns):
self.rows, self.columns = rows, columns
self.grid = self.prepare_grid()
self.configure_cells()
self.end = []
def prepare_grid(self):
grid_array = []
for row in range(self.rows):
row_array = []
for column in range(self.columns):
row_array.append(Cell(row, column))
grid_array.append(row_array)
return grid_array
def configure_cells(self):
for row in self.grid:
for cell in row:
row, col = cell.row, cell.column
cell.north = self.get_neighbor(row - 1, col)
cell.south = self.get_neighbor(row + 1, col)
cell.west = self.get_neighbor(row, col - 1)
cell.east = self.get_neighbor(row, col + 1)
def get_neighbor(self, row, column):
if 0 <= row < self.rows and 0 <= column < self.columns:
return self.grid[row][column]
return None
def random_cell(self):
return self.grid[random.randrange(self.rows)][random.randrange(self.columns)]
def size(self):
return self.rows * self.columns
def each_row(self):
for row in self.grid:
yield row
def each_cell(self):
cells = []
for row in self.grid:
for cell in row:
cells.append(cell)
return cells
def contents_of(self, cell):
return " "
def background_color_for(self, cell):
return None
def to_string(self):
print("+" + "---+" * self.columns)
for row in self.grid:
top = '|'
bottom='+'
for cell in row:
body=" " + self.contents_of(cell) + " "
east_boundary = " " if cell.linked(cell.east) else "|"
top = top + body + east_boundary
south_boundary = " " if cell.linked(cell.south) else '---'
corner = '+'
bottom = bottom + south_boundary + corner
print(top)
print(bottom)
def to_svg(self, cell_size = 10):
top_offset = 20
left_offset = 20
img_width = cell_size * self.columns
img_height = cell_size * self.rows
dwg = svgwrite.Drawing('./exports/maze.svg')
for cell in self.each_cell():
x1 = cell.column * cell_size + top_offset
y1 = cell.row * cell_size + left_offset
x2 = (cell.column + 1) * cell_size + top_offset
y2 = (cell.row + 1) * cell_size + left_offset
dwg.add(dwg.text(self.contents_of(cell), insert=(x1 + 5 ,y1 + cell_size - 5)))
if self.background_color_for(cell) is not None:
dwg.add(dwg.rect(insert=(x1, y1), size=(cell_size, cell_size), rx=None, ry=None, fill=self.background_color_for(cell)))
if not cell.north:
dwg.add(dwg.line((x1, y1), (x2, y1), stroke=svgwrite.rgb(10, 10, 16, '%')))
if not cell.west:
dwg.add(dwg.line((x1, y1), (x1, y2), stroke=svgwrite.rgb(10, 10, 16, '%')))
if not cell.linked(cell.east):
dwg.add(dwg.line((x2, y1), (x2, y2), stroke=svgwrite.rgb(10, 10, 16, '%')))
if not cell.linked(cell.south):
dwg.add(dwg.line((x1, y2), (x2, y2), stroke=svgwrite.rgb(10, 10, 16, '%')))
dwg.save()
def to_png(self, cell_size=20, index = "0"):
top_offset = 20
left_offset = 20
img_width = cell_size * self.columns
img_height = cell_size * self.rows
im = Image.new('RGB', (img_width + 2 * left_offset, img_height + 2* top_offset),(255,255,255))
draw = ImageDraw.Draw(im)
draw.rectangle((left_offset, top_offset, left_offset + img_width, top_offset + img_height), fill="#f0f0f0")
for cell in self.each_cell():
x1 = cell.column * cell_size + top_offset
y1 = cell.row * cell_size + left_offset
x2 = (cell.column + 1) * cell_size + top_offset
y2 = (cell.row + 1) * cell_size + left_offset
color = "#fff"
if len(cell.get_links()) > 0:
if self.background_color_for(cell) is not None:
color=self.background_color_for(cell)
draw.rectangle((x1 + 1, y1+1 , x2-1, y2-1), fill=color)
if not cell.north:
draw.line((x1, y1, x2, y1), (0,0,0))
if not cell.west:
draw.line((x1, y1, x1, y2), (0,0,0))
if not cell.linked(cell.east):
draw.line((x2, y1, x2, y2), (0,0,0))
elif cell.east:
draw.line((x2, y1 + 1, x2, y2 -1), fill=color)
if not cell.linked(cell.south):
draw.line((x1, y2, x2, y2), (0,0,0))
elif cell.south:
draw.line((x1 + 1, y2, x2 - 1, y2), fill=color)
im.save("./exports/maze"+index+".png", "PNG")