tiny_maze 1.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/tiny_maze.rb +109 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 973a744cce126099c5a272619cc194d50dc8bba042a5b9b9b4fc28388951d9fc
4
+ data.tar.gz: c0d1f3f184b88bb617dcd0fa464dc5b97fcf673032a065c02130d5484f810e00
5
+ SHA512:
6
+ metadata.gz: b31fc371644b33bf59c2a32298794ef8365ab6cd556a6dcc50174a4de70bd81e95af697cb44a0a1ecbaa103b407ec72993fe423018d90dcfcb5eb1f5dfe19554
7
+ data.tar.gz: 20cc825d83ad6c1e46d11c16ac13fcffc5b71647673febe78f789781ef95766aed905d76241ce09144c3eeb33f7c7634c9ac26b24e69d11fa2ffb44009dbffda
data/lib/tiny_maze.rb ADDED
@@ -0,0 +1,109 @@
1
+ require 'time'
2
+
3
+ class TinyMaze
4
+ UP = 0
5
+ DOWN = 1
6
+ LEFT = 2
7
+ RIGHT = 3
8
+
9
+ DIRS = [
10
+ [0, -1], # UP
11
+ [0, 1], # DOWN
12
+ [-1, 0], # LEFT
13
+ [1, 0] # RIGHT
14
+ ]
15
+
16
+ attr_reader :generated,
17
+ :width,
18
+ :height,
19
+ :start,
20
+ :finish,
21
+ :maze
22
+
23
+ def initialize(width:, height:)
24
+ @width = width
25
+ @height = height
26
+ @start = nil
27
+ @finish = nil
28
+ @maze = []
29
+ ((@height * 2) - 1).times do
30
+ row = []
31
+
32
+ ((@width * 2) - 1).times do
33
+ row << false
34
+ end
35
+
36
+ @maze << row
37
+ end
38
+
39
+ @dig_counter = 0
40
+ @max_digs = @height * @width
41
+
42
+ _dig_from!(*_rand_cell)
43
+ end
44
+
45
+ def _rand_cell
46
+ [rand(@width - 1) * 2, rand(@height - 1) * 2]
47
+ end
48
+
49
+ def _dig_from!(x, y)
50
+ @maze[y][x] = true
51
+
52
+ [UP, DOWN, LEFT, RIGHT].shuffle.each do |dir|
53
+ dest_cell = _coords_from(x, y, dir)
54
+ if _diggable?(*dest_cell)
55
+ step_cell = case dir
56
+ when UP
57
+ [dest_cell[0], dest_cell[1] + 1]
58
+ when DOWN
59
+ [dest_cell[0], dest_cell[1] - 1]
60
+ when LEFT
61
+ [dest_cell[0] + 1, dest_cell[1]]
62
+ when RIGHT
63
+ [dest_cell[0] - 1, dest_cell[1]]
64
+ else
65
+ raise "unknown direction: #{dir}"
66
+ end
67
+
68
+ @maze[step_cell[1]][step_cell[0]] = true
69
+ @maze[dest_cell[1]][dest_cell[0]] = true
70
+
71
+ @dig_counter += 1
72
+
73
+ _dig_from!(*dest_cell)
74
+ end
75
+ end
76
+ end
77
+
78
+ def _coords_from(x, y, dir)
79
+ [ x + (2 * DIRS[dir][0]), y + (2 * DIRS[dir][1]) ]
80
+ end
81
+
82
+ def _diggable?(x, y)
83
+ return false if x < 0
84
+ return false if y < 0
85
+ return false if x > ((@width * 2) - 1) # - 2 because of zero-based array indexes
86
+ return false if y > ((@height * 2) - 1) # - 2 because of zero-based array indexes
87
+
88
+ return !@maze[y][x]
89
+ end
90
+
91
+ def to_s
92
+ s = ''
93
+ s << ("---" * ((@width * 2) - 1))
94
+ s << "\n"
95
+ @maze.each do |row|
96
+ row.each do |cell|
97
+ s << case cell
98
+ when true then '███'
99
+ else
100
+ ' '
101
+ end
102
+ end
103
+ s << "\n"
104
+ end
105
+
106
+ s << ('---' * ((@width * 2) - 1))
107
+ s
108
+ end
109
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tiny_maze
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Lunt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-12-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: a tiny maze generator
14
+ email: jefflunt@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/tiny_maze.rb
20
+ homepage: https://github.com/jefflunt/tiny_maze
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubygems_version: 3.3.7
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: a tiny maze generator
43
+ test_files: []