@@ -66,7 +66,7 @@ const gridHeight = 13;
6666
6767const renderCanvas = ( ) => {
6868 const tileData = parseDataFromTextArea ( ) ;
69- if ( ! tileData [ 0 ] [ 0 ] ) return ;
69+ if ( tileData [ 0 ] [ 0 ] . length === 0 ) return ;
7070
7171 ctx . clearRect ( 0 , 0 , canvas . width , canvas . height ) ;
7272
@@ -82,7 +82,7 @@ const renderCanvas = () => {
8282 for ( let row = 0 ; row < gridHeight ; row ++ ) {
8383 for ( let column = 0 ; column < gridWidth ; column ++ ) {
8484 const { x, y } = getCoordFromLineIndex ( column , row ) ;
85- const [ index , rotation ] = [ ... tileData [ row ] [ column ] ] ;
85+ const [ index , rotation ] = tileData [ row ] [ column ] ;
8686 ctx . save ( ) ;
8787 ctx . translate ( x + tileSize / 2 , y + tileSize / 2 ) ;
8888 ctx . rotate ( rotation * Math . PI / 2 ) ;
@@ -120,7 +120,7 @@ const parseDataFromCondensed = () => {
120120 let index = 0 ;
121121 const data = [ [ ] ] ;
122122 while ( index < stringData . length ) {
123- data [ data . length - 1 ] . push ( ` ${ stringData [ index ] } ${ stringData [ index + 1 ] } ` ) ;
123+ data [ data . length - 1 ] . push ( [ stringData [ index ] , stringData [ index + 1 ] ] . map ( e => parseInt ( e ) ) ) ;
124124 index += 2 ;
125125 if ( index % ( 2 * gridWidth ) === 0 ) {
126126 data . push ( [ ] ) ;
@@ -131,17 +131,17 @@ const parseDataFromCondensed = () => {
131131
132132const writeDataToCondensed = ( inputData ) => {
133133 const condensedData = document . getElementById ( "condensed-data" ) ;
134- condensedData . value = inputData . map ( row => row . join ( "" ) ) . join ( "" ) ;
134+ condensedData . value = inputData . map ( row => row . map ( e => e . join ( "" ) ) . join ( "" ) ) . join ( "" ) ;
135135} ;
136136
137137const parseDataFromTextArea = ( ) => {
138- const elem = document . getElementById ( "data" ) ;
139- return elem . value . trim ( ) . split ( "\n" ) . map ( row => row . split ( " " ) ) ;
138+ const data = document . getElementById ( "data" ) . value . trim ( ) ;
139+ return data . split ( "\n" ) . map ( row => row . split ( " " ) . map ( t => [ ... t ] . map ( e => parseInt ( e ) ) ) ) ;
140140} ;
141141
142142const writeDataToTextArea = ( inputData ) => {
143143 const data = document . getElementById ( "data" ) ;
144- data . value = inputData . map ( row => row . join ( " " ) ) . join ( "\n" ) ;
144+ data . value = inputData . map ( row => row . map ( e => e . join ( "" ) ) . join ( " " ) ) . join ( "\n" ) ;
145145} ;
146146
147147const writeData = ( inputData ) => {
@@ -163,17 +163,17 @@ canvas.addEventListener('mousedown', (event) => {
163163 if ( event . shiftKey ) {
164164 const data = parseDataFromTextArea ( ) ;
165165 const item = data [ clicked . row ] [ clicked . column ] ;
166- data [ clicked . row ] [ clicked . column ] = ` ${ item [ 0 ] } ${ ( parseInt ( item [ 1 ] ) + 1 ) % 4 } ` ;
166+ item [ 1 ] = ( item [ 1 ] + 1 ) % 4 ;
167167 writeData ( data ) ;
168168 } else if ( selectedTile . column === - 1 ) {
169169 selectedTile . column = clicked . column ;
170170 selectedTile . row = clicked . row ;
171171 } else {
172172 if ( ! ( clicked . column === selectedTile . column && clicked . row === selectedTile . row ) ) {
173173 const data = parseDataFromTextArea ( ) ;
174- const tmp = data [ selectedTile . row ] [ selectedTile . column ] ;
174+ const [ selectedIndex , selectedRotation ] = data [ selectedTile . row ] [ selectedTile . column ] ;
175175 data [ selectedTile . row ] [ selectedTile . column ] = data [ clicked . row ] [ clicked . column ] ;
176- data [ clicked . row ] [ clicked . column ] = tmp ;
176+ data [ clicked . row ] [ clicked . column ] = [ selectedIndex , selectedRotation ] ;
177177 writeData ( data ) ;
178178 }
179179 resetSelectedTile ( ) ;
@@ -193,41 +193,45 @@ tilesContainer.addEventListener('mousedown', (event) => {
193193 const data = parseDataFromTextArea ( ) ;
194194 const selected = data [ selectedTile . row ] [ selectedTile . column ] ;
195195 replaceTile ( selected [ 0 ] ) ;
196- data [ selectedTile . row ] [ selectedTile . column ] = ` ${ newTile } ${ selected [ 1 ] } ` ;
196+ data [ selectedTile . row ] [ selectedTile . column ] [ 0 ] = newTile ;
197197 writeData ( data ) ;
198198 resetSelectedTile ( ) ;
199199 renderCanvas ( ) ;
200200} ) ;
201201
202202const randomise = ( noConsecutiveRotations ) => {
203203 resetTilesAvailable ( ) ;
204- const tileData = [ [ ] ] ;
204+ const tileData = new Array ( gridHeight ) ;
205205
206+ let lastTile = - 1 ;
206207 let lastRotation = - 1 ;
207- let lastRow = null ;
208+ let aboveTile = - 1 ;
208209 let aboveRotation = - 1 ;
209- let lastTile = - 1 ;
210210
211211 for ( let row = 0 ; row < gridHeight ; row ++ ) {
212- if ( row > 0 ) {
213- lastRow = tileData [ row - 1 ] ;
214- }
212+ tileData [ row ] = new Array ( gridWidth ) ;
215213 for ( let column = 0 ; column < gridWidth ; column ++ ) {
214+ if ( row > 0 ) {
215+ [ aboveTile , aboveRotation ] = tileData [ row - 1 ] [ column ] ;
216+ }
217+
216218 let imageIndex = - 1 ;
217219 while ( imageIndex === - 1 ) {
218220 const randomIndex = Math . floor ( Math . random ( ) * imageData . length ) ;
219221 imageIndex = pickTile ( randomIndex ) ;
220222 }
223+ lastTile = imageIndex ;
224+
221225 let rotation = Math . floor ( Math . random ( ) * 4 ) ;
222226 if ( noConsecutiveRotations ) {
223- while ( rotation === lastRotation || ( lastRow && rotation === parseInt ( lastRow [ column ] [ 1 ] ) ) ) {
227+ while ( rotation === lastRotation || rotation === aboveRotation ) {
224228 rotation = Math . floor ( Math . random ( ) * 4 ) ;
225229 }
226230 }
227- tileData [ row ] . push ( `${ imageIndex } ${ rotation } ` ) ;
228231 lastRotation = rotation ;
232+
233+ tileData [ row ] [ column ] = [ imageIndex , rotation ] ;
229234 }
230- tileData . push ( [ ] ) ;
231235 } ;
232236
233237 writeData ( tileData ) ;
@@ -242,7 +246,7 @@ const useAllWhiteTiles = () => {
242246 const randomRow = Math . floor ( Math . random ( ) * gridHeight ) ;
243247 const randomColumn = Math . floor ( Math . random ( ) * gridWidth ) ;
244248 replaceTile ( tileData [ randomRow ] [ randomColumn ] [ 0 ] ) ;
245- tileData [ randomRow ] [ randomColumn ] = ` ${ whiteTileIndex } ${ tileData [ randomRow ] [ randomColumn ] [ 1 ] } ` ;
249+ tileData [ randomRow ] [ randomColumn ] = [ whiteTileIndex , tileData [ randomRow ] [ randomColumn ] [ 1 ] ] ;
246250 }
247251
248252 writeDataToTextArea ( tileData ) ;
0 commit comments