Phase 4 - Water & Might

In the final phase, you're going to add one last kick to the game by introducing a new type of place and new ants that are able to occupy this place. One of these ants is the most important ant of them all: the queen of the colony.

Problem 11

Specs: https://inst.eecs.berkeley.edu/~cs61a/su19/proj/ants/#problem-11-1-pt

class Water(Place):
    """Water is a place that can only hold watersafe insects."""

    def add_insect(self, insect):
        """Add an Insect to this place. If the insect is not watersafe, reduce
        its armor to 0."""
        # BEGIN Problem 11
        Place.add_insect(self, insect)
        "*** YOUR CODE HERE ***"
        if not insect.is_watersafe:
            Insect.reduce_armor(insect, insect.armor)
        # END Problem 11

Problem 12

Specs: https://inst.eecs.berkeley.edu/~cs61a/su19/proj/ants/#problem-12-1-pt

# BEGIN Problem 12
# The ScubaThrower class
class ScubaThrower(ThrowerAnt):
    name = 'Scuba'
    food_cost = 6
    is_watersafe = True
    implemented = True

    def __init__(self, armor=1):
        self.armor = 1
# END Problem 12

Problem 13

Specs: https://inst.eecs.berkeley.edu/~cs61a/su19/proj/ants/#problem-13-4-pt

# BEGIN Problem 13
class QueenAnt(ScubaThrower):
# END Problem 13
    """The Queen of the colony. The game is over if a bee enters her place."""

    name = 'Queen'
    food_cost = 7
    first_queen = None
    # OVERRIDE CLASS ATTRIBUTES HERE
    # BEGIN Problem 13
    implemented = True   # Change to True to view in the GUI
    # END Problem 13

    def __init__(self, armor=1):
        # BEGIN Problem 13
        "*** YOUR CODE HERE ***"
        self.armor = armor
        self.buffed_ants = {}
        if not QueenAnt.first_queen:
            QueenAnt.first_queen = self
        # END Problem 13

    def action(self, colony):
        """A queen ant throws a leaf, but also doubles the damage of ants
        in her tunnel.

        Impostor queens do only one thing: reduce their own armor to 0.
        """
        # BEGIN Problem 13
        "*** YOUR CODE HERE ***"
        def buff_ant(ant):
            if ant not in self.buffed_ants:
                ant.damage = ant.damage * 2
                self.buffed_ants[ant] = True

        #double damage of all ants behind first_queen
        if self is QueenAnt.first_queen:
            self.throw_at(self.nearest_bee(colony.hive))
            
            prev_place = self.place.exit
            while prev_place != None:
                if prev_place.ant:
                    buff_ant(prev_place.ant)
                    if prev_place.ant.is_container and prev_place.ant.contained_ant:
                        buff_ant(prev_place.ant.contained_ant)
                prev_place = prev_place.exit
        else:
            #reduce armor of impostor to 0
            self.reduce_armor(self.armor)


        # END Problem 13

    def reduce_armor(self, amount):
        """Reduce armor by AMOUNT, and if the True QueenAnt has no armor
        remaining, signal the end of the game.
        """
        # BEGIN Problem 13
        "*** YOUR CODE HERE ***"
        self.armor -= amount
        if self.armor <= 0 and self is QueenAnt.first_queen:
            bees_win()
        elif self.armor <= 0:
            self.place.remove_insect(self)
        # END Problem 13

Last updated