1-
2-
31function strReverse ( str ) {
42 return str . split ( '' ) . reverse ( ) . join ( '' ) ;
53}
@@ -48,7 +46,6 @@ class LayoutTile {
4846 }
4947}
5048
51-
5249const tiles = [
5350 new LayoutTile ( 'BRRB' , 'BR' , 'RR' , 'RB' , 'BB' ) ,
5451 new LayoutTile ( 'BRRR' , 'BR' , 'RR' , 'RR' , 'RB' ) ,
@@ -76,11 +73,10 @@ const tiles = [
7673
7774const colors = {
7875 //'P': [84, 186, 185],
79- 'P' : [ 158 , 210 , 198 ] ,
80- 'R' : [ 255 , 255 , 255 ] ,
81- 'B' : [ 233 , 218 , 193 ] ,
82- }
83-
76+ P : [ 158 , 210 , 198 ] ,
77+ R : [ 255 , 255 , 255 ] ,
78+ B : [ 233 , 218 , 193 ] ,
79+ } ;
8480
8581class TileList {
8682 constructor ( tiles ) {
@@ -90,25 +86,33 @@ class TileList {
9086
9187 evaluateTileBellow ( otherTiles ) {
9288 let len = this . tiles . length ;
93- this . tiles = this . tiles . filter ( option => otherTiles . some ( ( tile => option . canHaveBellow ( tile ) ) ) ) ;
89+ this . tiles = this . tiles . filter ( ( option ) =>
90+ otherTiles . some ( ( tile ) => option . canHaveBellow ( tile ) )
91+ ) ;
9492 this . changed = this . tiles . length !== len ;
9593 }
9694
9795 evaluateTileAbove ( otherTiles ) {
9896 let len = this . tiles . length ;
99- this . tiles = this . tiles . filter ( option => otherTiles . some ( ( tile => option . canHaveAbove ( tile ) ) ) ) ;
97+ this . tiles = this . tiles . filter ( ( option ) =>
98+ otherTiles . some ( ( tile ) => option . canHaveAbove ( tile ) )
99+ ) ;
100100 this . changed = this . tiles . length !== len ;
101101 }
102102
103103 evaluateTileLeft ( otherTiles ) {
104104 let len = this . tiles . length ;
105- this . tiles = this . tiles . filter ( option => otherTiles . some ( ( tile => option . canHaveLeft ( tile ) ) ) ) ;
105+ this . tiles = this . tiles . filter ( ( option ) =>
106+ otherTiles . some ( ( tile ) => option . canHaveLeft ( tile ) )
107+ ) ;
106108 this . changed = this . tiles . length !== len ;
107109 }
108110
109111 evaluateTileRight ( otherTiles ) {
110112 let len = this . tiles . length ;
111- this . tiles = this . tiles . filter ( option => otherTiles . some ( ( tile => option . canHaveRight ( tile ) ) ) ) ;
113+ this . tiles = this . tiles . filter ( ( option ) =>
114+ otherTiles . some ( ( tile ) => option . canHaveRight ( tile ) )
115+ ) ;
112116 this . changed = this . tiles . length !== len ;
113117 }
114118
@@ -127,8 +131,7 @@ class TileList {
127131 draw ( x , y ) {
128132 //stroke(0);
129133 //rect(x * 20, y * 20, 20, 20);
130- if ( ! this . converged )
131- return ;
134+ if ( ! this . converged ) return ;
132135
133136 const tile = this . tile ;
134137 noStroke ( ) ;
@@ -151,7 +154,7 @@ class TileList {
151154 }
152155
153156 clone ( ) {
154- return new TileList ( this . tiles . map ( tile => tile . clone ( ) ) ) ;
157+ return new TileList ( this . tiles . map ( ( tile ) => tile . clone ( ) ) ) ;
155158 }
156159}
157160
@@ -163,8 +166,7 @@ export class Layout {
163166 constructor ( dimX , dimY ) {
164167 this . options = [ ] ;
165168 for ( let i = 0 ; i < tiles . length ; i ++ ) {
166- for ( let r = 0 ; r < 4 ; r ++ )
167- this . options . push ( tiles [ i ] . rotate ( ) . clone ( ) ) ;
169+ for ( let r = 0 ; r < 4 ; r ++ ) this . options . push ( tiles [ i ] . rotate ( ) . clone ( ) ) ;
168170 }
169171
170172 this . init ( dimX , dimY ) ;
@@ -182,7 +184,7 @@ export class Layout {
182184 reinit ( ) {
183185 for ( let i = 0 ; i < this . arr . length ; i ++ ) {
184186 for ( let j = 0 ; j < this . arr [ i ] . length ; j ++ ) {
185- this . arr [ i ] [ j ] = new TileList ( this . options . map ( option => option ) ) ;
187+ this . arr [ i ] [ j ] = new TileList ( this . options . map ( ( option ) => option ) ) ;
186188 }
187189 }
188190
@@ -192,9 +194,8 @@ export class Layout {
192194 //this.propagate(coords);
193195 }
194196
195-
196197 generateStep ( ) {
197- if ( this . status === FAILED ) {
198+ if ( this . status === FAILED ) {
198199 this . reinit ( ) ;
199200 }
200201
@@ -209,7 +210,6 @@ export class Layout {
209210 return this . status === CONVERGED ;
210211 }
211212
212-
213213 generate ( ) {
214214 while ( ! this . converged ) {
215215 this . generateStep ( ) ;
@@ -233,10 +233,10 @@ export class Layout {
233233
234234 printArrState ( ) {
235235 const state = [ ] ;
236- for ( let i = 0 ; i < this . arr . length ; i ++ ) {
236+ for ( let i = 0 ; i < this . arr . length ; i ++ ) {
237237 const row = [ ] ;
238- for ( let j = 0 ; j < this . arr [ i ] . length ; j ++ ) {
239- row . push ( this . arr [ i ] [ j ] . tiles . map ( tile => tile . code ) ) ;
238+ for ( let j = 0 ; j < this . arr [ i ] . length ; j ++ ) {
239+ row . push ( this . arr [ i ] [ j ] . tiles . map ( ( tile ) => tile . code ) ) ;
240240 }
241241 state . push ( row ) ;
242242 }
@@ -254,8 +254,7 @@ export class Layout {
254254 if ( coords . y > 0 ) {
255255 const above = this . arr [ coords . x ] [ coords . y - 1 ] ;
256256 above . evaluateTileBellow ( tilesList . tiles ) ;
257- if ( above . failed )
258- return ;
257+ if ( above . failed ) return ;
259258 if ( above . changed && ! inQueue . has ( above ) ) {
260259 queue . push ( { x : coords . x , y : coords . y - 1 } ) ;
261260 inQueue . add ( above ) ;
@@ -265,9 +264,8 @@ export class Layout {
265264 if ( coords . y < this . arr [ coords . x ] . length - 1 ) {
266265 const below = this . arr [ coords . x ] [ coords . y + 1 ] ;
267266 below . evaluateTileAbove ( tilesList . tiles ) ;
268- if ( below . failed )
269- return ;
270-
267+ if ( below . failed ) return ;
268+
271269 if ( below . changed && ! inQueue . has ( below ) ) {
272270 queue . push ( { x : coords . x , y : coords . y + 1 } ) ;
273271 inQueue . add ( below ) ;
@@ -277,8 +275,7 @@ export class Layout {
277275 if ( coords . x > 0 ) {
278276 const left = this . arr [ coords . x - 1 ] [ coords . y ] ;
279277 left . evaluateTileRight ( tilesList . tiles ) ;
280- if ( left . failed )
281- return ;
278+ if ( left . failed ) return ;
282279
283280 if ( left . changed && ! inQueue . has ( left ) ) {
284281 queue . push ( { x : coords . x - 1 , y : coords . y } ) ;
@@ -289,13 +286,12 @@ export class Layout {
289286 if ( coords . x < this . arr . length - 1 ) {
290287 const right = this . arr [ coords . x + 1 ] [ coords . y ] ;
291288 right . evaluateTileLeft ( tilesList . tiles ) ;
292- if ( right . failed )
293- return ;
289+ if ( right . failed ) return ;
294290
295291 if ( right . changed && ! inQueue . has ( right ) ) {
296292 queue . push ( { x : coords . x + 1 , y : coords . y } ) ;
297293 inQueue . add ( right ) ;
298- } ;
294+ }
299295 }
300296 }
301297 }
@@ -307,8 +303,7 @@ export class Layout {
307303 return FAILED ;
308304 }
309305
310- if ( ! this . arr [ i ] [ j ] . converged )
311- return NOTCOVERGED ;
306+ if ( ! this . arr [ i ] [ j ] . converged ) return NOTCOVERGED ;
312307 }
313308 }
314309 return CONVERGED ;
@@ -340,7 +335,6 @@ export class Layout {
340335 }
341336}
342337
343-
344338function validSize ( index , size ) {
345339 return index >= 0 && index < size ;
346340}
@@ -363,30 +357,29 @@ function addRiver(grid) {
363357 grid [ pos [ 0 ] + 3 ] [ pos [ 1 ] ] = 'W' ;
364358 //TODO check boundries
365359
366-
367360 next = [ pos [ 0 ] , pos [ 1 ] + 1 ] ;
368-
361+
369362 if ( stp % 8 === 0 ) {
370363 do {
371364 const moveDir = Math . round ( Math . random ( ) ) * 2 - 1 ;
372365 next = [ pos [ 0 ] , pos [ 1 ] ] ;
373366 next [ 0 ] += moveDir ;
374- } while ( ! validSize ( next [ 0 ] , sizeX ) || ! validSize ( next [ 1 ] , sizeY ) ) ;
367+ } while ( ! validSize ( next [ 0 ] , sizeX ) || ! validSize ( next [ 1 ] , sizeY ) ) ;
375368 }
376369
377370 pos = next ;
378371 stp ++ ;
379- } while ( ! reachedBorder ( pos [ 0 ] , sizeX ) && ! reachedBorder ( pos [ 1 ] , sizeY ) ) ;
372+ } while ( ! reachedBorder ( pos [ 0 ] , sizeX ) && ! reachedBorder ( pos [ 1 ] , sizeY ) ) ;
380373}
381374
382375function addBridgeAlongYAxis ( grid ) {
383376 let lastBefore , firstAfter , waterEncoutered ;
384- for ( let i = Math . min ( 5 , grid [ 0 ] . length - 1 ) ; i < grid [ 0 ] . length ; i ++ ) {
377+ for ( let i = Math . min ( 5 , grid [ 0 ] . length - 1 ) ; i < grid [ 0 ] . length ; i ++ ) {
385378 lastBefore = grid [ 0 ] [ i ] ;
386379 waterEncoutered = false ;
387380 firstAfter = null ;
388-
389- for ( let j = 0 ; j < grid . length ; j ++ ) {
381+
382+ for ( let j = 0 ; j < grid . length ; j ++ ) {
390383 if ( grid [ j ] [ i ] == 'W' ) {
391384 waterEncoutered = true ;
392385 } else if ( ! waterEncoutered ) {
@@ -397,18 +390,17 @@ function addBridgeAlongYAxis(grid) {
397390 }
398391 }
399392
400- if ( waterEncoutered && ( lastBefore == 'R' && firstAfter == 'R' ) ) {
401- for ( let j = 0 ; j < grid . length ; j ++ ) {
393+ if ( waterEncoutered && lastBefore == 'R' && firstAfter == 'R' ) {
394+ for ( let j = 0 ; j < grid . length ; j ++ ) {
402395 if ( grid [ j ] [ i ] == 'W' ) {
403396 grid [ j ] [ i ] = 'M' ;
404- }
397+ }
405398 }
406399 return ;
407400 }
408401 }
409402}
410403
411-
412404export function generateWithRiver ( x , y ) {
413405 const layout = new Layout ( x , y ) ;
414406 layout . generate ( ) ;
@@ -417,4 +409,4 @@ export function generateWithRiver(x, y) {
417409 addBridgeAlongYAxis ( grid ) ;
418410 addBridgeAlongYAxis ( grid ) ;
419411 return grid ;
420- }
412+ }
0 commit comments