Craps, anyone?
<HTML>
<HEAD><TITLE> CIS201_01_HW5_1.html </TITLE>
<SCRIPT LANGUAGE = "JavaScript">
var WON = 0, LOST = 1, CONTINUE = 2;
var firstRoll = true,
sum = 0,
point_total = 0,
gameStatus = CONTINUE,
won_first_roll = 0,
lost_first_roll = 0,
won_second_roll = 0,
lost_second_roll = 0,
won_twentieth_roll = 0,
lost_twentieth_roll = 0,
won_after_twentieth_roll = 0,
lost_after_twentieth_roll = 0,
total_wins = 0,
total_losses = 0;
function start_simulation(){
if (firstRoll) {
sum = rollDice();
switch (sum){
case 7: case 11:
gameStatus = WON;
won_first_roll++;
total_wins++;
break;
case 2: case 3: case 12:
gameStatus = LOST;
lost_first_roll++;
total_losses++;
break;
default:
gameStatus = CONTINUE;
point_total = sum;
firstRoll = false;
}
}
else {
sum = rollDice();
if (sum == point_total)
gameStatus = WON;
won_second_roll++;
if (sum == 7)
gameStatus = LOST;
lost_second_roll++;
total_losses++;
}
}
x = total_wins + total_losses
function rollDice() {
for (x = 0; x <= 1000;)
var die1, die2, workSum;
die1 = Math.floor(1 + Math.random() * 6);
die2 = Math.floor(1 + Math.random() * 6);
return workSum;
}
chances_of_winning = total_wins / 1000;
document.writeln("Number of times craps is made on first roll" + won_first_roll + "<br>");
document.writeln("Number of times craps is made on second roll" + won_second_roll + "<br>");
document.writeln("Number of times craps is made on twentieth roll" + won_twentieth_roll + "<br>");
document.writeln("Number of times craps is made after twentieth roll" + won_after_twentieth_roll + "<br>");
document.writeln("Chances of winning" + chances_of_winning + "<br>");
document.writeln("Number of times lost on first roll" + lost_first_roll + "<br>");
document.writeln("Number of times lost on second roll" + lost_second_roll + "<br>");
document.writeln("Number of times lost on twentieth roll" + lost_twentieth_roll + "<br>");
document.writeln("Number of times lost after twentieth roll" + lost_after_twentieth_roll + "<br>");
</SCRIPT>
</HEAD>
<BODY ONLOAD = "start_simulation()">
</HTML>
