这一个项目整体比较简单,只要搞清楚这只蚂蚁的作用代码基本很容易写出来,再通过测试中跑出来的问题调整下细节加一些限制条件就OK了。吐槽一下这个项目有些函数的设计,他没有告诉你要什么效果,读完题目一脸懵逼,要去跑测试才知道自己要干嘛。。。

核心概念

蚁巢:游戏发生的地方
格子:一个格子放一只蚂蚁或者多只蜜蜂
蜂巢:生产蜜蜂的地方
蚂蚁和蜜蜂,类似植物大战僵尸

火蚁: 范围性反甲 死亡之焚(默认三点伤害)
一些细节在于要把自身的位置先保存下来,后面蚂蚁寄了没办法通过自身去访问位置

1
2
3
4
5
6
7
8
9
10
11
12
13
def reduce_health(self, amount):
# BEGIN Problem 5
place = self.place
bees = place.bees[:]
#非空说明还没死
Ant.reduce_health(self, amount)
if self.place:
#找位置上的蜜蜂
for b in bees:
Insect.reduce_health(b, amount)
else:
for b in bees:
Insect.reduce_health(b, amount + self.damage)

在类的继承中,用super().的方法和直接名称的细微区别:

1
2
3
4
5
6
7
class Child(Parent):
def __init__(self, name, age):
# 直接调用父类的 __init__ 方法
Parent.__init__(self, name)
#super() 推荐!!
super().__init__(name)

Problem8b 这一问卡挺久的,拿纸推演了几遍(因为在前面的时候add_to里面的代码是通过了的)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def add_to(self, place):
if place.ant is None:
place.ant = self
else:
# BEGIN Problem 8b
#之前的
if place.ant.is_container and place.ant.ant_contained is None:
if self.is_container:
assert place.ant is None, 'Too many ants in {0}'.format(place)
else:
place.ant.store_ant(self)

else:
if self.is_container:
self.store_ant(place.ant)
place.ant = self
else:
assert place.ant is None, 'Too many ants in {0}'.format(place)

修改后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
def add_to(self, place):
if place.ant is None:
place.ant = self
else:
# BEGIN Problem 8b
#该地方的蚂蚁需要变成保护蚁
if place.ant.is_container :
if self.is_container:
assert place.ant is None, 'Too many ants in {0}'.format(place)
elif place.ant.ant_contained is None:
place.ant.store_ant(self)
else:
assert place.ant is None, 'Too many ants in {0}'.format(place)

else:
if self.is_container:
self.store_ant(place.ant)
place.ant = self
else:
assert place.ant is None, 'Too many ants in {0}'.format(place)

结算!