Created
May 16, 2018 18:38
-
-
Save 2ajoyce/af964472a4bef490338622b98b91b9fd to your computer and use it in GitHub Desktop.
WarriorJS Beginner Tower - https://github.com/olistic/warriorjs
This file contains 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.health = 20; | |
this.healing = false; | |
} | |
playTurn(warrior) { | |
if (warrior.health() > 12 && !this.healing) { | |
this.sense(warrior); | |
} else if (warrior.health() < this.health) { | |
this.sense(warrior, 'backward'); | |
} else { | |
this.healing = warrior.health() === 20 ? false : true; | |
warrior.rest(); | |
} | |
this.health = warrior.health(); | |
} | |
sense(warrior, direction = 'forward') { | |
const views = [ | |
{direction: direction, spaces: warrior.look(direction)}, | |
{direction: this.turn(direction), spaces: warrior.look(this.turn(direction))}, | |
{direction: this.turn(direction, 2), spaces: warrior.look(this.turn(direction, 2))}, | |
{direction: this.turn(direction, 3), spaces: warrior.look(this.turn(direction, 3))} | |
] | |
let threats = []; | |
for(let i=0; i<views.length; i++) { | |
if (views[i].spaces[0].isEmpty()) { | |
if (views[i].spaces[1].isEnemy() || views[i].spaces[2].isEnemy()) { | |
warrior.shoot(views[i].direction); | |
return; | |
} | |
} else { | |
threats.push(views[i]); | |
} | |
}; | |
threats = threats.sort((a, b) => a.spaces[0].isEnemy() ? -1 : 1); | |
this.move(warrior, threats.length > 0 ? threats[0].direction: direction); | |
} | |
move(warrior, direction = 'forward') { | |
if (warrior.feel(direction).isWall()) { | |
warrior.pivot(); | |
} else if (warrior.feel(direction).isCaptive()) { | |
warrior.rescue(); | |
} else if (warrior.feel(direction).isEnemy()) { | |
warrior.attack(direction); | |
} else { | |
warrior.walk(direction); | |
} | |
} | |
turn(direction, times=1) { | |
let newDirection = direction; | |
for (let i=0; i<times; i++) { | |
switch (newDirection) { | |
case 'forward': | |
newDirection = 'right'; | |
case 'right': | |
newDirection = 'backward'; | |
case 'backward': | |
newDirection = 'left'; | |
case 'left': | |
newDirection = 'forward'; | |
} | |
} | |
return newDirection; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment