Skip to content

Commit 033ddd4

Browse files
authored
Merge pull request #8709 from marialunatito/feature/typescript-48
#48 - Typescript
2 parents edb2de4 + 6bee01b commit 033ddd4

File tree

1 file changed

+368
-0
lines changed

1 file changed

+368
-0
lines changed
Lines changed: 368 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,368 @@
1+
// use this command for run npx ts-node "Roadmap/48 - ÁRBOL DE NAVIDAD/typescript/marialunatito.ts"
2+
3+
const readlline = require("readline").createInterface({
4+
input: process.stdin,
5+
output: process.stdout,
6+
});
7+
8+
function start() {
9+
readlline.question(
10+
"Crea un arbol de navidad, ingresa la altura: ",
11+
(height: string) => {
12+
const treeModel = new Tree(parseInt(height));
13+
treeModel.create();
14+
menu(treeModel);
15+
}
16+
);
17+
}
18+
19+
function menu(tree: Tree) {
20+
console.log(`
21+
0. Mostrar arbolito
22+
1. Añade la estrella en la copa (@)
23+
2. Elimina la estrella en la copa (@)
24+
3. Añade bolas de dos en dos (o) aleatoriamente
25+
4. Elimina bolas de dos en dos (o) aleatoriamente
26+
5. Añade luces de tres en tres (+) aleatoriamente
27+
6. Eliminar luces de tres en tres (+) aleatoriamente
28+
7. Apagar (*) las luces (conservando su posición)
29+
8. Encender (+) las luces (conservando su posición)
30+
10. Salir del sistema
31+
`);
32+
33+
readlline.question("Seleccione una option: ", (option: string) => {
34+
switch (option) {
35+
case "0":
36+
console.log(tree.Tree);
37+
menu(tree);
38+
break;
39+
case "1":
40+
tree.addStart();
41+
break;
42+
case "2":
43+
tree.removeStart();
44+
break;
45+
case "3":
46+
tree.addBalls();
47+
break;
48+
case "4":
49+
tree.removedBalls();
50+
break;
51+
case "5":
52+
tree.addLights();
53+
break;
54+
case "6":
55+
tree.removeLights();
56+
break;
57+
case "7":
58+
tree.turnOff();
59+
break;
60+
case "8":
61+
tree.turnOn();
62+
break;
63+
case "10":
64+
console.log("Ha salido del sistema");
65+
readlline.close();
66+
break;
67+
68+
default:
69+
console.log(`La opción ${option} es invalida.. vuelva a intentar`);
70+
menu(tree);
71+
break;
72+
}
73+
});
74+
}
75+
76+
class Tree {
77+
private tree: string;
78+
private height: number;
79+
80+
constructor(height: number) {
81+
this.height = height;
82+
this.tree = "";
83+
}
84+
85+
create() {
86+
let space = "";
87+
let asterisk = "";
88+
89+
// height = 2
90+
for (let i = 1; i <= this.height; i++) {
91+
Array.from({ length: this.height - i }).forEach((_) => {
92+
space += " ";
93+
});
94+
95+
Array.from({ length: 2 * i - 1 }).forEach((_) => {
96+
asterisk += "*";
97+
});
98+
99+
this.tree += `${space}${asterisk}\n`;
100+
space = "";
101+
asterisk = "";
102+
}
103+
104+
let spaceTrunk = "";
105+
106+
Array.from({ length: (2 * this.height - 1 - 3) / 2 }).forEach((_) => {
107+
spaceTrunk += " ";
108+
});
109+
const trunk = `${spaceTrunk}|||\n${spaceTrunk}|||`;
110+
111+
this.tree += trunk;
112+
/// RESULT
113+
console.log("\n");
114+
console.log(this.tree);
115+
console.log("\n");
116+
117+
return this.tree;
118+
}
119+
120+
addStart() {
121+
if (this.tree.includes("@")) {
122+
console.log("El tree ya contiene la estrella");
123+
} else {
124+
const indexStart = this.getIndexStart();
125+
const treeWithStart =
126+
this.tree.substring(0, indexStart) +
127+
"@" +
128+
this.tree.substring(indexStart + 1, this.tree.length);
129+
this.tree = treeWithStart;
130+
console.log(this.tree);
131+
}
132+
133+
menu(this);
134+
}
135+
136+
private getIndexStart() {
137+
let index = this.tree.indexOf("*");
138+
if (index === -1) {
139+
index = this.tree.indexOf("+");
140+
}
141+
return index;
142+
}
143+
144+
removeStart() {
145+
if (this.tree.includes("@")) {
146+
const treeWithStart = this.tree.replace("@", "*");
147+
this.tree = treeWithStart;
148+
console.log(this.tree);
149+
} else {
150+
console.log("***********Se quitó la estrella***********");
151+
}
152+
153+
menu(this);
154+
}
155+
156+
turnOn() {
157+
if (this.tree.includes("*")) {
158+
this.tree = this.tree.replaceAll("*", "+");
159+
console.log(this.tree);
160+
} else {
161+
console.log("El arbolito está encendido o tiene bolas!");
162+
}
163+
164+
menu(this);
165+
}
166+
167+
turnOff() {
168+
if (this.tree.includes("+")) {
169+
this.tree = this.tree.replaceAll("+", "*");
170+
console.log(this.tree);
171+
} else {
172+
console.log("El arbolito está apagado!");
173+
}
174+
175+
menu(this);
176+
}
177+
178+
addBalls() {
179+
let count = 0;
180+
let canBeBall = false;
181+
182+
let cant = 0;
183+
Array.from({ length: this.tree.length }).forEach((_, i) => {
184+
if (this.tree.substring(i, i + 1).includes("*")) {
185+
cant++;
186+
}
187+
});
188+
189+
if (cant >= 2) {
190+
canBeBall = true;
191+
} else {
192+
canBeBall = false;
193+
console.log(
194+
"************Ya no hay espacios posibles para colocar bolas************"
195+
);
196+
}
197+
198+
while (canBeBall) {
199+
const index = this.indexRandom();
200+
const indexTreeForChange = this.tree.substring(index, index + 1);
201+
if (indexTreeForChange.includes("*")) {
202+
const newTree =
203+
this.tree.substring(0, index) +
204+
"o" +
205+
this.tree.substring(index + 1, this.tree.length);
206+
207+
// set new tree
208+
this.tree = newTree;
209+
210+
count++;
211+
if (count === 2) {
212+
// stop the while
213+
canBeBall = false;
214+
}
215+
}
216+
}
217+
218+
console.log(this.tree);
219+
menu(this);
220+
}
221+
222+
removedBalls() {
223+
let count = 0;
224+
let canBeRemoveBall = false;
225+
226+
let cant = 0;
227+
Array.from({ length: this.tree.length }).forEach((_, i) => {
228+
if (this.tree.substring(i, i + 1).includes("o")) {
229+
cant++;
230+
}
231+
});
232+
233+
if (cant >= 2) {
234+
canBeRemoveBall = true;
235+
} else {
236+
canBeRemoveBall = false;
237+
console.log("************Ya no hay bolas que quitar!************");
238+
}
239+
240+
while (canBeRemoveBall) {
241+
const index = this.indexRandom();
242+
const indexTreeForChange = this.tree.substring(index, index + 1);
243+
if (indexTreeForChange.includes("o")) {
244+
const newTree =
245+
this.tree.substring(0, index) +
246+
"*" +
247+
this.tree.substring(index + 1, this.tree.length);
248+
249+
// set new tree
250+
this.tree = newTree;
251+
252+
count++;
253+
if (count === 2) {
254+
// stop the while
255+
canBeRemoveBall = false;
256+
}
257+
}
258+
}
259+
260+
console.log(this.tree);
261+
menu(this);
262+
}
263+
264+
addLights() {
265+
let count = 0;
266+
let canBeLights = false;
267+
268+
let cant = 0;
269+
Array.from({ length: this.tree.length }).forEach((_, i) => {
270+
if (this.tree.substring(i, i + 1).includes("*")) {
271+
cant++;
272+
}
273+
});
274+
275+
if (cant >= 3) {
276+
canBeLights = true;
277+
} else {
278+
canBeLights = false;
279+
console.log(
280+
"************Ya no hay espacios posibles para colocar más luces************"
281+
);
282+
}
283+
284+
while (canBeLights) {
285+
const index = this.indexRandom();
286+
const indexTreeForChange = this.tree.substring(index, index + 1);
287+
if (indexTreeForChange.includes("*")) {
288+
const newTree =
289+
this.tree.substring(0, index) +
290+
"+" +
291+
this.tree.substring(index + 1, this.tree.length);
292+
293+
// set new tree
294+
this.tree = newTree;
295+
296+
count++;
297+
if (count === 3) {
298+
// stop the while
299+
canBeLights = false;
300+
}
301+
}
302+
}
303+
304+
console.log(this.tree);
305+
menu(this);
306+
}
307+
308+
removeLights() {
309+
let count = 0;
310+
let canBeLights = false;
311+
312+
let cant = 0;
313+
Array.from({ length: this.tree.length }).forEach((_, i) => {
314+
if (this.tree.substring(i, i + 1).includes("+")) {
315+
cant++;
316+
}
317+
});
318+
319+
if (cant >= 3) {
320+
canBeLights = true;
321+
} else {
322+
canBeLights = false;
323+
console.log("************No hay mas luces que quitar!!************");
324+
}
325+
326+
while (canBeLights) {
327+
const index = this.indexRandom();
328+
const indexTreeForChange = this.tree.substring(index, index + 1);
329+
if (indexTreeForChange.includes("+")) {
330+
const newTree =
331+
this.tree.substring(0, index) +
332+
"*" +
333+
this.tree.substring(index + 1, this.tree.length);
334+
335+
// set new tree
336+
this.tree = newTree;
337+
338+
count++;
339+
if (count === 3) {
340+
// stop the while
341+
canBeLights = false;
342+
}
343+
}
344+
}
345+
346+
console.log(this.tree);
347+
menu(this);
348+
}
349+
350+
private indexRandom(): number {
351+
const indexRandom = Math.floor(Math.random() * this.tree.length);
352+
const indexStart = this.getIndexStart();
353+
if (indexRandom != indexStart) {
354+
return indexRandom;
355+
}
356+
return this.indexRandom();
357+
}
358+
359+
get Tree() {
360+
return this.tree;
361+
}
362+
363+
set Tree(tree: string) {
364+
this.tree = tree;
365+
}
366+
}
367+
368+
start();

0 commit comments

Comments
 (0)