Created
July 7, 2015 14:53
-
-
Save jackmcpickle/a4e43c0fbf02edb9547e to your computer and use it in GitHub Desktop.
my player.js from warriorjs challenge
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 { | |
playTurn(warrior) { | |
this.dir = 'backward'; | |
this.warrior = warrior; | |
this.saw = warrior.look(this.dir); | |
this.checkSpaceBeforeAction(); | |
this.saveCurrentHealth(); | |
} | |
checkSpaceBeforeAction() { | |
if ( this.isWall() ) { | |
this.changeDirection(); | |
} else if ( this.isCaptive() ) { | |
this.warrior.rescue(this.dir); | |
} else if (this.lookAheadToShoot() ) { | |
this.warrior.shoot(this.dir); | |
} else if ( this.goodToAttack() ) { | |
this.warrior.attack(this.dir); | |
} else if ( this.inFullHealth() || this.goodToCharge() ) { | |
this.warrior.walk(this.dir); | |
} else if ( this.goodToRetreat() ) { | |
this.retreat() | |
} else { | |
this.warrior.rest(); | |
} | |
} | |
changeDirection() { | |
this.warrior.pivot(); | |
// var change = this.isSpaceAttackable() && !this.underAttack() && this.inFullHealth(); | |
console.log('changed'); | |
// return change; | |
} | |
goodToAttack() { | |
var attack = this.isSpaceAttackable() && (this.underAttack() || this.inFullHealth() ); | |
console.log('attack?', attack); | |
return attack; | |
} | |
goodToCharge() { | |
var charge = this.isEmpty() && this.underAttack() && this.warrior.health() > 10; | |
console.log('change?', charge); | |
return charge; | |
} | |
goodToRetreat() { | |
var retreat = this.isEmpty() && this.underAttack() && this.warrior.health() < 10; | |
console.log('retreat', retreat); | |
return retreat; | |
} | |
underAttack() { | |
var underAttack = this.oldHealth > this.warrior.health(); | |
console.log('underAttack', underAttack); | |
return underAttack; | |
} | |
advanceOrRetreat() { | |
if ( this.inFullHealth() || this.goodToCharge() ) { | |
this.warrior.walk(this.dir); | |
} | |
else if ( this.goodToRetreat() ) { | |
this.retreat() | |
} else { | |
this.warrior.rest(); | |
} | |
} | |
retreat() { | |
this.warrior.walk('backward'); | |
} | |
inFullHealth() { | |
return this.warrior.health() === 20; | |
} | |
saveCurrentHealth() { | |
this.oldHealth = this.warrior.health(); | |
} | |
isEmpty(space) { | |
if (space == null) { | |
space = this.warrior.feel(this.dir); | |
} | |
return space.isEmpty(); | |
} | |
isStairs(space) { | |
if (space == null) { | |
space = this.warrior.feel(this.dir); | |
} | |
return space.isStairs(); | |
} | |
isCaptive(space){ | |
if (space == null) { | |
space = this.warrior.feel(this.dir); | |
} | |
return space.isCaptive(); | |
} | |
isSpaceAttackable(space) { | |
if (space == null) { | |
space = this.warrior.feel(this.dir); | |
} | |
return !this.isEmpty(space) && !this.isCaptive(space); | |
} | |
isWall(space) { | |
if (space == null) { | |
space = this.warrior.feel(this.dir); | |
} | |
return space.isWall(); | |
} | |
lookAheadToShoot() { | |
return this.checkForCaptive() && !this.canSeeTheFinish() && !this.pathIsAllClear() && !this.checkForWalls(); | |
} | |
checkForCaptive() { | |
var shoot = this.saw.every((function(_this) { | |
return function(space, index, array) { | |
if (_this.isCaptive(space) ) { | |
return false; | |
} | |
return true; | |
}; | |
})(this)); | |
console.log('shoot', shoot); | |
return shoot; | |
} | |
checkForWalls() { | |
var shoot = this.saw.every((function(_this) { | |
return function(space, index, array) { | |
if (_this.isWall(space) ) { | |
return false; | |
} | |
return true; | |
}; | |
})(this)); | |
console.log('shoot', shoot); | |
return shoot; | |
} | |
pathIsAllClear() { | |
var clear = this.saw.every((function(_this) { | |
return function(space, index, array) { | |
if (!_this.isEmpty(space) || _this.isStairs(space)) { | |
return false; | |
} | |
return true; | |
}; | |
})(this)); | |
console.log('all clear', clear); | |
return clear; | |
} | |
canSeeTheFinish() { | |
var stairs = this.saw.some((function(_this) { | |
return function(space, index, array) { | |
if (_this.isStairs(space) ) { | |
return true; | |
} | |
return false; | |
}; | |
})(this)); | |
console.log('stairs', stairs); | |
return stairs; | |
} | |
// getCurrentDirection() { | |
// if (this.lastDir === 'backward') { | |
// this.dir = 'backward'; | |
// } else { | |
// this.dir = 'forward'; | |
// } | |
// } | |
} | |
global.Player = Player; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment