- 不要简单地用大量代码填充
playTurn
方法, **使用方法和类 ** 来组织代码。例如:
class Player {
playTurn(warrior) {
if (this.isInjured(warrior)) {
warrior.rest();
}
}
isInjured(warrior) {
return warrior.health() < 20;
}
}
class Player {
constructor() {
this.health = 20;
}
}
- 你可以直接在一个感知动作(sense)后调用空间API的方法。 For example, the "feel" sense in the "Baby Steps" tower returns one space. You can call
isEmpty()
on this to determine if the space is clear before walking there:
class Player {
playTurn(warrior) {
if (warrior.feel().isEmpty()) {
warrior.walk();
}
}
}
- Some senses (like "look" and "listen" in the "Baby Steps" tower) return an array of spaces instead, so you might find many of the Array prototype methods really useful. Here is an example of the Array.prototype.find method:
class Player {
isEnemyInSight(warrior) {
const spaceWithUnit = warrior.look().find(space => space.isUnit());
return spaceWithUnit && spaceWithUnit.getUnit().isEnemy();
}
}