secret
Created

  • Download Gist
douglasalexandre.rb
Ruby
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 64 65 66 67 68 69 70 71
class Maze
 
START = "A"
GOAL = "B"
NAVIGABLE = " "
MARKED = "+"
UNMARKED = "x"
 
def initialize(maze)
@maze = []
@steps = 0
to_a(maze)
end
 
def to_a(maze)
maze.each_line { |line| @maze << line.chomp.split(//) }
end
 
def find_start_point
@maze.size.times do |@row|
@column = (0..@maze[@row].size).detect { |i| @maze[@row][i] == START }
break if @column
end
end
def outside_maze?(x, y)
x > @maze.size || y > @maze[0].size
end
 
def is_goal?(x, y)
@maze[x][y] == GOAL
end
 
def not_open?(x, y)
@maze[x][y] != NAVIGABLE && @maze[x][y] != START
end
 
def mark_as_part_of_solution_path(x, y)
@maze[x][y] = MARKED
@steps += 1
end
 
def unmark_as_part_of_solution_path(x, y)
@maze[x][y] = UNMARKED
@steps -= 1
end
 
def find_path(x, y)
return false if outside_maze?(x, y)
return true if is_goal?(x, y)
return false if not_open?(x, y)
mark_as_part_of_solution_path(x, y)
return true if find_path(x, y - 1) # North of (x, y)
return true if find_path(x + 1, y) # East of (x, y)
return true if find_path(x, y + 1) # South of (x, y)
return true if find_path(x - 1, y) # West (x, y)
unmark_as_part_of_solution_path(x, y)
false
end
 
def solvable?
find_start_point
find_path(@row, @column)
end
 
def steps
solvable? unless @row && @column
@steps
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.