The Towers of Hanoi is a mathematical puzzle with a deceptively simple rule set: move all N disks from tower A to tower C, using tower B as auxiliary, and never place a larger disk on top of a smaller one. Despite the simple rules, solving it optimally requires exactly 2ⁿ − 1 moves — and the recursive structure of that solution maps beautifully to JavaScript. This project implements a fully interactive browser UI with three clickable towers, live move counting, colored disks sized proportionally to their value, a victory alert when you complete the puzzle, and an auto-solve button that animates the optimal solution step by step.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/HotCode2025/Print-Estoy-Cansado-Jefe-TercerSemestre/llms.txt
Use this file to discover all available pages before exploring further.
State Representation
The game state is stored as an array of three stacks — one per tower. Each stack is a plain JavaScript array of integers where the integer represents the disk’s size (1 = smallest, 5 = largest). Disks are stored bottom-to-top, so the last element of each array is the top disk.Initial state and global variables
JSON.parse(JSON.stringify(...)) creates a deep clone of the initial state so that the restart button can always restore to this exact snapshot without mutation.
The five disk sizes map to five distinct colors through the colores array:
Color mapping for disks
Rendering
Therenderizar() function is called after every state change. It clears all .disco elements from the DOM, then rebuilds the tower contents from the torres array. Disk widths are calculated proportionally so larger disks are visually wider:
renderizar() — DOM rebuild after each move
tam * 35 + 50 gives each disk a base width of 50 px plus 35 px per unit of size, creating clearly distinguishable widths from 85 px (smallest) to 225 px (largest).
User Interaction
Clicking a tower triggers a two-step interaction: the first click selects the source tower (highlighted with a drop-shadow glow), and the second click selects the destination and attempts the move.Click handler — source/destination selection
Move Validation
moverDisco() enforces the core game rule — you can only place a disk on an empty tower or on top of a larger disk:
moverDisco() — validation and state update
Victory Check
After every successful move,verificarVictoria() checks whether all five disks have been stacked onto tower C (index 2). If so, a congratulatory alert fires after a short 200 ms delay — enough time for the final renderizar() call to update the UI before the dialog appears:
verificarVictoria() — win condition check
The Recursive Solver
The auto-solve feature uses a classic recursive algorithm to generate the complete optimal move sequence.generarMovimientos() produces an ordered list of [origin, destination] pairs, then resolverAutomaticamente() plays them back with a 500 ms delay between each step.
generarMovimientos() — recursive move generation
Animated Playback
resolverAutomaticamente() — async animation loop
resolviendo flag prevents user clicks from interfering with the animation. Each move is applied to the state and re-rendered, then the loop pauses 500 ms via a Promise/setTimeout combination before proceeding to the next step.
Optimal Move Sequence for 5 Disks
The pre-computed solution table from the README shows all 31 moves (2⁵ − 1 = 31) for the 5-disk puzzle:| Move | From | To | Move | From | To | |
|---|---|---|---|---|---|---|
| 1 | A | C | 17 | B | A | |
| 2 | A | B | 18 | B | C | |
| 3 | C | B | 19 | A | C | |
| 4 | A | C | 20 | B | A | |
| 5 | B | A | 21 | C | B | |
| 6 | B | C | 22 | C | A | |
| 7 | A | C | 23 | B | A | |
| 8 | A | B | 24 | B | C | |
| 9 | C | B | 25 | A | C | |
| 10 | C | A | 26 | A | B | |
| 11 | B | A | 27 | C | B | |
| 12 | C | B | 28 | A | C | |
| 13 | A | C | 29 | B | A | |
| 14 | A | B | 30 | B | C | |
| 15 | C | B | 31 | A | C | |
| 16 | A | C |
Restart
The restart button restores the initial state with a deep clone and resets all counters:Restart handler
The minimum number of moves to solve the Towers of Hanoi with N disks is always 2ⁿ − 1. For the 5-disk puzzle that means 31 moves. For 10 disks it’s 1,023 — and for 64 disks (the mythical temple version of the legend) it’s over 18 quintillion moves. At one move per second, that would take roughly 585 billion years.