@@ -23,11 +23,17 @@ def initialize(tiles, width, height)
23
23
@width = width . to_i
24
24
@height = height . to_i
25
25
26
- @grid = Array . new ( width ) { |x | Array . new ( height ) { |y | Cell . new ( x , y , @tiles . shuffle ) } }
27
- @uncollapsed_cells_grid = @grid . flatten . reject ( &:collapsed )
26
+ # @grid = Array.new(width) { |x| Array.new(height) { |y| Cell.new(x, y, @tiles.shuffle) } }
27
+ @grid = [ ]
28
+ @height . times { |y | @width . times { |x | @grid << Cell . new ( x , y , @tiles . shuffle ) } }
29
+ @uncollapsed_cells_grid = @grid . reject ( &:collapsed )
28
30
@max_entropy = @tiles . length
29
31
end
30
32
33
+ def grid_cell ( x , y )
34
+ @grid [ @width * y + x ]
35
+ end
36
+
31
37
def complete?
32
38
@uncollapsed_cells_grid . empty?
33
39
end
@@ -53,6 +59,8 @@ def iterate
53
59
end
54
60
55
61
def prepend_empty_row
62
+ raise
63
+
56
64
x = 0
57
65
while x < @width
58
66
@grid [ x ] . shift
@@ -79,18 +87,18 @@ def random_cell
79
87
def generate_grid
80
88
x = 0
81
89
result = [ ]
82
- lx = @grid . length
83
- while x < lx
90
+
91
+ while x < @width
84
92
rx = result [ x ] = [ ]
85
93
y = 0
86
- pgx = @grid [ x ]
87
- ly = pgx . length
88
- while y < ly
89
- rx [ y ] = pgx [ y ] . tile
94
+
95
+ while y < @height
96
+ rx [ y ] = grid_cell ( x , y ) . tile
90
97
y += 1
91
98
end
92
99
x += 1
93
100
end
101
+
94
102
result
95
103
end
96
104
@@ -110,7 +118,7 @@ def propagate(source_cell)
110
118
end
111
119
112
120
def evaluate_neighbor ( source_cell , evaluation_direction )
113
- neighbor_cell = source_cell . neighbors ( @grid ) [ evaluation_direction ]
121
+ neighbor_cell = source_cell . neighbors ( self ) [ evaluation_direction ]
114
122
return if neighbor_cell . nil? || neighbor_cell . collapsed
115
123
116
124
original_tile_count = neighbor_cell . tiles . length
@@ -122,23 +130,27 @@ def evaluate_neighbor(source_cell, evaluation_direction)
122
130
neighbor_tiles = neighbor_cell . tiles
123
131
sci = 0
124
132
scil = source_tiles . length
133
+
125
134
while sci < scil
126
135
source_tile = source_tiles [ sci ]
127
136
sci += 1
128
137
source_edge_hash = source_tile . send ( evaluation_direction )
129
138
nci = 0
130
139
ncil = neighbor_tiles . length
140
+
131
141
while nci < ncil
132
142
tile = neighbor_tiles [ nci ]
133
143
nci += 1
134
- next if new_tile_ids . has_key? ( tile . tileid )
144
+ next if new_tile_ids [ tile . tileid ]
145
+
135
146
tile_edge_hash = tile . send ( opposite_direction )
136
147
if tile_edge_hash == source_edge_hash
137
148
new_tile_ids [ tile . tileid ] = true
138
149
new_tiles << tile
139
150
end
140
151
end
141
152
end
153
+
142
154
neighbor_cell . tiles = new_tiles unless new_tiles . empty?
143
155
@uncollapsed_cells_grid . delete ( neighbor_cell ) if neighbor_cell . collapsed
144
156
0 commit comments