Hi,
Recently, when some friends and I were playing risk, we realized that towards the end of the game (when you have very large battles) it might be benificial to use a small program to repeatedly roll the dice and keep track of troops. Cutting down a 10 minute battle into a few seconds. This is my first attempt at it. I really don’t know what I’m doing wrong, but then again I haven’t done any programming in over a year. (and that was C++, this is the first JavaScript program I’ve actually written) If anyone has any suggestions it would be greatly appreciated. Thanks! Included is the whole source of the HTML page that it is on, because I think I might be having problems communicating between html and javascript.
~Hal
EDIT: My apologies for not commenting the code, I tried to name the variables in a way that would be self explanitory. If you have any questions about how a Risk! battle is conducted or stuff in the code just ask.
[code]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<title>Risk! Battle Calculator</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<style>
body {
height: 1024px;
padding: 24px 0 0;
margin: 0;
background-color: #A7B4C1;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 0.7em;
line-height: 1.75em;
}
</style>
<script type=”text/javascript”>
var roll = 0;
function rollDie() {
roll = Math.round(Math.random() * 6) % 7;
return roll;
}
var attHigh1 = 0;
var attHigh2 = 0;
var attackRoll[3] = 0;
var defenseRoll[2] = 0;
var attackDice = 0;
var defenseDice = 0;
function getAttackHighest(){
attackDice = document.riskCalc.attDice.value;
for(i = 0; i < attackDice; i++){
attackRoll[i] = roll();
}
var aHI = 0;
attHigh1 = attackRoll[0];
for(i = 0; i < attackDice; i++){
if(attackRoll[i] > attHigh1)
attHigh1 = attackRoll[i];
}
if(attackDice = 3){
if(attackroll [0] > attackroll [1] && attackRoll[0] < attackRoll[2])
attHigh2 = attackRoll[0];
if(attackroll [1] > attackroll [0] && attackRoll[1] < attackRoll[2])
attHigh2 = attackRoll[1];
if(attackroll [2] > attackroll [0] && attackRoll[2] < attackRoll[1])
attHigh2 = attackRoll[2];
if(attackroll [0] < attackroll [1] && attackRoll[0] > attackRoll[2])
attHigh2 = attackRoll[0];
if(attackroll [1] < attackroll [0] && attackRoll[1] > attackRoll[2])
attHigh2 = attackRoll[1];
if(attackroll [2] < attackroll [0] && attackRoll[2] > attackRoll[1])
attHigh2 = attackRoll[2];
}
if(attackDice = 2){
if(attackRoll[0] > attackRoll[1]){
attHigh1 = attackRoll[0];
attHigh2 = attackRoll[1];
}
else {
attHigh1 = attackRoll[1];
attHigh2 = attackRoll[0];
}
}
if(attackDice = 1){
attHigh1 = attackRoll[0];
attHigh2 = 0;
}
}
var defHigh1 = 0;
var defHigh2 = 0;
function getDefenseHighest (){
defenseDice = document.riskCalc.defDice.value;
for(i = 0; i < defenseDice; i++){
defenseRoll[i] = roll();
}
if(defenseDice = 2){
if(defenseRoll[0] > defenseRoll[1]){
defHigh1 = defenseRoll[0];
defHigh2 = defenseRoll[1];
}
else {
defHigh1 = defenseRoll[1];
defHigh2 = defenseRoll[0];
}
}
if(defenseDice = 1){
defHigh1 = defenseRoll[0];
defHigh2 = 0;
}
}
function fight() {
var attackTroops = document.riskCalc.attTroops.value;
var defenseTroops = document.riskCalc.defTroops.value;
var minAttack = document.riskCalc.attMin.value;
var minDefense = document.riskCalc.defMin.value;
var attackRem = 10;
var defenseRem = 150;
while((attackRem > minAttack) && (defenseRem > minDefense)){
getAttackHighest();
getDefenseHighest();
if(attackDice == 1 || defenseDice == 1){
if(attHigh1 > defHigh1)
defenseTroops–;
else attackTroops–;
}
if(attackDice == 2 && defenseDice == 2){
if(attHigh1 > defHigh1)
defenseTroops–;
else attackTroops–;
if(attHigh2 > defHigh2)
defenseTroops–;
else attackTroops–;
}
}
else{
document.riskCalc.attRem.value = attackRem;
document.riskCalc.defRem.value = defenseRem;
}
</script>
</head>
<body>
<div style=”margin-left: 200px;; “>
<form name=”riskCalc” onSubmit=”javascript:fight()”>
<h1>Risk! Battle Calculator</h1>
<p>Compliments of Hal Mayes.</p>
<table width=”326″ border=”1″>
<tr>
<td width=”166″><strong>Attacker</strong></td>
<td width=”144″> </td>
</tr>
<tr>
<td># of troops: </td>
<td><input type=”text” name=”attTroops”></td>
</tr>
<tr>
<td>Stop when troops reach:</td>
<td><input type=”text” name=”attMin”></td>
</tr>
<tr>
<td>Stop when defense reaches: </td>
<td><input type=”text” name=”defMin”></td>
</tr>
<tr>
<td># of dice to roll: </td>
<td><input type=”text” name=”attDice”></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td><strong>Defender</strong></td>
<td> </td>
</tr>
<tr>
<td># of troops:</td>
<td><input type=”text” name=”defTroops”></td>
</tr>
<tr>
<td># of dice to roll: </td>
<td><input type=”text” name=”defDice”></td>
</tr>
</table>
<p>
<input type=”submit” name=”Submit” value=”Fight!”>
</p>
<table width=”326″ border=”1″>
<tr>
<td width=”166″>Attacker has: </td>
<td width=”144″><input type=”text” name=”attRem”></td>
</tr>
<tr>
<td>Defender has: </td>
<td><input type=”text” name=”defRem”></td>
</tr>
</table>
<p> </p>
</form>
</div>
</body>
</html>