|
| 1 | +<?php |
| 2 | + |
| 3 | +function createTree($height, $hasStar, $decorations, $lightsOn) { |
| 4 | + $tree = []; |
| 5 | + // Estrella opcional |
| 6 | + if ($hasStar) { |
| 7 | + $tree[] = str_repeat(" ", $height - 1) . "@"; |
| 8 | + } |
| 9 | + |
| 10 | + // Ramas |
| 11 | + for ($i = 1; $i <= $height; $i++) { |
| 12 | + $line = str_repeat(" ", $height - $i); |
| 13 | + for ($j = 0; $j < (2 * $i - 1); $j++) { |
| 14 | + if (isset($decorations[$i][$j])) { |
| 15 | + $line .= $decorations[$i][$j]; |
| 16 | + } else { |
| 17 | + $line .= "*"; |
| 18 | + } |
| 19 | + } |
| 20 | + $tree[] = $line; |
| 21 | + } |
| 22 | + |
| 23 | + // Tronco |
| 24 | + $trunkPadding = str_repeat(" ", $height - 2); // Ajustamos el padding para centrar |
| 25 | + $tree[] = $trunkPadding . "|||"; |
| 26 | + $tree[] = $trunkPadding . "|||"; |
| 27 | + |
| 28 | + return $tree; |
| 29 | +} |
| 30 | + |
| 31 | +function displayTree($tree) { |
| 32 | + foreach ($tree as $line) { |
| 33 | + echo $line . PHP_EOL; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function addRandomDecoration(&$decorations, $height, $type, $count) { |
| 38 | + $added = 0; |
| 39 | + while ($added < $count) { |
| 40 | + $row = rand(1, $height); |
| 41 | + $col = rand(0, 2 * $row - 2); |
| 42 | + |
| 43 | + if (!isset($decorations[$row][$col])) { |
| 44 | + $decorations[$row][$col] = $type; |
| 45 | + $added++; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function removeRandomDecoration(&$decorations, $type, $count) { |
| 51 | + $removed = 0; |
| 52 | + foreach ($decorations as $row => &$cols) { |
| 53 | + foreach ($cols as $col => $decor) { |
| 54 | + if ($decor === $type && $removed < $count) { |
| 55 | + unset($cols[$col]); |
| 56 | + $removed++; |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function toggleLights(&$decorations, $lightsOn) { |
| 63 | + foreach ($decorations as &$cols) { |
| 64 | + foreach ($cols as &$decor) { |
| 65 | + if ($decor === "+") { |
| 66 | + $decor = $lightsOn ? "+" : "*"; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +$height = (int)readline("Ingrese la altura del árbol: "); |
| 73 | +$hasStar = true; |
| 74 | +$decorations = []; |
| 75 | +$lightsOn = true; |
| 76 | + |
| 77 | +while (true) { |
| 78 | + $tree = createTree($height, $hasStar, $decorations, $lightsOn); |
| 79 | + displayTree($tree); |
| 80 | + |
| 81 | + echo PHP_EOL . "Opciones: " . PHP_EOL; |
| 82 | + echo "1. Añadir/Eliminar estrella" . PHP_EOL; |
| 83 | + echo "2. Añadir bolas (o)" . PHP_EOL; |
| 84 | + echo "3. Eliminar bolas (o)" . PHP_EOL; |
| 85 | + echo "4. Añadir luces (+)" . PHP_EOL; |
| 86 | + echo "5. Eliminar luces (+)" . PHP_EOL; |
| 87 | + echo "6. Apagar/Encender luces" . PHP_EOL; |
| 88 | + echo "7. Salir" . PHP_EOL; |
| 89 | + |
| 90 | + $option = (int)readline("Seleccione una opción: "); |
| 91 | + |
| 92 | + switch ($option) { |
| 93 | + case 1: |
| 94 | + $hasStar = !$hasStar; |
| 95 | + echo $hasStar ? "Estrella añadida." : "Estrella eliminada." . PHP_EOL; |
| 96 | + break; |
| 97 | + case 2: |
| 98 | + addRandomDecoration($decorations, $height, "o", 2); |
| 99 | + echo "Bolas añadidas." . PHP_EOL; |
| 100 | + break; |
| 101 | + case 3: |
| 102 | + removeRandomDecoration($decorations, "o", 2); |
| 103 | + echo "Bolas eliminadas." . PHP_EOL; |
| 104 | + break; |
| 105 | + case 4: |
| 106 | + addRandomDecoration($decorations, $height, "+", 3); |
| 107 | + echo "Luces añadidas." . PHP_EOL; |
| 108 | + break; |
| 109 | + case 5: |
| 110 | + removeRandomDecoration($decorations, "+", 3); |
| 111 | + echo "Luces eliminadas." . PHP_EOL; |
| 112 | + break; |
| 113 | + case 6: |
| 114 | + $lightsOn = !$lightsOn; |
| 115 | + toggleLights($decorations, $lightsOn); |
| 116 | + echo $lightsOn ? "Luces encendidas." : "Luces apagadas." . PHP_EOL; |
| 117 | + break; |
| 118 | + case 7: |
| 119 | + echo "¡Feliz Navidad!" . PHP_EOL; |
| 120 | + exit; |
| 121 | + default: |
| 122 | + echo "Opción no válida." . PHP_EOL; |
| 123 | + } |
| 124 | +} |
0 commit comments