[2021 Spring] CS61A Project 3: Ants Vs. SomeBees (Phase 2)

项目说明:https://inst.eecs.berkeley.edu/~cs61a/sp21/proj/ants/
蚂蚁大战蜜蜂 灵感来源:植物大战僵尸(Plants Vs. Zombies,PVZ)
Phase 1: https://www.cnblogs.com/ikventure/p/14986805.html
Phase 2: https://www.cnblogs.com/ikventure/p/14988467.html
Phase 3: https://www.cnblogs.com/ikventure/p/14992754.html
Phase 4: https://www.cnblogs.com/ikventure/p/14994734.html
效果展示: https://www.cnblogs.com/ikventure/p/15001093.html

Phase 2: Ants!

添加更多的蚂蚁类型。

Problem 4 (3 pt)

豌豆射手ThrowerAnt费用是3,为节省费用,建立费用为2的豌豆射手子类,长距离射手LongThrower和短距离射手ShortThrower:

Class Food Cost Initial Health
ShortThrower 2 1
LongThrower 2 1

长距离射手理论射程为 5~float('inf'),短距离射手理论射程为 0~3,只需要在两个类变量中加入max_range和min_range,再在nearest_bee中加入距离变量distance即可。
注意:LongThrower和ShortThrower的类变量implemented设置为True。
代码:

class ThrowerAnt(Ant):
    """ThrowerAnt throws a leaf each turn at the nearest Bee in its range."""

    name = 'Thrower'
    implemented = True
    damage = 1
    # ADD/OVERRIDE CLASS ATTRIBUTES HERE
    food_cost = 3
    max_range = float('inf')
    min_range = 0

    def nearest_bee(self, beehive):
        """Return the nearest Bee in a Place that is not the HIVE (beehive), 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
        next_place = self.place
        distance = 0
        while not next_place.is_hive():
            if self.min_range <= distance <= self.max_range and next_place.bees:
                return bee_selector(next_place.bees)
            next_place = next_place.entrance
            distance += 1
        return None
        # END Problem 3 and 4
class ShortThrower(ThrowerAnt):
    """A ThrowerAnt that only throws leaves at Bees at most 3 places away."""

    name = 'Short'
    food_cost = 2
    # OVERRIDE CLASS ATTRIBUTES HERE
    # BEGIN Problem 4
    implemented = True   # Change to True to view in the GUI
    max_range = 3
    # END Problem 4


class LongThrower(ThrowerAnt):
    """A ThrowerAnt that only throws leaves at Bees at least 5 places away."""

    name = 'Long'
    food_cost = 2
    # OVERRIDE CLASS ATTRIBUTES HERE
    # BEGIN Problem 4
    implemented = True   # Change to True to view in the GUI
    min_range = 5
    # END Problem 4

Problem 5 (3 pt)

FireAnt机制:

  • 受伤害量为amount的伤害时,对当前位置的所有蜜蜂造成amount的伤害(等量反伤)
  • 生命值health跌至0时,对当前位置的所有蜜蜂造成类变量damage=3的伤害,再从场地中移除。

步骤分解:

  1. 获得蚂蚁位置:self.place
  2. 获得当前位置的蜜蜂列表 self.place.bees
  3. 对列表中的所有蜜蜂造成amount伤害,遍历列表时可能会删除元素(bee),所以for循环中使用self.place.bees[:],使用bee.reduce_health(amount)
  4. 蚂蚁生命值降至0以下,遍历蜜蜂列表,bee.reduce_health(self.damage),这个self指的蚂蚁
  5. 要求使用父类的reduce_health回收蚂蚁,直接super().reduce_health(0)

注意每次需要应用的类都要将类变量implemented设置为True。
代码:

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

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

    def __init__(self, health=3):
        """Create an Ant with a HEALTH quantity."""
        super().__init__(health)

    def reduce_health(self, amount):
        """Reduce health by AMOUNT, and remove the FireAnt from its place if it
        has no health remaining.

        Make sure to reduce the health of each bee in the current place, and apply
        the additional damage if the fire ant dies.
        """
        # BEGIN Problem 5
        "*** YOUR CODE HERE ***"
        self.health -= amount
        for bee in self.place.bees[:]:
            bee.reduce_health(amount)
        if self.health <= 0:
            for bee in self.place.bees[:]:
                bee.reduce_health(self.damage)
        super().reduce_health(0)
        # END Problem 5
posted @ 2021-07-09 10:38  ikventure  阅读(3170)  评论(0编辑  收藏  举报