secret
Created — forked from pranavsingh/maze.rb

  • Download Gist
maze.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
class Maze
def initialize(maze)
raise ArgumentError, "Invalid maze." if maze.nil? or maze.empty? or not maze.include?("\n")
@maze_width = maze.index("\n")
@maze = maze.gsub("\n", '').split(//) # The maze is represented as an array.
end
 
def solvable?
path_finder(@maze.index('A'))
end
 
protected
 
# Recursively determines if from _here_ we can reach the end of the maze
# by looking at the adjacent cells (Up, Down, Left and Right). Moreover, cells that have
# already been visited are tracked so we don't repeat ourselfs.
#
# here: starting index in the maze
# visited_cells: the indicies that were already visited during the path search.
def path_finder(here, visited_cells=[])
return false if out_of_bounds?(here) or visited_cells.include?(here) or @maze[here] == "#"
return true if @maze[here] == 'B'
visited_cells << here
return (path_finder(here-1, visited_cells) or path_finder(here+1, visited_cells) or path_finder(here-@maze_width, visited_cells) or path_finder(here+@maze_width, visited_cells))
end
 
# Determine if it's a valid location in the maze.
def out_of_bounds?(index)
return (index >= @maze.length or index < 0) ? true : false
end
end
test_maze.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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
require 'test/unit'
require 'maze'
 
MAZE1 = %{#####################################
# # # #A # # #
# # # # # # ####### # ### # ####### #
# # # # # # # # #
# ##### # ################# # #######
# # # # # # # # #
##### ##### ### ### # ### # # # # # #
# # # # # # B# # # # # #
# # ##### ##### # # ### # # ####### #
# # # # # # # # # # # #
# ### ### # # # # ##### # # # ##### #
# # # # # # #
#####################################}
# Maze 1 should SUCCEED
 
MAZE2 = %{#####################################
# # # # # #
# ### ### # ########### ### # ##### #
# # # # # # # # # #
# # ###A##### # # # # ### ###########
# # # # # # # # #
####### # ### ####### # ### ####### #
# # # # # # # #
# ####### # # # ####### # ##### # # #
# # # # # # # # # # #
# ##### # # ##### ######### # ### # #
# # # # #B#
#####################################}
# Maze 2 should SUCCEED
 
MAZE3 = %{#####################################
# # # # #
# ### # ####### # # # ############# #
# # # # # # # # # #
### ##### ### ####### # ##### ### # #
# # # # A # # # # #
# ######### ##### # ####### ### ### #
# ### # # # # #
# ### ### ####### ####### # # # # ###
# # # # # #B# # # # # # #
# # # ##### ### # # # # ### # ##### #
# # # # # #
#####################################}
# Maze 3 should FAIL
 
class MazeTest < Test::Unit::TestCase
def test_good_mazes
assert Maze.new(MAZE1).solvable?
assert Maze.new(MAZE2).solvable?
end
 
def test_bad_mazes
assert_equal false, Maze.new(MAZE3).solvable?
end
 
# def test_maze_steps
# assert_equal 44, Maze.new(MAZE1).steps
# assert_equal 75, Maze.new(MAZE2).steps
# assert_equal 0, Maze.new(MAZE3).steps
# end
 
def test_empty_string
assert_raise ArgumentError do
Maze.new('')
end
end
 
def test_nil_string
assert_raise ArgumentError do
Maze.new(nil)
end
end
 
def test_one_line_maze
assert_raise ArgumentError do
Maze.new("AB")
end
end
 
def test_correct_string
assert_nothing_raised ArgumentError do
Maze.new("AB\n")
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.