Skip to content

Instantly share code, notes, and snippets.

@IndianGuru
Forked from anonymous/krzysztofkotlarek
Created January 16, 2010 01:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IndianGuru/539cb97178f44374bd58 to your computer and use it in GitHub Desktop.
Save IndianGuru/539cb97178f44374bd58 to your computer and use it in GitHub Desktop.
class Maze
attr_reader :steps
def initialize(maze)
#make array
@tab = maze.split('')
#find A and B
@start = find_start
@end = find_end
@solvable = false
@steps = 0
#start queue
@q = []
@q = [@start, 0]
#algorithm
work_with_queue
end
def solvable?
@solvable
end
private
def find_start
@tab.index('A')
end
def find_end
@tab.index('B')
end
def find_helper(a)
if @tab[a] == " "
return a
elsif @tab[a] == 'B'
return "finish"
else
return nil
end
end
#find neighbours
def find_top(a)
find_helper(a-38)
end
def find_bottom(a)
find_helper(a+38)
end
def find_left(a)
find_helper(a-1)
end
def find_right(a)
find_helper(a+1)
end
#mark neighbours
def mark_neighbour(position,step)
if position and position != "finish"
@tab[position] = step
@q << position << (step + 1)
end
if position == "finish"
@steps = step + 1
@solvable = true
end
end
def find_and_mark_neighbour(position,step)
mark_neighbour(find_top(position),step)
mark_neighbour(find_bottom(position),step)
mark_neighbour(find_left(position),step)
mark_neighbour(find_right(position),step)
end
def work_with_queue
while @q.count > 0
position = @q.shift
step = @q.shift
find_and_mark_neighbour(position,step)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment