Question: Python Codewriting Question . NEED SOME EDITING to my solution =) some lines could be done in pseuvdocode if I forgot to specify something. The

Python Codewriting Question. NEED SOME EDITING to my solution =)

some lines could be done in pseuvdocode if I forgot to specify something. The changes can only be made withing "#BEGIN EC and END EC", and next to most of the I say ***This part should be good, I think****, which means it shoulldn't need any editing, unless it's really wrong in your opinion.

**** I will also add a screenshot of the error I get now when testing in the end of the post***

I coded most the question already, but need some help putting it all together. Thank you!

Here's the question:

Implement two final thrower ants that do zero damage, but instead produce a temporary "effect" on theaction_method of a Bee instance that they throw_at. This effect is an alternative action that lasts for a certain number of .action(colony) calls, after which the Bee's action reverts to its original behavior.

We will be implementing two new ants that subclass ThrowerAnt.

SlowThrower throws sticky syrup at a bee, applying a slow effect for 3 turns.

ScaryThrower intimidates a nearby bee, causing it to back away instead of advancing. (If the bee is already right next to the Hive and cannot go back further, it should not move.) The scare effect lasts for 2 turns. Once a bee has been scared once, it can't be scared again.

Class Food Cost Armor
SlowThrower 4 1
ScaryThrower 6 1

In order to complete the implementations of these two ants, you will need to set their class attributes appropriately and implement the following three functions:

make_slow is an effect that takes an action method and a bee, and returns a new action method that performs the new action on turns where colony.time is even and does nothing on other turns.

make_scare is an effect that takes an action method and a bee, and returns a new action method thatmakes the bee go backwards.

apply_effect takes an effect (either make_slow or make_scare), a Bee, and a duration. It uses the effect on the bee.action method to produce a new action method, and then arranges to have the new method become the bee's action method for the next duration times that .action is called, after which the previous .action method is restored.

Hint: to make a bee go backwards, consider adding an instance variable indicating its current direction. Where should you change the bee's direction? Once the direction is known, how can you modify the action method of Bee to move appropriately? ***I'm using self.move_to(self.place.entrance) here for moving backwards and self.move_to(self.place.exit) for moving forward normally, when those two special ants are not attacking the bees.***

Hint: To prevent the same bee from being scared twice, you will also need to add an instance variable to Bee and set it appropriately! ***I create a instance attribute in the Bee class bee.scared_once and set it to False at first, and then to True***

CODE SO FAR (only stuff in bold is likely to need edits):

class Bee(Insect): """A Bee moves from place to place, following exits and stinging ants."""

name = 'Bee' damage = 1 is_watersafe = True scared_once = False direction = 'forward' slow_duration = 0 scary_duration = 0 # OVERRIDE CLASS ATTRIBUTES HERE

def action(self, colony): """A Bee's action stings the Ant that blocks its exit if it is blocked, or moves to the exit of its current place otherwise.

colony -- The AntColony, used to access game state information. """ destination = self.place.exit # Extra credit: Special handling for bee direction # BEGIN EC if self.scared_once and self.direction == 'backwards': if self.place.entrance is not colony.hive: self.move_to(self.place.entrance) elif self.direction == 'forward': # or self.scared_once == True: # END EC if self.blocked(): self.sting(self.place.ant) elif self.armor > 0 and destination is not None: self.move_to(destination)

def make_slow(action, bee): """Return a new action method that calls ACTION every other turn.

action -- An action method of some Bee """ # BEGIN Problem EC def slow_action(colony): if colony.time % 2 == 0: return action(colony) return return slow_action # END Problem EC

def make_scare(action, bee): """Return a new action method that makes the bee go backwards.

action -- An action method of some Bee """ # BEGIN Problem EC def scare_action(colony): if bee.scared_once == False: bee.scared_once = True bee.direction = 'backwards' action(colony) return scare_action # END Problem EC

def apply_effect(effect, bee, duration): """Apply a status effect to a BEE that lasts for DURATION turns.""" # BEGIN Problem EC original_action = bee.action new_action = effect(bee.action, bee) # new__action method if hasattr(effect, 'make_scare'): duration = bee.scary_duration else: duration = bee.slow_duration

def inner(colony): nonlocal duration if duration > 0: new_action(colony) duration -= 1 elif duration == 0: bee.direction = 'forward' bee.action = original_action original_action(colony) bee.action = inner # END Problem EC

class SlowThrower(ThrowerAnt): """ThrowerAnt that causes Slow on Bees."""

name = 'Slow' food_cost = 4 # BEGIN Problem EC implemented = True # Change to True to view in the GUI # END Problem EC

def throw_at(self, target): if target: target.slow_duration += 3 apply_effect(make_slow, target, 3)

class ScaryThrower(ThrowerAnt): """ThrowerAnt that intimidates Bees, making them back away instead of advancing."""

name = 'Scary' food_cost = 6 # BEGIN Problem EC implemented = True # Change to True to view in the GUI # END Problem EC

def throw_at(self, target): # BEGIN Problem EC if target.scared_once: return else: target.scary_duration += 2 return apply_effect(make_scare, target, 2)# back away / (stay if at the back) + still for 2 turns # END Problem EC

CLARIFICATION ***Bees start at the Hive and move forward from tonnel_9 to tonnel_0 (where they win) or when moowing backwards they should move from lower number to higher, or stand still if the Hive is behing them***

With that in mind, here's the error:

Python Codewriting Question. NEED SOME EDITING to my solution =) some lines

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!