Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <meta http-equiv="Content-Type" content="text/html;charset=UTF8">
  4. <body>
  5.  
  6. <script>
  7.  
  8. ///////////////////////////////////////////////////////////////////////////////
  9. // Begin Framework
  10.  
  11. function printMessage(message) {
  12. document.write(message);
  13.  
  14. document.write("<br/>");
  15. }
  16.  
  17. function rollDice(numberOfDice) {
  18. // Setup our array for storing all of the dice rolls
  19. this.diceRolls = [];
  20.  
  21. // Setup our empty display message for all of the rolls
  22. var diceRollsMessage = "";
  23.  
  24. // Roll each of the die in turn
  25. for (var diceRoll = 0; diceRoll < numberOfDice; ++diceRoll) {
  26. // If this is not our first roll then add in a comma to the output string
  27. if (diceRoll > 0) {
  28. diceRollsMessage += ", ";
  29. }
  30.  
  31. // Roll the current die
  32. currentRoll = rollDie(6);
  33.  
  34. // Add the current die roll to the message
  35. diceRollsMessage += currentRoll;
  36.  
  37. // Store the current die value
  38. this.diceRolls[this.diceRolls.length] = currentRoll;
  39. }
  40.  
  41. // Display all of the dice rolls
  42. printMessage("Dice Rolls: " + diceRollsMessage);
  43. }
  44.  
  45. rollDice.prototype.numDie = function(dieValue) {
  46. var matchingDieCount = 0;
  47.  
  48. // Iterate over all of the stored dice rolls
  49. for (var roll = 0; roll < this.diceRolls.length; ++roll) {
  50. // If the roll matches our search value then increment the count of matching die
  51. if (this.diceRolls[roll] == dieValue) {
  52. ++matchingDieCount;
  53. }
  54. }
  55.  
  56. return matchingDieCount;
  57. }
  58.  
  59. // Run the game
  60. game();
  61.  
  62. // End Framework
  63. ///////////////////////////////////////////////////////////////////////////////
  64.  
  65. function rollDie(numberOfSides) {
  66. ///////////////////////////////////////////////////////////////////////////////
  67. // Begin Block 1 to Complete
  68. Math.floor((Math.random() * 6) + 1); //produce a random number between 1 and 6 inclusive
  69. // End Block 1 to Complete
  70. ///////////////////////////////////////////////////////////////////////////////
  71. }
  72.  
  73. function turn() {
  74. // Roll all 6 dice
  75. var diceRolls = new rollDice(6);
  76.  
  77. // Start the score for this turn at 0
  78. var score = 0;
  79.  
  80. ///////////////////////////////////////////////////////////////////////////////
  81. // Begin Block 2 to Complete
  82. var num1 = diceRolls.numDie(1);
  83. var num2 = diceRolls.numDie(2);
  84. var num3 = diceRolls.numDie(3);
  85. var num4 = diceRolls.numDie(4);
  86. var num5 = diceRolls.numDie(5);
  87. var num6 = diceRolls.numDie(6);
  88.  
  89. if(num1 == 1) //if one 1 is rolled score +100
  90. {
  91. score += 100 // add 100 to score
  92. }
  93. if(num1 == 2) //if two 1's are rolled
  94. {
  95. score += 200 //add 200 to score
  96. }
  97. if(num1 == 3) //if 3 1's are rolled
  98. {
  99. score += 1000 //add 1000 to score
  100. }
  101. if(num5 == 1) //if one 5 is rolled
  102. {
  103. score += 50 //add 50 to score
  104. }
  105. if(num5 == 2) //if two 5's are rolled
  106. {
  107. score += 10 //add 10 to score
  108. }
  109. if(num2 == 3) //if three 2's are rolled
  110. {
  111. score += (100*2) //add 100*2
  112. }
  113. if(num3 == 3) //if three 3's are rolled
  114. {
  115. score += (100*3) //add 100*3
  116. }
  117. if(num4 == 3) //if three 4's are rolled
  118. {
  119. score += (100*4) //add 100*4
  120. }
  121. if(num5 == 3) //if three 5's are rolled
  122. {
  123. score += (100*5) //add 100*5
  124. } //add 100*5
  125. if(num6 == 3) //if three 6's are rolled
  126. {
  127. score += (100*6) //add 100*6
  128. }
  129.  
  130. var randomMultiplier = Math.floor((Math.random() * 6) + 1)
  131.  
  132. if(num1 !=1 && num1 !=2 && num1 !=3)
  133. {
  134. score += ((2*3000) + (3000 * randomMultiplier))
  135. }
  136. if(num2 !=1 && num2 !=2 && num2 !=3)
  137. {
  138. score += ((2*200) + (200 * randomMultiplier))
  139. }
  140. if(num3 !=1 && num2 !=2 && num2 !=3)
  141. {
  142. score += ((2*300) + (300 * randomMultiplier))
  143. }
  144. if(num4 !=1 && num2 !=2 && num2 !=3)
  145. {
  146. score += ((2*400) + (400 * randomMultiplier))
  147. }
  148. if(num5 !=1 && num2 !=2 && num2 !=3)
  149. {
  150. score += ((2*500) + (500 * randomMultiplier))
  151. }
  152. if(num6 !=1 && num2 !=2 && num2 !=3)
  153. {
  154. score += ((2*600) + (600 * randomMultiplier))
  155. }
  156. // End Block 2 to Complete
  157. ///////////////////////////////////////////////////////////////////////////////
  158.  
  159. printMessage("This turn was worth " + score + " points");
  160.  
  161. return score;
  162. }
  163.  
  164. function didWin(score) {
  165. ///////////////////////////////////////////////////////////////////////////////
  166. // Begin Block 3 to Complete
  167.  
  168. var win = 1
  169.  
  170. if(score >= 30000)
  171. {
  172. var win = 1
  173. }
  174. else
  175. {
  176. var win = 0
  177. }
  178. document.write(win)
  179. // End Block 3 to Complete
  180. ///////////////////////////////////////////////////////////////////////////////
  181. }
  182.  
  183. function game() {
  184. // Start the score for the game at 0
  185. var score = 0;
  186.  
  187. ///////////////////////////////////////////////////////////////////////////////
  188. // Begin Block 4 to Complete
  189. var score = turn();
  190. // End Block 4 to Complete
  191. ///////////////////////////////////////////////////////////////////////////////
  192.  
  193. // Determine if we won and display a suitable message
  194. if (didWin(score)) {
  195. ///////////////////////////////////////////////////////////////////////////////
  196. // Begin Block 5 to Complete
  197. if(win == 1)
  198. {
  199. printMessage("You win!")
  200. }
  201. // End Block 5 to Complete
  202. ///////////////////////////////////////////////////////////////////////////////
  203.  
  204. else {
  205. ///////////////////////////////////////////////////////////////////////////////
  206. // Begin Block 6 to Complete
  207.  
  208. printMessage("You lose")
  209.  
  210. // End Block 6 to Complete
  211. ///////////////////////////////////////////////////////////////////////////////
  212. }
  213. }
  214. }
  215.  
  216. </script>
  217.  
  218. </body>
  219. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement