2014-04-11 142 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

相關問題