/    Sign up×
Community /Pin to ProfileBookmark

Risk! Boardgame. Battle Calculator

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″>&nbsp;</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>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><strong>Defender</strong></td>
<td>&nbsp;</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>&nbsp;</p>
</form>
</div>
</body>
</html>

[/code]

to post a comment
JavaScript

29 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — To start... The following is a standard roll of a die:

roll = Math.[COLOR=Blue]floor[/COLOR](Math.random() * 6) [COLOR=Blue]+ 1[/COLOR];

For what is the modulus 7 in your calc?

roll = Math.round(Math.random() * 6) [COLOR=Red]% 7[/COLOR];
Copy linkTweet thisAlerts:
@lahmayesauthorJun 09.2006 — hm, I am not really sure. I copied the code off of a website. Thank you for informing me of the correct way to do it.
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — So, we can roll our dice as follows:
function rollDice(count) {
var dice = new Array();
do { dice.push(Math.floor(Math.random() * 6) + 1);
} while(--count &gt; 0);
return dice;
}

This will return an array containing the number of elements requested as the number of die to roll.
Copy linkTweet thisAlerts:
@lahmayesauthorJun 09.2006 — Ok, so say the defender wants to roll 2 dice, and the attacker wants to roll three. then i would write something like.

<i>
</i>var new defRoll() = rollDice(2);
var new attRoll() = rollDice(3);


is this the right way to do it? also can you allocate an array into dynamic memory (that is what the "new" means, correct?) and just pass a whole array to it, if you get what i'm saying?

or should i do it this way... let aD = number of dice the attacker wants to roll, dD the number the defender wants to roll:

<i>
</i>var defRoll(dD);
var attRoll(aD);

defRoll() = rollDice(dD);
attRoll() = rollDice(aD);
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — Ok, so say the defender wants to roll 2 dice, and the attacker wants to roll three. then i would write something like.

<i>
</i>var new defRoll() = rollDice(2);
var new attRoll() = rollDice(3);


is this the right way to do it? [/QUOTE]

No, it would be more like this:

var defRoll = rollDice(2);

var attRoll = rollDice(3);

The result are both arrays. No pre-allocation necessary.
Copy linkTweet thisAlerts:
@lahmayesauthorJun 09.2006 — Ok, do you have any suggestions for the getAttackHighest and getDefenseHighest? The defender can roll either one or two dice, the attacker can roll 1, 2, or 3 dice. What i need to do is figure out the highest two dice for the attacker, and the highest two dice for the defender. Then i need to compare the highest defenders roll with the highest attackers roll. Then the second highest defenders roll vs the second highest attackers roll. If either party rolls only one die then you just compare that with his/her opponent's highest die.
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — This will put the results in descending order with the highest roll first:

var defRoll = rollDice(2).sort(function(a,b) { return b-a; });

var attRoll = rollDice(3).sort(function(a,b) { return b-a; });
Copy linkTweet thisAlerts:
@lahmayesauthorJun 09.2006 — wow, that makes things alot easier.

here is what i have now. I have a feeling there are syntax errors but I don't know how to check them.

<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Risk! Battle Calculator&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;style&gt;
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;
}

&lt;/style&gt;
&lt;script type="text/javascript"&gt;

function rollDice(count) {
var dice = new Array();
do { dice.push(Math.floor(Math.random() * 6) + 1);
} while(--count &gt; 0);
return dice;
}

var attRoll = new Array();
var defRoll = new Array();

var attackRem = 0;
var defenseRem = 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 attackDice = document.riskCalc.attDice.value;
var defenseDice = document.riskCalc.defDice.value;

var defRoll = rollDice(defenseDice).sort(function(a,b) { return b-a; });
var attRoll = rollDice(attackDice).sort(function(a,b) { return b-a; });

<i> </i>while((attackTroops &gt; minAttack) &amp;&amp; (defenseTroops &gt; minDefense)){
<i> </i> if(attackDice == 1 || defenseDice == 1){
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i> else{
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> if(defRoll[1] &lt; attRoll[1])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i>}
<i> </i>document.riskCalc.attRem.value = attackTroops;
<i> </i>document.riskCalc.defRem.value = defenseTroops;
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div style="margin-left: 200px;; "&gt;
&lt;form name="riskCalc" onSubmit="return fight(this)"&gt;
&lt;h1&gt;Risk! Battle Calculator&lt;/h1&gt;
&lt;p&gt;Compliments of Hal Mayes.&lt;/p&gt;
&lt;table width="326" border="1"&gt;
&lt;tr&gt;
&lt;td width="166"&gt;&lt;strong&gt;Attacker&lt;/strong&gt;&lt;/td&gt;
&lt;td width="144"&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;# of troops: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="attTroops"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop when troops reach:&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="attMin"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop when defense reaches: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="defMin"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;# of dice to roll: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="attDice"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Defender&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;# of troops:&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="defTroops"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;# of dice to roll: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="defDice"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;
&lt;input type="submit" name="Submit" value="Fight!"&gt;
&lt;/p&gt;
&lt;table width="326" border="1"&gt;
&lt;tr&gt;
&lt;td width="166"&gt;Attacker has: &lt;/td&gt;
&lt;td width="144"&gt;&lt;input type="text" name="attRem"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Defender has: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="defRem"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;


I think I am very close now. I just can't quite get it to work. Thank you for all of your help so far.

EDIT: updated the code
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — I fixed all the syntax errors and adjusted your HTML a bit. The battle results are invalid though.
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Risk! Battle Calculator&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;style&gt;
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;
}

&lt;/style&gt;
&lt;script type="text/javascript"&gt;

function rollDice(count) {
var dice = new Array();
do { dice.push(Math.floor(Math.random() * 6) + 1);
} while(--count &gt; 0);
return dice;
}

var attackRem = 0;
var defenseRem = 0;

function fight(f) {
var attackTroops = f.attTroops.value;
var defenseTroops = f.defTroops.value;
var minAttack = f.attMin.value;
var minDefense = f.defMin.value;
var attackDice = f.attDice.value;
var defenseDice = f.defDice.value;

<i> </i>var defRoll = rollDice(defenseDice).sort(function(a,b) { return b-a; });
<i> </i>var attRoll = rollDice(attackDice).sort(function(a,b) { return b-a; });

<i> </i>while((attackRem &gt; minAttack) &amp;&amp; (defenseRem &gt; minDefense)){

<i> </i> if(attackDice == 1 || defenseDice == 1){
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i> else{
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> if(defRoll[1] &lt; attRoll[1])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i>}
<i> </i>f.attRem.value = attackRem;
<i> </i>f.defRem.value = defenseRem;
<i> </i>return true;
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div style="margin-left: 200px;; "&gt;
&lt;form action="" onsubmit="return false;"&gt;
&lt;h1&gt;Risk! Battle Calculator&lt;/h1&gt;
&lt;p&gt;Compliments of Hal Mayes.&lt;/p&gt;
&lt;table width="326" border="1"&gt;
&lt;tr&gt;
&lt;td width="166"&gt;&lt;strong&gt;Attacker&lt;/strong&gt;&lt;/td&gt;
&lt;td width="144"&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;# of troops: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="attTroops"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop when troops reach:&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="attMin"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop when defense reaches: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="defMin"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;# of dice to roll: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="attDice"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Defender&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;# of troops:&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="defTroops"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;# of dice to roll: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="defDice"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;
&lt;input type="button" value="Fight!" onclick="return fight(this.form)"&gt;
&lt;/p&gt;
&lt;table width="326" border="1"&gt;
&lt;tr&gt;
&lt;td width="166"&gt;Attacker has: &lt;/td&gt;
&lt;td width="144"&gt;&lt;input type="text" name="attRem"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Defender has: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="defRem"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@lahmayesauthorJun 09.2006 — wow. thanks a ton. i see that the troop numbers are not being changed. Basically there are two situations that are important.

1) one or both parties roll only one die.

2) neither parties roll only one die.

for the first situation. I would like to compare the highest of each. Whoever loses this comparison also loses one troop.

for the second situation, I would like to compare the highest of each, and again, whoever loses this comparison also loses one troop. I would then like to compare the second highest of each. Whoever loses this comparison will lose one troop.

based on what i have so far. can you tell what I am doing wrong? did make one minor change in the while() statement. Here is what i have:

<i>
</i>while((attackTroops &gt; minAttack) &amp;&amp; (defenseTroops &gt; minDefense)){

<i> </i> if(attackDice == 1 || defenseDice == 1){
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i> else{
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> if(defRoll[1] &lt; attRoll[1])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i>}
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — Here's the latest:
function fight(f) {
var attackTroops = f.attTroops.value;
var defenseTroops = f.defTroops.value;
var minAttack = f.attMin.value;
var minDefense = f.defMin.value;
var attackDice = f.attDice.value;
var defenseDice = f.defDice.value;
while((attackTroops &gt; minAttack) &amp;&amp; (defenseTroops &gt; minDefense)){

<i> </i> if(defenseDice &lt;= defenseTroops) defenseDice = defenseTroops - 1;
<i> </i> if(attackDice &lt;= attackTroops) attackDice = attackTroops - 1;

<i> </i> var defRoll = rollDice(defenseDice).sort(function(a,b) { return b-a; });
<i> </i> var attRoll = rollDice(attackDice).sort(function(a,b) { return b-a; });

<i> </i> if(attackDice == 1 || defenseDice == 1){
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i> else{
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> if(defRoll[1] &lt; attRoll[1])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i>}
<i> </i>f.attRem.value = attackRem;
<i> </i>f.defRem.value = defenseRem;
<i> </i>return true;
}
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — More corrections:
function fight(f) {
var attackTroops = f.attTroops.value;
var defenseTroops = f.defTroops.value;
var minAttack = f.attMin.value;
var minDefense = f.defMin.value;
var attackDice = f.attDice.value;
var defenseDice = f.defDice.value;
while((attackTroops &gt; minAttack) &amp;&amp; (defenseTroops &gt; minDefense)){

<i> </i> if(defenseDice &lt;= defenseTroops) defenseDice = defenseTroops - 1;
<i> </i> if(attackDice &lt;= attackTroops) attackDice = attackTroops - 1;

<i> </i> var defRoll = rollDice(defenseDice).sort(function(a,b) { return b-a; });
<i> </i> var attRoll = rollDice(attackDice).sort(function(a,b) { return b-a; });

<i> </i> if(attackDice == 1 || defenseDice == 1){
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i> else{
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> if(defRoll[1] &lt; attRoll[1])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i>}
<i> </i>f.attRem.value = attackTroops;
<i> </i>f.defRem.value = defenseTroops;
<i> </i>return true;
}
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — Adjustment to dice count logic:
function fight(f) {
var attackTroops = f.attTroops.value;
var defenseTroops = f.defTroops.value;
var minAttack = f.attMin.value;
var minDefense = f.defMin.value;
var attackDice = f.attDice.value;
var defenseDice = f.defDice.value;
while(attackTroops &gt; minAttack &amp;&amp; attackDice &gt; 0
&amp;&amp; defenseTroops &gt; minDefense &amp;&amp; defenseDice &gt; 0){

<i> </i> if(defenseDice &lt;= defenseTroops) defenseDice = defenseTroops - 1;
<i> </i> if(attackDice &lt;= attackTroops) attackDice = attackTroops - 1;

<i> </i> if(defenseDice &gt; 0 &amp;&amp; attackDice &gt; 0) {
<i> </i> var defRoll = rollDice(defenseDice).sort(function(a,b) { return b-a; });
<i> </i> var attRoll = rollDice(attackDice).sort(function(a,b) { return b-a; });

<i> </i> if(attackDice == 1 || defenseDice == 1){
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i> else{
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> if(defRoll[1] &lt; attRoll[1])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i> }
<i> </i>}
<i> </i>f.attRem.value = attackTroops;
<i> </i>f.defRem.value = defenseTroops;
<i> </i>return true;
}
Copy linkTweet thisAlerts:
@lahmayesauthorJun 09.2006 — I am still not getting any reduction in troops. Also, doesn't this change the number of dice rolled to the number of troops - 1 ?

<i>
</i> if(defenseDice &lt;= defenseTroops) defenseDice = defenseTroops - 1;
if(attackDice &lt;= attackTroops) attackDice = attackTroops - 1;


I want the number of dice rolled to always stay the same (whatever the user enters)
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — The number of dice can't stay the same. The rules are that the maximum dice you can roll is one less than the number of troops you have -- or something like that.
I am still not getting any reduction in troops.[/QUOTE]
You need these two lines at the end of the function:

f.attRem.value = attackTroops;

f.defRem.value = defenseTroops;
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — The latest:
function fight(f) {
var attackTroops = f.attTroops.value;
var defenseTroops = f.defTroops.value;
var minAttack = f.attMin.value;
var minDefense = f.defMin.value;
var attackDice = f.attDice.value;
var defenseDice = f.defDice.value;

<i> </i>while(attackTroops &gt; minAttack &amp;&amp; attackDice &gt; 0
<i> </i> &amp;&amp; defenseTroops &gt; minDefense &amp;&amp; defenseDice &gt; 0){

<i> </i> if(attackDice &lt;= attackTroops) attackDice = attackTroops - 1;
<i> </i> if(defenseDice &lt; defenseTroops) defenseDice = defenseTroops;

<i> </i> if(attackDice &gt; 0 &amp;&amp; defenseDice &gt; 0) {
<i> </i> var attRoll = rollDice(attackDice).sort(function(a,b) { return b-a; });
<i> </i> var defRoll = rollDice(defenseDice).sort(function(a,b) { return b-a; });

<i> </i> if(attackDice == 1 || defenseDice == 1){
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i> else{
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> if(defRoll[1] &lt; attRoll[1])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i> }
<i> </i>}

<i> </i>f.attRem.value = attackTroops;
<i> </i>f.defRem.value = defenseTroops;
<i> </i>return true;
}
Copy linkTweet thisAlerts:
@lahmayesauthorJun 09.2006 — Yes, you are correct. I forgot about that. The attacker must have one mor troops than he is rolling dice. But also you can only throw a maximum of 3 dice (for the attacker) and 2 dice for the defender. so the number of dice has to stay the same for the attacker until his troops goes below 4, and fthe defender can throw as many dice as he has troops. so his dice must be cut down to one when he has one troop left. Hmmmmm any idea how to do that?
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — Yeah, the rule is that the attacker can only roll one less die than the number of troops the attacker has. The defender can only roll as many die as the number of troops the defender has. This is so that the attacker cannot completely disappear during a battle, but the defender can.
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — That latest function has that change in it. max 3 and max 2

Add these before the [B]while[/B] loop.


if(attackDice > 3) attackDice = 3;
if(defenseDice > 2) defenseDice = 2;
Copy linkTweet thisAlerts:
@lahmayesauthorJun 09.2006 — hm. Yeah. Everything looks good, the only problem is if you want to stop when the defense has say, 3 men left, it doesn't seem to calculate. Oh i also made a change to the dice-changing if statements ">" was facing the wrong way.


if(attackDice &gt;= attackTroops) attackDice = attackTroops - 1;
if(defenseDice &gt;= defenseTroops) defenseDice = defenseTroops;
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — Ah, well ? nobody's perfect. But, mine look fine:

if(attackTroops <= attackDice) attackDice = attackTroops - 1;

if(defenseTroops < defenseDice) defenseDice = defenseTroops;

? Yours need correcting (note second test).
Copy linkTweet thisAlerts:
@lahmayesauthorJun 09.2006 — ah, yes. any idea how to fix the numbers problem. Say the attacker wishes to cease attacking once the defense reaches say 5 troops. It doesn't seem to work. :-/

the values i tried were

attacker

# of troops: 50


stop when attacking troops reach: 5

stop when defending troops reach: 5

#of dice to roll: 3

defender

# of troops: 20


# of dice to roll: 2



i just get 50 and 20 as results. Its as if the while loop was never entered into. But i don't know why.
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — This contains all fixes discussed and addresses that last problem:
function fight(f) {
var attackTroops = Number(f.attTroops.value);
var defenseTroops = Number(f.defTroops.value);
var minAttack = Number(f.attMin.value);
var minDefense = Number(f.defMin.value);
var attackDice = Number(f.attDice.value);
var defenseDice = Number(f.defDice.value);

<i> </i>if(attackDice &gt; 3) attackDice = 3;
<i> </i>if(defenseDice &gt; 2) defenseDice = 2;

<i> </i>while(attackTroops &gt; minAttack &amp;&amp; attackDice &gt; 0
<i> </i> &amp;&amp; defenseTroops &gt; minDefense &amp;&amp; defenseDice &gt; 0){

<i> </i> if(attackTroops &lt;= attackDice) attackDice = attackTroops - 1;
<i> </i> if(defenseTroops &lt; defenseDice) defenseDice = defenseTroops;

<i> </i> if(attackDice &gt; 0 &amp;&amp; defenseDice &gt; 0) {
<i> </i> var attRoll = rollDice(attackDice).sort(function(a,b) { return b-a; });
<i> </i> var defRoll = rollDice(defenseDice).sort(function(a,b) { return b-a; });

<i> </i> if(attackDice == 1 || defenseDice == 1){
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i> else{
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> if(defRoll[1] &lt; attRoll[1])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i> }
<i> </i>}

<i> </i>f.attRem.value = attackTroops;
<i> </i>f.defRem.value = defenseTroops;
<i> </i>return true;
}
Copy linkTweet thisAlerts:
@lahmayesauthorJun 09.2006 — wow, i just found out a really wierd behavior. If the ratio of minimum defense troops to starting defense troops is 1/10 or lower, then it works fine. if it is higher than 1/10 then it does nothing. weird...
Copy linkTweet thisAlerts:
@lahmayesauthorJun 09.2006 — beautiful. was i all wet about the 1/10 thing or was that just a coincidence? i take it you did some kind of typecasting?
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — Post above fixes that problem.
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — Yep, all form fields are string values only. Thus, the numeric comparisons were failing because of the string values.
Copy linkTweet thisAlerts:
@lahmayesauthorJun 09.2006 — Thank you very much for all your help on this. I will post the full working code below for anyone else who would like to use it.

<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Risk! Battle Calculator&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;style&gt;
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;
}

&lt;/style&gt;
&lt;script type="text/javascript"&gt;

function rollDice(count) {
var dice = new Array();
do { dice.push(Math.floor(Math.random() * 6) + 1);
} while(--count &gt; 0);
return dice;
}

function fight(f) {
var attackTroops = Number(f.attTroops.value);
var defenseTroops = Number(f.defTroops.value);
var minAttack = Number(f.attMin.value);
var minDefense = Number(f.defMin.value);
var attackDice = Number(f.attDice.value);
var defenseDice = Number(f.defDice.value);

<i> </i>if(attackDice &gt; 3) attackDice = 3;
<i> </i>if(defenseDice &gt; 2) defenseDice = 2;

<i> </i>while(attackTroops &gt; minAttack &amp;&amp; attackDice &gt; 0
<i> </i> &amp;&amp; defenseTroops &gt; minDefense &amp;&amp; defenseDice &gt; 0){

<i> </i> if(attackTroops &lt;= attackDice) attackDice = attackTroops - 1;
<i> </i> if(defenseTroops &lt; defenseDice) defenseDice = defenseTroops;

<i> </i> if(attackDice &gt; 0 &amp;&amp; defenseDice &gt; 0) {
<i> </i> var attRoll = rollDice(attackDice).sort(function(a,b) { return b-a; });
<i> </i> var defRoll = rollDice(defenseDice).sort(function(a,b) { return b-a; });

<i> </i> if(attackDice == 1 || defenseDice == 1){
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i> else{
<i> </i> if(defRoll[0] &lt; attRoll[0])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> if(defRoll[1] &lt; attRoll[1])
<i> </i> defenseTroops--;
<i> </i> else attackTroops--;
<i> </i> }
<i> </i> }
<i> </i>}

<i> </i>f.attRem.value = attackTroops;
<i> </i>f.defRem.value = defenseTroops;
<i> </i>return true;
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div style="margin-left: 200px;"&gt;
&lt;form action="" onsubmit="return false;"&gt;
&lt;h1&gt;Risk! Battle Calculator&lt;/h1&gt;
&lt;p&gt;Compliments of phpnovice and Hal Mayes.&lt;/p&gt;
&lt;table width="326" border="1"&gt;
&lt;tr&gt;
&lt;td width="166"&gt;&lt;strong&gt;Attacker&lt;/strong&gt;&lt;/td&gt;
&lt;td width="144"&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;# of troops: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="attTroops"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop when troops reach:&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="attMin"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stop when defense reaches: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="defMin"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;# of dice to roll: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="attDice"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Defender&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;# of troops:&lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="defTroops"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;# of dice to roll: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="defDice"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;
&lt;input type="button" value="Fight!" onclick="return fight(this.form)"&gt;
&lt;/p&gt;
&lt;table width="326" border="1"&gt;
&lt;tr&gt;
&lt;td width="166"&gt;Attacker has: &lt;/td&gt;
&lt;td width="144"&gt;&lt;input type="text" name="attRem"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Defender has: &lt;/td&gt;
&lt;td&gt;&lt;input type="text" name="defRem"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2006 — You're welcome.

Cheers.
×

Success!

Help @lahmayes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...