public
Last active — forked from rhyhann/maze.rb

  • Download Gist
maze.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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
# maze.rb, by Othmane Benkirane.
# only tested with ruby 1.9.1p376 (2009-12-07 revision 26041).
# will definitely not work with ruby 1.8.
=begin
Algorithm for steps:
- @steps is populated with the starting cell in the beginnign ('A')
- Until a solution is found, @steps replaces its content by the
possible next steps
- If a solution is found, the number is returned, if a solution isn't found
(if @steps is empty), an error is raised (the loop is broken) and rescued
to return 0
Algorithm for solvable?:
- steps must be superior to zero, since if it's zero, there is no solution.
=end
# Shortcuts
class String
def chars
split(//)
end
end
class Integer
def prev
self - 1
end
end
 
class Maze
# Raised when there is no solution
SolutionNotFound = Class.new(StopIteration)
# @labyrinth is generated in the initialize method, and
# is the string's array: each row contains one array of cells, which
# enables the cell calling like that: @labyrinth[row][column]
# it is used later by cells to find their relative neighbours.
attr_reader :labyrinth
def initialize(string)
# Making it a 2-D array
@labyrinth = string.lines.map {|row| row.chomp.chars}
# Because the cells aren't unique, it's better to hardcode
# their coordinates by turning them into a Cell element, which
# is explained later and takes the coordinates and the maze as an argument.
@labyrinth.map!.with_index do |row, row_index|
row.map!.with_index do |value, column_index|
Cell.new value:value, row:row_index, column:column_index,maze:self
end
end
# The starting cell is used to create an initial step, which is used in
# #steps to begin the solution searching
start_cell = @labyrinth.flatten.find {|cell| cell.status == :start}
(@steps = []) << Step.new(cell:start_cell,number:0)
end
# If a solution is found, steps must be superior to 0, since A and B can't
# be on the same cell. In #steps, 0 is returned if there is no solution.
def solvable?
steps > 0
end
def steps
# An infinite loop until the solution is found, or no ways left
# which means there's no solution : an exception is raised and rescued
# to return 0.
until output = @steps.find {|step| step.cell.status == :stop}
@steps.map! do |step|
step.next_steps # replaces the step by its correct neighbours
end
# @steps is flattened since step.next_steps generates an array.
raise SolutionNotFound if @steps.flatten!.empty?
end
output.number # we just want an integer.
rescue SolutionNotFound
return 0
end
class Step
attr_reader :cell, :number, :previous
# Cell: the cell where the step it
# Count: the step number
# Previous: the previous step, because we don't want to go back
# params: cell(Maze::Cell), number(Integer), previous(Step, not required)
def initialize(params = {})
# @previous is nil if there is no params[:previous] given
@cell,@number,@previous = params[:cell],params[:number],params[:previous]
end
# Takes all the cell's neighbours, converts them into a new step,
# and rejects every '#' one and the previous one, which is not
# a good way because we don't want to go back.
def next_steps
@cell.neighbours.map do |cell|
Step.new(cell:cell,number:@number+1,previous:self)
end.reject { |step| (step.cell.status == :wall)}.reject do |step|
step.cell == @previous.cell if @previous
# we must add 'if @previous' since, if it's the starting cell,
# @previous is nil.
end
end
end
class Cell
attr_reader :row, :column, :status
# Row, Column: row and column _number_.
# Maze: the related maze. Used to find the neighbours by taking
# its 2-d labyrinth
# the status is dynamically generated from the given value
# params: row(Integer), column(Integer), value(String), maze(Maze)
def initialize(params = {})
@row, @column = params[:row], params[:column]
@laby = params[:maze].labyrinth # the maze labyrinth
{empty:' ',wall:'#',start:'A',stop:'B'}.each do |status, value|
@status = status if value == params[:value]
end
end
def neighbours
# up/down, right/left
[@laby[@row][@column.succ], @laby[@row][@column.prev],
@laby[@row.succ][@column], @laby[@row.prev][@column]]
end
# Used by Maze::Step#neighbours to remove the previous cell from the
# next steps.
def ==(other)
return false unless self.class === other
other.row == @row &&
other.column == @column &&
other.status == @status
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.