Advertisement
daniel_c05

Dice Game

Feb 3rd, 2012
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var diceThrow = function () {
  2.     dice1 = Math.floor((Math.random()*6)+1);
  3.     dice2 = Math.floor((Math.random()*6)+1);
  4.     sumOfDices = dice1 + dice2;
  5.     console.log("You threw a " + dice1 +
  6.     " and a " + dice2 + " for a combined score of " + sumOfDices);
  7.     return sumOfDices;
  8. };
  9.  
  10. var status;
  11. var score;
  12. var myPoint;
  13.  
  14. score = diceThrow();
  15.  
  16. switch (score) {
  17.     case 7:
  18.     case 11:
  19.         status = "Wins";
  20.         break;
  21.     case 2:
  22.     case 3:
  23.     case 12:
  24.         status = "Loses";
  25.         break;
  26.     default:
  27.         status = "Continue";
  28.         myPoint = score;
  29.         console.log("Your score is " + score);
  30. }
  31.  
  32. while (status === "Continue") {
  33.     score = diceThrow();
  34.     if (score === myPoint) {
  35.         status = "Wins";
  36.     }
  37.     else if (score === 7) {
  38.         status = "Loses";
  39.     }
  40. }
  41.  
  42. if (status === "Wins" ) {
  43.     console.log("Player won this round!");
  44. }
  45. else {
  46.     console.log("Player lost this round");
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement