defcan_contain(self, other: Ant) -> bool: # BEGIN Problem 8a "*** YOUR CODE HERE ***" ifself.ant_contained isNoneandnot other.is_container: returnTrue returnFalse # END Problem 8a
defstore_ant(self, ant: Ant): # BEGIN Problem 8a "*** YOUR CODE HERE ***" self.ant_contained = ant # END Problem 8a
defremove_ant(self, ant: Ant): ifself.ant_contained isnot ant: assertFalse, "{} does not contain {}".format(self, ant) self.ant_contained = None
defremove_from(self, place: Place): # Special handling for container ants if place.ant isself: # Container was removed. Contained ant should remain in the game place.ant = self.ant_contained Insect.remove_from(self, place) else: # default to normal behavior Ant.remove_from(self, place)
defaction(self, gamestate: GameState): # BEGIN Problem 8a "*** YOUR CODE HERE ***" ifnotself.ant_contained isNone: self.ant_contained.action(gamestate) # END Problem 8a
classAnt(Insect): """An Ant occupies a place and does work for the colony."""
implemented = False# Only implemented Ant classes should be instantiated food_cost = 0 is_container = False # ADD CLASS ATTRIBUTES HERE has_double = False # BEGIN Problem 8a "*** YOUR CODE HERE ***" defcan_contain(self,other): returnFalse # END Problem 8a
def__init__(self, health: int = 1): super().__init__(health)
defstore_ant(self, ant: Ant): assertFalse, "{0} cannot contain an ant".format(self)
defremove_ant(self, ant: Ant): assertFalse, "{0} cannot contain an ant".format(self)
defadd_to(self, place: Place): if place.ant isNone: place.ant = self else: # BEGIN Problem 8b if place.ant.can_contain(self): place.ant.store_ant(self) self.place = place return elifself.can_contain(place.ant): self.store_ant(place.ant) place.ant = self else: assert place.ant isNone, 'Too many ants in {0}'.format(place) # END Problem 8b Insect.add_to(self, place)
defremove_from(self, place: Place): if place.ant isself: place.ant = None elif place.ant isNone: assertFalse, '{0} is not in {1}'.format(self, place) else: place.ant.remove_ant(self) Insect.remove_from(self, place)
defdouble(self): """Double this ants's damage, if it has not already been doubled.""" # BEGIN Problem 12 "*** YOUR CODE HERE ***" ifnotself.has_double: self.damage *= 2 self.has_double = True # END Problem 12
classQueenAnt(ThrowerAnt): """QueenAnt boosts the damage of all ants behind her."""
name = 'Queen' food_cost = 7 # OVERRIDE CLASS ATTRIBUTES HERE # BEGIN Problem 12 implemented = True# Change to True to view in the GUI # END Problem 12
defaction(self, gamestate: GameState): """A queen ant throws a leaf, but also doubles the damage of ants in her tunnel. """ # BEGIN Problem 12 "*** YOUR CODE HERE ***" super().action(gamestate) current_place = self.place.exit while current_place isnotNone: if current_place.ant isnotNone: current_place.ant.double() if current_place.ant.is_container and current_place.ant.ant_contained: current_place.ant.ant_contained.double() current_place = current_place.exit # END Problem 12
defreduce_health(self, damage_taken: float): """Reduce health by DAMAGE_TAKEN, and if the QueenAnt has no health remaining, signal the end of the game. """ # BEGIN Problem 12 "*** YOUR CODE HERE ***" super().reduce_health(damage_taken) ifself.health == 0: ants_lose() # END Problem 12
defaction(self, gamestate: GameState): """A queen ant throws a leaf, but also doubles the damage of ants in her tunnel. """ # BEGIN Problem 12 "*** YOUR CODE HERE ***" super().action(gamestate) current_place = self.place.exit while current_place isnotNone: if current_place.ant isnotNone: current_place.ant.double() if current_place.ant.is_container and current_place.ant.ant_contained: current_place.ant.ant_contained.double() current_place = current_place.exit # END Problem 12
defaction(self, gamestate: GameState): # BEGIN Problem 8a "*** YOUR CODE HERE ***" ifnotself.ant_contained isNone: self.ant_contained.action(gamestate) # END Problem 8a