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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: af6ea5967ce320c641b22ccc126afd464d79546ab900100a35e2399958c2abbc
4
- data.tar.gz: e08581a4b99d792c5187ea79d4d4a685e0837657aa9c45626e7dc6980f8040fc
3
+ metadata.gz: 14ddf78b4ee346ccd47871a10ea3da7217f0a2d4b8b4057d8024bb02c8b6940c
4
+ data.tar.gz: 7ccba0d9c547d891381142c46a862487d9571af0c6183fe7dfb250faaed44249
5
5
  SHA512:
6
- metadata.gz: a4a1164a115ea289648b802486cacae6b1d839f915f81f522d7e429b09c3ed0a82951591ce49990897d88d95f5a98e1b380c3761702596c44c13044c600aa9ab
7
- data.tar.gz: 412bedde05650f80014e68342dc6c56d72bb07f891d098cb71d28658ef1a5827dad8f11b4e9dd39bb33acd5cd23db9365b0ab2e40191d1e4095225c12d01c667
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 Algorithm
2
- class AldousBroder
3
- def self.carve(grid:)
4
- cell = grid.random_cell
5
- unvisited = grid.cell_count - 1
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
- while unvisited > 0
8
- neighbor = grid.neighborhood(cell).sample
8
+ while unvisited > 0
9
+ neighbor = grid.neighborhood(cell).sample
9
10
 
10
- if neighbor.links.empty?
11
- grid.build_passage(cell, neighbor)
12
- unvisited -= 1
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
- cell = neighbor
19
+ grid
16
20
  end
17
-
18
- grid
19
21
  end
20
22
  end
21
23
  end
@@ -1,20 +1,22 @@
1
- module Algorithm
2
- class BinaryTree
3
- def self.carve(grid:)
4
- grid.each_cell do |cell|
5
- neighbors = []
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
- north = grid.north(cell)
8
- neighbors << north if north
8
+ north = grid.north(cell)
9
+ neighbors << north if north
9
10
 
10
- east = grid.east(cell)
11
- neighbors << east if east
11
+ east = grid.east(cell)
12
+ neighbors << east if east
12
13
 
13
- neighbor = neighbors.sample
14
- grid.build_passage(cell, neighbor) if neighbor
15
- end
14
+ neighbor = neighbors.sample
15
+ grid.build_passage(cell, neighbor) if neighbor
16
+ end
16
17
 
17
- grid
18
+ grid
19
+ end
18
20
  end
19
21
  end
20
22
  end
@@ -1,23 +1,25 @@
1
- module Algorithm
2
- class RecursiveBacktracker
3
- def self.carve(grid:, start_at: grid.random_cell)
4
- stack = []
5
- stack.push start_at
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
- while stack.any?
8
- current = stack.last
9
- neighbors = grid.neighborhood(current).select { |n| n.links.empty? }
8
+ while stack.any?
9
+ current = stack.last
10
+ neighbors = grid.neighborhood(current).select { |n| n.links.empty? }
10
11
 
11
- if neighbors.empty?
12
- stack.pop
13
- else
14
- neighbor = neighbors.sample
15
- grid.build_passage(current, neighbor)
16
- stack.push(neighbor)
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
- grid
21
+ grid
22
+ end
21
23
  end
22
24
  end
23
25
  end
@@ -1,41 +1,43 @@
1
- module Algorithm
2
- class Sidewinder
3
- def self.carve(grid:)
4
- grid.each_row do |row|
5
- run = []
6
- row.each do |cell|
7
- run << cell
8
-
9
- if close_out_run?(grid, cell)
10
- member = run.sample
11
-
12
- north = grid.north(member)
13
- grid.build_passage(member, north) if north
14
-
15
- run.clear
16
- else
17
- east = grid.east(cell)
18
- grid.build_passage(cell, east)
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
- grid
24
- end
24
+ grid
25
+ end
25
26
 
26
- class << self
27
- private
27
+ class << self
28
+ private
28
29
 
29
- def close_out_run?(grid, cell)
30
- at_east_border?(grid, cell) || (!at_north_border?(grid, cell) && rand(2) == 0)
31
- end
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
- def at_east_border?(grid, cell)
34
- grid.east(cell).nil?
35
- end
34
+ def at_east_border?(grid, cell)
35
+ grid.east(cell).nil?
36
+ end
36
37
 
37
- def at_north_border?(grid, cell)
38
- grid.north(cell).nil?
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 Algorithm
2
- class Wilsons
3
- def self.carve(grid:)
4
- unvisited = []
5
- grid.each_cell { |cell| unvisited << cell }
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
- unvisited.delete(unvisited.sample)
8
- while unvisited.any?
9
- cell = unvisited.sample
10
- path = [cell]
8
+ unvisited.delete(unvisited.sample)
9
+ while unvisited.any?
10
+ cell = unvisited.sample
11
+ path = [cell]
11
12
 
12
- while unvisited.include?(cell)
13
- cell = grid.neighborhood(cell).sample
13
+ while unvisited.include?(cell)
14
+ cell = grid.neighborhood(cell).sample
14
15
 
15
- position = path.index(cell)
16
- if position
17
- path = path[0..position]
18
- else
19
- path << cell
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
- 0.upto(path.length - 2) do |index|
24
- grid.build_passage(path[index], path[index + 1])
25
- unvisited.delete(path[index])
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
- grid
30
+ grid
31
+ end
30
32
  end
31
33
  end
32
34
  end
@@ -1,56 +1,58 @@
1
- module Renderer
2
- class Image
3
- def initialize(grid, **options)
4
- options = defaults.merge(options)
5
-
6
- @grid = grid
7
- @cell_size = options[:cell_size]
8
- @background_color = options[:background_color]
9
- @wall_color = options[:wall_color]
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
- @image = create_canvas
12
- end
15
+ def render
16
+ grid.each_cell do |cell, row, column|
17
+ x1, y1, x2, y2 = coordinates_for(row, column)
13
18
 
14
- def render
15
- grid.each_cell do |cell, row, column|
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
- image.line(x1, y1, x2, y1, wall_color) unless grid.north(cell)
19
- image.line(x1, y1, x1, y2, wall_color) unless grid.west(cell)
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.line(x2, y1, x2, y2, wall_color) unless cell.linked?(grid.east(cell))
22
- image.line(x1, y2, x2, y2, wall_color) unless cell.linked?(grid.south(cell))
26
+ image
23
27
  end
24
28
 
25
- image
26
- end
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
- def defaults
33
- { cell_size: 10,
34
- background_color: PNGAdapter::Color::WHITE,
35
- wall_color: PNGAdapter::Color::BLACK
36
- }
37
- end
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
- def create_canvas
40
- width = grid.columns * cell_size
41
- height = grid.rows * cell_size
40
+ def create_canvas
41
+ width = grid.columns * cell_size
42
+ height = grid.rows * cell_size
42
43
 
43
- PNGAdapter::Image.new(width + 1, height + 1, background_color)
44
- end
44
+ PNGAdapter::Image.new(width + 1, height + 1, background_color)
45
+ end
45
46
 
46
- def coordinates_for(row, column)
47
- x1 = cell_size * column
48
- y1 = cell_size * row
47
+ def coordinates_for(row, column)
48
+ x1 = cell_size * column
49
+ y1 = cell_size * row
49
50
 
50
- x2 = cell_size * (column + 1)
51
- y2 = cell_size * (row + 1)
51
+ x2 = cell_size * (column + 1)
52
+ y2 = cell_size * (row + 1)
52
53
 
53
- [x1, y1, x2, y2]
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 Renderer
4
- module PNGAdapter
5
- class Image
6
- def initialize(width, height, color)
7
- @image = ChunkyPNG::Image.new(width, height, color)
8
- end
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
- def line(x0, y0, x1, y1, color)
11
- image.line(x0, y0, x1, y1, color)
12
- end
11
+ def line(x0, y0, x1, y1, color)
12
+ image.line(x0, y0, x1, y1, color)
13
+ end
13
14
 
14
- def save(filename)
15
- image.save(filename)
16
- end
15
+ def save(filename)
16
+ image.save(filename)
17
+ end
17
18
 
18
- private
19
- attr_accessor :image
20
- end
19
+ private
20
+ attr_accessor :image
21
+ end
21
22
 
22
- module Color
23
- WHITE = ChunkyPNG::Color::WHITE
24
- BLACK = ChunkyPNG::Color::BLACK
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 Renderer
2
- class Text
3
- def initialize
4
- # nothing to do
5
- end
1
+ module Knossos
2
+ module Renderer
3
+ class Text
4
+ def initialize
5
+ # nothing to do
6
+ end
6
7
 
7
- def render(grid:)
8
- output = "+" + "---+" * grid.columns + "\n"
8
+ def render(grid:)
9
+ output = "+" + "---+" * grid.columns + "\n"
9
10
 
10
- grid.each_row do |row|
11
- body = "|"
12
- bottom = "+"
11
+ grid.each_row do |row|
12
+ body = "|"
13
+ bottom = "+"
13
14
 
14
- row.each do |cell|
15
- east = grid.east(cell)
16
- east_border = cell.linked?(east) ? " " : "|"
15
+ row.each do |cell|
16
+ east = grid.east(cell)
17
+ east_border = cell.linked?(east) ? " " : "|"
17
18
 
18
- south = grid.south(cell)
19
- south_border = cell.linked?(south) ? " " : "---"
19
+ south = grid.south(cell)
20
+ south_border = cell.linked?(south) ? " " : "---"
20
21
 
21
- body << " " << east_border
22
- bottom << south_border << "+"
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 << body << "\n"
26
- output << bottom << "\n"
30
+ output
27
31
  end
28
-
29
- output
30
32
  end
31
33
  end
32
34
  end
@@ -1,3 +1,3 @@
1
1
  module Knossos
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
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.1.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-08 00:00:00.000000000 Z
11
+ date: 2020-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chunky_png