secret
Created

  • Download Gist
paulsmith.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
class Maze
attr_accessor :maze, :ax, :ay, :bx, :by
 
def initialize(maze)
@maze = []
maze.each_line do |line|
@maze << line.chomp
if @ax.nil?
@ax = line.index('A')
@ay = @maze.length - 1
end
if @bx.nil?
@bx = line.index('B')
@by = @maze.length - 1
end
end
set_at(@ax, @ay, ' ') unless @ax == nil or @ay == nil
set_at(@bx, @by, ' ') unless @bx == nil or @by == nil
@steps=0
end
def symbol_at(x,y)
@maze[y][x]
end
 
def set_at(x, y, symbol)
@maze[y][x] = symbol
end
def colourize(x,y,colour = 'A')
return true if x==bx && y==by
if symbol_at(x,y) == ' '
set_at(x,y,colour)
return true if colourize(x, y-1, colour)
return true if colourize(x-1, y, colour)
return true if colourize(x, y+1, colour)
return colourize(x+1, y, colour)
else
return false
end
end
def solvable?
colourize(ax, ay)
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.