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
|
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
|