Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python please when Test the code it said 3 rounds expecter by your function returned 2 rounds or 4 rounds expecter by your function returned

python please
when Test the code it said
3 rounds expecter by your function returned 2 rounds
or
4 rounds expecter by your function returned 3 rounds
looks like i always miss 1 rounds.
image text in transcribed
class Monster():
def __init__(self, name, hp=20):
self.name = name
self.type = "Normal"
self.max_hp = hp
self.current_hp = hp
self.attacks = {'wait': 0}
self.exp = 0
self.possible_attacks = {'sneak_attack': 1, 'slash': 2, 'ice_storm': 3, 'fire_storm': 3, 'whirlwind': 3,
'earthquake': 2, 'double_hit': 4, 'tornado': 4, 'wait': 0 }
def add_attack(self, attack_name):
if ((attack_name in self.possible_attacks) and (attack_name not in self.attacks)):
if(len(self.attacks)
self.attacks[attack_name] = self.possible_attacks[attack_name]
return True
else:
weak_attack = min(self.attacks.values())
weak_list = []
for attack in self.attacks:
if self.attacks[attack] == weak_attack:
weak_list.append(attack)
weak_list.sort()
del self.attacks[weak_list[0]]
self.attacks[attack_name] = self.possible_attacks[attack_name]
return True
else:
return False
def remove_attack(self, attack_name):
if((attack_name in self.possible_attacks) and (attack_name in self.attacks)):
del self.attacks[attack_name]
if len(self.attacks) == 0:
self.add_attack('wait')
return True
else:
return False
def win_fight(self):
self.exp = self.exp + 5
self.current_hp = self.max_hp
def lose_fight(self):
self.exp = self.exp + 1
self.current_hp = self.max_hp
def monster_fight(monster1, monster2):
monster1.attacks = {k: v for k, v in sorted(monster1.attacks.items(), key=lambda item: item[1], reverse=True)}
monster1_attacks = list(monster1.attacks)
monster2.attacks = {k: v for k, v in sorted(monster2.attacks.items(), key=lambda item: item[1], reverse=True)}
monster2_attacks = list(monster2.attacks)
round_number = 0
used_attack1 = []
used_attack2 = []
if len(monster1.attacks) == len(monster2.attacks) == 1:
if "wait" in monster1.attacks and "wait" in monster2.attacks:
return -1, None, None
while True:
monster2.current_hp -= monster1.attacks[monster1_attacks[round_number % len(monster1_attacks)]]
used_attack1.append(monster1_attacks[round_number % len(monster1_attacks)])
if monster2.current_hp
break
monster1.current_hp -= monster2.attacks[monster2_attacks[round_number % len(monster2_attacks)]]
used_attack2.append(monster2_attacks[round_number % len(monster2_attacks)])
if monster1.current_hp
break
round_number += 1
winner = None
if monster1.current_hp > 0:
monster1.win_fight()
monster2.lose_fight()
return round_number, monster1.name, used_attack1
else:
monster2.win_fight()
monster1.lose_fight()
return round_number, monster2.name, used_attack2
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. 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: Explanation Round No Move 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 Round number Monster that won (return the corresponding Monster object) 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)

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_2

Step: 3

blur-text-image_3

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

More Books

Students also viewed these Databases questions