forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouredev.py
122 lines (89 loc) · 3.41 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
import random
class Fighter:
def __init__(self, name: str, speed: int, attack: int, defense: int):
self.name = name
self.speed = speed
self.attack = attack
self.defense = defense
self.health = 100
def reset_health(self):
self.health = 100
def is_alive(self) -> bool:
return self.health > 0
def take_damage(self, damage: int):
attack_damage = 0
if random.random() < 0.2:
print(f"{self.name} ha esquivado el ataque")
else:
if self.defense >= damage:
attack_damage = damage * 0.1
else:
attack_damage = damage - self.defense
attack_damage = int(attack_damage)
self.health = max(self.health - attack_damage, 0)
print(f"{self.name} ha recibido {attack_damage} de daño")
print(f"Salud restante de {self.name}: {self.health}")
class Battle:
def __init__(self, fighter1: Fighter, fighter2: Fighter):
self.fighter1 = fighter1
self.fighter2 = fighter2
def fight(self) -> Fighter:
print(f"\n=== {self.fighter1.name} vs. {self.fighter2.name} ===")
while self.fighter1.is_alive() and self.fighter2.is_alive():
if self.fighter1.speed >= self.fighter2.speed:
self.turn(self.fighter1, self.fighter2)
if self.fighter2.is_alive():
self.turn(self.fighter2, self.fighter1)
else:
self.turn(self.fighter2, self.fighter1)
if self.fighter1.is_alive():
self.turn(self.fighter1, self.fighter2)
if self.fighter1.is_alive():
print(f"\n{self.fighter1.name} es el ganador de la batalla")
return self.fighter1
else:
print(f"\n{self.fighter2.name} es el ganador de la batalla")
return self.fighter2
def turn(self, attacker: Fighter, defender: Fighter):
print(f"\n{attacker.name} ataca a {defender.name}")
defender.take_damage(attacker.attack)
class Tournament:
def __init__(self, fighters: list):
if not self.is_power_of_two(len(fighters)):
raise ValueError(
"El número de luchadores debe ser una potencia de 2")
self.fighters = fighters
def start(self):
round = 1
while len(self.fighters) > 1:
print(f"\n=== Ronda {round} ===")
random.shuffle(self.fighters)
winners = []
for index in range(0, len(self.fighters), 2):
fighter1 = self.fighters[index]
fighter2 = self.fighters[index + 1]
battle = Battle(fighter1, fighter2)
winner = battle.fight()
winner.reset_health()
winners.append(winner)
self.fighters = winners
round += 1
print(f"\n¡El ganador del torneo es {self.fighters[0].name}!")
def is_power_of_two(self, n) -> bool:
if n <= 0:
return False
while n % 2 == 0:
n //= 2
return n == 1
fighters = [
Fighter("Goku", 90, 95, 80),
Fighter("Vegeta", 95, 90, 82),
Fighter("Piccolo", 80, 85, 90),
Fighter("Freezer", 95, 90, 75),
Fighter("Krillin", 95, 90, 75),
Fighter("Célula", 92, 70, 73),
Fighter("Gohan", 97, 95, 70),
Fighter("Trunks", 88, 88, 88)
]
tournament = Tournament(fighters)
tournament.start()