diff options
author | Nat Lasseter <user@4574.co.uk> | 2019-09-18 01:48:35 +0100 |
---|---|---|
committer | Nat Lasseter <user@4574.co.uk> | 2019-09-18 01:48:35 +0100 |
commit | 5da7fa5f8d0cbbf73edd3902de96c720f399de53 (patch) | |
tree | 9b9d3f26a0dbd7b60a927f6f12620de513bbaf48 /lib/maze.rb |
Diffstat (limited to 'lib/maze.rb')
-rw-r--r-- | lib/maze.rb | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/maze.rb b/lib/maze.rb new file mode 100644 index 0000000..4001bea --- /dev/null +++ b/lib/maze.rb @@ -0,0 +1,63 @@ +class Maze + def initialize(w, h, sx = 0, sy = 0, tx = 0, ty = 0) + @width = w + @height = h + @start = [sx, sy] + @target = [tx, ty] + @blocks = [] + end + + attr_reader :width, :height, :start, :target + + def generate_random(t = -1) + @start = [rand(@width), rand(@height)] + @target = [rand(@width), rand(@height)] + + t = rand(100) if t == -1 + t.times do + x = rand(@width) + y = rand(@height) + w = [rand(5), @width - x].min + h = [rand(5), @height - y].min + @blocks << [x, y, w, h] + end + + @blocks.reject! do |blk| + sx = @start[0] + sy = @start[1] + tx = @target[0] + ty = @target[1] + bx1 = blk[0] + by1 = blk[1] + bx2 = blk[0] + blk[2] + by2 = blk[1] + blk[3] + + sx >= bx1 && sx < bx2 && sy >= by1 && sy < by2 || + tx >= bx1 && tx < bx2 && ty >= by1 && ty < by2 + end + + self + end + + def blocked(x, y) + @blocks.any? do |blk| + bx1 = blk[0] + by1 = blk[1] + bx2 = blk[0] + blk[2] + by2 = blk[1] + blk[3] + + x >= bx1 && x < bx2 && y >= by1 && y < by2 + end + end + + def walkable(x,y) + !blocked(x,y) + end + + def mml + "#{@width} #{@height}\n" + + "#{@start[0]} #{@start[1]}\n" + + "#{@target[0]} #{@target[1]}\n" + + @blocks.map{|b| b.join(" ")}.join("\n") + end +end |