Skip to content

Instantly share code, notes, and snippets.

@beall49
Created October 5, 2018 05:47
Show Gist options
  • Save beall49/74f3b51b1bc9de711dc4e7695e2e6631 to your computer and use it in GitHub Desktop.
Save beall49/74f3b51b1bc9de711dc4e7695e2e6631 to your computer and use it in GitHub Desktop.
class Player {
constructor() {
this.maxHealth = 20;
this.roomLength = 7;
this.hitWall = false;
}
playTurn(warrior) {
warrior.think(`wall: ${this.hitWall}`);
this.warrior = warrior;
this.health = warrior.health();
this.takeAction();
}
takeAction() {
const healthPercent = this.health / this.maxHealth;
const isHurt = healthPercent < 0.5;
this.warrior.think(`health: ${healthPercent}`);
if (isHurt) {
this.isInjured(this.warrior)
} else {
this.attack(this.warrior)
}
}
isEmpty(feels) {
return feels.find(space => space.isEmpty());
}
doFeels(warrior) {
return [warrior.feel(), warrior.feel('backward')];
}
findUnit(feels) {
return feels.find(space => space.isUnit());
}
isInjured(warrior) {
const feels = this.doFeels(warrior);
const isUnit = this.findUnit(feels);
warrior.think(`isUnit: ${isUnit}`)
let isEnemy = false;
if (isUnit) {
feels.forEach(_space => {
warrior.think(_space);
if (_space) {
if (_space.isUnit()) {
if (_space.getUnit().isEnemy()) {
isEnemy = true;
}
}
}
});
}
if (isEnemy) {
warrior.walk('backward');
} else {
warrior.rest();
}
}
// Beall49 thinks health: 1
// Beall49 thinks feel: false -1,0
// Beall49 thinks isUnit: [object Object]
// Beall49 walks backward and bumps into Captive
// warrior.think(`feel: ${feel.isEmpty()} ${feel.getLocation()}`)
attack(warrior) {
const feels = this.doFeels(warrior);
const isUnit = this.findUnit(feels);
const isEmpty = this.isEmpty(feels);
let dir = 1;
if (this.health / this.maxHealth === 1) {
const feelBW = warrior.feel('backward');
warrior.think(`feel: ${feelBW.getLocation()}`)
if (feelBW.isWall()) {
this.hitWall = true;
warrior.walk();
} else {
if (feelBW.isEmpty() && !feelBW.isWall()) {
warrior.walk('backward')
} else {
if (feelBW.isUnit()) {
if (feelBW.getUnit().isBound()) {
warrior.rescue('backward');
}
}
}
}
}
// 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