-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamaze.rb
79 lines (66 loc) · 1.71 KB
/
amaze.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
75
76
77
78
79
# A call to 'ruby maze.rb [WIDTH] [HEIGHT]' should be all that is needed for
# basic use.
class Maze
DIRECTIONS = [:n, :s, :e, :w]
class Room
def initialize
@walls = {n: true, s: true, e: true, w: true}
@visited = false
end
def enter(direction)
@walls[direction] = false if @walls[direction]
@visited = true
end
def leave(direction)
@walls[direction] = false if @walls[direction]
end
def visited?(direction)
@visited
end
private
def invert_direction(direction)
case direction
when :n
return :s
when :s
return :n
when :e
return :w
when :w
return :e
end
end
end
def initialize(width ,height)
@width = width
@height = height
@state = Array.new(x) { Array.new(y) { Room.new } }
@x = (0..x-1);
@y = (0..y-1);
end
def generate
@state[@x][@y].enter(DIRECTIONS.sample)
end
def visit(x, y)
possible_directions = [:n, :s, :e, :w].shuffle
possible_directions.each do |direction|
new_x, new_y = position_from_direction direction
# Check that we are still inside the maze
unless new_x >= @width or new_y >= @height or new_x < 0 or new_y < 0
# Only visit each room once
unless @state[new_x][new_y].visited?
@state[x][y].leave direction
@state[new_x][new_y].enter direction
end
end
end
def position_from_direction(direction)
height_modifier = {n: -1, s: +1, e: 0, w: 0}
width_modifier = {n: 0, s: 0, e: +1, w: -1}
return @x+width_modifier[direction], @y+height_modifier[direction]
end
def to_s
@state.inspect
end
end
p Maze.new(ARGV[0].to_i,ARGV[1].to_i)