Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class Monster: # Creating class of monster def __init__(self, name , hp=20) -> None: # Constructor of class self.exp = 0 self.name = name self.hp

image text in transcribedimage text in transcribed

class Monster: # Creating class of monster

def __init__(self, name , hp=20) -> None: # Constructor of class self.exp = 0 self.name = name self.hp = hp self.max_hp = hp self.attacks = { 'wait' : 0 } def win_fight(self, ): # Win_fight method self.exp += 5 self.hp = self.max_hp def lose_fight(self, ): # lose_fight method self.exp += 1 self.hp = self.max_hp

def add_attack(self, attack_name, attack_point): # method to add attacks in the monster self.attacks[attack_name] = attack_point

def remove_attack(self, attack_name): # method to remove attack from the monster del self.attacks[attack_name]

def monster_fight(monster1 : Monster, monster2 : Monster): # Method to make two monster fight

# Checking for edge case if list(monster1.attacks.keys()) == ['wait'] and list(monster2.attacks.keys() == ['wait']): # This condition means edge case is detected and no fight will take place return -1, None, None #Declaring variables. round_number = 0 # List of name of attacks of monster sorted by their power m1Attacks = sorted( monster1.attacks.keys(), key=lambda x: monster1.attacks[x]) m2Attacks = sorted( monster2.attacks.keys(), key=lambda x: monster2.attacks[x]) # Index to keep track of which attack will be used m1Index = 0 m2Index = 0 # Max index limit m1Max = len(m1Attacks) m2Max = len(m2Attacks) # List to store attacks of fight. attack_list = []

# loop to make monster fight. while monster1.hp >0 and monster2.hp > 0: # Using round_number to decide which monster will attack

# If rounder number is even monster1 will attack if round_number % 2 == 0: # Monster1 attacks

# finding the index of attack attack_index = m1Index % m1Max # getting attack name from the list. attack = m1Attacks[attack_index] # Decreasing monster2 hp by attack monster2.hp -= monster1.attacks[attack] # Increasing the m1index. m1Index += 1 # If rounder_number is odd monster2 will attack. else: # Saame as above block, just in this monster2 attacks. attack_index = m2Index % m2Max

attack = m2Attacks[attack_index]

monster1.hp -= monster2.attacks[attack]

m2Index += 1

# Adding attack to attack list attack_list.append(attack) # Increasing round number round_number += 1 # Checking which monster loosed if monster1.hp

# calling lose_fight and win_fight on respective monster. monster1.lose_fight() monster2.win_fight()

# returning the data return round_number, monster2, attack_list else: # This means monster2 loosed the fight monster2.lose_fight() monster1.win_fight()

# returning the data return round_number, monster1, attack_list image text in transcribed

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 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(): 2 def __init__(self, name, hp=20): Self.exp = 0 4 #your code here def add_attack(self, attack_name): 6 pass #your code here 7 def remove_attack(self, attack_name): 8 pass #your code here 9 def win_fight (self): 19 pass #your code here 11 def lose_fight (self): 12 pass #your code here 13 14 def monster_fight (monsteri, monster): 15 pass #your code here 16 Develop mode Submit mode When done developing your program, press the Submit for grading button below. This will submit your program for auto-grading. Submit for grading Signature of your work What is this? 2/28.. U------- M---10 R-10-10- 51010----- U10-1010-----10-1010--10---1010----1010-- 100--10 M------10 WIO RIO ..3/11 Latest submission - 2:42 PM PST on 03/11/21 Total score: 0 / 30 Only show failing tests Download this submission 1: Test 1 A 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 A 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

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

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions

Question

What are the objectives of job evaluation ?

Answered: 1 week ago

Question

Write a note on job design.

Answered: 1 week ago

Question

Compute the derivative of f(x)cos(-4/5x)

Answered: 1 week ago

Question

Discuss the process involved in selection.

Answered: 1 week ago

Question

Is this investment worthwhile? Why or why not?

Answered: 1 week ago