Phase 1 - Basic gameplay & Phase 2 - Ants

In the first phase you will complete the implementation that will allow for basic gameplay with the two basic Ants: the HarvesterAnt and the ThrowerAnt.

Now that you've implemented basic gameplay with two types of Ants, let's add some flavor to the ways ants can attack bees. In this phase, you'll be implementing several different Ants with different offensive capabilities.

Problem 1

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

class HarvesterAnt(Ant):
    """HarvesterAnt produces 1 additional food per turn for the colony."""

    name = 'Harvester'
    implemented = True
    food_cost = 2

    def action(self, colony):
        """Produce 1 additional food for the COLONY.

        colony -- The AntColony, used to access game state information.
        """

        "*** YOUR CODE HERE ***"
        colony.food += 1

Problem 2

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

class Place(object):
    """A Place holds insects and has an exit to another Place."""

    def __init__(self, name, exit=None):
        """Create a Place with the given NAME and EXIT.

        name -- A string; the name of this Place.
        exit -- The Place reached by exiting this Place (may be None).
        """
        self.name = name
        self.exit = exit
        self.bees = []        # A list of Bees
        self.ant = None       # An Ant
        self.entrance = None  # A Place
        # Phase 1: Add an entrance to the exit
        # BEGIN Problem 2
        "*** YOUR CODE HERE ***"
        if self.exit:
            self.exit.entrance = self
        # END Problem 2

Problem 3 & Phase 2 - Problem 4

Specs - Problem 3: https://inst.eecs.berkeley.edu/~cs61a/su19/proj/ants/#problem-3-2-pt

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

class ThrowerAnt:
... ... ...     
    def nearest_bee(self, hive):
        """Return the nearest Bee in a Place that is not the HIVE, connected to
        the ThrowerAnt's Place by following entrances.

        This method returns None if there is no such Bee (or none in range).
        """
        # BEGIN Problem 3 and 4
        curr_place = self.first_place_in_range(self.min_range)
        distance = 0
        bee_place = None
        
        while curr_place and curr_place != hive and distance <= self.max_range:
            if curr_place.bees:
                bee_place = curr_place
                break
            curr_place = curr_place.entrance
            distance += 1

        if bee_place:
            return random_or_none(bee_place.bees)
        else:
            return bee_place
        # END Problem 3 and 4

Problem 5

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

class FireAnt(Ant):
    """FireAnt cooks any Bee in its Place when it expires."""

    name = 'Fire'
    food_cost = 5
    damage = 3
    # OVERRIDE CLASS ATTRIBUTES HERE
    # BEGIN Problem 5
    implemented = True   # Change to True to view in the GUI
    # END Problem 5

    def reduce_armor(self, amount):
        """Reduce armor by AMOUNT, and remove the FireAnt from its place if it
        has no armor remaining. If the FireAnt dies, damage each of the bees in
        the current place.
        """
        # BEGIN Problem 5
        "*** YOUR CODE HERE ***"
        self.armor -= amount
        bees = list(self.place.bees)

        #ant dies, remove from place and damage all bees in its place
        if self.armor <= 0:
            self.place.remove_insect(self)
            for bee in bees:
                Insect.reduce_armor(bee, self.damage)

        # END Problem 5

Problem 6

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

class HungryAnt(Ant):
    """HungryAnt will take three turns to digest a Bee in its place.
    While digesting, the HungryAnt can't eat another Bee.
    """
    name = 'Hungry'
    food_cost = 4
    time_to_digest = 3
    implemented = True   # Change to True to view in the GUI

    def __init__(self, armor=1):
        self.armor = armor
        self.digesting = 0

    def eat_bee(self, bee):
        Insect.reduce_armor(bee, bee.armor)
        self.digesting = self.time_to_digest

    def action(self, colony):
        # BEGIN Problem 6
        if self.digesting > 0:
            self.digesting -= 1
            return None
        random_bee = random_or_none(self.place.bees)

        if random_bee:
            self.eat_bee(random_bee)

Problem 7

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

class NinjaAnt(Ant):
    """NinjaAnt does not block the path and damages all bees in its place."""

    name = 'Ninja'
    food_cost = 5
    damage = 1
    blocks_path = False
    implemented = True   # Change to True to view in the GUI

    def action(self, colony):
        bees = list(colony.bees)

        for bee in bees:
            Insect.reduce_armor(bee, self.damage)

Last updated