Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View leonardobessa.rb
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
class Maze
attr_reader :start_point, :end_point
def initialize map
@cell_map = map.lines.map { |b| b.scan(/./)}
map.lines.each_with_index do |line,line_index|
@start_point ||= [line_index,line.index('A')] if line.include?('A')
@end_point ||= [line_index,line.index('B')] if line.include?('B')
break if @start_point and @end_point
end
end
 
def solvable?
@steps = 1
origins = [start_point]
until origins.empty?
selected_neighbors = []
origins.each do |(x,y)|
neighbors = [[x-1,y],[x+1,y],[x,y-1],[x,y+1]]
return true if neighbors.include? @end_point
selected_neighbors += neighbors.find_all do |(a,b)|
@cell_map[a][b] = @steps if @cell_map[a][b] == " "
end
end
origins = selected_neighbors
@steps += 1
end
false
end
 
def steps
solvable? ? @steps : 0
end
 
end
View leonardobessa.rb
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
require 'test/unit'
require 'leonardobessa'
 
MAZE1 = %{#####################################
# # # #A # # #
# # # # # # ####### # ### # ####### #
# # # # # # # # #
# ##### # ################# # #######
# # # # # # # # #
##### ##### ### ### # ### # # # # # #
# # # # # # B# # # # # #
# # ##### ##### # # ### # # ####### #
# # # # # # # # # # # #
# ### ### # # # # ##### # # # ##### #
# # # # # # #
#####################################}
# Maze 1 should SUCCEED
 
MAZE2 = %{#####################################
# # # # # #
# ### ### # ########### ### # ##### #
# # # # # # # # # #
# # ###A##### # # # # ### ###########
# # # # # # # # #
####### # ### ####### # ### ####### #
# # # # # # # #
# ####### # # # ####### # ##### # # #
# # # # # # # # # # #
# ##### # # ##### ######### # ### # #
# # # # #B#
#####################################}
# Maze 2 should SUCCEED
 
MAZE3 = %{#####################################
# # # # #
# ### # ####### # # # ############# #
# # # # # # # # # #
### ##### ### ####### # ##### ### # #
# # # # A # # # # #
# ######### ##### # ####### ### ### #
# ### # # # # #
# ### ### ####### ####### # # # # ###
# # # # # #B# # # # # # #
# # # ##### ### # # # # ### # ##### #
# # # # # #
#####################################}
# Maze 3 should FAIL
 
SIMPLE_MAZE = %{#####
# #
# A #
# #
#####%}
class MazeTest < Test::Unit::TestCase
def test_start_point
assert_equal [2,2],Maze.new(SIMPLE_MAZE).start_point
end
 
def test_good_mazes
assert_equal true, Maze.new(MAZE1).solvable?
assert_equal true, 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
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.