1. 程式人生 > 其它 >[2021 Spring] CS61A Project 3: Ants Vs. SomeBees (Phase 1)

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

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

專案說明:https://inst.eecs.berkeley.edu/~cs61a/sp21/proj/ants/
螞蟻大戰蜜蜂 靈感來源:植物大戰殭屍(Plants Vs. Zombies,PVZ)
樣圖:

目錄

遊戲說明

遊戲按輪次進行,每一輪中,新的蜜蜂都可能進入蟻群領地。然後,放置新的螞蟻來保衛它們的領地。 最後,所有昆蟲(螞蟻,蜜蜂)都會採取單獨的行動。蜜蜂沿著tunnel從右往左移動,遇到螞蟻則攻擊螞蟻。不同型別的螞蟻執行不同的動作,例如收集更多食物或向蜜蜂扔樹葉。 當一隻蜜蜂到達tunnel盡頭或者摧毀了 QueenAnt(如果存在),你就輸了;整個蜜蜂艦隊都被打敗,你就贏了;遊戲結束。
核心概念和核心類都跟PVZ類似。

Phase 1: Basic gameplay

Problem 1 (1 pt)

Part A: 當前放置任一型別Ant都沒有cost,基類Ant的food_cost為0。任務:根據表格的"Food cost"重寫HarvesterAnt和ThrowerAnt的類屬性。

Class Food Cost Initial Health
HarvesterAnt 2 1
ThrowerAnt 3 1

Part B: 放置ant需要消耗食物,HarvesterAnt生產食物(類似PVZ向日葵) 任務:完成HarvesterAnt action方法。
程式碼:

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

    name = 'Harvester'
    implemented = True
    # OVERRIDE CLASS ATTRIBUTES HERE
    food_cost = 2

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

        gamestate -- The GameState, used to access game state information.
        """
        # BEGIN Problem 1
        "*** YOUR CODE HERE ***"
        gamestate.food += 1
        # END Problem 1

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

Problem 2 (2 pt)

任務:完成 Place.__init__記錄入口entrance。
分析:類似連結串列結構,建立Place物件時,entrance為None;如果這個物件的exit不為None,將這個exit的entrance設定為這個物件。
程式碼:

class Place:
    """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 (2 pt)

ThrowerAnt類似豌豆射手,需要根據nearest_bee方法確定攻擊哪隻蜜蜂(攻擊身前且不在蜂巢中的蜜蜂)。
任務:修改nearest_bee讓它返回最近的蜜蜂之一。

  • 從豌豆射手ThrowerAnt的位置開始
  • 對於每個place,如果其中有蜜蜂,則隨機返回蜜蜂之一;如果沒有蜜蜂,查詢更前面的place
  • 如果沒有蜜蜂可以攻擊,返回None

遍歷豌豆射手身前的place時,由於self.place不會變,所以引入新引數next_place,place中沒有蜜蜂,則賦值為下一個格子next_place.entrance,直到找到有蜜蜂的place或者到達蜂巢。
注意:phase 2中problem 4有新要求,要考慮ant的攻擊距離,相應的problem 3部分會在phase 2進行修改。

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

    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
        # 不能改變ant的位置self.place
        next_place = self.place
        while not next_place.is_hive():
            if next_place.bees:
                return bee_selector(next_place.bees)  # REPLACE THIS LINE
            next_place = next_place.entrance
        return None
        # END Problem 3 and 4