Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My code wont work, it keeps saying to check the logic of the add attack. Can anyone help me see what im doing wrong? class

My code wont work, it keeps saying to check the logic of the add attack. Can anyone help me see what im doing wrong?

image text in transcribed

class Monster(): def __init__(self, name, hp=20): self.exp = 0 self.name = name self.type = "Normal" self.current_hp = hp self.max_hp = hp 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 add_attack(self, attack_name): # check if attack_name is in the possible list and not already in self.attacks if attack_name in self.possible_attacks: image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedif attack_name not in self.attacks: if len(self.attacks)

def remove_attack(self, attack_name): # check if attack name is in the attacks list if attack_name not in self.attacks: return False else: self.attacks.pop(attack_name)

if len(self.attacks) == 0: # add wait if there is no attacks in self.attacks self.attacks["wait"] = 0 return True

# win_fight should add 5 to monster self.exp and reset curent_hp to max_hp # lose_fight reset hp but add 1 exp to self.exp

def win_fight(self): self.exp += 5 self.current_hp = self.max_hp

def lose_fight(self): self.exp += 1 self.current_hp = self.max_hp

# monster1 goes first # each monster takes a turn using one attack move

class Ghost(Monster): def win_fight(self): self.exp += 5 # apparently not going through the win_fight of monster class for the exp increase self.current_hp = self.max_hp u = 10 k = 19 if self.exp in range(u, k): u += 10 k += 10 self.max_hp += 5

def lose_fight(self): self.exp += 1 self.current_hp = self.max_hp

class Dragon(Monster): def win_fight(self): self.exp += 5 self.current_hp = self.max_hp c = 10 v = 19 if self.exp in range(c, v): c += 10 v += 10 for key in self.attacks: self.attacks[key] += 1

def lose_fight(self): self.exp += 1 self.current_hp = self.max_hp

def monster_fight(monster1, monster2): # return round1, monster1, moves1#movies of winner # if both monsters have wait as attack if monster1.attacks == {"wait": 0} and monster2.attacks == {"wait": 0}: return -1, None, None movecopy = {} # dict of sorted monster1 attacks movecopy2 = {} # dict of sorted monster2 attacks # used StackOverflow to find a method to sort dict by values for x, y in sorted(monster1.attacks.items(), key=lambda z: z[1], reverse=True): movecopy[x] = y for c, d in sorted(monster2.attacks.items(), key=lambda z: z[1], reverse=True): movecopy2[c] = d # print(movecopy) # print(movecopy2)

M1_list = [] # list of keys of attacks for key in movecopy.keys(): M1_list.append(key) M2_list = [] for key in movecopy2.keys(): M2_list.append(key) # print(M1_list) # print(M2_list) # print(movecopy['ice_storm']) round = 0

#count1 = 0 #count2 = 0 while (monster1.current_hp > 0) and (monster2.current_hp > 0): if monster1.current_hp monster2.current_hp: # check for winner monster1.win_fight() monster2.lose_fight() return round1, monster1, moves1 elif monster1.current_hp

a = Dragon("a", 45) b = Ghost("b", 45) a.add_attack("ice_storm") b.add_attack("double_hit") b.remove_attack("wait") round1, winner, moves = monster_fight(a, b) print(round1) print(winner.name) print(winner.attacks) print(winner.exp) print(winner.max_hp) print(moves) round1, winner, moves = monster_fight(a, b) print(round1) print(winner.name) print(winner.attacks) print(winner.exp) print(winner.max_hp) print(moves)

Construct a class "Monster" with the following attributes: 1. self.name (a string) 2. self.type (a string, default is 'Normal') 3. self.current_hp (int, start out equal to max_hp) 4. self.max_hp (int, is given as input when the class instance is created, default is 20) 5. self attacks (a dictionary of all known attacks) 6. self possible_attacks (a dictionary of all possible attacks) The dictionary of possible_attacks will map the name of an attack (the key) to how many points of damage the attack does. They must be of the following list: 1. sneak attack: 1 2. slash: 2 3. ice_storm: 3 4. fire_storm: 3 5. whirlwind: 3 6. earthquake: 2 7. double_hit: 4 8. tornado: 4 9. wait: 0 Every monster will start out with only the "wait" attack within self attacks. You will need to construct the method add_attack and remove_attack. Both methods will take in an attack name as a parameter. A monster can only have a maximum 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 the 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 attack or an attack that has not been learned return false. 299672. 1720236.qx3zqy7 = name class Monster(): def __init__(self, name, hp=20): self.exp 0 self.name self.type "Normal" self.current_hp = hp self.max_hp = hp 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 } dat add allaellenle , def add_attack(self, attack_name): if attack_name in self.possible_attacks: if attack_name not in self.attacks: if len(self.attacks)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2017 Skopje Macedonia September 18 22 2017 Proceedings Part 3 Lnai 10536

Authors: Yasemin Altun ,Kamalika Das ,Taneli Mielikainen ,Donato Malerba ,Jerzy Stefanowski ,Jesse Read ,Marinka Zitnik ,Michelangelo Ceci ,Saso Dzeroski

1st Edition

3319712721, 978-3319712727

More Books

Students also viewed these Databases questions