Created
July 2, 2016 06:30
-
-
Save jchadwick/491caf701822b18ed97d5de6c1fa6487 to your computer and use it in GitHub Desktop.
Warrior.js - beginner
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
const MaxHealth = 20, | |
DangerousHealth = 7, | |
Forward = 'forward', | |
Backward = 'backward', | |
Left = 'left', | |
Right = 'right', | |
Directions = [Backward, Forward, Left, Right]; | |
class Player { | |
constructor() { | |
this.health = MaxHealth; | |
} | |
playTurn(warrior) { | |
let player = this, | |
shouldRest = needsRest(warrior, player); | |
player.resting = false; | |
if (shouldRest) { | |
if (isTakingDamage(warrior, player)) { | |
warrior.walk(safeDirection(warrior)); | |
} | |
else { | |
warrior.rest(); | |
player.resting = true; | |
} | |
} | |
else if (canSeeEnemy(warrior)) { | |
warrior.shoot(enemyDirection(warrior)); | |
} | |
else if (canSeeCaptive(warrior)) { | |
if (isNextToCaptive(warrior)) { | |
warrior.rescue(captiveDirection(warrior)); | |
} else { | |
warrior.walk(captiveDirection(warrior)); | |
} | |
} | |
else if (!levelCleared(warrior)) { | |
warrior.walk(closestTarget(warrior)); | |
} | |
else { | |
if (isLookingAtWall(warrior)) { | |
warrior.pivot(); | |
} else { | |
warrior.walk(findStairs(warrior) || Forward); | |
} | |
} | |
player.health = warrior.health(); | |
} | |
} | |
function levelCleared(warrior) { | |
return closestTarget(warrior) == null; | |
} | |
function allVisibleSpaces(warrior) { | |
let spaces = []; | |
Directions.forEach(x => spaces.concat(warrior.look(x))); | |
return spaces; | |
} | |
function findStairs(warrior) { | |
let spaces = allVisibleSpaces(warrior); | |
return spaces.find(x => x.isStairs()); | |
} | |
function closestTarget(warrior) { | |
let spaces = allVisibleSpaces(warrior); | |
return spaces.find(x => !(x.isEmpty() && x.isWall())); | |
} | |
function canSeeEnemy(warrior) { | |
for (let space of warrior.look()) { | |
if (space.isCaptive()) | |
return false; | |
if (space.isEnemy()) | |
return true; | |
} | |
return false; | |
} | |
function canSeeCaptive(warrior) { | |
return !!captiveDirection(warrior); | |
} | |
function isLookingAtWall(warrior, player) { | |
return warrior.feel().isWall(); | |
} | |
function captiveDirection(warrior) { | |
return Directions.find(x => | |
!!warrior.look(x).find(y => y.isCaptive()) | |
); | |
} | |
function isNextToCaptive(warrior, player) { | |
let direction = captiveDirection(warrior); | |
return warrior.feel(direction || Forward).isCaptive(); | |
} | |
function isNextToEnemy(warrior) { | |
let direction = enemyDirection(warrior); | |
return !!direction; | |
} | |
function enemyDirection(warrior) { | |
return Directions.find( | |
direction => warrior.look(direction).some(x => x.isEnemy())); | |
} | |
function isTakingDamage(warrior, player) { | |
return warrior.health() < player.health; | |
} | |
function isAtFullHealth(warrior) { | |
return warrior.health() >= MaxHealth; | |
} | |
function needsRest(warrior, player) { | |
return warrior.health() < DangerousHealth | |
|| (player.resting && !isAtFullHealth(warrior)); | |
} | |
function safeDirection(warrior) { | |
let frontDistance = distanceToEnemy(warrior, Forward), | |
backDistance = distanceToEnemy(warrior, Backward); | |
return (frontDistance > backDistance) | |
? Forward | |
: Backward; | |
} | |
function distanceToEnemy(warrior, direction) { | |
let spaces = warrior.look(direction); | |
for (let x = 0; x < spaces.length; x++) { | |
if (spaces[x].isEnemy) | |
return x; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment