Skip to content

Instantly share code, notes, and snippets.

@luisangelma
Last active November 19, 2015 23:06
Show Gist options
  • Select an option

  • Save luisangelma/bd8278bfc5a3cee713a3 to your computer and use it in GitHub Desktop.

Select an option

Save luisangelma/bd8278bfc5a3cee713a3 to your computer and use it in GitHub Desktop.
Penguins VS Communists
var Party = function(name) {
var self = this;
this.name = name;
this.population = 1000000;
this.getAttacked = function(nuke) {
self.population -= nuke.impact;
if(self.population <= 0) {
self.population = 0;
}
};
};
var NukeDamage = function() {
this.impact = Math.floor(Math.random() * (1000000 - 150000));
};
var penguins = new Party('Penguins');
var communist = new Party('Communists');
var printAttack = function(attacker, victim, loss) {
console.log(victim.name + ' was attacked by the ' + attacker.name + ' party and loss ' + loss + ' members in population');
console.log(victim.name + ' population is now ' + victim.population);
console.log('===========================================\n');
console.log('===========================================\n');
};
var successfulAttack = function(partyBeingAttacked, loss) {
if(partyBeingAttacked === penguins) {
printAttack(communist, penguins, loss);
if(penguins.population <= 0) {
console.log('the ' + communist.name + ' party has killed the whole ' + penguins.name + ' party');
} else {
console.log(penguins.name + ' party is prepping for retaliation');
setTimeout(function(){
sendNuke(communist, successfulAttack, failedAttack);
}, 2000);
}
} else {
printAttack(penguins, communist, loss);
if(communist.population <= 0) {
console.log('the ' + penguins.name + ' party has killed the whole ' + communist.name + ' party');
} else {
console.log(communist.name + ' party is prepping for retaliation');
setTimeout(function(){
sendNuke(penguins, successfulAttack, failedAttack);
}, 2000);
}
}
};
var failedAttack = function(partyBeingAttacked) {
if(partyBeingAttacked === penguins) {
console.log('the ' + communist.name + ' party tried to attack the ' + penguins.name);
console.log(penguins.name + ' party is prepping for retaliation');
setTimeout(function(){
sendNuke(communist, successfulAttack, failedAttack);
}, 2000);
} else {
console.log('the ' + communist.name + ' party tried to attack the ' + penguins.name);
setTimeout(function(){
console.log(penguins.name + ' party is prepping for retaliation');
sendNuke(penguins, successfulAttack, failedAttack);
}, 2000);
}
};
var sendNuke = function(partyToAttack, onHit, onMiss) {
var nukeHit = Math.floor(Math.random() * (10 - 5));
if (nukeHit <= 5) {
var nuke = new NukeDamage();
partyToAttack.getAttacked(nuke);
onHit(partyToAttack, nuke.impact);
} else {
onMiss(partyToAttack);
}
};
sendNuke(communist, successfulAttack, failedAttack);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment