Skip to content

Instantly share code, notes, and snippets.

@eratos
Created December 29, 2009 23:59
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save eratos/d0cdc257885888e24187 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment