|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | +import java.util.Random; |
| 4 | +import java.util.Scanner; |
| 5 | +public class MohamedElderkaoui { |
| 6 | + private int altura; |
| 7 | + private boolean tieneEstrella; |
| 8 | + private boolean lucesEncendidas; |
| 9 | + private List<String[]> arbol; |
| 10 | + |
| 11 | + public ArbolNavidad(int altura) { |
| 12 | + this.altura = altura; |
| 13 | + this.tieneEstrella = false; |
| 14 | + this.lucesEncendidas = false; |
| 15 | + this.arbol = new ArrayList<>(); |
| 16 | + generarArbolBase(); |
| 17 | + } |
| 18 | + |
| 19 | + private void generarArbolBase() { |
| 20 | + arbol.clear(); |
| 21 | + for (int i = 0; i < altura; i++) { |
| 22 | + int ancho = 1 + 2 * i; |
| 23 | + char[] fila = new char[altura + i]; |
| 24 | + for (int j = 0; j < fila.length; j++) { |
| 25 | + fila[j] = (j >= altura - i - 1 && j < altura + i) ? '*' : ' '; |
| 26 | + } |
| 27 | + arbol.addAll(new String(fila).toCharArray()); |
| 28 | + } |
| 29 | + // Agregar el tronco |
| 30 | + for (int i = 0; i < 2; i++) { |
| 31 | + char[] tronco = new char[2 * altura - 1]; |
| 32 | + for (int j = 0; j < tronco.length; j++) { |
| 33 | + tronco[j] = (j >= altura - 2 && j <= altura) ? '|' : ' '; |
| 34 | + } |
| 35 | + arbol.add(tronco); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + public void toggleEstrella() { |
| 40 | + if (tieneEstrella) { |
| 41 | + arbol.get(0)[altura - 1] = '*'; |
| 42 | + System.out.println("Se ha eliminado la estrella del árbol."); |
| 43 | + } else { |
| 44 | + arbol.get(0)[altura - 1] = '@'; |
| 45 | + System.out.println("Se ha añadido la estrella al árbol."); |
| 46 | + } |
| 47 | + tieneEstrella = !tieneEstrella; |
| 48 | + } |
| 49 | + |
| 50 | + public void añadirBolas(int cantidad) { |
| 51 | + Random rand = new Random(); |
| 52 | + for (int i = 0; i < cantidad; i++) { |
| 53 | + int fila; |
| 54 | + int columna; |
| 55 | + do { |
| 56 | + fila = rand.nextInt(altura); |
| 57 | + columna = rand.nextInt(altura + fila); |
| 58 | + } while (arbol.get(fila)[columna] != '*'); |
| 59 | + arbol.get(fila)[columna] = 'o'; |
| 60 | + } |
| 61 | + System.out.println(cantidad + " bola(s) añadida(s)."); |
| 62 | + } |
| 63 | + |
| 64 | + public void eliminarBolas(int cantidad) { |
| 65 | + int eliminadas = 0; |
| 66 | + for (int i = 0; i < altura; i++) { |
| 67 | + for (int j = 0; j < arbol.get(i).length; j++) { |
| 68 | + if (arbol.get(i)[j] == 'o') { |
| 69 | + arbol.get(i)[j] = '*'; |
| 70 | + eliminadas++; |
| 71 | + if (eliminadas == cantidad) break; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + System.out.println(eliminadas + " bola(s) eliminada(s)."); |
| 76 | + } |
| 77 | + |
| 78 | + public void añadirLuces(int cantidad) { |
| 79 | + Random rand = new Random(); |
| 80 | + for (int i = 0; i < cantidad; i++) { |
| 81 | + int fila; |
| 82 | + int columna; |
| 83 | + do { |
| 84 | + fila = rand.nextInt(altura); |
| 85 | + columna = rand.nextInt(altura + fila); |
| 86 | + } while (arbol.get(fila)[columna] != '*'); |
| 87 | + arbol.get(fila)[columna] = '+'; |
| 88 | + } |
| 89 | + System.out.println(cantidad + " luz/luces añadida(s)."); |
| 90 | + } |
| 91 | + |
| 92 | + public void eliminarLuces(int cantidad) { |
| 93 | + int eliminadas = 0; |
| 94 | + for (int i = 0; i < altura; i++) { |
| 95 | + for (int j = 0; j < arbol.get(i).length; j++) { |
| 96 | + if (arbol.get(i)[j] == '+') { |
| 97 | + arbol.get(i)[j] = '*'; |
| 98 | + eliminadas++; |
| 99 | + if (eliminadas == cantidad) break; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + System.out.println(eliminadas + " luz/luces eliminada(s)."); |
| 104 | + } |
| 105 | + |
| 106 | + public void encenderApagarLuces() { |
| 107 | + for (int i = 0; i < altura; i++) { |
| 108 | + for (int j = 0; j < arbol.get(i).length; j++) { |
| 109 | + if (arbol.get(i)[j] == '+') { |
| 110 | + arbol.get(i)[j] = lucesEncendidas ? '*' : '+'; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + lucesEncendidas = !lucesEncendidas; |
| 115 | + System.out.println(lucesEncendidas ? "Luces encendidas." : "Luces apagadas."); |
| 116 | + } |
| 117 | + |
| 118 | + public void mostrarArbol() { |
| 119 | + for (char[] fila : arbol) { |
| 120 | + System.out.println(new String(fila)); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public static void main(String[] args) { |
| 125 | + Scanner sc = new Scanner(System.in); |
| 126 | + System.out.print("Introduce la altura del árbol: "); |
| 127 | + int altura = sc.nextInt(); |
| 128 | + ArbolNavidad arbol = new ArbolNavidad(altura); |
| 129 | + |
| 130 | + int opcion; |
| 131 | + do { |
| 132 | + System.out.println("\nOpciones:"); |
| 133 | + System.out.println("1. Alternar estrella"); |
| 134 | + System.out.println("2. Añadir bolas (2)"); |
| 135 | + System.out.println("3. Eliminar bolas (2)"); |
| 136 | + System.out.println("4. Añadir luces (3)"); |
| 137 | + System.out.println("5. Eliminar luces (3)"); |
| 138 | + System.out.println("6. Encender/Apagar luces"); |
| 139 | + System.out.println("7. Mostrar árbol"); |
| 140 | + System.out.println("0. Salir"); |
| 141 | + System.out.print("Selecciona una opción: "); |
| 142 | + opcion = sc.nextInt(); |
| 143 | + |
| 144 | + switch (opcion) { |
| 145 | + case 1 -> arbol.toggleEstrella(); |
| 146 | + case 2 -> arbol.añadirBolas(2); |
| 147 | + case 3 -> arbol.eliminarBolas(2); |
| 148 | + case 4 -> arbol.añadirLuces(3); |
| 149 | + case 5 -> arbol.eliminarLuces(3); |
| 150 | + case 6 -> arbol.encenderApagarLuces(); |
| 151 | + case 7 -> arbol.mostrarArbol(); |
| 152 | + case 0 -> System.out.println("¡Feliz Navidad!"); |
| 153 | + default -> System.out.println("Opción inválida."); |
| 154 | + } |
| 155 | + } while (opcion != 0); |
| 156 | + |
| 157 | + sc.close(); |
| 158 | + } |
| 159 | +} |
0 commit comments