diff options
Diffstat (limited to 'maze.rb')
-rw-r--r-- | maze.rb | 32 |
1 files changed, 29 insertions, 3 deletions
@@ -3,6 +3,8 @@ Tile = Struct.new(:pos, :up, :right, :down, :left) class Maze attr_reader :width attr_reader :height + attr_accessor :start_pos + attr_accessor :end_pos def initialize(width, height) @width = width @@ -37,6 +39,18 @@ class Maze neighbors end + def open_neighbors(pos) + neighbors = [] + + tile = get(pos) + + [:up, :right, :down, :left].each do |dir| + neighbors.push(pos.move(dir)) if tile[dir] == false + end + + neighbors + end + def set(pos, dir, state) raise IndexError if pos.x >= @width raise IndexError if pos.y >= @height @@ -53,8 +67,8 @@ class Maze end end - def to_s(prefix='') - drawingField = Array.new(@height * 2 + 1) { " " * (@width * 2 + 1) } + def to_s(prefix='', path=[]) + drawingField = Array.new(@height * 2 + 1) { Array.new(@width * 2 + 1) { " " } } @v_walls.each_index do |x| @v_walls[x].each_index do |y| @@ -76,6 +90,18 @@ class Maze end end - drawingField.map { |line| prefix + line }.join("\n") + bright_red_block = "\e[0;31;1m█\e[0m" + + path.each_index do |i| + drawingField[path[i].y * 2 + 1][path[i].x * 2 + 1] = bright_red_block + if i > 0 + wall_pos = Pos.new(path[i - 1].x * 2 + 1, path[i - 1].y * 2 + 1) + .move(path[i].dir_from(path[i - 1])) + + drawingField[wall_pos.y][wall_pos.x] = bright_red_block + end + end + + drawingField.map { |line| prefix + line.join("") }.join("\n") end end |