Created
April 24, 2015 20:53
-
-
Save sscotth/c36aff4d90c40c4bdd4c to your computer and use it in GitHub Desktop.
Pirates vs Ninjas
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
function Pirate() { | |
this.says = 'ARRRRRRRRRRRRRR!'; | |
this.toString = function() { return 'Pirate'; }; | |
} | |
function Ninja() { | |
this.says = ''; | |
this.toString = function() { return 'Ninja'; }; | |
} | |
function Player() { | |
this.health = 1; | |
} | |
Pirate.prototype = new Player(); | |
Ninja.prototype = new Player(); | |
Player.prototype.isAlive = function () { | |
return this.health > 0; | |
} | |
Player.prototype.healthPct = function () { | |
var pct = Math.ceil(this.health * 100); | |
if (pct >= 0) { | |
return pct + '%'; | |
} else { | |
return '0%'; | |
} | |
} | |
function Weapon(type) { | |
this.type = type; | |
this.damage = Math.random() / 5; | |
this.toString = function () { return this.type }; | |
} | |
function game() { | |
var pirate = new Pirate(); | |
var ninja = new Ninja(); | |
pirate.weapon = new Weapon('Cannon'); | |
ninja.weapon = new Weapon('Katana'); | |
function fight() { | |
var rnd = Math.floor(Math.random() * 2); | |
var attacker = rnd ? pirate : ninja; | |
var attacked = rnd ? ninja : pirate; | |
attacked.health -= attacker.weapon.damage; | |
console.log('The ' + attacker + ' attacked the ' + attacked + ' with a ' + attacker.weapon + '.'); | |
console.log('Status\nPirate: ' + pirate.healthPct() + ' Ninja: ' + ninja.healthPct()); | |
} | |
while (pirate.isAlive() && ninja.isAlive()) { | |
fight(); | |
} | |
var winner = pirate.isAlive() ? pirate : ninja; | |
console.log(winner + ' says, "' + winner.says + '"'); | |
} | |
game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment