Class: TinyMaze
- Inherits:
-
Object
- Object
- TinyMaze
- Defined in:
- lib/tiny_maze.rb
Constant Summary collapse
- UP =
0- DOWN =
1- LEFT =
2- RIGHT =
3- DIRS =
[ [0, -1], # UP [0, 1], # DOWN [-1, 0], # LEFT [1, 0] # RIGHT ]
Instance Attribute Summary collapse
-
#finish ⇒ Object
readonly
Returns the value of attribute finish.
-
#generated ⇒ Object
readonly
Returns the value of attribute generated.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#maze ⇒ Object
readonly
Returns the value of attribute maze.
-
#start ⇒ Object
readonly
Returns the value of attribute start.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #_coords_from(x, y, dir) ⇒ Object
- #_dig_from!(x, y) ⇒ Object
- #_diggable?(x, y) ⇒ Boolean
- #_rand_cell ⇒ Object
-
#initialize(width:, height:) ⇒ TinyMaze
constructor
A new instance of TinyMaze.
- #to_s ⇒ Object
Constructor Details
#initialize(width:, height:) ⇒ TinyMaze
Returns a new instance of TinyMaze.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tiny_maze.rb', line 23 def initialize(width:, height:) @width = width @height = height @start = nil @finish = nil @maze = [] ((@height * 2) - 1).times do row = [] ((@width * 2) - 1).times do row << false end @maze << row end @dig_counter = 0 @max_digs = @height * @width _dig_from!(*_rand_cell) end |
Instance Attribute Details
#finish ⇒ Object (readonly)
Returns the value of attribute finish.
16 17 18 |
# File 'lib/tiny_maze.rb', line 16 def finish @finish end |
#generated ⇒ Object (readonly)
Returns the value of attribute generated.
16 17 18 |
# File 'lib/tiny_maze.rb', line 16 def generated @generated end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
16 17 18 |
# File 'lib/tiny_maze.rb', line 16 def height @height end |
#maze ⇒ Object (readonly)
Returns the value of attribute maze.
16 17 18 |
# File 'lib/tiny_maze.rb', line 16 def maze @maze end |
#start ⇒ Object (readonly)
Returns the value of attribute start.
16 17 18 |
# File 'lib/tiny_maze.rb', line 16 def start @start end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
16 17 18 |
# File 'lib/tiny_maze.rb', line 16 def width @width end |
Instance Method Details
#_coords_from(x, y, dir) ⇒ Object
78 79 80 |
# File 'lib/tiny_maze.rb', line 78 def _coords_from(x, y, dir) [ x + (2 * DIRS[dir][0]), y + (2 * DIRS[dir][1]) ] end |
#_dig_from!(x, y) ⇒ Object
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 |
# File 'lib/tiny_maze.rb', line 49 def _dig_from!(x, y) @maze[y][x] = true [UP, DOWN, LEFT, RIGHT].shuffle.each do |dir| dest_cell = _coords_from(x, y, dir) if _diggable?(*dest_cell) step_cell = case dir when UP [dest_cell[0], dest_cell[1] + 1] when DOWN [dest_cell[0], dest_cell[1] - 1] when LEFT [dest_cell[0] + 1, dest_cell[1]] when RIGHT [dest_cell[0] - 1, dest_cell[1]] else raise "unknown direction: #{dir}" end @maze[step_cell[1]][step_cell[0]] = true @maze[dest_cell[1]][dest_cell[0]] = true @dig_counter += 1 _dig_from!(*dest_cell) end end end |
#_diggable?(x, y) ⇒ Boolean
82 83 84 85 86 87 88 89 |
# File 'lib/tiny_maze.rb', line 82 def _diggable?(x, y) return false if x < 0 return false if y < 0 return false if x > ((@width * 2) - 1) # - 2 because of zero-based array indexes return false if y > ((@height * 2) - 1) # - 2 because of zero-based array indexes return !@maze[y][x] end |
#_rand_cell ⇒ Object
45 46 47 |
# File 'lib/tiny_maze.rb', line 45 def _rand_cell [rand(@width - 1) * 2, rand(@height - 1) * 2] end |
#to_s ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/tiny_maze.rb', line 91 def to_s s = '' s << ("---" * ((@width * 2) - 1)) s << "\n" @maze.each do |row| row.each do |cell| s << case cell when true then '███' else ' ' end end s << "\n" end s << ('---' * ((@width * 2) - 1)) s end |