secret
Created — forked from pantras/PhilippeAntras.rb

  • Download Gist
PhilippeAntras.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
START = 'A'
ARRIVAL = 'B'
WALL = '#'
SPACE = ' '
VISITED = '.'
 
class Maze
def initialize(mazestring)
@steps = 0
@solvable = false
maze_string_to_array(mazestring)
start_point = localize_start_point()
explore(*start_point)
end
def solvable?
@solvable
end
def steps
@steps
end
def maze_string_to_array(mazestring)
@maze = []
mazestring.each do |line|
@maze.push line.chomp
end
end
def localize_start_point
@maze.each_index do |i|
if j = @maze[i].index(START)
return [i, j]
end
end
end
def get_cell(x, y)
@maze[x][y, 1]
end
def mark_cell(x, y, value)
@maze[x][y, 1] = value
end
def explore(x, y, step=0)
cell = get_cell(x, y)
case cell
when WALL, VISITED then return
when ARRIVAL
if (! @solvable || step < @steps)
@steps = step
end
@solvable = true
return
when START
return unless (step == 0) # stop exploring if back to start point
when SPACE
mark_cell(x, y, VISITED)
else
abort "ABORT: unknown cell : '#{cell}'"
end
 
step += 1
explore(x-1, y, step)
explore(x, y+1, step)
explore(x+1, y, step)
explore(x, y-1, step)
mark_cell(x, y, SPACE) unless (cell == START)
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.