|
| 1 | +# _____________________________________ |
| 2 | +# https://github.com/kenysdev |
| 3 | +# 2024 - Python |
| 4 | +# _____________________________________ |
| 5 | +# 42 TORNEO DRAGON BALL |
| 6 | +# ------------------------------------ |
| 7 | + |
| 8 | +""" |
| 9 | +* EJERCICIO: |
| 10 | + * ¡El último videojuego de Dragon Ball ya está aquí! |
| 11 | + * Se llama Dragon Ball: Sparking! ZERO. |
| 12 | + * |
| 13 | + * Simula un Torneo de Artes Marciales, al más puro estilo |
| 14 | + * de la saga, donde participarán diferentes luchadores, y el |
| 15 | + * sistema decidirá quién es el ganador. |
| 16 | + * |
| 17 | + * Luchadores: |
| 18 | + * - Nombre. |
| 19 | + * - Tres atributos: velocidad, ataque y defensa |
| 20 | + * (con valores entre 0 a 100 que tú decidirás). |
| 21 | + * - Comienza cada batalla con 100 de salud. |
| 22 | + * Batalla: |
| 23 | + * - En cada batalla se enfrentan 2 luchadores. |
| 24 | + * - El luchador con más velocidad comienza atacando. |
| 25 | + * - El daño se calcula restando el daño de ataque del |
| 26 | + * atacante menos la defensa del oponente. |
| 27 | + * - El oponente siempre tiene un 20% de posibilidad de |
| 28 | + * esquivar el ataque. |
| 29 | + * - Si la defensa es mayor que el ataque, recibe un 10% |
| 30 | + * del daño de ataque. |
| 31 | + * - Después de cada turno y ataque, el oponente pierde salud. |
| 32 | + * - La batalla finaliza cuando un luchador pierde toda su salud. |
| 33 | + * Torneo: |
| 34 | + * - Un torneo sólo es válido con un número de luchadores |
| 35 | + * potencia de 2. |
| 36 | + * - El torneo debe crear parejas al azar en cada ronda. |
| 37 | + * - Los luchadores se enfrentan en rondas eliminatorias. |
| 38 | + * - El ganador avanza a la siguiente ronda hasta que sólo |
| 39 | + * quede uno. |
| 40 | + * - Debes mostrar por consola todo lo que sucede en el torneo, |
| 41 | + * así como el ganador. |
| 42 | +""" |
| 43 | + |
| 44 | +import random |
| 45 | +import math |
| 46 | + |
| 47 | +class Fighter: |
| 48 | + def __init__(self, name: str, speed: int, attack: int, defense: int): |
| 49 | + self._name: str = name |
| 50 | + self._speed: int = speed |
| 51 | + self._attack: int = attack |
| 52 | + self._defense: int = defense |
| 53 | + self._health: float = 100 |
| 54 | + |
| 55 | + def execute_attack(self, opponent): |
| 56 | + print(f"'{self._name}' ataca a '{opponent._name}'") |
| 57 | + |
| 58 | + damage: float = 0 |
| 59 | + if opponent._defense >= self._attack: |
| 60 | + damage = self._attack * 0.1 # 10% |
| 61 | + else: |
| 62 | + damage = self._attack - opponent._defense |
| 63 | + |
| 64 | + if not opponent.activate_defense(): |
| 65 | + opponent._health -= damage |
| 66 | + print(f"'{opponent._name}' ha recibido '{damage}' de daño") |
| 67 | + print(f"Salud restante '{opponent._health}'\n") |
| 68 | + else: |
| 69 | + print(f"'{opponent._name}' ha esquivado el ataque.\n") |
| 70 | + |
| 71 | + def activate_defense(self) -> bool: |
| 72 | + return random.random() <= 0.2 # 20% |
| 73 | + |
| 74 | +# __________________________________________________________ |
| 75 | +class Battle: |
| 76 | + def __init__(self, fighter1, fighter2): |
| 77 | + self._fighter1 = fighter1 |
| 78 | + self._fighter2 = fighter2 |
| 79 | + print(f"__'{self._fighter1._name} VS '{self._fighter2._name}'__\n") |
| 80 | + |
| 81 | + def __combat(self, fighter_a, fighter_b) -> Fighter: |
| 82 | + while True: |
| 83 | + fighter_a.execute_attack(fighter_b) |
| 84 | + if fighter_b._health <= 0: |
| 85 | + print(f"--> '{fighter_a._name}' gana la batalla.__\n") |
| 86 | + return fighter_a |
| 87 | + |
| 88 | + fighter_b.execute_attack(fighter_a) |
| 89 | + if fighter_a._health <= 0: |
| 90 | + print(f"--> '{fighter_b._name}' gana la batalla.\n") |
| 91 | + return fighter_b |
| 92 | + |
| 93 | + def start(self) -> Fighter: |
| 94 | + if self._fighter1._speed > self._fighter2._speed: |
| 95 | + return self.__combat(self._fighter1, self._fighter2) |
| 96 | + else: |
| 97 | + return self.__combat(self._fighter2, self._fighter1) |
| 98 | + |
| 99 | +# __________________________________________________________ |
| 100 | +class Tournament: |
| 101 | + def __init__(self, fighter, battle, fighters: dict): |
| 102 | + self._fighter = fighter |
| 103 | + self._battle = battle |
| 104 | + self._fighters: dict = fighters |
| 105 | + |
| 106 | + def __is_power_of_2(self) -> bool: |
| 107 | + n: int = len(self._fighters) |
| 108 | + if n <= 1: |
| 109 | + return False |
| 110 | + return math.log2(n).is_integer() |
| 111 | + |
| 112 | + def __get_random_pairs(self) -> tuple: |
| 113 | + fighters_random = random.sample(list(self._fighters.keys()), 2) |
| 114 | + fighter1_data = {**self._fighters[fighters_random[0]], 'name': fighters_random[0]} |
| 115 | + fighter2_data = {**self._fighters[fighters_random[1]], 'name': fighters_random[1]} |
| 116 | + |
| 117 | + fighter1 = self._fighter(**fighter1_data) |
| 118 | + fighter2 = self._fighter(**fighter2_data) |
| 119 | + |
| 120 | + del self._fighters[fighters_random[0]] |
| 121 | + del self._fighters[fighters_random[1]] |
| 122 | + |
| 123 | + return fighter1, fighter2 |
| 124 | + |
| 125 | + def start_rounds(self, round_num = 1): |
| 126 | + if not self.__is_power_of_2(): |
| 127 | + print("El número de luchadores debe ser una potencia de 2.") |
| 128 | + return |
| 129 | + |
| 130 | + print(f"\n__Ronda #{round_num}__") |
| 131 | + next_round: dict = {} |
| 132 | + while True: |
| 133 | + fighter1, fighter2 = self.__get_random_pairs() |
| 134 | + battle = self._battle(fighter1, fighter2) |
| 135 | + winner = battle.start() |
| 136 | + |
| 137 | + next_round[winner._name] = { |
| 138 | + "speed": winner._speed, |
| 139 | + "attack": winner._attack, |
| 140 | + "defense": winner._defense |
| 141 | + } |
| 142 | + |
| 143 | + if len(self._fighters) == 0 and len(next_round) > 1: |
| 144 | + self._fighters = next_round |
| 145 | + self.start_rounds(round_num + 1) |
| 146 | + break |
| 147 | + |
| 148 | + if len(self._fighters) == 0 and len(next_round) == 1: |
| 149 | + print(f"\n--> El vencedor del torneo es '{winner._name}'.") |
| 150 | + break |
| 151 | + |
| 152 | +# __________________________________________________________ |
| 153 | +FIGHTERS: dict = { |
| 154 | + "Goku": {"speed": 100, "attack": 95, "defense": 85}, |
| 155 | + "Vegeta": {"speed": 95, "attack": 90, "defense": 90}, |
| 156 | + "Gohan": {"speed": 85, "attack": 95, "defense": 85}, |
| 157 | + "Freezer": {"speed": 90, "attack": 90, "defense": 90}, |
| 158 | + "Piccolo": {"speed": 90, "attack": 85, "defense": 90}, |
| 159 | + "Krillin": {"speed": 85, "attack": 75, "defense": 75}, |
| 160 | + "Cell": {"speed": 90, "attack": 95, "defense": 85}, |
| 161 | + "Majin Buu": {"speed": 80, "attack": 85, "defense": 95}, |
| 162 | +} |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + print("Simulación del Torneo de Artes Marciales") |
| 166 | + tournament = Tournament(Fighter, Battle, FIGHTERS) |
| 167 | + tournament.start_rounds() |
| 168 | + |
| 169 | +# end |
0 commit comments