|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +import random |
| 3 | +import time |
| 4 | + |
| 5 | + |
| 6 | +class Interfaz(ABC): |
| 7 | + @abstractmethod |
| 8 | + def __init__(self, vida) -> None: |
| 9 | + self.vida = vida |
| 10 | + |
| 11 | + @abstractmethod |
| 12 | + def atacar(self) -> int: |
| 13 | + pass |
| 14 | + |
| 15 | + @abstractmethod |
| 16 | + def evadir(self) -> bool: |
| 17 | + pass |
| 18 | + |
| 19 | + @abstractmethod |
| 20 | + def devolver_vida(self) -> int: |
| 21 | + pass |
| 22 | + |
| 23 | + @abstractmethod |
| 24 | + def recibir_daño(self, daño) -> None: |
| 25 | + pass |
| 26 | + |
| 27 | + |
| 28 | +class Wolverine(Interfaz): |
| 29 | + def __init__(self, vida) -> None: |
| 30 | + self.vida = vida |
| 31 | + |
| 32 | + def atacar(self) -> int: |
| 33 | + daño_aleatorio = random.randint(10, 120) |
| 34 | + return daño_aleatorio |
| 35 | + |
| 36 | + def evadir(self) -> bool: |
| 37 | + num_evasion = random.randint(0, 100) |
| 38 | + return num_evasion < 20 |
| 39 | + |
| 40 | + def devolver_vida(self) -> int: |
| 41 | + return self.vida |
| 42 | + |
| 43 | + def recibir_daño(self, daño) -> None: |
| 44 | + self.vida -= daño |
| 45 | + |
| 46 | + |
| 47 | +class Deadpool(Interfaz): |
| 48 | + def __init__(self, vida) -> None: |
| 49 | + self.vida = vida |
| 50 | + |
| 51 | + def atacar(self) -> int: |
| 52 | + daño_aleatorio = random.randint(10, 100) |
| 53 | + return daño_aleatorio |
| 54 | + |
| 55 | + def evadir(self) -> bool: |
| 56 | + num_evasion = random.randint(0, 100) |
| 57 | + return num_evasion < 25 |
| 58 | + |
| 59 | + def devolver_vida(self) -> int: |
| 60 | + return self.vida |
| 61 | + |
| 62 | + def recibir_daño(self, daño) -> None: |
| 63 | + self.vida -= daño |
| 64 | + |
| 65 | + |
| 66 | +wolverine = Wolverine(500) |
| 67 | +deadpool = Deadpool(500) |
| 68 | + |
| 69 | + |
| 70 | +class Simulador: |
| 71 | + def __init__(self) -> None: |
| 72 | + self.turno = 0 |
| 73 | + |
| 74 | + def siguiente_turno(self): |
| 75 | + print("Cambio de turno") |
| 76 | + time.sleep(1) |
| 77 | + self.turno += 1 |
| 78 | + |
| 79 | + def omitir_turno(self): |
| 80 | + print("Turno omitido por golpe critico") |
| 81 | + time.sleep(1) |
| 82 | + self.turno += 2 |
| 83 | + |
| 84 | + def simular(self): |
| 85 | + """Si la vida de uno es menor a 0 finaliza el combate, |
| 86 | + si el turno es par ataca Wolverino, si no Deadpool""" |
| 87 | + |
| 88 | + while True: |
| 89 | + vida_wolverine = wolverine.devolver_vida() |
| 90 | + vida_deadpool = deadpool.devolver_vida() |
| 91 | + if vida_deadpool <= 0: |
| 92 | + print("Wolverine derroto a su oponente") |
| 93 | + break |
| 94 | + elif vida_wolverine <= 0: |
| 95 | + print("Deadpool derroto a su oponente") |
| 96 | + break |
| 97 | + else: |
| 98 | + print( |
| 99 | + f"Puntos de vida\nWolverine: {vida_wolverine}\nDeadpool: {vida_deadpool}" |
| 100 | + ) |
| 101 | + print(f"\nTurno {self.turno}") |
| 102 | + |
| 103 | + if self.turno % 2 == 0: |
| 104 | + daño = wolverine.atacar() |
| 105 | + exito_evadir = deadpool.evadir() |
| 106 | + if exito_evadir: |
| 107 | + print("Deadpool ha evadido con exito") |
| 108 | + Simulador.siguiente_turno(self) |
| 109 | + |
| 110 | + else: |
| 111 | + deadpool.recibir_daño(daño) |
| 112 | + |
| 113 | + if daño == 120: |
| 114 | + print("Wolverine inflinge golpe critico") |
| 115 | + print("Volverá a atacar en el siguiente turno") |
| 116 | + Simulador.omitir_turno(self) |
| 117 | + |
| 118 | + print(f"Wolverine inflinge {daño} puntos de daño.") |
| 119 | + Simulador.siguiente_turno(self) |
| 120 | + |
| 121 | + else: |
| 122 | + daño = deadpool.atacar() |
| 123 | + exito_evadir = wolverine.evadir() |
| 124 | + if exito_evadir: |
| 125 | + print("Wolverine ha evadido con exito") |
| 126 | + Simulador.siguiente_turno(self) |
| 127 | + |
| 128 | + else: |
| 129 | + wolverine.recibir_daño(daño) |
| 130 | + if daño == 100: |
| 131 | + print("Deadpool inflinge golpe critico") |
| 132 | + print("Volverá a atacar en el siguiente turno") |
| 133 | + Simulador.omitir_turno(self) |
| 134 | + |
| 135 | + print(f"Deadpool inflinge {daño} puntos de daño.") |
| 136 | + Simulador.siguiente_turno(self) |
| 137 | + |
| 138 | + |
| 139 | +simulador = Simulador() |
| 140 | +simulador.simular() |
0 commit comments