|
@@ -1,3 +1,4 @@
|
|
|
+require 'chunky_png'
|
|
|
require 'cell'
|
|
|
|
|
|
class Grid
|
|
@@ -87,4 +88,29 @@ class Grid
|
|
|
output
|
|
|
end
|
|
|
|
|
|
+ def to_png(cell_size: 10)
|
|
|
+ img_width = cell_size * columns
|
|
|
+ img_height = cell_size * rows
|
|
|
+
|
|
|
+ background = ChunkyPNG::Color::WHITE
|
|
|
+ wall = ChunkyPNG::Color::BLACK
|
|
|
+
|
|
|
+ img = ChunkyPNG::Image.new(img_width+1, img_height+1, background)
|
|
|
+
|
|
|
+ each_cell do |cell|
|
|
|
+ x1 = cell.column * cell_size
|
|
|
+ y1 = cell.row * cell_size
|
|
|
+ x2 = (cell.column + 1) * cell_size
|
|
|
+ y2 = (cell.row + 1) * cell_size
|
|
|
+
|
|
|
+ img.line(x1, y1, x2, y1, wall) unless cell.north
|
|
|
+ img.line(x1, y1, x1, y2, wall) unless cell.west
|
|
|
+
|
|
|
+ img.line(x2, y1, x2, y2, wall) unless cell.linked?(cell.east)
|
|
|
+ img.line(x1, y2, x2, y2, wall) unless cell.linked?(cell.south)
|
|
|
+ end
|
|
|
+
|
|
|
+ img
|
|
|
+ end
|
|
|
+
|
|
|
end
|