// Definiciones var DISCOS = new Array (); // Características de los Discos var TORRES = new Array (); // Matriz discosxtorre var NUMERO_TORRES = 3; var NUMERO_DISCOS = 3; // Variables Algoritmo var CANTIDAD_DISCOS; var ORIGEN; var DESTINO; var AUXILIAR; /* ------------------------------------------------------- */ function inicializarDiscos () { DISCOS [ 0 ] = new Array (); DISCOS [ 0 ] [ 'background-color' ] = "#ff7712"; DISCOS [ 0 ] [ 'width' ] = "30px"; DISCOS [ 0 ] [ 'color' ] = "#FFFFFF"; DISCOS [ 0 ] [ 'left' ] = "95px"; DISCOS [ 0 ] [ 'texto' ] = "1"; DISCOS [ 1 ] = new Array (); DISCOS [ 1 ] [ 'background-color' ] = "#0000C9"; DISCOS [ 1 ] [ 'width' ] = "60px"; DISCOS [ 1 ] [ 'color' ] = "#FFFFFF"; DISCOS [ 1 ] [ 'left' ] = "80px"; DISCOS [ 1 ] [ 'texto' ] = "2"; DISCOS [ 2 ] = new Array (); DISCOS [ 2 ] [ 'background-color' ] = "#DE0000"; DISCOS [ 2 ] [ 'width' ] = "100px"; DISCOS [ 2 ] [ 'color' ] = "#FFFFFF"; DISCOS [ 2 ] [ 'left' ] = "59px"; DISCOS [ 2 ] [ 'texto' ] = "3"; } /* ------------------------------------------------------- */ function inicializarTorres () { TORRES [ 0 ] = new Array (); TORRES [ 1 ] = new Array (); TORRES [ 2 ] = new Array (); // Torre 1 TORRES [ 0 ] [ 0 ] = 1; TORRES [ 0 ] [ 1 ] = 2; TORRES [ 0 ] [ 2 ] = 3; // Torre 2 TORRES [ 1 ] [ 0 ] = -1; TORRES [ 1 ] [ 1 ] = -1; TORRES [ 1 ] [ 2 ] = -1; // Torre 3 TORRES [ 2 ] [ 0 ] = -1; TORRES [ 2 ] [ 1 ] = -1; TORRES [ 2 ] [ 2 ] = -1; } /* ------------------------------------------------------- */ function dibujarTorres () { var i; var j; var id; var obj; var idDisco; // Recorrido de Torres for ( i = 0; i < NUMERO_TORRES; i++ ) { // Recorrido de Discos x Torre for ( j = NUMERO_DISCOS - 1; j >= 0; j-- ) { id = "disco-" + String ( i + 1 ) + "-" + String ( j + 1 ); obj = document.getElementById ( id ); idDisco = TORRES [ i ] [ j ]; if ( idDisco == -1 ) { obj.style.backgroundColor = "transparent"; obj.innerHTML = ""; } else { idDisco = idDisco - 1; obj.style.backgroundColor = DISCOS [ idDisco ] [ 'background-color' ]; obj.style.color = DISCOS [ idDisco ] [ 'color' ]; obj.style.left = DISCOS [ idDisco ] [ 'left' ]; obj.style.width = DISCOS [ idDisco ] [ 'width' ]; obj.innerHTML = DISCOS [ idDisco ] [ 'texto' ]; } } } } /* ------------------------------------------------------- */ function moverDisco ( disco, torreOrigen, torreDestino ) { var i; disco = disco - 1; torreOrigen = torreOrigen - 1; torreDestino = torreDestino - 1; // Recorrer Torre Origen for ( i = 0; i < NUMERO_DISCOS; i++ ) if ( TORRES [ torreOrigen ] [ i ] == disco + 1 ) { TORRES [ torreOrigen ] [ i ] = -1; break; } // Recorrer Torre Destino for ( i = NUMERO_DISCOS; i >= 0; i-- ) if ( TORRES [ torreDestino ] [ i ] == -1 ) { TORRES [ torreDestino ] [ i ] = disco + 1; break; } dibujarTorres (); } /* ------------------------------------------------------- */ function obtenerCodigo () { var codigo = new Array (); var espacio = "    "; codigo [ codigo.length ] = "/* Torres de Hanoi */"; codigo [ codigo.length ] = "void hanoi(int cant_discos, int origen, int destino, int auxiliar)"; codigo [ codigo.length ] = "{"; codigo [ codigo.length ] = espacio + "if (cant_discos == 0)"; codigo [ codigo.length ] = espacio + espacio + "return;"; codigo [ codigo.length ] = " "; codigo [ codigo.length ] = espacio + "hanoi(cant_discos - 1, origen, auxiliar, destino);"; codigo [ codigo.length ] = espacio + "printf(\"Disco %d, Torre %d => Torre %d\", cant_discos, origen, destino);"; codigo [ codigo.length ] = espacio + "hanoi(cant_discos - 1, auxiliar, destino, origen);"; codigo [ codigo.length ] = "}"; return codigo; } /* ------------------------------------------------------- */ function procesarLineaOperacion ( linea ) { var i; var temporal; // void hanoi(int cant_discos, int origen, int destino, int auxiliar) if ( linea == 2 ) { insertarLlamado (); cargarLlamado (); seleccionarLlamado ( 1 ); } // if (cant_discos == 0) else if ( linea == 4 ) { if ( CANTIDAD_DISCOS != 0 ) return 6; } // return ; else if ( linea == 5 ) { if ( pendientesXllamar () == 1 ) return 10; else { moverLlamado (); return 8; } } // hanoi(cant_discos - 1, origen, auxiliar, destino); else if ( linea == 7 ) { CANTIDAD_DISCOS = CANTIDAD_DISCOS - 1; ORIGEN = ORIGEN; temporal = DESTINO; DESTINO = AUXILIAR; AUXILIAR = temporal; return 2; } // hanoi(cant_discos - 1, auxiliar, destino, origen); else if ( linea == 9 ) { moverLlamado (); CANTIDAD_DISCOS = CANTIDAD_DISCOS - 1; temporal = ORIGEN; ORIGEN = AUXILIAR; AUXILIAR = temporal; return 2; } return linea + 1; } /* ------------------------------------------------------- */ function cargarLlamado () { var row = tables_getFirstRow ( "code-stack-table-id" ); CANTIDAD_DISCOS = row [ 0 ]; ORIGEN = row [ 1 ]; DESTINO = row [ 2 ]; AUXILIAR = row [ 3 ]; } /* ------------------------------------------------------- */ function seleccionarLlamado ( linea ) { deseleccionarLlamado (); var row = tables_getRowObject ( "code-stack-table-id", linea ); row.className = "selected"; } /* ------------------------------------------------------- */ function deseleccionarLlamado () { var tableObject = document.getElementById ( "code-stack-table-id" ); if ( !tableObject ) return; // Get Body var tableBody = tableObject.tBodies [ 0 ]; var r; for ( r = 0; r < tableBody.rows.length; r++ ) tableBody.rows [ r ].className = "normal"; } /* ------------------------------------------------------- */ function insertarLlamado () { tables_insertRow ( "code-stack-table-id", new Array ( CANTIDAD_DISCOS, ORIGEN, DESTINO, AUXILIAR ) ); } /* ------------------------------------------------------- */ function moverLlamado () { tables_addRow ( "code-stack-table-id2", new Array ( CANTIDAD_DISCOS, ORIGEN, DESTINO, AUXILIAR ) ); tables_removeFirstRow ( "code-stack-table-id" ); cargarLlamado (); seleccionarLlamado ( 1 ); } /* ------------------------------------------------------- */ function pendientesXllamar () { return tables_getRowsNumber ( "code-stack-table-id" ); } /* ------------------------------------------------------- */ function inicializarDatosOperacion () { tables_removeRows ( "code-stack-table-id" ); tables_removeRows ( "code-stack-table-id2" ); CANTIDAD_DISCOS = 3; ORIGEN = 1; DESTINO = 2; AUXILIAR = 3; inicializarDiscos (); inicializarTorres (); dibujarTorres (); } /* ------------------------------------------------------- */ // function adicionarEjecutado () // { // tables_addRow ( "code-stack-table-id2", LLAMADO ); // eliminarLlamado (); // // if ( pendientesXllamar () != 0 ) // seleccionarLlamado ( 1 ); // } /* ------------------------------------------------------- */ /* ------------------------------------------------------- */ // function adicionarLlamado () // { // tables_insertRow ( "code-stack-table-id", LLAMADO ); // // seleccionarLlamado ( 1 ); // } /* ------------------------------------------------------- */ /* ------------------------------------------------------- */ // function seleccionarLlamado ( linea ) // { // deseleccionarLlamados (); // // var tableObject = document.getElementById ( "code-stack-table-id" ); // // if ( !tableObject ) // return; // // // Get Body // var tableBody = tableObject.tBodies [ 0 ]; // // tableBody.rows [ linea - 1 ].className = "selected"; // LLAMADO [ 0 ] = tableBody.rows [ linea - 1 ].cells [ 0 ].innerHTML; // LLAMADO [ 1 ] = tableBody.rows [ linea - 1 ].cells [ 1 ].innerHTML; // LLAMADO [ 2 ] = tableBody.rows [ linea - 1 ].cells [ 2 ].innerHTML; // LLAMADO [ 3 ] = tableBody.rows [ linea - 1 ].cells [ 3 ].innerHTML; // } /* ------------------------------------------------------- */