Javascript help

From Visual Basic to GNU C, this is the place to talk programming.

Moderators: SecretSquirrel, just brew it!

Javascript help

Postposted on Thu Apr 24, 2003 3:48 pm

Hey, Im having a hell of a time trying to get my logic write for this web page. I was supposed to take our craps game code and make it loop 1000 times, and then display how many games were won or lost on each of the first 20 rolls, and then how many for 21 and up together. I have most of it working except for the main part dealing with the wins/losses for each roll. I have looked at a couple examples I found on the web, but they werent designed in the manner our original craps game was. Can some take a look at this (horribly designed) html/javascript and point me in the right direction?
Thanks

-------------------------------------------------------------------------------

<!--Alan -->
<!--MIS 431 -->
<!--HW11.15 -->

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<html xmlns = "http://www.w3.org/1999/xhtml">
<head><title>Alan: Homework 11.15</title>

<script type = "text/javascript">
<!--
var WON = 0, LOST = 1, CONTINUE_ROLLING = 2;

var wins,
losses,
firstRoll = true, // true if first roll
sumOfDice = 0, // sum of the dice
myPoint = 0, // point if no win/loss on first roll
gameStatus = CONTINUE_ROLLING, // game not over yet
wins = new Array(),
rolls = 0,
rollsX = 1,
avgGame,
winPer,
lossPer,
losses = new Array(); //2 arrays win and loss counters and rolls????


// process one roll of the dice
function play() //do I need to loop this whole function until it wins or loses?
{

var complete = 1;

while ( complete != 0 )
{

if ( firstRoll ) { // first roll of the dice
sumOfDice = rollDice();

switch ( sumOfDice ) {
case 7: case 11: // win on first roll
gameStatus = WON;

for (var i=0; i<1; i++) //total up all wins on first roll??????
{
wins[i]++;
document.craps.win1.value = wins[i];
}

document.craps.point.value = "";
rolls++;
document.craps.rollsTotal.value = rolls;
complete = 0;
break;

case 2: case 3: case 12: // lose on first roll
gameStatus = LOST;

for (var i=0; i<1; i++) //total up all losses on first roll??????
{
losses[i]++;
document.craps.loss1.value = losses[i];
}

rolls++;
document.craps.rollsTotal.value = rolls;
document.craps.point.value = "";
complete = 0;
break;

default: // remember point
gameStatus = CONTINUE_ROLLING;
myPoint = sumOfDice;
document.craps.point.value = myPoint;
firstRoll = false;

}
}
else {
sumOfDice = rollDice();

if ( sumOfDice == myPoint ) // win by making point
{ gameStatus = WON;
rolls++;
document.craps.rollsTotal.value = rolls;
complete = 0; }
else
if ( sumOfDice == 7 ) // lose by rolling 7
{ gameStatus = LOST;
rolls++;
document.craps.rollsTotal.value = rolls;
complete = 0; }
}

if ( gameStatus == CONTINUE_ROLLING ) {
rolls++;
document.craps.rollsTotal.value = rolls; }
else {
firstRoll = true;
}

}

}




// roll the dice
function rollDice()
{
var die1, die2, workSum;

die1 = Math.floor( 1 + Math.random() * 6 );
die2 = Math.floor( 1 + Math.random() * 6 );
workSum = die1 + die2;

document.craps.firstDie.value = die1;
document.craps.secondDie.value = die2;
document.craps.sum.value = workSum;

return workSum;
}


function play1000()
{
var totalw = 0;

for (var x=0; x<1000; x++)
{
wins[x] = 0;
losses[x] = 0;
}

for (var y=0; y<1000; y++)
{
play();
}

//document.craps.win1.value = wins[1];
document.craps.win2.value = wins[2];
document.craps.win3.value = wins[3];
document.craps.win4.value = wins[4];
document.craps.win5.value = wins[5];
document.craps.win6.value = wins[6];
document.craps.win7.value = wins[7];
document.craps.win8.value = wins[8];
document.craps.win9.value = wins[9];
document.craps.win10.value = wins[10];
document.craps.win11.value = wins[11];
document.craps.win12.value = wins[12];
document.craps.win13.value = wins[13];
document.craps.win14.value = wins[14];
document.craps.win15.value = wins[15];
document.craps.win16.value = wins[16];
document.craps.win17.value = wins[17];
document.craps.win18.value = wins[18];
document.craps.win19.value = wins[19];
document.craps.win20.value = wins[20];
document.craps.win21.value = wins[21];

//document.craps.loss1.value = losses[1];
document.craps.loss2.value = losses[2];
document.craps.loss3.value = losses[3];
document.craps.loss4.value = losses[4];
document.craps.loss5.value = losses[5];
document.craps.loss6.value = losses[6];
document.craps.loss7.value = losses[7];
document.craps.loss8.value = losses[8];
document.craps.loss9.value = losses[9];
document.craps.loss10.value = losses[10];
document.craps.loss11.value = losses[11];
document.craps.loss12.value = losses[12];
document.craps.loss13.value = losses[13];
document.craps.loss14.value = losses[14];
document.craps.loss15.value = losses[15];
document.craps.loss16.value = losses[16];
document.craps.loss17.value = losses[17];
document.craps.loss18.value = losses[18];
document.craps.loss19.value = losses[19];
document.craps.loss20.value = losses[20];
document.craps.loss21.value = losses[21];

avgGame = rolls / 1000; //average number of rolls per game
document.craps.avgLength.value = avgGame;

for (var element in wins )
{
totalw += wins[ element ]; //adds up all valid elements in win array
}
winPer = (totalw / 1000) * 100; //determines the win percentage
document.craps.winPercentage.value = winPer;
lossPer = 100 - winPer;
document.craps.lossPercentage.value = lossPer;
}



// -->
</script>

</head><body>
<form name = "craps" action = "">

<table border = "1">
<caption>Craps</caption>
<tr><td>Die 1</td>
<td><input name = "firstDie" type = "text" />
</td></tr>
<tr><td>Die 2</td>
<td><input name = "secondDie" type = "text" />
</td></tr>
<tr><td>Sum</td>
<td><input name = "sum" type = "text" />
</td></tr>
<tr><td>Point</td>
<td><input name = "point" type = "text" />
</td></tr>
<tr><td><input type = "button" value = "Roll Dice" name = "rollButton"
onclick = "play()" /></td></tr>
</table><br />


<table border = "1">
<tr><td>Roll Number</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
<td>13</td>
<td>14</td>
<td>15</td>
<td>16</td>
<td>17</td>
<td>18</td>
<td>19</td>
<td>20</td>
<td>20+</td></tr>

<tr><td>Games Won</td>
<td><input size = 2 name = "win1" type = "text" /></td>
<td><input size = 2 name = "win2" type = "text" /></td>
<td><input size = 2 name = "win3" type = "text" /></td>
<td><input size = 2 name = "win4" type = "text" /></td>
<td><input size = 2 name = "win5" type = "text" /></td>
<td><input size = 2 name = "win6" type = "text" /></td>
<td><input size = 2 name = "win7" type = "text" /></td>
<td><input size = 2 name = "win8" type = "text" /></td>
<td><input size = 2 name = "win9" type = "text" /></td>
<td><input size = 2 name = "win10" type = "text" /></td>
<td><input size = 2 name = "win11" type = "text" /></td>
<td><input size = 2 name = "win12" type = "text" /></td>
<td><input size = 2 name = "win13" type = "text" /></td>
<td><input size = 2 name = "win14" type = "text" /></td>
<td><input size = 2 name = "win15" type = "text" /></td>
<td><input size = 2 name = "win16" type = "text" /></td>
<td><input size = 2 name = "win17" type = "text" /></td>
<td><input size = 2 name = "win18" type = "text" /></td>
<td><input size = 2 name = "win19" type = "text" /></td>
<td><input size = 2 name = "win20" type = "text" /></td>
<td><input size = 2 name = "win21" type = "text" /></td></tr>

<tr><td>Games Lost</td>
<td><input size = 2 name = "loss1" type = "text" /></td>
<td><input size = 2 name = "loss2" type = "text" /></td>
<td><input size = 2 name = "loss3" type = "text" /></td>
<td><input size = 2 name = "loss4" type = "text" /></td>
<td><input size = 2 name = "loss5" type = "text" /></td>
<td><input size = 2 name = "loss6" type = "text" /></td>
<td><input size = 2 name = "loss7" type = "text" /></td>
<td><input size = 2 name = "loss8" type = "text" /></td>
<td><input size = 2 name = "loss9" type = "text" /></td>
<td><input size = 2 name = "loss10" type = "text" /></td>
<td><input size = 2 name = "loss11" type = "text" /></td>
<td><input size = 2 name = "loss12" type = "text" /></td>
<td><input size = 2 name = "loss13" type = "text" /></td>
<td><input size = 2 name = "loss14" type = "text" /></td>
<td><input size = 2 name = "loss15" type = "text" /></td>
<td><input size = 2 name = "loss16" type = "text" /></td>
<td><input size = 2 name = "loss17" type = "text" /></td>
<td><input size = 2 name = "loss18" type = "text" /></td>
<td><input size = 2 name = "loss19" type = "text" /></td>
<td><input size = 2 name = "loss20" type = "text" /></td>
<td><input size = 2 name = "loss21" type = "text" /></td></tr>
</table><br />

<input type = "button" value = "Roll Dice 1000X " name = "rollButton" onclick = "play1000()" /><br /><br />

<table border = "1" ><tr><td>Total Number of Rolls</td>
<td><input size = 4 name = "rollsTotal" type = "text" /></td></tr>
<tr><td>Average Rolls per Game</td>
<td><input size = 4 name = "avgLength" type = "text" /></td></tr>
<tr><td>Winning Percentage</td>
<td><input size = 4 name = "winPercentage" type = "text" /></td></tr>
<tr><td>Losing Percentage</td>
<td><input size = 4 name = "lossPercentage" type = "text" /></td></tr>
</table><br /><br />

</form>
</body></html>
Your bargaining posture is highly dubious.
Coldfirex
Graphmaster Gerbil
 
Posts: 1109
Joined: Wed Dec 26, 2001 6:00 pm
Location: College Station, TX

Postposted on Fri Apr 25, 2003 7:50 am

I am not very good with switch statements, but they look somewhat messy to me. Try using if statements instead

if (sumOfDice == 7)
{
gameSTATUS = WON;
....(other code);
}
if (sumOfDice == 2 || sumOfDice == 3 || sumOfDice == 12)
{
gameSTAUS = LOST;
......(other code);
}
Windows XP - The 64-bit wannabe with a 32-bit graphics interface for 16-bit extensions to a 8-bit patch on a 4-bit operating system designed to run on a 2-bit processor by a company that can't stand 1-bit of competition
fc34
Minister of Gerbil Affairs
 
Posts: 2816
Joined: Wed May 08, 2002 7:39 am
Location: Somewhere

Postposted on Fri Apr 25, 2003 2:42 pm

thanks for the reply. Well, that part wrks fine. Its just the actual part of assignning the wins and losses to each assigned roll.
Your bargaining posture is highly dubious.
Coldfirex
Graphmaster Gerbil
 
Posts: 1109
Joined: Wed Dec 26, 2001 6:00 pm
Location: College Station, TX


Return to Developer's Den

Who is online

Users browsing this forum: No registered users and 1 guest