Created
October 4, 2018 05:47
-
-
Save beall49/58b9c4270abf4970f07031a6f37e56e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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