In: Computer Science
PYTHON PLEASE:
Construct a class “Monster” with the following attributes:
The dictionary of possible_attacks will map the name of an attack (the key) to how many points of damage the attack does. These are all the possible attacks with their corresponding damage points.
Every monster will start out with only the “wait” attack within self.attacks, but you will need to construct the method add_attacks and method remove_attacks. Both methods will take in an attack_name as a parameter.
A monster can only to have a max of four attacks at a time. If you add an attack when the monster already has four, the weakest one should be dropped automatically. If there is a tie for weakest attack, drop the attack that comes first alphabetically. If adding the attack ended successfully return True if you try to add an invalid attack return False.
If all of a monster’s attacks are removed, “wait” should automatically be added again, so that every monster always has at least 1 attack. If removing an attack ended successfully return True if you try to remove an invalid or an attack which has not been learned return False.
Monster with attacks
-----------------------------------------------------code start-------------------------------------------------------------------------------------------
class Monster:
def __init__(self, name, typee = 'Normal', max_hpm = 20, exp =
0):
self.name = name
self.type = typee
self.current_hp = max_hpm
self.max_hp = max_hpm
self.exp = 0
self.attacks = {"wait":0}
self.possible_attacks = {"sneak_attack": 1,"slash": 2,"ice_storm":
3,"fire_storm": 3,"whirlwind": 3,"earthquake": 2,"double_hit":
4,"wait": 0}
def __repr__(self):
return f" monster {self.name} has attacks
{list(self.attacks.keys())}"
def add_attacks(self,attack):
if "wait" in self.attacks.keys():
del self.attacks["wait"]
if len(self.attacks) < 4 and (attack not in
self.attacks.keys()):
self.attacks.update({attack:self.possible_attacks[attack]})
return True
elif attack not in self.attacks.keys():
# len(self.attacks) > 4:
# print("delete and insert")
min_val = sorted(list(self.attacks.values()))
mini = min_val[0]
min_val_count = min_val.count(mini)
# print(mini, min_val_count)
if min_val_count == 1:
delete_key =
list(self.attacks.keys())[list(self.attacks.values()).index(mini)]
# print(delete_key)
del self.attacks[delete_key]
self.attacks.update({attack:self.possible_attacks[attack]})
elif min_val_count > 1:
keys = []
for k,v in self.attacks.items():
if v == mini:
keys.append(k)
# print("unsorted",keys)
keys = sorted(keys)
print(keys)
# print("deleting alpha",keys[0])
del self.attacks[keys[0]]
self.attacks.update({attack:self.possible_attacks[attack]})
return True
else:
return False
def remove_attack(self,attack):
if len(self.attacks) != 0 and (attack in
self.attacks.keys()):
del self.attacks[attack]
if len(self.attacks) == 0:
self.attacks.update({"wait":0})
return True
else:
return False
--------------------------------------------------------end-----------------------------------------------------------------------------------------
readme usage:
1) creating an object: cj = Monster("cj")
2) Adding attack: (it follows the guidelines and constraints you provided)
Please rate my answer, if you found it useful and If you still need the jupyter notebook.