Java教程

[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)
样图:

目录
  • 游戏说明
  • Phase 1: Basic gameplay
    • Problem 1 (1 pt)
    • Problem 2 (2 pt)
    • Problem 3 (2 pt)

游戏说明

游戏按轮次进行,每一轮中,新的蜜蜂都可能进入蚁群领地。然后,放置新的蚂蚁来保卫它们的领地。 最后,所有昆虫(蚂蚁,蜜蜂)都会采取单独的行动。蜜蜂沿着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
这篇关于[2021 Spring] CS61A Project 3: Ants Vs. SomeBees (Phase 1)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!