1
1
let tiles = [ ] ;
2
2
const tileImages = [ ] ;
3
-
4
3
let grid = [ ] ;
5
4
6
5
const DIM = 25 ;
@@ -70,15 +69,15 @@ function setup() {
70
69
function startOver ( ) {
71
70
// Initialize grid with cells
72
71
for ( let i = 0 ; i < DIM * DIM ; i ++ ) {
73
- grid [ i ] = new Cell ( tiles . length ) ;
72
+ grid [ i ] = new Cell ( i % DIM , floor ( i / DIM ) , tiles . length ) ;
74
73
}
75
74
}
76
75
77
76
function checkValid ( arr , valid ) {
78
77
// Remove invalid options from array
79
78
for ( let i = arr . length - 1 ; i >= 0 ; i -- ) {
80
79
let element = arr [ i ] ;
81
- if ( ! valid . includes ( element ) ) {
80
+ if ( ! valid . has ( element ) ) {
82
81
arr . splice ( i , 1 ) ;
83
82
}
84
83
}
@@ -92,15 +91,7 @@ function draw() {
92
91
const h = height / DIM ;
93
92
for ( let j = 0 ; j < DIM ; j ++ ) {
94
93
for ( let i = 0 ; i < DIM ; i ++ ) {
95
- let cell = grid [ i + j * DIM ] ;
96
- if ( cell . collapsed ) {
97
- let index = cell . options [ 0 ] ;
98
- image ( tiles [ index ] . img , i * w , j * h , w , h ) ;
99
- } else {
100
- noFill ( ) ;
101
- stroke ( 51 ) ;
102
- rect ( i * w , j * h , w , h ) ;
103
- }
94
+ grid [ posIdx ( i , j ) ] . draw ( w , h ) ;
104
95
}
105
96
}
106
97
@@ -134,60 +125,50 @@ function draw() {
134
125
}
135
126
cell . options = [ pick ] ;
136
127
137
- // Update grid with new options
138
- const nextGrid = [ ] ;
139
- for ( let j = 0 ; j < DIM ; j ++ ) {
140
- for ( let i = 0 ; i < DIM ; i ++ ) {
141
- let index = i + j * DIM ;
142
- if ( grid [ index ] . collapsed ) {
143
- nextGrid [ index ] = grid [ index ] ;
144
- } else {
145
- let options = new Array ( tiles . length ) . fill ( 0 ) . map ( ( x , i ) => i ) ;
146
- // Look up
147
- if ( j > 0 ) {
148
- let up = grid [ i + ( j - 1 ) * DIM ] ;
149
- let validOptions = [ ] ;
150
- for ( let option of up . options ) {
151
- let valid = tiles [ option ] . down ;
152
- validOptions = validOptions . concat ( valid ) ;
153
- }
154
- checkValid ( options , validOptions ) ;
155
- }
156
- // Look right
157
- if ( i < DIM - 1 ) {
158
- let right = grid [ i + 1 + j * DIM ] ;
159
- let validOptions = [ ] ;
160
- for ( let option of right . options ) {
161
- let valid = tiles [ option ] . left ;
162
- validOptions = validOptions . concat ( valid ) ;
163
- }
164
- checkValid ( options , validOptions ) ;
165
- }
166
- // Look down
167
- if ( j < DIM - 1 ) {
168
- let down = grid [ i + ( j + 1 ) * DIM ] ;
169
- let validOptions = [ ] ;
170
- for ( let option of down . options ) {
171
- let valid = tiles [ option ] . up ;
172
- validOptions = validOptions . concat ( valid ) ;
173
- }
174
- checkValid ( options , validOptions ) ;
175
- }
176
- // Look left
177
- if ( i > 0 ) {
178
- let left = grid [ i - 1 + j * DIM ] ;
179
- let validOptions = [ ] ;
180
- for ( let option of left . options ) {
181
- let valid = tiles [ option ] . right ;
182
- validOptions = validOptions . concat ( valid ) ;
183
- }
184
- checkValid ( options , validOptions ) ;
185
- }
128
+ grid = optimizedNextGrid ( cell ) ;
129
+ }
130
+
131
+ // propagate options from src to dest. If dest is above src, dir == UP.
132
+ function propagate ( src , dest , dir ) {
133
+ let oldLen = dest . options . length ;
134
+ checkValid ( dest . options , src . validOptions ( dir ) ) ;
135
+ return oldLen != dest . options . length ;
136
+ }
186
137
187
- nextGrid [ index ] = new Cell ( options ) ;
138
+ function optimizedNextGrid ( pick ) {
139
+ let touched = [ posIdx ( pick . i , pick . j ) ] ;
140
+
141
+ while ( touched . length > 0 ) {
142
+ let cell = grid [ touched . pop ( ) ] ;
143
+
144
+ let check = function ( i , j , dir ) {
145
+ const idx = posIdx ( i , j ) ;
146
+ if ( propagate ( cell , grid [ idx ] , dir ) ) {
147
+ if ( ! touched . includes ( idx ) ) {
148
+ touched . push ( idx ) ;
149
+ }
188
150
}
151
+ } ;
152
+
153
+ if ( cell . i > 0 ) {
154
+ check ( cell . i - 1 , cell . j , LEFT ) ;
155
+ }
156
+
157
+ if ( cell . i < DIM - 1 ) {
158
+ check ( cell . i + 1 , cell . j , RIGHT ) ;
159
+ }
160
+
161
+ if ( cell . j > 0 ) {
162
+ check ( cell . i , cell . j - 1 , UP ) ;
163
+ }
164
+
165
+ if ( cell . j < DIM - 1 ) {
166
+ check ( cell . i , cell . j + 1 , DOWN ) ;
189
167
}
190
168
}
169
+ return grid ;
170
+ }
191
171
192
- grid = nextGrid ;
172
+ function posIdx ( i , j ) {
173
+ return i + j * DIM ;
193
174
}
0 commit comments