Created
December 5, 2012 18:17
-
-
Save melondonkey/4218105 to your computer and use it in GitHub Desktop.
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
var war = function(attackArmies,defenseArmies){ | |
var battle=function(attack,defense){ | |
var a=0; | |
var d=0; | |
//roll attack dice | |
var attackDice= new Array(); | |
for (a=1; a <= attack; a++){ | |
attackDice[a]=Math.floor(Math.random()*6)+1; | |
if(a===1){ | |
attackHigh=attackDice[1]; | |
// console.log("Condition A1"); | |
} | |
else if(a===2){ | |
attackHigh=Math.max(attackDice[1],attackDice[2]); | |
attackLow=Math.min(attackDice[1],attackDice[2]); | |
// console.log("Condition A2"); | |
} | |
else if (a===3){ | |
attackHigh=Math.max(attackDice[1], attackDice[2],attackDice[3]); | |
attackDice.sort(); | |
//console.log(attackDice); | |
attackLow=attackDice.slice(1,2); | |
// console.log("Condition A3"); | |
} | |
} | |
//roll defense dice | |
var defenseDice= new Array(); | |
for (d=1; d <= defense; d++){ | |
defenseDice[d]=Math.floor(Math.random()*6)+1; | |
if(d===1){ | |
defenseHigh=defenseDice[1]; | |
// console.log("Condition D1"); | |
} | |
else if(d===2){ | |
defenseHigh=Math.max(defenseDice[1],defenseDice[2]); | |
defenseLow=Math.min(defenseDice[1],defenseDice[2]); | |
//console.log(defenseDice); | |
// console.log("Condition D2"); | |
} | |
} | |
//compare attack and defense | |
if (attackHigh > defenseHigh){ | |
defenseArmies--; | |
// console.log("D Loses"); | |
} | |
else{ | |
attackArmies--; | |
// console.log("A Loses"); | |
} | |
//compare if 2 dice | |
if (a>2 && d>2){ | |
if (attackLow > defenseLow){ | |
defenseArmies--; | |
// console.log("D Loses"); | |
} | |
else{ | |
attackArmies--; | |
// console.log("A Loses"); | |
} | |
} | |
//console.log(a,' Attackers vs. ',d,"Defenders"); | |
}//end of battle function | |
; | |
while(attackArmies>1 && defenseArmies > 0 ){ | |
//Determine number of Attack Dice | |
if (attackArmies>3 ){ | |
attack=3; | |
} | |
else if (attackArmies===3){ | |
attack=2; | |
} | |
else if (attackArmies===2){ | |
attack=1; | |
} | |
//Determine number of Defense dice | |
if(defenseArmies>1){ | |
defense=2; | |
} | |
else if(defenseArmies===1){ | |
defense=1; | |
} | |
battle(attack,defense); | |
}//end of while loop | |
console.log("Remaining Attackers:",attackArmies,"Remaining Defenders:",defenseArmies); | |
document.print("Test"); | |
};//end of function war | |
war(5,10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment