// Author: Andy Asberry // CS316 Program 3: program to simulate the game of Craps // October 2007 var age; // variable for age of user var bet; // variable for the current bet var MAXbet; // variable for the maximum bet of the user var stake; // variable for the current stake var MAXstake; // variable for the maximum stake that the user reached var MINstake; // variable for the minimum stake that the user fell to var die1; // variable for the first die var die2; // variable for the second die var sum; // variable for the sum of the dice var numGames; // variable for the number of games var point; // variable for the point var numRolls; // variable for the number of rolls var originalBet; // variable for the original bet if double on loss functionality is applied var firstOriginalBet; // variable with boolean value whether or not the current bet is the original bet before doubling on loss var ifdouble; // variable with boolean value whether or not the double on loss feature is turned on or off var MAXnumGames = 2000; // variable for the maximum number of games that can be played var MAX_BET = 1000; // variable for the maximum bet that can be used // init function to initialize all necessary variables and text fields to their respective start states function init() { bet = 1; // Initially, the current bet is set to 1 MAXbet = 0; // initially, the maximum bet is set to 0 MAXstake = 0; // initially, the maximum stake is set to 0 MINstake = 0; // initially, the minimum stake is set to 0 stake = 0; // initially, the current stake is set to 0 die1 = 0; // initially, the first die is set to 0. If it remains 0, there must have been some error since a die is 1-6 die2 = 0; // initially, the second die is set to 0. If it remains 0, there must have been some error " " " " " sum = 0; // initially, the sum of the dice is set to 0 point = 0; // initially, the point is set to 0 numGames = 1000; // initially, the number of games for the run simulation functionality is 1000 numRolls = 0; // initially, the number of dice rolls is zero since the dice have not yet been rolled originalBet = 1; // initially, since the initial bet is 1, the initial original bet is set to 1 also firstOriginalBet = true; // initially, this flag is set to true since it is not yet the second bet ifdouble = false; // initially, this flag is set to false since the double on loss button has not yet been clicked document.getElementById('bet').value = bet; // set the bet display with the value of the variable bet (1) document.getElementById('stake').value = ''; // set the stake display with blank contents document.getElementById('minstake').value = MINstake; // set the minimum stake display with the value being the value of variable MINstake (0) document.getElementById('maxstake').value = MAXstake; // set the maximum stake display with the value being the value of variable MAXstake (0) document.getElementById('maxbet').value = MAXbet; // set the maximum bet display with the value being the value of variable MAXbet (0) document.getElementById('die1').value = ''; // set the die 1 display with blank contents document.getElementById('die2').value = ''; // set the die 2 display with blank contents document.getElementById('sum').value = ''; // set the sum display with blank contents document.getElementById('double').value = ''; // set the double on loss display with blank contents document.getElementById('winstatus').value = ''; // set the win/loss status display with blank contents } // function to simulate rolling dice, and determining win/loss status. this function also updates the corresponding displays (MAXbet, MAXstake, etc) as it goes function rollDice() { numRolls++; // increment the number of rolls since the dice have just been rolled bet = document.getElementById('bet').value; // set the value of the bet variable equal to the contents from the bet display die1 = Math.floor(Math.random()* (6 + 1)); // set the value of the die1 variable to some random number between 0 and 6 inclusively // while the random number for die1 is 0, while (die1 == 0) { die1 = Math.floor(Math.random()* (6 + 1)); // we need a number between 1 and 6 inclusively since a die does not have a side with 0 dots } die2 = Math.floor(Math.random()* (6 + 1)); // set the value of the die2 variable to some random number between 0 and 6 inclusively // while the random number for die2 is 0, while (die2 == 0) { die2 = Math.floor(Math.random()* (6 + 1)); // we need a number between 1 and 6 inclusively since a die does not have a side with 0 dots } document.getElementById('die1').value = die1; // set the die1 display to the value of the die1 variable document.getElementById('die2').value = die2; // set the die2 display to the value of the die2 variable sum = parseInt(die1,10) + parseInt(die2,10); // set the value of the sum variable to the value of die1 variable plus the value of the die2 variable document.getElementById('sum').value = sum; // set the sum display to the value of the sum variable // if the current bet is larger than the maximum bet made so far if (bet > MAXbet) { MAXbet = bet; // make the current bet the maximum bet document.getElementById('maxbet').value = MAXbet; // then change the maxbet display to the value of the MAXbet variable } // if the current roll is the first roll in this round if (numRolls == 1) { // if the sum of the dice is 7 or 11 if (sum == 7 || sum == 11) { document.getElementById('winstatus').value = 'You win!'; // change the win/loss status display to win status stake = parseInt(stake,10) + parseInt(bet,10); // add the current bet to the current stake document.getElementById('stake').value = stake; // set the stake display // if the double on loss option has been enabled if (ifdouble == true) { if (firstOriginalBet == false) { bet = parseInt(originalBet,10); // the bet is equal to the original bet made at the beginning of the double on loss document.getElementById('bet').value = bet; // set the bet display back to the original bet firstOriginalBet = true; // the loss sequence has been broken so reset it } } // if the current stake is larger than the previous maximum stake if (stake > MAXstake) { MAXstake = parseInt(stake,10); // then set the maximum stake equal to the current stake document.getElementById('maxstake').value = MAXstake; // set the maximum stake display to the value of the maximum stake variable } numRolls = 0; // the round has been rolled, so the number of rolls is reset to 0 for the next round } // else if the sum of the dice is 2, 3, or 12 else if (sum == 2 || sum == 3 || sum == 12) { document.getElementById('winstatus').value = 'You lose.'; // set the win/loss status display stake = parseInt(stake,10) - parseInt(bet,10); // subtract the bet from the stake document.getElementById('stake').value = stake; // set the stake display with the value of the stake variable // if the current stake is less than the current minimum stake if (stake < MINstake) { MINstake = parseInt(stake,10); // set the minimum stake variable to the current stake document.getElementById('minstake').value = MINstake; // set the minimum stake display to the value of the minimum stake variable } // if the double on loss option is enabled if (ifdouble == true) { // if it is the first loss in the loss sequence if (firstOriginalBet == true) { bet = document.getElementById('bet').value; // set the bet to the value of the bet dispay originalBet = parseInt(bet,10); // set the original bet to the value of the bet variable firstOriginalBet = false; // it is no longer the first loss in the loss sequence } bet = 2*parseInt(bet,10); // double the bet document.getElementById('bet').value = bet; // set the bet display // if the current bet is larger than the current maximum bet if (bet > MAXbet) { MAXbet = parseInt(bet,10); // set the maximum bet variable to the current bet document.getElementById('maxbet').value = MAXbet; // set the maximum bet display } } numRolls = 0; // reset the number of rolls to 0 since the user lost. } // else the sum is made to be the point for this round else { point = parseInt(sum,10); // set the point variable to the value of the sum document.getElementById('winstatus').value = 'Roll again to make ' + point; // set the win/loss staus display to roll again to make the point numRolls++; // increment the number of rolls } } // else it is not the first roll in this round and the rules differ slightly else { // if the sum of the dice is 7 if (sum == 7) { document.getElementById('winstatus').value = 'You lose.'; // the user loses, set the win/loss status display stake = parseInt(stake,10) - parseInt(bet,10); // subtract the bet from the current stake // if the double on loss option has been enabled if (ifdouble == true) { // if it is the first loss in the loss sequence if (firstOriginalBet == true) { bet = document.getElementById('bet').value; // set the value of the bet variable to the value of the bet display originalBet = parseInt(bet,10); // set the value of the original bet to the current bet firstOriginalBet = false; // it is no longer the first loss in the loss sequence } bet = 2*parseInt(bet,10); // double the current bet document.getElementById('bet').value = bet; // set the value of the bet display to the value of the bet variable // if the current bet is larger than the current maximum bet if (bet > MAXbet) { MAXbet = parseInt(bet,10); // set the maximum bet variable to the value of the bet variable document.getElementById('maxbet').value = MAXbet; // set the value of the maximum bet display to the value of the maximum bet variable } } document.getElementById('stake').value = stake; // set the stake display to the current value of the stake variable // if the current stake is less than the minimum stake if (stake < MINstake) { MINstake = parseInt(stake,10); // set the value of the minimum stake variable to the value of the current stake document.getElementById('minstake').value = MINstake; // set the minimum stake display to the value of the minimum stake variable } numRolls = 0; // reset the number of rolls to 0 } // else if the sum of the dice is equal to the point else if (sum == point) { document.getElementById('winstatus').value = 'You win!'; // set the win/loss status display to the win message stake = parseInt(stake,10) + parseInt(bet,10); // add the bet to the current stake document.getElementById('stake').value = stake; // set the stake display to the value of the current stake // if the double on loss option has been enabled if (ifdouble == true) { // if it is not the first loss in the loss sequence if (firstOriginalBet == false) { bet = parseInt(originalBet,10); // set the bet to the value of the original bet document.getElementById('bet').value = bet; // set the bet display to the value of the current bet (original bet) firstOriginalBet = true; // set the first original bet flag to true } } // if the current stake is larger than the current maximum stake if (stake > MAXstake) { MAXstake = parseInt(stake,10); // set the value of the maximum stake variable to the value of the current stake variable document.getElementById('maxstake').value = MAXstake; // set the value of the maximum stake display to the value of the maximum stake variable } numRolls = 0; // reset the number of rolls to 0 } // else the sum of the dice is not equal to 7 or the point else { document.getElementById('winstatus').value = 'Roll again to make ' + point; // set the win/loss status display to roll again to make the point numRolls++; // increment the number of rolls } } } // function to toggle the double on loss flag. when button is clicked, it reverses the flag function doubleLosingBet() { // if the option was enabled when the button was clicked if (ifdouble == true) { ifdouble = false; // then we now toggle (disable) the option document.getElementById('double').value = ''; // the option is disabled, so clear the double on loss display } // else the option was disabled when the button was clicked else { ifdouble = true; // the we now toggle (enable) the option document.getElementById('double').value = 'Double on loss'; // the option is enabled, so set the double on loss display to "Double on loss" } } // function to simulate X number of games, where X represents a number of games that were either won or lost. This automates the game. function runSimulation() { var numGames = prompt('How many games?',1000); // prompt the user for the number of games to be run in the simulation // do not accept a number of games larger than 2000. if the user supplies one larger than 2000, reject it and ask again until 2000 or less is attained while (numGames > 2000) { var numGames = prompt('You entered a number too large.\nHow many games?',1000); // prompt again for number of games } // note the default for the above is 1000 var i; // variable i for the iterating up to numGames // for loop from 0 to numGames, there are [numGames] iterations for (i = 0; i < numGames;) { rollDice(); // call the rollDice function var place = document.getElementById('winstatus').value; // get the win/loss status of the game // if the user won if (place == 'You win!') { i++; // increment the number of games played } // else if the user lost else if (place == 'You lose.') { i++; // increment the number of games played } } }