Guest User

Untitled

a guest
Jul 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. class Map
  2. def initialize width, height
  3. @values = Array.new(width){ Array.new(height){ nil } }
  4. end
  5.  
  6. def [](x, y)
  7. @values[x][y]
  8. end
  9.  
  10. def []=(x, y, value)
  11. @values[x][y] = value
  12. end
  13. end
  14.  
  15. class Maze
  16. def initialize config={}
  17. @width = config[:width] || 100
  18. @height = config[:height] || 100
  19. @num_runners = config[:num_runners] || 5
  20. @dead_end_chance = config[:dead_end_chance] || 0.05
  21.  
  22. case Random.rand
  23. when 0..0.25
  24. @start_index = [0, Random.rand(@height)]
  25. when 0.25..0.5
  26. @start_index = [@width - 1, Random.rand(@height)]
  27. when 0.5..0.75
  28. @start_index = [Random.rand(@width), 0]
  29. when 0.75..1
  30. @start_index = [Random.rand(@width), @height - 1]
  31. end
  32. end
  33.  
  34. def generate
  35. @map = Map.new(@width, @height)
  36. @map[*@start_index] = :empty
  37. runners = [@start_index]
  38. while (! runners.empty?)
  39. current = runners.shift
  40. nixt = self.choose_direction current
  41. while runners.length < @num_runners && nixt != nil
  42. # if Random.rand < @dead_end_chance
  43. # @map[*nixt] = :wall
  44. # else
  45. @map[*nixt] = :empty
  46. runners << nixt
  47. # end
  48. nixt = self.choose_direction current
  49. end
  50. self.neighbours(current).each do |point|
  51. @map[*point] = :wall
  52. end
  53. end
  54. @map[*current] = :treasure
  55. end
  56.  
  57. def choose_direction point
  58. n = self.neighbours point
  59. n.empty? ? nil : n[Random.rand(n.length)]
  60. end
  61.  
  62. def all_neighbours point
  63. [
  64. [point[0] - 1, point[1] ],
  65. [point[0] - 1, point[1] + 1],
  66. [point[0] - 1, point[1] - 1],
  67. [point[0] + 1, point[1] ],
  68. [point[0] + 1, point[1] + 1],
  69. [point[0] + 1, point[1] - 1],
  70. [point[0] , point[1] - 1],
  71. [point[0] , point[1] + 1]
  72. ].select {|point| point[0] > 0 and point[0] < @width and point[1] > 0 and point[1] < @height }
  73. end
  74.  
  75. def horiz_neighbours point
  76. [
  77. [point[0] - 1, point[1] ],
  78. [point[0] + 1, point[1] ],
  79. ].select {|point| point[0] > 0 and point[0] < @width }
  80. end
  81.  
  82. def four_neighbours point
  83. [
  84. [point[0] - 1, point[1] ],
  85. [point[0] + 1, point[1] ],
  86. [point[0] , point[1] - 1],
  87. [point[0] , point[1] + 1]
  88. ].select {|point| point[0] > 0 and point[0] < @width and point[1] > 0 and point[1] < @height }
  89. end
  90.  
  91.  
  92. def neighbours point
  93. [
  94. [point[0] - 1, point[1]],
  95. [point[0] + 1, point[1]],
  96. [point[0], point[1] - 1],
  97. [point[0], point[1] + 1]
  98. ].select {|point| point[0] > 1 and point[0] < @width - 1 and point[1] > 1 and point[1] < @height - 1 and @map[*point] == nil }
  99. end
  100.  
  101. def determine_wall_char x, y
  102. if all_neighbours([x,y]).map{|p|@map[*p]}&[:empty, :treasure] == []
  103. ' '
  104. else
  105. case (four_neighbours([x,y]).map{|p|@map[*p]}.select{|y|[:empty,:treasure].include? y}).length
  106. when 0, 3, 4
  107. '+'
  108. when 1
  109. case (horiz_neighbours([x,y]).map{|p|@map[*p]}.select{|y|[:empty,:treasure].include? y}).length
  110. when 0
  111. '-'
  112. when 1
  113. '|'
  114. end
  115. when 2
  116. case (horiz_neighbours([x,y]).map{|p|@map[*p]}.select{|y|[:empty,:treasure].include? y}).length
  117. when 0
  118. '-'
  119. when 1
  120. '+'
  121. when 2
  122. '|'
  123. end
  124. else
  125. '?'
  126. end
  127. end
  128. end
  129.  
  130. def determine_char x, y
  131. case @map[x,y]
  132. when :treasure
  133. '#'
  134. when :empty
  135. ' '
  136. when :wall, nil
  137. determine_wall_char x, y
  138. else
  139. '?'
  140. end
  141. end
  142.  
  143. def draw
  144. @height.times do |y|
  145. @width.times do |x|
  146. print determine_char x, y
  147. end
  148. puts
  149. end
  150. end
  151. end
  152.  
  153. if $0 == __FILE__
  154. if ARGV.length != 2
  155. puts "Usage: ruby #{$0} <width> <height>"
  156. exit -1
  157. end
  158. maze = Maze.new :width => ARGV[0].to_i, :height => ARGV[1].to_i
  159. maze.generate
  160. maze.draw
  161. end
Add Comment
Please, Sign In to add comment