Ok so I had to crack a Heineken for this one. Trying to solve a shortest path problem with a text file.
My research took me to: link text link text pretty good information for my algorithm
Basically, I'm trying to start out at 'S' for start and quit at 'F' for finish or return no solution message
the two self styled tests or text files being considered
##########
#S #
###### #
#F #
##########
and non-solvable
##########
#S #
##########
#F #
##########
My problem is I'm having trouble writing a pathfinding def for this and returning it to the custom print def. Below is the code for the whole darn thing. As well as my first attempt at it. Spaces are free to be a path. Walls are defined as any other key but the space bar. Also be warned my while loop for quiting will return an error, I'm working that out right now.
puts "enter a file name (example maze1.mz) PRESS ENTER"
filename = gets.chomp.to_s
while filename != 'quit' do
def read_maze( filename )
local_maze = []
mz_file = File.open(filename,"r")
while ! mz_file.eof? do
line = mz_file.gets.chomp
local_maze << line.scan(/./)
end
mz_file.close
return local_maze
end
def solve_maze(row, column, blank_spot, blank_spot_not ) #parse throug args
#from http://en.wikipedia.org/wiki/Maze
#from http://en.wikipedia.org/wiki/Pathfinding
return unless self[column][row] #condition for its full
return if self[column][row] != space #condition for printable spot
return if self[column][row] == blank_spot_not #can't use this spot
return if self[column][row]== full or f encountered
#maze is full can't solve or finished
self[column][row] = blank_spot_not
#direction up down left right
solve_maze(column+1, row, space, blank_spot_not) #right
solve_maze(column-1, row, space, blank_spot_not) #left
solve_maze(column, row+1, space, blank_spot_not) #up
solve_maze(column, row-1, space, blank_spot_not) #down
end
def print_maze( maze )
maze.each {|row|
puts row.join('')
}
end
maze = read_maze(filename)
print_maze(maze)
solve_maze(maze)
print_maze(maze)
puts "would you like to further drive yourself nuts with this maze solver"
filename = gets.chomp.to_s
maze = read_maze(filename)
print_maze(maze)
end
puts "you have quit"