knossos 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/lib/knossos/algorithm/aldous_broder.rb +15 -13
- data/lib/knossos/algorithm/binary_tree.rb +15 -13
- data/lib/knossos/algorithm/recursive_backtracker.rb +18 -16
- data/lib/knossos/algorithm/sidewinder.rb +33 -31
- data/lib/knossos/algorithm/wilsons.rb +24 -22
- data/lib/knossos/renderer/image.rb +43 -41
- data/lib/knossos/renderer/png_adapter.rb +20 -18
- data/lib/knossos/renderer/text.rb +23 -21
- data/lib/knossos/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14ddf78b4ee346ccd47871a10ea3da7217f0a2d4b8b4057d8024bb02c8b6940c
|
4
|
+
data.tar.gz: 7ccba0d9c547d891381142c46a862487d9571af0c6183fe7dfb250faaed44249
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd6e51fd8a7116c1f5844e9b0bd98e80ad7edc540d0f3e9a014d16b1cc332013aa825bca03d63adea764e93d9c48e1b8492fb812619d1e2e956def4a8669f98f
|
7
|
+
data.tar.gz: caa60fb3da899b1e69affbdf172e2a8621a8dd588bc9631b0fbf710aa8985f07ae2e2aa02c924940144a501bf0c74fd7c0ee0b60adff9e67e7dcee2180aae59f
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
This project uses [Semantic Versioning][sv].
|
5
5
|
|
6
6
|
## [Unreleased][new]
|
7
|
+
### Changed
|
8
|
+
- Correctly nest the `Algorithm` module within the `Knossos` namespace.
|
9
|
+
- Correctly nest the `Renderer` module within the `Knossos` namespace.
|
7
10
|
|
8
11
|
## [0.1.0][0.1.0] — 2020-01-07
|
9
12
|
### Added
|
@@ -1,21 +1,23 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module Knossos
|
2
|
+
module Algorithm
|
3
|
+
class AldousBroder
|
4
|
+
def self.carve(grid:)
|
5
|
+
cell = grid.random_cell
|
6
|
+
unvisited = grid.cell_count - 1
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
while unvisited > 0
|
9
|
+
neighbor = grid.neighborhood(cell).sample
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
if neighbor.links.empty?
|
12
|
+
grid.build_passage(cell, neighbor)
|
13
|
+
unvisited -= 1
|
14
|
+
end
|
15
|
+
|
16
|
+
cell = neighbor
|
13
17
|
end
|
14
18
|
|
15
|
-
|
19
|
+
grid
|
16
20
|
end
|
17
|
-
|
18
|
-
grid
|
19
21
|
end
|
20
22
|
end
|
21
23
|
end
|
@@ -1,20 +1,22 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
grid
|
5
|
-
|
1
|
+
module Knossos
|
2
|
+
module Algorithm
|
3
|
+
class BinaryTree
|
4
|
+
def self.carve(grid:)
|
5
|
+
grid.each_cell do |cell|
|
6
|
+
neighbors = []
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
north = grid.north(cell)
|
9
|
+
neighbors << north if north
|
9
10
|
|
10
|
-
|
11
|
-
|
11
|
+
east = grid.east(cell)
|
12
|
+
neighbors << east if east
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
14
|
+
neighbor = neighbors.sample
|
15
|
+
grid.build_passage(cell, neighbor) if neighbor
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
+
grid
|
19
|
+
end
|
18
20
|
end
|
19
21
|
end
|
20
22
|
end
|
@@ -1,23 +1,25 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module Knossos
|
2
|
+
module Algorithm
|
3
|
+
class RecursiveBacktracker
|
4
|
+
def self.carve(grid:, start_at: grid.random_cell)
|
5
|
+
stack = []
|
6
|
+
stack.push start_at
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
while stack.any?
|
9
|
+
current = stack.last
|
10
|
+
neighbors = grid.neighborhood(current).select { |n| n.links.empty? }
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
if neighbors.empty?
|
13
|
+
stack.pop
|
14
|
+
else
|
15
|
+
neighbor = neighbors.sample
|
16
|
+
grid.build_passage(current, neighbor)
|
17
|
+
stack.push(neighbor)
|
18
|
+
end
|
17
19
|
end
|
18
|
-
end
|
19
20
|
|
20
|
-
|
21
|
+
grid
|
22
|
+
end
|
21
23
|
end
|
22
24
|
end
|
23
25
|
end
|
@@ -1,41 +1,43 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
grid
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
1
|
+
module Knossos
|
2
|
+
module Algorithm
|
3
|
+
class Sidewinder
|
4
|
+
def self.carve(grid:)
|
5
|
+
grid.each_row do |row|
|
6
|
+
run = []
|
7
|
+
row.each do |cell|
|
8
|
+
run << cell
|
9
|
+
|
10
|
+
if close_out_run?(grid, cell)
|
11
|
+
member = run.sample
|
12
|
+
|
13
|
+
north = grid.north(member)
|
14
|
+
grid.build_passage(member, north) if north
|
15
|
+
|
16
|
+
run.clear
|
17
|
+
else
|
18
|
+
east = grid.east(cell)
|
19
|
+
grid.build_passage(cell, east)
|
20
|
+
end
|
19
21
|
end
|
20
22
|
end
|
21
|
-
end
|
22
23
|
|
23
|
-
|
24
|
-
|
24
|
+
grid
|
25
|
+
end
|
25
26
|
|
26
|
-
|
27
|
-
|
27
|
+
class << self
|
28
|
+
private
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
def close_out_run?(grid, cell)
|
31
|
+
at_east_border?(grid, cell) || (!at_north_border?(grid, cell) && rand(2) == 0)
|
32
|
+
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
34
|
+
def at_east_border?(grid, cell)
|
35
|
+
grid.east(cell).nil?
|
36
|
+
end
|
36
37
|
|
37
|
-
|
38
|
-
|
38
|
+
def at_north_border?(grid, cell)
|
39
|
+
grid.north(cell).nil?
|
40
|
+
end
|
39
41
|
end
|
40
42
|
end
|
41
43
|
end
|
@@ -1,32 +1,34 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module Knossos
|
2
|
+
module Algorithm
|
3
|
+
class Wilsons
|
4
|
+
def self.carve(grid:)
|
5
|
+
unvisited = []
|
6
|
+
grid.each_cell { |cell| unvisited << cell }
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
unvisited.delete(unvisited.sample)
|
9
|
+
while unvisited.any?
|
10
|
+
cell = unvisited.sample
|
11
|
+
path = [cell]
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
while unvisited.include?(cell)
|
14
|
+
cell = grid.neighborhood(cell).sample
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
position = path.index(cell)
|
17
|
+
if position
|
18
|
+
path = path[0..position]
|
19
|
+
else
|
20
|
+
path << cell
|
21
|
+
end
|
20
22
|
end
|
21
|
-
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
0.upto(path.length - 2) do |index|
|
25
|
+
grid.build_passage(path[index], path[index + 1])
|
26
|
+
unvisited.delete(path[index])
|
27
|
+
end
|
26
28
|
end
|
27
|
-
end
|
28
29
|
|
29
|
-
|
30
|
+
grid
|
31
|
+
end
|
30
32
|
end
|
31
33
|
end
|
32
34
|
end
|
@@ -1,56 +1,58 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
module Knossos
|
2
|
+
module Renderer
|
3
|
+
class Image
|
4
|
+
def initialize(grid, **options)
|
5
|
+
options = defaults.merge(options)
|
6
|
+
|
7
|
+
@grid = grid
|
8
|
+
@cell_size = options[:cell_size]
|
9
|
+
@background_color = options[:background_color]
|
10
|
+
@wall_color = options[:wall_color]
|
11
|
+
|
12
|
+
@image = create_canvas
|
13
|
+
end
|
10
14
|
|
11
|
-
|
12
|
-
|
15
|
+
def render
|
16
|
+
grid.each_cell do |cell, row, column|
|
17
|
+
x1, y1, x2, y2 = coordinates_for(row, column)
|
13
18
|
|
14
|
-
|
15
|
-
|
16
|
-
x1, y1, x2, y2 = coordinates_for(row, column)
|
19
|
+
image.line(x1, y1, x2, y1, wall_color) unless grid.north(cell)
|
20
|
+
image.line(x1, y1, x1, y2, wall_color) unless grid.west(cell)
|
17
21
|
|
18
|
-
|
19
|
-
|
22
|
+
image.line(x2, y1, x2, y2, wall_color) unless cell.linked?(grid.east(cell))
|
23
|
+
image.line(x1, y2, x2, y2, wall_color) unless cell.linked?(grid.south(cell))
|
24
|
+
end
|
20
25
|
|
21
|
-
image
|
22
|
-
image.line(x1, y2, x2, y2, wall_color) unless cell.linked?(grid.south(cell))
|
26
|
+
image
|
23
27
|
end
|
24
28
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
private
|
29
|
-
attr_reader :grid, :cell_size, :background_color, :wall_color
|
30
|
-
attr_accessor :image
|
29
|
+
private
|
30
|
+
attr_reader :grid, :cell_size, :background_color, :wall_color
|
31
|
+
attr_accessor :image
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
33
|
+
def defaults
|
34
|
+
{ cell_size: 10,
|
35
|
+
background_color: PNGAdapter::Color::WHITE,
|
36
|
+
wall_color: PNGAdapter::Color::BLACK
|
37
|
+
}
|
38
|
+
end
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
40
|
+
def create_canvas
|
41
|
+
width = grid.columns * cell_size
|
42
|
+
height = grid.rows * cell_size
|
42
43
|
|
43
|
-
|
44
|
-
|
44
|
+
PNGAdapter::Image.new(width + 1, height + 1, background_color)
|
45
|
+
end
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
47
|
+
def coordinates_for(row, column)
|
48
|
+
x1 = cell_size * column
|
49
|
+
y1 = cell_size * row
|
49
50
|
|
50
|
-
|
51
|
-
|
51
|
+
x2 = cell_size * (column + 1)
|
52
|
+
y2 = cell_size * (row + 1)
|
52
53
|
|
53
|
-
|
54
|
+
[x1, y1, x2, y2]
|
55
|
+
end
|
54
56
|
end
|
55
57
|
end
|
56
58
|
end
|
@@ -1,27 +1,29 @@
|
|
1
1
|
require 'chunky_png'
|
2
2
|
|
3
|
-
module
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
module Knossos
|
4
|
+
module Renderer
|
5
|
+
module PNGAdapter
|
6
|
+
class Image
|
7
|
+
def initialize(width, height, color)
|
8
|
+
@image = ChunkyPNG::Image.new(width, height, color)
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
def line(x0, y0, x1, y1, color)
|
12
|
+
image.line(x0, y0, x1, y1, color)
|
13
|
+
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
def save(filename)
|
16
|
+
image.save(filename)
|
17
|
+
end
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
19
|
+
private
|
20
|
+
attr_accessor :image
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
module Color
|
24
|
+
WHITE = ChunkyPNG::Color::WHITE
|
25
|
+
BLACK = ChunkyPNG::Color::BLACK
|
26
|
+
end
|
25
27
|
end
|
26
28
|
end
|
27
29
|
end
|
@@ -1,32 +1,34 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module Knossos
|
2
|
+
module Renderer
|
3
|
+
class Text
|
4
|
+
def initialize
|
5
|
+
# nothing to do
|
6
|
+
end
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
def render(grid:)
|
9
|
+
output = "+" + "---+" * grid.columns + "\n"
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
grid.each_row do |row|
|
12
|
+
body = "|"
|
13
|
+
bottom = "+"
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
row.each do |cell|
|
16
|
+
east = grid.east(cell)
|
17
|
+
east_border = cell.linked?(east) ? " " : "|"
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
south = grid.south(cell)
|
20
|
+
south_border = cell.linked?(south) ? " " : "---"
|
20
21
|
|
21
|
-
|
22
|
-
|
22
|
+
body << " " << east_border
|
23
|
+
bottom << south_border << "+"
|
24
|
+
end
|
25
|
+
|
26
|
+
output << body << "\n"
|
27
|
+
output << bottom << "\n"
|
23
28
|
end
|
24
29
|
|
25
|
-
output
|
26
|
-
output << bottom << "\n"
|
30
|
+
output
|
27
31
|
end
|
28
|
-
|
29
|
-
output
|
30
32
|
end
|
31
33
|
end
|
32
34
|
end
|
data/lib/knossos/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: knossos
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter J. Hinckley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-01-
|
11
|
+
date: 2020-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chunky_png
|