2014-04-11 66 views
0

因为我现在有游戏,所以会循环多次用户选择,我需要把玩的数量和胜利数量分开以获得胜率。我不知道如何去解决这个问题,所以任何帮助表示赞赏。提前致谢。我需要从我的游戏中返回一个变量

function rockPaperScissors(){ 
      function game(wantToPlay){ 
       if (wantToPlay=="Yes"||wantToPlay=="yes"){ 
        var userChoice = prompt("Do you choose rock, paper or scissors?"); 
        //gets a random number 
        var computerChoice = Math.random(); 

        //assigns that random number to be either rock, paper or scissors based on the number 
        if (computerChoice < .34) { 
         computerChoice = "rock";} 
        else if(computerChoice <= .66) { 
         computerChoice = "paper";} 
        else { 
         computerChoice = "scissors"; 
        } 
        console.log("computerChoice: " + computerChoice); 
        console.log("userChoice: " + userChoice); 

        //compares the computer choice to the user choice and then prints out the outcome on the page. 
        function compare(choice1,choice2){ 
         console.log(choice1 + "<-----User"); 
         console.log(choice2 + "<-----Computer"); 
         if (choice1==choice2){ 
          alert("It was a tie!"); 
          game("yes"); 
         } 
         if (choice1=="rock"){ 
          if (choice2=="scissors"){ 
           document.getElementById("messages").innerHTML=""; 
           document.getElementById("win").innerHTML="You win. Rock crushes scissors."; 
           document.getElementById("loss").innerHTML=""; 
          } 
          if (choice2 =="paper"){ 
           document.getElementById("messages").innerHTML=""; 
           document.getElementById("win").innerHTML=""; 
           document.getElementById("loss").innerHTML="You lose. Paper smothers rock."; 

          } 
          /**else{ 
           document.getElementById("messages").innerHTML=""; 
           document.getElementById("loss").innerHTML="You lose. Rock crushes scissors"; 
           document.getElementById("win").innerHTML=""; 
          }**/ 
         } 
         else if (choice1=="paper"){ 
          if (choice2=="rock"){ 
           document.getElementById("messages").innerHTML=""; 
           document.getElementById("win").innerHTML="You win. Paper smothers rock."; 
           document.getElementById("loss").innerHTML=""; 
          } 
          if (choice2 =="scissors"){ 
           document.getElementById("messages").innerHTML=""; 
           document.getElementById("win").innerHTML=""; 
           document.getElementById("loss").innerHTML="You lose. Scissors cut paper."; 

          } 
          /** else{ 
           document.getElementById("messages").innerHTML=""; 
           document.getElementById("loss").innerHTML="You lose. Paper smothers rock."; 
           document.getElementById("win").innerHTML=""; 
          }**/ 
         } 
         else if (choice1=="scissors"){ 
          if (choice2=="paper"){ 
           document.getElementById("messages").innerHTML=""; 
           document.getElementById("win").innerHTML="You win. Scissors cut paper."; 
           document.getElementById("loss").innerHTML=""; 
          } 
          if (choice2 =="rock"){ 
           document.getElementById("messages").innerHTML=""; 
           document.getElementById("win").innerHTML=""; 
           document.getElementById("loss").innerHTML="You lose. Rock crushes scissors."; 
          } 
          /** else{ 
           document.getElementById("messages").innerHTML=""; 
           document.getElementById("loss").innerHTML="You lose. Scissors cut paper."; 
           document.getElementById("win").innerHTML=""; 
          }**/ 
         } 
         else{ 
          alert("Very funny. Read the help menu and do it right."); 
          game("yes"); 
         } 

        }; 
        compare(userChoice,computerChoice); 
       } 

       //this only runs if the user does not type yes 
       else{ 
        document.getElementById("messages").innerHTML="Well alrighty then."; 
        document.getElementById("loss").innerHTML=""; 
        document.getElementById("win").innerHTML=""; 
       } 
      } 
      //promts the start of the game and loops x amount of times based on user input. 
      var start = prompt ("Do you want to play?","Yes"); 
      var i = prompt("How many times do you want to play?", 5); 
      for (n = 0; n < i; n ++){ 
      game(start); 
     }} 
+4

Protip:将您的元素存储为变量,并停止滥用'innerHTML' D: –

+1

您必须*计*胜利和损失。看起来好像你可以很容易地用你在if语句中增加的几个变量来做到这一点,然后显示除以获得百分比的游戏数量...... –

+0

cale_b在if语句内工作,游戏是所以每次游戏结束时,赢利和损失变量都不会重新设置为指定值? – user3524591

回答

0

声明变量,像这样(在​​和创造功能initVars()

var wins=0; 
var plays=0; 

当玩家赢了,你做wins++plays++,当损失 - 仅plays++
游戏结束时你做wins/plays

相关问题