Skip to content

Instantly share code, notes, and snippets.

@beall49
Created October 4, 2018 06:35
Show Gist options
  • Save beall49/f1a9bf1732eea34384689131d0765b73 to your computer and use it in GitHub Desktop.
Save beall49/f1a9bf1732eea34384689131d0765b73 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) {
const isSpace = warrior.look().find(space => space.isUnit());
let isEnemy = false;
if (isSpace) {
warrior.look().forEach(_space => {
warrior.think(_space);
if (_space) {
if (_space.isUnit()) {
if (_space.getUnit().isEnemy()) {
isEnemy = true;
}
}
}
});
}
if (isEnemy) {
warrior.walk('backward');
} else {
warrior.rest();
}
}
isAttacking(warrior) {
const space = warrior.look().find(space => space.isUnit());
const isEnemy = space && space.getUnit().isEnemy();
const captive = !isEnemy && space && space.getUnit().isBound();
if (isEnemy) {
const isEnemyClose = isEnemy && !warrior.feel().isEmpty();
if (isEnemyClose) {
warrior.attack();
} else {
warrior.shoot();
}
} else {
if (captive) {
const isClose = !warrior.feel().isEmpty();
if (isClose) {
warrior.rescue();
} else {
warrior.walk()
}
} else {
warrior.walk();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment