Skip to content

Instantly share code, notes, and snippets.

@RLGGHC

RLGGHC/maze.rb Secret

Forked from pranavsingh/maze.rb
Created January 8, 2010 04:29
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 RLGGHC/bf943806ce5af0f7f934 to your computer and use it in GitHub Desktop.
Save RLGGHC/bf943806ce5af0f7f934 to your computer and use it in GitHub Desktop.
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
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment