11let tiles = [ ] ;
22const tileImages = [ ] ;
3-
43let grid = [ ] ;
54
65const DIM = 25 ;
@@ -70,15 +69,15 @@ function setup() {
7069function startOver ( ) {
7170 // Initialize grid with cells
7271 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 ) ;
7473 }
7574}
7675
7776function checkValid ( arr , valid ) {
7877 // Remove invalid options from array
7978 for ( let i = arr . length - 1 ; i >= 0 ; i -- ) {
8079 let element = arr [ i ] ;
81- if ( ! valid . includes ( element ) ) {
80+ if ( ! valid . has ( element ) ) {
8281 arr . splice ( i , 1 ) ;
8382 }
8483 }
@@ -92,15 +91,7 @@ function draw() {
9291 const h = height / DIM ;
9392 for ( let j = 0 ; j < DIM ; j ++ ) {
9493 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 ) ;
10495 }
10596 }
10697
@@ -134,60 +125,50 @@ function draw() {
134125 }
135126 cell . options = [ pick ] ;
136127
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+ }
186137
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+ }
188150 }
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 ) ;
189167 }
190168 }
169+ return grid ;
170+ }
191171
192- grid = nextGrid ;
172+ function posIdx ( i , j ) {
173+ return i + j * DIM ;
193174}
0 commit comments