Question: class Monster(): # constructor def __init__(self, name, hp=20): self.exp = 0 self.name = name self.hp = hp self.attacks = {} # Adds an attack to



class Monster(): # constructor def __init__(self, name, hp=20): self.exp = 0 self.name = name self.hp = hp self.attacks = {} # Adds an attack to monster's attack dictionary def add_attack(self, attack_name): self.attacks.update(attack_name) # Removes an attack from attack dictionary def remove_attack(self, attack_name): self.attacks.pop(attack_name) # Resets hp and increases experience by 5 def win_fight(self): self.exp += 5 self.hp = 20 # Resets hp and increases experience by 5 def lose_fight(self): self.exp += 1 self.hp = 20 # Fight between 2 monsters and returns rounds, winner monster and attacks used def monster_fight(monster1, monster2): # Sort attacks from powerful to weakest sorted(monster1.attacks.items()) sorted(monster2.attacks.items()) # Separate attack names and corresponding values att1_names = [] att1_value = [] attacks1 = [] for key in monster1.attacks: att1_names.append(key) att1_value.append(monster1.attacks[key]) i, len1 = 0, len(att1_names) # Separate attack names and corresponding values att2_names = [] att2_value = [] attacks2 = [] for key in monster2.attacks: att2_names.append(key) att2_value.append(monster2.attacks[key]) j, len2 = 0, len(att2_names) # Round count rnd = 1 # Start fight while monster1.hp > 0 and monster2.hp > 0: # Edge case if att1_names[i] == "wait" and att2_names[j] == "wait": return -1, None, None # Monster 1 attacks first monster2.hp -= att1_value[i] attacks1.append(att1_names[i]) i = (i+1)%len1 # Update attack index in circular fashion # If monster 2 loses if monster2.hp 13.12 PA4 Q2: Fight! Now we want a way to make our monsters fight! Before two monsters can fight, we need to give 2 new class methods that update their stats. Implement a method for "win_fight" and "lose_fight. Win_fight should add 5 to the monster's self.exp and reset their hp to max_hp. "lose_fight" should also reset their hp but only adds 1 exp to self.exp. Now write a function that takes 2 instances of the monster class and makes them fight. This function should be defined outside the Monster class, i.e. it is not a Monster method. A fight goes as follows: 1. The monster that entered as the first function goes first. 2. Each monster takes a turn using one attack move. The monster selects this attack move from the strongest to the weakest in a circular function. For example: A monster has a dictionary of possible attack as follows: ["fire_storm": 3, "double_hit": 4, "earthquake": 2, "ice_storm": 3] Monster will select the following attacks: Round No Move Explanation 1 double_hit Highest hit points 2 fire_storm Same hit points as ice_storm but comes first alphabetically 3 ice_storm 4 earthquake 5 double_hit 6 fire_storm 7 ice_storm And so on. 3. An attack is always successful and will decrease the opponent's hp by the given number of points in self attacks dictionary. The monsters continue taking turns until their current hp becomes less than or equal to zero. 4. At this point, the win_fight and lose_fight method should be invoked. Once this complete, return 3 things from the function. o Round_number o Monster that won (return the corresponding Monster object) o List of attacks the monster used Special Edge Case: If both monster only have "wait" as an attack, return -1 (round_number) None (for monster name that won) None (for list of attack that monster use) 2996721716238.qx3zqy7 LAB ACTIVITY 13.12.1: PA4 Q2: Fight! 0/30 main.py Load default template... 1 class Monster(): def __init__(self, name, hp=20): Self.exp = 0 #your code here 5 def add_attack(self, attack_name): 6 pass #your code here 7 def remove_attack(self, attack_name): pass #your code here 9 def win_fight(self): 10 pass #your code here 11 def lose_fight(self): 12 pass #your code here 13 14 def monster_fight(monsteri, monster2): 15 pass #your code here 16 Develop mode Submit mode Run your program as often as you'd like, before submitting for grading. Below, type any needed input values in the first box, then click Run program and observe the program's output in the second box Enter program input (optional) If your code requires input values, provide them here. Run program Input (from above) main.py (Your program) Output (shown below) Program output displayed here Submit for grading Signature of your work What is this? 2/28.. U---- M---10 R-10-10- S1010----- U10-1010-----10-1010--10---1010----1010-- 100--10 M------10 W0..3/10 Latest submission - 7:05 PM PST on 03/10/21 Total score: 0 / 30 Only show failing tests Download this submission 1: Test 14 0/5 Traceback (most recent call last): TypeError: add_attack() missing 1 required positional argument: 'attack_point' 2: Test 2 A 0/5 Traceback (most recent call last): TypeError: 'bool' object is not iterable 3: Test 3 4 0/20 Traceback (most recent call last): TypeError: add_attack() missing 1 required positional argument: 'attack_point' 13.12 PA4 Q2: Fight! Now we want a way to make our monsters fight! Before two monsters can fight, we need to give 2 new class methods that update their stats. Implement a method for "win_fight" and "lose_fight. Win_fight should add 5 to the monster's self.exp and reset their hp to max_hp. "lose_fight" should also reset their hp but only adds 1 exp to self.exp. Now write a function that takes 2 instances of the monster class and makes them fight. This function should be defined outside the Monster class, i.e. it is not a Monster method. A fight goes as follows: 1. The monster that entered as the first function goes first. 2. Each monster takes a turn using one attack move. The monster selects this attack move from the strongest to the weakest in a circular function. For example: A monster has a dictionary of possible attack as follows: ["fire_storm": 3, "double_hit": 4, "earthquake": 2, "ice_storm": 3] Monster will select the following attacks: Round No Move Explanation 1 double_hit Highest hit points 2 fire_storm Same hit points as ice_storm but comes first alphabetically 3 ice_storm 4 earthquake 5 double_hit 6 fire_storm 7 ice_storm And so on. 3. An attack is always successful and will decrease the opponent's hp by the given number of points in self attacks dictionary. The monsters continue taking turns until their current hp becomes less than or equal to zero. 4. At this point, the win_fight and lose_fight method should be invoked. Once this complete, return 3 things from the function. o Round_number o Monster that won (return the corresponding Monster object) o List of attacks the monster used Special Edge Case: If both monster only have "wait" as an attack, return -1 (round_number) None (for monster name that won) None (for list of attack that monster use) 2996721716238.qx3zqy7 LAB ACTIVITY 13.12.1: PA4 Q2: Fight! 0/30 main.py Load default template... 1 class Monster(): def __init__(self, name, hp=20): Self.exp = 0 #your code here 5 def add_attack(self, attack_name): 6 pass #your code here 7 def remove_attack(self, attack_name): pass #your code here 9 def win_fight(self): 10 pass #your code here 11 def lose_fight(self): 12 pass #your code here 13 14 def monster_fight(monsteri, monster2): 15 pass #your code here 16 Develop mode Submit mode Run your program as often as you'd like, before submitting for grading. Below, type any needed input values in the first box, then click Run program and observe the program's output in the second box Enter program input (optional) If your code requires input values, provide them here. Run program Input (from above) main.py (Your program) Output (shown below) Program output displayed here Submit for grading Signature of your work What is this? 2/28.. U---- M---10 R-10-10- S1010----- U10-1010-----10-1010--10---1010----1010-- 100--10 M------10 W0..3/10 Latest submission - 7:05 PM PST on 03/10/21 Total score: 0 / 30 Only show failing tests Download this submission 1: Test 14 0/5 Traceback (most recent call last): TypeError: add_attack() missing 1 required positional argument: 'attack_point' 2: Test 2 A 0/5 Traceback (most recent call last): TypeError: 'bool' object is not iterable 3: Test 3 4 0/20 Traceback (most recent call last): TypeError: add_attack() missing 1 required positional argument: 'attack_point
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
