top of page

Dice Game

The game of craps is played by rolling two dice and summing the uppermost faces. Remember there are six sides of each die. When you roll two dice together, the possible different rolls would 6 × 6 = 6² or a total of 36 possible outcomes (possibilities). 

With each roll of the two dice, the face values of the dice are added together. If the total is 7 or 11 (a "natural"), the thrower wins and retains the dice for another throw. If the total is 2, 3, or 12 ("craps"), the thrower loses but retains the dice. If the total is any other number (called the thrower's "point"), the thrower must continue throwing and roll the "point" value again before throwing a 7. If he/she succeeds, they would win and would retain the dice for another roll. If the player rolls a 7 first, the player loses and passes the dice to another player.

 

 

 

 

Remaining code: Copy and paste the code below under the code that you have already entered.

 

 

<TD align="center"><B>Player Score:</B><BR><br>Games Won: <input type="text" name="pscore" value="0" size=2 readonly><br>Money Won: <input type="text" name="pmoney" value="0" size=2 readonly></td><td align="center"><B>Computer Score:</B><BR><br> Games Won: <input type="text" name="cscore" value="0" size=2 readonly><br>Money Won:<input type="text" name="cmoney" value="0" size=2 readonly></TD></TR></TABLE></FORM></CENTER><script type="text/javascript"> var dice1, dice2, shoot, state = 0, point, pwin = 0, cwin = 0, bet;function startgame(thisform) { getshooter(thisform); thisform.message.value = "The selected shooter is " + shoot + "."; state = 1; getbet(thisform); }function throwdice(thisform) {dice1=Math.round(Math.random()*6+1);document.images["mydice1"].src=eval("face"+dice1+".src");thisform.dice1.value=dice1;dice2=Math.round(Math.random()*6+1);document.images["mydice2"].src=eval("face"+dice2+".src"); thisform.dice2.value=dice2;thisform.dicesum.value=dice1+dice2; if (state == 2) { if (thisform.dicesum.value == point) { shooter_wins(thisform); } else { if (thisform.dicesum.value == 7) { shooter_loses(thisform); } else { thisform.message.value = "Point is " + point + ". Roll again."; } } } else { if (state == 1) { if (thisform.dicesum.value == 7 || thisform.dicesum.value == 11) { shooter_wins(thisform); } else { if (thisform.dicesum.value == 2 || thisform.dicesum.value == 3 || thisform.dicesum.value == 12) { shooter_loses(thisform); } else { state = 2; point = thisform.dicesum.value; thisform.message.value = "Point is " + point + ". Roll again."; } } } else { if (state == 0) { thisform.message.value = "Click 'Play New Game' to start."; } } } } function getshooter(thisform) { s = -1; for (var i=thisform.shooter.length-1; i>-1; i--) { if (thisform.shooter[i].checked) { s = i; i = -1; } } shoot = thisform.shooter[s].value; } function getbet(thisform) { bet = thisform.betvalue.value; if (bet.length == 0) { bet = 0; thisform.message.value = "Empty bet field, default none."; } else { if (("" + parseInt(bet)) == bet) { // Do nothing. } else { bet = 0; thisform.message.value = "Invalid bet, default none."; } } } function shooter_wins(thisform) { if (shoot == "Player") { thisform.pscore.value = Number(thisform.pscore.value) + 1; thisform.pmoney.value = Number(thisform.pmoney.value) + Number(bet); thisform.cmoney.value = Number(thisform.cmoney.value) - Number(bet); thisform.message.value = "You win this game!"; } else { thisform.cscore.value = Number(thisform.cscore.value) + 1; thisform.pmoney.value = Number(thisform.pmoney.value) - Number(bet); thisform.cmoney.value = Number(thisform.cmoney.value) + Number(bet); thisform.message.value = "The computer wins this game!"; } state = 0; } function shooter_loses(thisform) { if (shoot == "Player") { thisform.cscore.value = Number(thisform.cscore.value) + 1; thisform.pmoney.value = Number(thisform.pmoney.value) - Number(bet); thisform.cmoney.value = Number(thisform.cmoney.value) + Number(bet); thisform.message.value = "The computer wins this game!"; } else { thisform.pscore.value = Number(thisform.pscore.value) + 1; thisform.pmoney.value = Number(thisform.pmoney.value) + Number(bet); thisform.cmoney.value = Number(thisform.cmoney.value) - Number(bet); thisform.message.value = "You win this game!"; } state = 0; } </script> </BODY></HTML>

bottom of page