@@ -820,10 +820,58 @@ <h4 class="title">Maze ASCII</h4>
820820 buildMazeAscii ( ) ;
821821 } ;
822822
823+ let updateBordersFromAscii = function ( maze_ascii ) {
824+ let maze_table = document . getElementById ( "maze-table" ) ;
825+ let rows = maze_table . rows . length ;
826+ let cols = maze_table . rows [ 0 ] . cells . length ;
823827
824- ( function ( ) {
828+ let ascii_lines = maze_ascii . split ( "\n" ) . filter ( line => line . trim ( ) !== "" ) ;
829+ if ( ascii_lines . length !== rows * 2 + 1 ) {
830+ throw new Error ( "El número de filas en el ASCII no coincide con el tamaño del laberinto." ) ;
831+ }
825832
833+ for ( let row = 0 ; row < rows ; row ++ ) {
834+ let top_line = ascii_lines [ row * 2 ] ;
835+ let mid_line = ascii_lines [ row * 2 + 1 ] ;
826836
837+ for ( let col = 0 ; col < cols ; col ++ ) {
838+ let cell = maze_table . rows [ row ] . cells [ col ] ;
839+
840+ // Verificar la pared superior
841+ if ( top_line [ col * 4 ] === '+' && top_line [ col * 4 + 1 ] === '-' && top_line [ col * 4 + 2 ] === '-' && top_line [ col * 4 + 3 ] === '-' ) {
842+ cell . classList . add ( "border-top" ) ;
843+ } else {
844+ cell . classList . remove ( "border-top" ) ;
845+ }
846+
847+ // Verificar la pared izquierda
848+ if ( mid_line [ col * 4 ] === '|' ) {
849+ cell . classList . add ( "border-left" ) ;
850+ } else {
851+ cell . classList . remove ( "border-left" ) ;
852+ }
853+
854+ // Verificar la pared derecha
855+ if ( mid_line [ ( col + 1 ) * 4 ] === '|' ) {
856+ cell . classList . add ( "border-right" ) ;
857+ } else {
858+ cell . classList . remove ( "border-right" ) ;
859+ }
860+
861+ // Verificar la pared inferior
862+ let bottom_line = ascii_lines [ ( row + 1 ) * 2 ] ;
863+ if ( bottom_line [ col * 4 ] === '+' && bottom_line [ col * 4 + 1 ] === '-' && bottom_line [ col * 4 + 2 ] === '-' && bottom_line [ col * 4 + 3 ] === '-' ) {
864+ cell . classList . add ( "border-bottom" ) ;
865+ } else {
866+ cell . classList . remove ( "border-bottom" ) ;
867+ }
868+ }
869+ }
870+
871+ buildMazeArray ( ) ;
872+ } ;
873+
874+ ( function ( ) {
827875 let rows = document . getElementById ( "rows" ) . value ;
828876 let cols = document . getElementById ( "cols" ) . value ;
829877
@@ -847,7 +895,17 @@ <h4 class="title">Maze ASCII</h4>
847895 }
848896 } ) ;
849897
850-
898+ document . getElementById ( "maze-ascii" ) . addEventListener ( "change" , function ( e ) {
899+ let value = e . target . value ;
900+ try {
901+ updateBordersFromAscii ( value ) ;
902+ } catch ( e ) {
903+ alert ( e . message ) ;
904+ let rows = document . getElementById ( "rows" ) . value ;
905+ let cols = document . getElementById ( "cols" ) . value ;
906+ drawMaze ( rows , cols ) ;
907+ }
908+ } ) ;
851909 } ) ( ) ;
852910 </ script >
853911</ body >
0 commit comments