Skip to content

Commit 20ea0b7

Browse files
authored
Merge pull request mouredev#7467 from Jesusway69/main
mouredev#48 - Java
2 parents 6fd83be + cb9108f commit 20ea0b7

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package reto_49;
2+
3+
import java.util.ArrayList;
4+
import java.util.Scanner;
5+
6+
public class JesusWay69 {
7+
8+
public static ArrayList<Integer> coordinates = new ArrayList<>();
9+
10+
public static void main(String[] args) {
11+
12+
char[][] myTree = null;
13+
14+
do {
15+
Scanner sc = new Scanner(System.in);
16+
System.out.println("""
17+
1- Crear nuevo árbol
18+
2- Añadir estrella
19+
3- Eliminar estrella
20+
4- Añadir 2 bolas aleatoriamente
21+
5- Quitar todas las bolas
22+
6- Añadir 3 luces aleatoriamente
23+
7- Encender las luces
24+
8- Apagar las luces
25+
""");
26+
System.out.print("Elija una opción (enter para salir): ");
27+
String option = sc.nextLine();
28+
if (option.equals("")) break;
29+
30+
switch (option) {
31+
case "1" -> {
32+
System.out.print("Introduzca la altura del árbol (mayor de 3): ");
33+
int hight = sc.nextInt();
34+
myTree = createTree(hight);
35+
showTree(myTree);
36+
}
37+
case "2" -> showTree(topStar(myTree, true));
38+
case "3" -> showTree(topStar(myTree, false));
39+
case "4" -> showTree(addBalls(myTree));
40+
case "5" -> showTree(removeBalls(myTree));
41+
case "6" -> showTree(addLights(myTree));
42+
case "7" -> showTree(turnOnLights(myTree, coordinates));
43+
case "8" ->showTree(turnOffLigths(myTree));
44+
default -> System.out.println("El dato tiene que ser numérico del 1 al 8\n");
45+
}
46+
} while (true);
47+
}
48+
49+
public static ArrayList<Integer> storageCoordinates(int x, int y) {
50+
coordinates.add(x);
51+
coordinates.add(y);
52+
return coordinates;
53+
}
54+
55+
public static char[][] createTree(int files) {
56+
int columns = files * 2 - 1;
57+
int branch = 1;
58+
char[][] tree = new char[files + 2][columns];
59+
60+
if (files <= 3) {
61+
System.out.println("No se puede crear un árbol de menos de 4 alturas");
62+
tree = null;
63+
return tree;
64+
}
65+
for (int i = 0; i < files; i++) {
66+
for (int j = 0; j < columns / 2; j++) tree[i][j] = ' ';
67+
for (int k = columns / 2; k < columns / 2 + branch; k++) tree[i][k] = '*';
68+
for (int l = columns / 2 + branch; l < files * 2 - 1; l++) tree[i][l] = ' ';
69+
columns -= 2;
70+
branch += 2;
71+
}
72+
for (int m = 0; m < 2; m++) {
73+
for (int n = 0; n < files - 2; n++) tree[files + m][n] = ' ';
74+
for (int p = files - 2; p < files + 1; p++) tree[files + m][p] = '|';
75+
for (int q = files + 1; q < files * 2 - 1; q++) tree[files + m][q] = ' ';
76+
}
77+
return tree;
78+
}
79+
public static void showTree(char[][] tree) {
80+
if (tree != null) {
81+
int i = 0;
82+
for (char[] branch : tree) {
83+
i++;
84+
for (char c : branch) System.out.print(c);
85+
System.out.println();
86+
}
87+
}
88+
}
89+
90+
public static char[][] topStar(char[][] tree, boolean switchStar) {
91+
if (tree == null) {
92+
System.out.println("ERROR: Hay que crear un árbol antes de poder modificarlo (opción 1)");
93+
return null;
94+
} else if (switchStar) {
95+
tree[0][tree[0].length / 2] = '@';
96+
System.out.println("\n--- ESTRELLA AÑADIDA ---");
97+
} else {
98+
tree[0][tree[0].length / 2] = '*';
99+
System.out.println("\n--- ESTRELLA ELIMINADA ---");
100+
}
101+
return tree;
102+
}
103+
104+
public static char[][] addBalls(char[][] tree) {
105+
if (tree == null) {
106+
System.out.println("ERROR: Hay que crear un árbol antes de poder modificarlo (opción 1)");
107+
return null;
108+
}
109+
for (int i = 0; i < 2; i++) {
110+
int randomFile = (int) (Math.random() * tree.length - 2 + 1);
111+
int randomColumn = (int) (Math.random() * tree[0].length);
112+
if (tree[randomFile][randomColumn] == '*') tree[randomFile][randomColumn] = 'O';
113+
else i--;
114+
}
115+
System.out.println("\n--- SE AÑADEN 2 BOLAS DE ADORNO ---");
116+
return tree;
117+
}
118+
119+
public static char[][] removeBalls(char[][] tree) {
120+
if (tree == null) {
121+
System.out.println("ERROR: Hay que crear un árbol antes de poder modificarlo (opción 1)");
122+
return null;
123+
}
124+
for (int i = 1; i < tree.length - 2; i++) {
125+
for (int j = 0; j < tree[i].length; j++) {
126+
if (tree[i][j] == 'O') tree[i][j] = '*';
127+
}
128+
}
129+
System.out.println("\n--- BOLAS DE ADORNO ELIMINADAS ---");
130+
return tree;
131+
}
132+
133+
public static char[][] addLights(char[][] tree) {
134+
if (tree == null) {
135+
System.out.println("ERROR: Hay que crear un árbol antes de poder modificarlo (opción 1)");
136+
return null;
137+
}
138+
for (int i = 0; i < 3; i++) {
139+
int randomFile = (int) (Math.random() * tree.length - 2) + 1;
140+
int randomColumn = (int) (Math.random() * tree[0].length);
141+
if (tree[randomFile][randomColumn] == '*') {
142+
tree[randomFile][randomColumn] = 'X';
143+
storageCoordinates(randomFile, randomColumn);
144+
} else i--;
145+
}
146+
System.out.println("\n--- AÑADIDAS 3 LUCES ENCENDIDAS AL ÁRBOL ---");
147+
return tree;
148+
}
149+
150+
public static char[][] turnOnLights(char[][] tree, ArrayList<Integer> coordinates) {
151+
if (tree == null) {
152+
System.out.println("ERROR: Hay que crear un árbol antes de poder modificarlo (opción 1)");
153+
return null;
154+
}
155+
int x = 0;
156+
for (int j = 0; j < coordinates.size(); j++) {
157+
if (j % 2 == 0) {
158+
x = coordinates.get(j);
159+
} else {
160+
int y = coordinates.get(j);
161+
tree[x][y] = 'X';
162+
}
163+
}
164+
System.out.println("\n--- TODAS LAS LUCES ENCENDIDAS ---");
165+
return tree;
166+
}
167+
168+
public static char[][] turnOffLigths(char[][] tree) {
169+
if (tree == null) {
170+
System.out.println("ERROR: Hay que crear un árbol antes de poder modificarlo (opción 1)");
171+
return null;
172+
}
173+
for (int i = 1; i < tree.length - 2; i++) {
174+
for (int j = 0; j < tree[i].length; j++) {
175+
if (tree[i][j] == 'X') tree[i][j] = '*';
176+
}
177+
}
178+
System.out.println("\n--- TODAS LAS LUCES APAGADAS ---");
179+
return tree;
180+
}
181+
}

0 commit comments

Comments
 (0)