forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouredev.py
164 lines (136 loc) · 4.78 KB
/
mouredev.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import random
class ChristmasTree:
def __init__(self, height: int):
self.height = height
self.tree = [
[" " for _ in range(2 * height - 1)]
for _ in range(height)
]
for i in range(height):
for j in range(height - i - 1, height + i):
self.tree[i][j] = "*"
self.trunk = [
[" " for _ in range(2 * height - 1)]
for _ in range(2)
]
for i in range(2):
for j in range(height - 2, height + 1):
self.trunk[i][j] = "|"
self.star = False
self.balls = []
self.lights = []
self.lights_on = False
def display_tree(self):
for index, row in enumerate(self.tree):
if index == 0 and self.star:
print("".join(row).replace("*", "@"))
else:
print("".join(row))
for row in self.trunk:
print("".join(row))
def add_star(self):
if self.star:
print("Ya existe una estrella en el árbol.")
else:
self.star = True
print("Se ha puesto la estrella en el árbol.")
def remove_star(self):
if not self.star:
print("No existe una estrella en el árbol para quitar.")
else:
self.star = False
print("Se ha quitado la estrella en el árbol.")
def add_balls(self):
available = self.available()
if len(available) < 2:
print("No hay suficiente espacio para añadir más bolas.")
else:
selected = random.sample(available, 2)
for i, j in selected:
self.tree[i][j] = "o"
self.balls.append((i, j))
print("Se han añadido 2 bolas al árbol.")
def remove_balls(self):
if len(self.balls) < 2:
print("No hay suficientes bolas para quitar.")
else:
selected = random.sample(self.balls, 2)
for i, j in selected:
self.tree[i][j] = "*"
self.balls.remove((i, j))
print("Se han eliminado 2 bolas del árbol.")
def add_lights(self):
available = self.available()
if len(available) < 3:
print("No hay suficiente espacio para añadir más luces.")
else:
selected = random.sample(available, 3)
for i, j in selected:
self.tree[i][j] = "+" if self.lights_on else "*"
self.lights.append((i, j))
print("Se han añadido 3 luces al árbol.")
def remove_lights(self):
if len(self.lights) < 2:
print("No hay suficientes luces para quitar.")
else:
selected = random.sample(self.lights, 3)
for i, j in selected:
self.tree[i][j] = "*"
self.lights.remove((i, j))
print("Se han eliminado 3 luces del árbol.")
def toggle_lights(self, turn_on):
if not self.lights:
print("No hay luces en el árbol.")
self.lights_on = turn_on
for i, j in self.lights:
self.tree[i][j] = "+" if turn_on else "*"
print(f"Las luces fueron {'encendidas' if turn_on else 'apagadas'}.")
def available(self):
available_tree = [
(i, j) for i in range(1, self.height) for j in range(
self.height - i - 1, self.height + i) if self.tree[i][j] == "*"
]
if not self.lights_on:
for i, j in self.lights:
available_tree.remove((i, j))
return available_tree
height = input("Introduce la altura del árbol: ")
if height.isdigit() and int(height) > 0:
tree = ChristmasTree(int(height))
while True:
tree.display_tree()
print("\nAcciones:")
print("1. Añadir estrella")
print("2. Quitar estrella")
print("3. Añadir bolas")
print("4. Quitar bolas")
print("5. Añadir luces")
print("6. Quitar luces")
print("7. Encender luces")
print("8. Apagar luces")
print("9. Salir")
action = input("Selecciona una acción: ")
match action:
case "1":
tree.add_star()
case "2":
tree.remove_star()
case "3":
tree.add_balls()
case "4":
tree.remove_balls()
case "5":
tree.add_lights()
case "6":
tree.remove_lights()
case "7":
tree.toggle_lights(True)
case "8":
tree.toggle_lights(False)
case "9":
print("¡Feliz Navidad!")
break
case _:
print("Opción no válida.")
else:
print(f"Altura '{height}' no válida")