summaryrefslogtreecommitdiff
path: root/maze_solver.rb
blob: 2f75111dfe833d58850b8b1357d93af5c0f65a35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require_relative 'pos'
require_relative 'maze'
require_relative 'tui'

class MazeSolver
  attr_reader :maze
  attr_reader :stack

  def initialize(maze, start_pos, end_pos)
    @maze = maze
    @start_pos = start_pos
    @end_pos = end_pos
    @stack = [start_pos]
    @visitedTiles = Array.new(@maze.width) { Array.new(@maze.height) { false } }
  end

  def solve!()
    if ENV["DEBUG"] == "visual"
      TUI::Screen.save
      TUI::Screen.reset
      puts
      puts "   [ #{TUI::Color.magenta}Solving maze...#{TUI::Color.reset} ]"
      puts
      TUI::Cursor.save
    end

    while @stack.last != @end_pos
      step()

      if ENV["DEBUG"] == "visual"
        TUI::Cursor.restore
        puts @maze.to_s("   ", @stack)
        puts
        TUI::Screen.reset_line
        puts "   Stack size: #{@stack.length}"
        puts "   Current algorithm: Depth-first search"
      end
    end

    if ENV["DEBUG"] == "visual"
      TUI::Screen.restore
    end
  end

  def step()
    current_tile = @stack.last

    neighbors = @maze.open_neighbors(current_tile)
    neighbors.select! do |neighbor|
      @visitedTiles[neighbor.x][neighbor.y] == false
    end

    if neighbors.empty?
      @stack.pop()
      puts "Popping to #{stack.last}" if ENV["DEBUG"] == "log"
    else
      randomNeighbor = neighbors.sample
      @stack.push(randomNeighbor)
      puts "Pushing to #{stack.last}" if ENV["DEBUG"] == "log"
      @visitedTiles[randomNeighbor.x][randomNeighbor.y] = true
    end
  end
end