Guest User

Untitled

a guest
Sep 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. class Maze
  2. BREADCRUMBS = '.'
  3. WALKABLE = ' '
  4. WALL = 'X'
  5. DEAD_END = 'D'
  6.  
  7. attr_reader :map, :coords
  8.  
  9. def initialize(map)
  10. @map = map
  11. @coords = to_a
  12. end
  13.  
  14. def draw_path
  15. coords.each_with_index do |row, y|
  16. row.each_with_index do |icon, x|
  17. @y, @x = y, x
  18. walk
  19. end
  20. end
  21. end
  22.  
  23. def to_a
  24. rows.inject([]) do |board, row|
  25. board << row.split(//).inject([]) {|arr, icon| arr << icon}
  26. end
  27. end
  28.  
  29. def to_s
  30. coords.inject('') do |maze, row|
  31. maze << "#{row.join}\n"
  32. end.gsub(DEAD_END, ' ')
  33. end
  34.  
  35. def walk
  36. if current_position == WALKABLE
  37. coords[@y][@x] = walkable? ? BREADCRUMBS : DEAD_END
  38. end
  39. end
  40.  
  41. def rows
  42. @rows ||= map.split("\n")
  43. end
  44.  
  45. def width
  46. rows.first.size
  47. end
  48.  
  49. def walkable?
  50. nearby_coords.select do |position|
  51. [WALL, DEAD_END].include? get(position)
  52. end.size < 3
  53. end
  54.  
  55. def current_position
  56. get [@y, @x]
  57. end
  58.  
  59. def get(coord)
  60. coords[coord.first][coord.last]
  61. end
  62.  
  63. def nearby_coords
  64. [[@y-1, @x], [@y+1, @x], [@y, @x-1], [@y, @x+1]]
  65. end
  66. end
  67.  
  68.  
  69. require 'rspec'
  70.  
  71. map = <<-MAZE
  72. XXXXXXXXXXXX
  73. # XXX X o
  74. XXX X X X XX
  75. XX XX
  76. XXXXXXXXXXXX
  77. MAZE
  78.  
  79. solution = <<-MAZE
  80. XXXXXXXXXXXX
  81. #...XXX X..o
  82. XXX.X X X.XX
  83. XX .......XX
  84. XXXXXXXXXXXX
  85. MAZE
  86.  
  87. describe Maze do
  88. subject { Maze.new(map) }
  89.  
  90. it 'assings map' do
  91. subject.map.should == map
  92. end
  93.  
  94. describe '#rows' do
  95. it 'splits map in rows' do
  96. subject.rows.size.should == 5
  97. end
  98. end
  99.  
  100. describe '#width' do
  101. it 'returns the map width' do
  102. subject.width.should == 12
  103. end
  104. end
  105.  
  106. describe '#to_a' do
  107. it 'returns bidimensional array' do
  108. subject.to_a.should be_an(Array)
  109. subject.to_a.first.should be_an(Array)
  110. end
  111. end
  112.  
  113. describe '#to_s' do
  114. it 'returns a string' do
  115. subject.to_s.should be_a(String)
  116. end
  117.  
  118. context 'when path has not been walked yet' do
  119. it 'returns original map' do
  120. subject.to_s.should == map
  121. end
  122. end
  123. end
  124.  
  125. describe '#coords' do
  126. it 'returns the maze in array format' do
  127. subject.coords.should == subject.to_a
  128. end
  129. end
  130.  
  131. describe '#get' do
  132. it 'wraps array access to coords' do
  133. subject.get([1,2]).should == subject.coords[1][2]
  134. end
  135. end
  136.  
  137. describe '#current_position' do
  138. it 'should return the icon in the current position' do
  139. subject.instance_eval { @x, @y = 11, 1 }
  140. subject.current_position.should == 'o'
  141. end
  142. end
  143.  
  144. describe '#nearby_coords' do
  145. it 'returns an array of coordinates' do
  146. subject.instance_eval { @x, @y = 2, 1}
  147. subject.nearby_coords.should =~ [[0, 2], [2, 2], [1, 1], [1,3]]
  148. end
  149. end
  150.  
  151. describe '#walkable?' do
  152. context 'when the coord is surrounded by 3 or more walls/dead ends' do
  153. it 'is false' do
  154. subject.instance_eval {@x, @y = 2, 3}
  155. subject.should_not be_walkable
  156. end
  157. end
  158.  
  159. context 'when the coord is surrounded by less than 3 walls/dead ends' do
  160. it 'is true' do
  161. subject.instance_eval {@x, @y = 1, 1}
  162. subject.should be_walkable
  163. end
  164. end
  165. end
  166.  
  167. describe '#walk' do
  168. context 'when the coord is walkable' do
  169. it 'should change it to breadcrumbs icon' do
  170. subject.instance_eval { @x, @y = 1, 1 }
  171. subject.walk
  172. subject.get([1, 1]).should == '.'
  173. end
  174. end
  175. end
  176.  
  177. describe '#draw_path' do
  178. it 'should draw the path to exit' do
  179. subject.draw_path
  180. subject.to_s.should == solution
  181. end
  182. end
  183. end
  184.  
  185.  
  186. puts Maze.new(map).tap {|bc| bc.draw_path}.to_s
  187.  
  188.  
  189. require 'benchmark'
  190.  
  191. Benchmark.bm do |bm|
  192. bm.report 'Andrea Longhi' do
  193. 10000.times { Maze.new(map).draw_path }
  194. end
  195. end
Add Comment
Please, Sign In to add comment