-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattle.py
More file actions
126 lines (113 loc) · 6.19 KB
/
Copy pathBattle.py
File metadata and controls
126 lines (113 loc) · 6.19 KB
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
import copy
from Trainer import Trainer
class Battle:
# This class represents battles between Pokemons and their Trainers.
def __init__(self, trainer1, trainer2):
# Validation of the Input.
if (not isinstance(trainer1, Trainer)) and (not isinstance(trainer2, Trainer)):
raise TypeError("Expected 2 Trainers as input")
self.__trainer1 = copy.copy(trainer1)
self.__trainer2 = copy.copy(trainer2)
# This method takes 2 Indexes which represent a pokemon in the Trainers lists, and making them fight each other.
# The method returns a tuple with the information of how many rounds the battle took, and who won.
def dual_battle(self, trainer1_pokemon_id, trainer2_pokemon_id):
# Defining the 2 pokemons whom are fighting
pokemon1 = self.__trainer1.get_pokemon_lst()[trainer1_pokemon_id]
pokemon2 = self.__trainer2.get_pokemon_lst()[trainer2_pokemon_id]
# Validation if any of the pokemons can fight.
if (not pokemon1.can_fight()) and (not pokemon2.can_fight()):
no_winners = (0, 0)
return no_winners
# Validation if one of the pokemons can't fight.
if (pokemon1.can_fight()) and (not pokemon2.can_fight()):
winner_1 = (0, 1)
return winner_1
if (not pokemon1.can_fight()) and (pokemon2.can_fight()):
winner_2 = (0, 2)
return winner_2
result = self.help_dual_battle(pokemon1, pokemon2)
return result
# Sub method for the dual_battle. The input requires 2 Pokemons
def help_dual_battle(self, pokemon1, pokemon2):
rounds = 0
while (pokemon1.can_fight()) and (pokemon2.can_fight()):
rounds += 1
pokemon1.attack(pokemon2)
if (not pokemon1.can_fight()) or (not pokemon2.can_fight()):
break
pokemon2.attack(pokemon1)
if (pokemon1.can_fight()) and (not pokemon2.can_fight()):
winner_1 = (rounds, 1)
return winner_1
elif (not pokemon1.can_fight()) and (pokemon2.can_fight()):
winner_2 = (rounds, 2)
return winner_2
else:
no_winners = (rounds, 0)
return no_winners
# This method manages a battle between 2 Trainers.
def total_battle(self):
counter1 = 0
trainer1_pokemon_id = -1
rival1 = None
# A loop that runs on the pokemon list to check if all of them can't fight.
for i in range(self.__trainer1.__len__()):
if not self.__trainer1.get_pokemon_lst()[i].can_fight():
counter1 += 1
else:
# Defining the first pokemon in the list as the first rival (a pokemon object).
rival1 = self.__trainer1.get_pokemon_lst()[i]
trainer1_pokemon_id = i
break
# This condition checks if it's a technical win for Trainer 2 or a tie.
if counter1 == self.__trainer1.__len__():
counter2 = 0
for j in range(self.__trainer2.__len__()):
if not self.__trainer2.get_pokemon_lst()[j].can_fight():
counter2 += 1
else:
print("Trainer " + str(self.__trainer2.get_name()) + " won the battle in 0 rounds")
break
if counter2 == self.__trainer2.__len__():
print("The battle ended with a draw")
# Trainer 1 has his first pokemon ready to go. time to check if Trainer 2's has anyone ready.
else:
counter2 = 0
for j in range(self.__trainer2.__len__()):
if not self.__trainer2.get_pokemon_lst()[j].can_fight():
counter2 += 1
else:
break
if counter2 == self.__trainer2.__len__():
print("Trainer " + str(self.__trainer1.get_name()) + " won the battle in 0 rounds")
else: # Found a healthy pokemon, lets check who is the most effective against him out of Trainer 2's squad.
trainer2_pokemon_id = self.__trainer2.max_damage(rival1) # returned the index of the selected pokemon.
# After finding the 2 eligible pokemon contenders, lets make them battle.
battle = self.dual_battle(trainer1_pokemon_id, trainer2_pokemon_id)
total_rounds = battle[0]
winner = battle[1]
# using the sub method is_all_can_fight() in a while loop.
# The loop will run until one of the Trainers runs out of durable pokemons to use in the battle.
while (not self.__trainer1.is_all_can_not_fight()) and (not self.__trainer2.is_all_can_not_fight()):
if winner == 0:
trainer1_pokemon_id = self.__trainer1.next_in_line()
trainer2_pokemon_id = self.__trainer2.max_damage(self.__trainer1.get_pokemon_lst()[self.__trainer1.next_in_line()])
battle = self.dual_battle(trainer1_pokemon_id, trainer2_pokemon_id)
total_rounds += battle[0]
winner = battle[1]
elif winner == 1:
trainer2_pokemon_id = self.__trainer2.max_damage(self.__trainer1.get_pokemon_lst()[trainer1_pokemon_id])
battle = self.dual_battle(trainer1_pokemon_id, trainer2_pokemon_id)
total_rounds += battle[0]
winner = battle[1]
else: # winner == 2.
trainer1_pokemon_id = self.__trainer1.max_damage(self.__trainer2.get_pokemon_lst()[trainer2_pokemon_id])
battle = self.dual_battle(trainer1_pokemon_id, trainer2_pokemon_id)
total_rounds += battle[0]
winner = battle[1]
if winner == 0:
print("The battle ended with a draw")
elif winner == 1:
print("Trainer " + str(self.__trainer1.get_name()) + " won the battle in " + str(total_rounds) + " rounds")
else: # winner == 2.
print("Trainer " + str(self.__trainer2.get_name()) + " won the battle in " + str(total_rounds) + " rounds")