Skip to content

Commit 32f5629

Browse files
author
Miguel Ángel Delgado
committed
Add typescript
1 parent 68cb4dd commit 32f5629

File tree

2 files changed

+161
-0
lines changed

2 files changed

+161
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function generateCode(): string {
2+
const chars = ['A', 'B', 'C', '1', '2', '3'];
3+
return chars.sort(() => Math.random() - 0.5).slice(0, 4).join('');
4+
}
5+
6+
const code: string = generateCode();
7+
let attempts: number = 10;
8+
9+
console.log("¡Bienvenido! Tienes 10 intentos para descifrar el código.");
10+
11+
while (attempts > 0) {
12+
const input: string | null = prompt(`Intento #${attempts}: Ingresa un código de 4 caracteres:`)?.toUpperCase() || '';
13+
14+
if (input.length !== 4 || !/^[A-C1-3]{4}$/.test(input)) {
15+
console.log("Código inválido. Debe tener 4 caracteres (letras A-C, números 1-3).");
16+
continue;
17+
}
18+
19+
const result = input.split('').map((char, i) => {
20+
if (char === code[i]) return "Correcto";
21+
if (code.includes(char)) return "Presente";
22+
return "Incorrecto";
23+
});
24+
25+
console.log(result.join(", "));
26+
27+
if (input === code) {
28+
console.log(`¡Felicidades! Descifraste el código: ${code}`);
29+
break;
30+
}
31+
32+
attempts--;
33+
}
34+
35+
if (attempts === 0) {
36+
console.log(`Lo siento, no lograste descifrar el código: ${code}`);
37+
}

0 commit comments

Comments
 (0)