1+ import random
2+ class Figther :
3+
4+ def __init__ (self ,name ,speed ,attack ,defense ):
5+ self .name = name
6+ self .speed = speed
7+ self .attack = attack
8+ self .defense = defense
9+ self ._health = 100
10+ def setHeath (self ,value :int ):
11+ self ._health = value
12+ def getHealth (self ):
13+ return self ._health
14+ def getName (self ):
15+ return self .name
16+ def getAttack (self ):
17+ return self .attack
18+ def getDefense (self ):
19+ return self .defense
20+ def getSpeed (self ):
21+ return self .speed
22+ def reduceHealth (self ,value ):
23+ self ._health -= value
24+
25+ def is_pow_of_two (number ):
26+ if number <= 0 :
27+ return False
28+ return (number & (number - 1 )) == 0
29+ def simulate_combat (figther1 :Figther ,figther2 :Figther )-> Figther :
30+
31+ figth_turn = []
32+ figther1 .setHeath (100 )
33+ figther2 .setHeath (100 )
34+ if figther1 .getSpeed () > figther2 .getSpeed ():
35+ figth_turn .append (figther1 )
36+ figth_turn .append (figther2 )
37+ else :
38+ figth_turn .append (figther2 )
39+ figth_turn .append (figther1 )
40+
41+ i = 0
42+ turno = 1
43+ print (f'Combate entre { figth_turn [i ].getName ()} y { figth_turn [(i + 1 )% 2 ].getName ()} ' )
44+ while figther1 .getHealth () > 0 and figther2 .getHealth () > 0 :
45+ print (f'Turno { turno } ' )
46+ damage = abs (figth_turn [i ].getAttack () - figth_turn [(i + 1 )% 2 ].getDefense ())
47+ skip = random .randint (0 ,100 )
48+ if skip > 20 :
49+ #Oponent doesn't skip the attack
50+ if figth_turn [(i + 1 )% 2 ].getDefense () > damage :
51+ figth_turn [(i + 1 )% 2 ].reduceHealth (damage * 0.1 )
52+ else :
53+ figth_turn [(i + 1 )% 2 ].reduceHealth (damage )
54+ print (f'Luchador { figth_turn [i ].getName ()} ataca con { damage } a { figth_turn [(i + 1 )% 2 ].getName ()} salud restante es: { figth_turn [(i + 1 )% 2 ].getHealth ()} ' )
55+ else :
56+ print (f'{ figth_turn [(i + 1 )% 2 ].getName ()} esquiva el ataque de { figth_turn [i ].getName ()} ' )
57+ i = (i + 1 ) % 2
58+ turno += 1
59+
60+ if figther1 .getHealth () > 0 :
61+ print (f'Ganador del combate es: { figther1 .getName ()} ' )
62+ return figther1
63+ else :
64+ print (f'El ganador del combate es: { figther2 .getName ()} ' )
65+ return figther2
66+
67+ def main ():
68+
69+ figthers = []
70+ figther = Figther ("Goku" ,98 ,88 ,70 )
71+ figthers .append (figther )
72+ figther = Figther ("Krilin" ,90 ,80 ,50 )
73+ figthers .append (figther )
74+ figther = Figther ("Vegeta" ,95 ,85 ,45 )
75+ figthers .append (figther )
76+ figther = Figther ("Tortuga duende" ,80 ,90 ,53 )
77+ figthers .append (figther )
78+ figther = Figther ("Piccolo" ,85 ,78 ,51 )
79+ figthers .append (figther )
80+ figther = Figther ("Freezer" ,94 ,81 ,42 )
81+ figthers .append (figther )
82+ figther = Figther ("Celula" ,90 ,89 ,65 )
83+ figthers .append (figther )
84+ figther = Figther ("Zarbon" ,50 ,50 ,30 )
85+ figthers .append (figther )
86+ if is_pow_of_two (len (figthers )):
87+ number_rounds = len (figthers )// 2
88+ round = figthers .copy ()
89+
90+ for round_number in range (1 ,number_rounds ):
91+ print (f'Empezando ronda { round_number } ' )
92+ winners = []
93+ while len (round ) > 0 :
94+ choice = random .randint (0 ,len (round )- 1 )
95+ figther1 = round [choice ]
96+ del (round [choice ])
97+ choice = random .randint (0 ,len (round )- 1 )
98+ fighter2 = round [choice ]
99+ del (round [choice ])
100+ winner = simulate_combat (figther1 = figther1 ,figther2 = fighter2 )
101+ winners .append (winner )
102+ round = winners .copy ()
103+ if len (round ) == 1 :
104+ print (f'Ganador del torneo es: { round [0 ].getName ()} ' )
105+ else :
106+ print ("Numero de participantes debe ser potencia de 2" )
107+
108+
109+ main ()
0 commit comments