Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. function ticTacToe(){
  2. function game(wantToPlay){
  3. if (wantToPlay=="Yes"||wantToPlay=="yes"){
  4. var userChoice = prompt("Do you choose rock, paper or scissors?");
  5. //gets a random number
  6. var computerChoice = Math.random();
  7.  
  8. //assigns that random number to be either rock, paper or scissors based on the number
  9. if (computerChoice < .34) {
  10. computerChoice = "rock";}
  11. else if(computerChoice <= .66) {
  12. computerChoice = "paper";}
  13. else {
  14. computerChoice = "scissors";
  15. }
  16. console.log("computerChoice: " + computerChoice);
  17. console.log("userChoice: " + userChoice);
  18.  
  19. //compares the computer choice to the user choice and then prints out the outcome on the page.
  20. function compare(choice1,choice2){
  21. console.log(choice1 + "<-----User");
  22. console.log(choice2 + "<-----Computer");
  23. if (choice1==choice2){
  24. alert("It was a tie!");
  25. game("yes");
  26. }
  27. if (choice1=="rock"){
  28. if (choice2=="scissors"){
  29. document.getElementById("messages").innerHTML="";
  30. document.getElementById("win").innerHTML="You win. Rock crushes scissors.";
  31. document.getElementById("loss").innerHTML="";
  32. console.log("wins " + wins);
  33. }
  34. if (choice2 =="paper"){
  35. document.getElementById("messages").innerHTML="";
  36. document.getElementById("win").innerHTML="";
  37. document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";
  38. console.log("losses " + loss);
  39.  
  40. }
  41. /**else{
  42. document.getElementById("messages").innerHTML="";
  43. document.getElementById("loss").innerHTML="You lose. Rock crushes scissors";
  44. document.getElementById("win").innerHTML="";
  45. }**/
  46. }
  47. else if (choice1=="paper"){
  48. if (choice2=="rock"){
  49. document.getElementById("messages").innerHTML="";
  50. document.getElementById("win").innerHTML="You win. Paper smothers rock.";
  51. document.getElementById("loss").innerHTML="";
  52. }
  53. if (choice2 =="scissors"){
  54. document.getElementById("messages").innerHTML="";
  55. document.getElementById("win").innerHTML="";
  56. document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";
  57.  
  58. }
  59. /** else{
  60. document.getElementById("messages").innerHTML="";
  61. document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";
  62. document.getElementById("win").innerHTML="";
  63. }**/
  64. }
  65. else if (choice1=="scissors"){
  66. if (choice2=="paper"){
  67. document.getElementById("messages").innerHTML="";
  68. document.getElementById("win").innerHTML="You win. Scissors cut paper.";
  69. document.getElementById("loss").innerHTML="";
  70. }
  71. if (choice2 =="rock"){
  72. document.getElementById("messages").innerHTML="";
  73. document.getElementById("win").innerHTML="";
  74. document.getElementById("loss").innerHTML="You lose. Rock crushes scissors.";
  75. }
  76. /** else{
  77. document.getElementById("messages").innerHTML="";
  78. document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";
  79. document.getElementById("win").innerHTML="";
  80. }**/
  81. }
  82. else{
  83. alert("Very funny. Read the help menu and do it right.");
  84. game("yes");
  85. }
  86.  
  87. };
  88. compare(userChoice,computerChoice);
  89. }
  90.  
  91. //this only runs if the user does not type yes
  92. else{
  93. document.getElementById("messages").innerHTML="Well alrighty then.";
  94. document.getElementById("loss").innerHTML="";
  95. document.getElementById("win").innerHTML="";
  96. }
  97. }
  98. //promts the start of the game and loops x amount of times based on user input.
  99. var start = prompt ("Do you want to play?","Yes");
  100. var i = prompt("How many times do you want to play?", 5);
  101. for (n = 0; n < i; n ++){
  102. game(start);
  103. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement