Skip to content

Instantly share code, notes, and snippets.

@beall49
Created October 4, 2018 05:47
Show Gist options
  • Save beall49/58b9c4270abf4970f07031a6f37e56e3 to your computer and use it in GitHub Desktop.
Save beall49/58b9c4270abf4970f07031a6f37e56e3 to your computer and use it in GitHub Desktop.
class Player {
constructor() {
this.maxHealth = 20;
}
playTurn(warrior) {
this.warrior = warrior;
this.health = warrior.health();
this.takeAction();
}
takeAction() {
const healthPercent = this.health / this.maxHealth;
const isHurt = healthPercent < 0.5;
this.warrior.think(healthPercent);
if (isHurt) {
this.isInjured(this.warrior)
} else {
this.isAttacking(this.warrior)
}
}
isInjured(warrior) {
if (warrior.feel().isEmpty()) {
warrior.rest();
} else {
warrior.walk('backward');
}
}
isAttacking(warrior) {
const spaceWithUnit = warrior.look().find(space => space.isUnit());
const isEnemy = spaceWithUnit && spaceWithUnit.getUnit().isEnemy();
if (isEnemy) {
const isEnemyClose = isEnemy && !warrior.feel().isEmpty();
if (isEnemyClose) {
warrior.attack();
} else {
warrior.shoot();
}
} else {
warrior.walk()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment