Yehosua_Selah

Rock, Paper, scissors, spock or lizards by schamanu

Aug 5th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1.  
  2. alert("Welcome to Rock, Paper, Scissors, Lizard, Spock\n\t\t*Programmed by Schamanu*");
  3.  
  4. /*Variables and game setup*/
  5. var played = 0;
  6. var score1 = 0;
  7. var score2 = 0;
  8. var times = Number(prompt("How often do you want to play?"));
  9. if (isNaN(times)) {
  10. times = 1;
  11. alert("\nInvalid input!\nNumber of rounds set to 1!");
  12. }
  13. else
  14. {
  15. alert("\nYou chose to play "+ times +" times.");
  16.  
  17. /*FUNCTION COMPARING CHOICES*/
  18. var compare=function(choice1,choice2) {
  19. if (choice1===choice2) {
  20. alert("Its a tie. Choose again!");
  21. }
  22. else if (choice1==="rock") {
  23. if (choice2==="scissors") {
  24. alert("Rock crushes scissors.");
  25. alert("Player 1 wins this round.");
  26. score1+=1;
  27. }
  28. else if (choice2==="paper") {
  29. alert("Paper covers rock.");
  30. alert("The computer wins this round.");
  31. score2+=1;
  32. }
  33. else if (choice2==="spock") {
  34. alert("Spock vaporizes rock.");
  35. alert("The computer wins this round.");
  36. score2+=1;
  37. }
  38. else { // choice2=lizard
  39. alert("Rock crushes lizard.");
  40. alert("Player 1 wins this round.");
  41. score1+=1;
  42. }
  43. }
  44. else if (choice1==="paper") {
  45. if (choice2==="rock") {
  46. alert("Paper covers rock.");
  47. alert("Player 1 wins this round.");
  48. score1+=1;
  49. }
  50. else if (choice2==="scissors") {
  51. alert("Scissors cut paper.");
  52. alert("The computer wins this round.");
  53. score2+=1;
  54. }
  55. else if (choice2==="spock") {
  56. alert("Paper disproves Spock.");
  57. alert("Player 1 wins this round.");
  58. score1+=1;
  59. }
  60. else { // choice2=lizard
  61. alert("Lizard eats paper.");
  62. alert("The computer wins this round.");
  63. score2+=1;
  64. }
  65. }
  66. else if (choice1==="scissors") {
  67. if (choice2==="rock") {
  68. alert("Rock crushes scissors.");
  69. alert("The computer wins this round.");
  70. score2+=1;
  71. }
  72. else if (choice2==="paper") {
  73. alert("Scissors cut paper.");
  74. alert("Player 1 wins this round.");
  75. score1+=1;
  76. }
  77. else if (choice2==="spock") {
  78. alert("Spock smashes scissors.");
  79. alert("The computer wins this round.");
  80. score2+=1;
  81. }
  82. else { // choice2=lizard
  83. alert("Scissors decapitate lizard.");
  84. alert("Player 1 wins this round.");
  85. score1+=1;
  86. }
  87. }
  88. else if (choice1==="spock") {
  89. if (choice2==="rock") {
  90. alert("Spock vaporizes rock.");
  91. alert("Player 1 wins this round.");
  92. score1+=1;
  93. }
  94. else if (choice2==="paper") {
  95. alert("Paper disproves Spock.");
  96. alert("The computer wins this round.");
  97. score2+=1;
  98. }
  99. else if (choice2==="scissors") {
  100. alert("Spock smashes scissors.");
  101. alert("Player 1 wins this round.");
  102. score1+=1;
  103. }
  104. else { // choice2=lizard
  105. alert("Lizard poisons Spock.");
  106. alert("The computer wins this round.");
  107. score2+=1;
  108. }
  109. }
  110. else if (choice1==="lizard") {
  111. if (choice2==="rock") {
  112. alert("Rock crushes lizard.");
  113. alert("The computer wins this round.");
  114. score2+=1;
  115. }
  116. else if (choice2==="paper") {
  117. alert("Lizard eats paper.");
  118. alert("Player 1 wins this round.");
  119. score1+=1;
  120. }
  121. else if (choice2==="scissors") {
  122. alert("Scissors decapitate lizard.");
  123. alert("The computer wins this round.");
  124. score2+=1;
  125. }
  126. else { // choice2=spock
  127. alert("Lizard poisons Spock.");
  128. alert("Player 1 wins this round.");
  129. score1+=1;
  130. }
  131. }
  132. };
  133. /*END OF FUNCTION*/
  134.  
  135. do { // repeats game x times, as specified by player
  136. played += 1;
  137. do { // repeats code as long as game is tied
  138. do { // repeats code until player enters proper input
  139. if (userChoice==="rules") {
  140. alert("\nRules:\nScissors cuts paper. Paper covers rock.");
  141. alert("Rock crushes lizard. Lizard poisons Spock.\nSpock smashes scissors. Scissors decapitates lizard.");
  142. alert("Lizard eats paper. Paper disproves Spock.\nSpock vaporizes rock. Rock crushes scissors.");
  143. }
  144. var userChoice = prompt("Do you choose rock, paper, scissors, lizard or spock?","Type <rules> to see the rules.").toLowerCase();
  145. } while (userChoice !="rock" && userChoice !="paper" && userChoice !="scissors" && userChoice !="spock" && userChoice !="lizard");
  146. var computerChoice = Math.random();
  147. if (computerChoice < 0.2) {
  148. computerChoice = "rock";
  149. } else if (computerChoice <= 0.4) {
  150. computerChoice = "paper";
  151. } else if (computerChoice <=0.6) {
  152. computerChoice = "scissors";
  153. } else if (computerChoice <=0.8) {
  154. computerChoice = "spock";
  155. }
  156. else {
  157. computerChoice = "lizard";
  158. }
  159.  
  160. if (times >1) {
  161. alert("\nRound "+ played+"/"+times);
  162. }
  163. alert("\nYour choice: "+ userChoice);
  164. alert("Computer: " + computerChoice);
  165.  
  166. compare(userChoice,computerChoice);
  167. } while (userChoice === computerChoice);
  168. } while (played <= times-1); // times minus 1 because its a do-loop
  169.  
  170. if (times >1) {
  171. alert("\nFinal Score: "+ score1+":"+score2);
  172. if (score1>score2) {
  173. alert("Player 1 is the overall winner!");
  174. }
  175. else if (score2>score1) {
  176. alert("The computer is the overall winner!");
  177. }
  178. else {
  179. alert("It's a tie after all.");
  180. }
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment