-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrainer.py
More file actions
186 lines (157 loc) · 7.9 KB
/
Copy pathTrainer.py
File metadata and controls
186 lines (157 loc) · 7.9 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import copy
from Pokemon import Pokemon
from Charmander import Charmander
from Charmeleon import Charmeleon
from Charizard import Charizard
from Squirtle import Squirtle
from Wartortle import Wartortle
from Blastoise import Blastoise
class Trainer:
# This class contains all the necessary attributes for a Pokemon Trainer.
def __init__(self, name, age, exp_modifier, pokemons_lst=None):
# Validation of the Input.
if name == "":
raise ValueError("The name must contain a String of a positive number of characters")
if not isinstance(name, str):
raise TypeError("The name must contain a String of a positive number of characters")
self.__name = name
# Validation of the Input.
if (age < 16) and (age > 120):
raise ValueError("The age must be an Integer between 16-120")
if not isinstance(age, int):
raise TypeError("The age must be an Integer between 16-120")
self.__age = age
# Validation of the Input.
if (exp_modifier < 1.5) and (exp_modifier > 12.5):
raise ValueError("The experience modifier variable must be a float between 1.5 - 12.5")
if (not isinstance(exp_modifier, float)) and (not isinstance(exp_modifier, int)):
raise TypeError("The experience modifier variable must be a float between 1.5 - 12.5")
self.__exp_modifier = float(exp_modifier)
if pokemons_lst is None:
pokemons_lst = []
self.__pokemons_lst = copy.deepcopy(pokemons_lst)
# This method return the number of Pokemons which the Trainer has.
def __len__(self):
return len(self.__pokemons_lst)
def __repr__(self):
res = "The Trainer " + self.__name + " is " + str(self.__age) + " years old and has the following pokemons (" + \
str(self.__len__()) + " in total):"
for i in range(self.__len__()):
res += "\n" + " " + str(self.__pokemons_lst[i].__repr__())
return res
# This method returns the name of the Trainer.
def get_name(self):
return self.__name
# This method returns the age of the Trainer.
def get_age(self):
return self.__age
# This method returns the experience modifier of the Trainer.
def get_exp_modifier(self):
return self.__exp_modifier
# This method returns a list of the pokemons which the Trainer has.
def get_pokemon_lst(self):
return self.__pokemons_lst
# This method changes a pokemon in the pokemon list according to the given index and inputted pokemon.
def change_pokemon_lst(self, pokemon, pokemon_id):
# Validation of inputs.
if not isinstance(pokemon, Pokemon):
raise TypeError("The expected input for pokemon must be a Pokemon object")
if not isinstance(pokemon_id, int):
raise TypeError("The expected input for pokemon_id must be an Integer between 0-" + str(self.__len__() - 1))
if (pokemon_id < 0) and (pokemon_id > self.__len__() - 1):
raise ValueError("The expected input for pokemon_id must be an index between 0-" + str(self.__len__() - 1))
# A for loop for exchanging the required pokemon with the one in the given index's spot.
for i in range(self.__len__() - 1):
if i == pokemon_id:
self.__pokemons_lst[pokemon_id] = pokemon
# This method represents catching a free Pokemon according the variables of the Trainer and the Pokemon itself.
# the method will check if the pokemon can be caught. If so, the pokemon_lst will be updated.
def catch_pokemon(self, pokemon):
# Defining the capture chances of the given pokemon.
capture_chances = ((pokemon.get_catch_rate()) * self.__exp_modifier * ((100 - pokemon.get_hit_points()) / 100))
if capture_chances > 50:
self.__pokemons_lst.append(pokemon)
print(self.__name + " caught " + pokemon.get_name())
else:
print(self.__name + " couldn't catch " + pokemon.get_name())
# A method that returns the power of the Trainer. Useful for the comparison operators.
def get_total_power(self):
total_power = 0
for i in range(self.__len__()):
total_power += self.__pokemons_lst[i].get_hit_points()
return total_power
# The next six methods will be comparison operators for comparing 2 Trainers, and defining who is the strongest.
# The stronger Trainer is determined by the total amount of the HP of his entire Pokemon squad.
def __eq__(self, other): # Equality operator.
if self.get_total_power() == other.get_total_power():
return True
else:
return False
def __ne__(self, other): # Inequality operator.
if self.get_total_power() != other.get_total_power():
return True
else:
return False
def __gt__(self, other): # Greater than operator.
if self.get_total_power() > other.get_total_power():
return True
else:
return False
def __lt__(self, other): # Less than operator.
if self.get_total_power() < other.get_total_power():
return True
else:
return False
def __ge__(self, other): # Greater than or Equal to operator.
if self.get_total_power() >= other.get_total_power():
return True
else:
return False
def __le__(self, other): # Less than or Equal to operator.
if self.get_total_power() <= other.get_total_power():
return True
else:
return False
# This method creates a new Trainer by combining two Trainers into a brand new one.
def __add__(self, other):
if not isinstance(other, Trainer): # Validation of Input.
raise TypeError("Wrong input, expected a Trainer object")
if self.__ge__(other): # Defining new name based on the stronger trainer.
name = self.__name + "-" + other.get_name()
else:
name = other.get_name() + "-" + self.__name
# The new age will be the average age between the two trainers.
age = int((self.__age + other.get_age()) // 2)
# The new experience modifier will be average between the two trainers.
exp_modifier = float((self.__exp_modifier + other.get_exp_modifier()) / 2)
# The new pokemon squad will be a united list of all the pokemons combined.
# The order is defined by the strongest Trainer.
if self.__ge__(other):
pokemons_lst = copy.copy(self.__pokemons_lst + other.get_pokemon_lst())
else:
pokemons_lst = copy.copy(other.get_pokemon_lst() + self.__pokemons_lst)
new_trainer = Trainer(name, age, exp_modifier, pokemons_lst)
return new_trainer
# A sub method for total_battle in class Battle. returns the index of the max get_damage out of a pokemon list.
# Self is represented as Trainer 2, pokemon is Trainer 1's pokemon.
def max_damage(self, pokemon):
max_damage = 0
trainer2_pokemon_id = -1
for i in range(self.__len__()):
if (self.get_pokemon_lst()[i].get_damage(pokemon) > max_damage) and (self.get_pokemon_lst()[i].can_fight()):
max_damage = self.get_pokemon_lst()[i].get_damage(pokemon)
trainer2_pokemon_id = i
return trainer2_pokemon_id
# A sub method for total_battle in class Battle. returns the next healthy pokemon's index.
def next_in_line(self):
trainer1_pokemon_id = -1
for i in range(self.__len__()):
if self.get_pokemon_lst()[i].can_fight():
trainer1_pokemon_id = i
return trainer1_pokemon_id
# A sub method for total_battle in class Battle. returns a boolean value if all the pokemons cant fight or not.
def is_all_can_not_fight(self):
for i in range(self.__len__()):
if self.get_pokemon_lst()[i].can_fight():
return False
return True