2014-12-07 174 views
0

我似乎無法弄清楚爲什麼我不能計算積分,並保持它們爲每個用戶總數..任何建議?請幫忙。我能夠使用提示等。但是,試圖將其帶到生活在瀏覽器一直是個挑戰,以建立這個遊戲..難以計算總數Javascript

下面是代碼:

function getName() { 
    var name = document.getElementById('fName').value; 
    // console.log(name); 
    $(".greetingPlayer").append("<h3 class='greeting'>Greetings " + name + "!</h3><br><h4>Please choose from the following Weapons...</h4>"); 
    $(".statsA").css("display", "none"); 
} 

var userChoice; 

function choices(weapon) { 
    userChoice = weapon; 
    // console.log(userChoice); 
    $(".greetingPlayer").append("<h4 class='userChoice'>Users Choice : " + userChoice + "!</h4>"); 
    var computerChoice = Math.random(); 
    if (computerChoice <= 0.34) { 
    computerChoice = "rock"; 
    } else if (computerChoice <= 0.67) { 
    computerChoice = "paper"; 
    } else { 
    computerChoice = "scissors"; 
    } 
    // console.log(computerChoice); 
    $(".greetingPlayer").append("<h4 class='computerChoice'>Computers Choice : " + computerChoice + "!</h4>"); 

    function compare(choice1, choice2) { 
    var playing = true; 
    var human = 0; 
    var comp = 0; 
    while (playing) { 
     if (choice1 === choice2) { 
     console.log("The result is a tie!"); 
     human = score; 
     comp = score; 
     } else if (choice1 === "rock") { 
     if (choice2 === "scissors") { 
      console.log("rock wins!"); 
     } else { 
      console.log("paper wins!"); 
     } 
     } else if (choice1 === "paper") { 
     if (choice2 === "rock") { 
      console.log("paper wins!"); 
     } else { 
      console.log("scissors wins!"); 
     } 
     } else if (choice1 === "scissors") { 
     if (choice2 === "paper") { 
      console.log("scissors wins!"); 
     } else { 
      console.log("rock wins!"); 
     } 
     } else { 
     console.log("That's not an option! Do it over " + name + " and try again!"); 
     } 
     playing = false; 
    } 
    console.log("human : " + human); 
    console.log("comp : " + comp); 
    var numpoints = 0; 
    function points() { 
     if (++score >= str.length) { 
     numpoints++; 
     document.getElementByClassName("points").textContent = "Score: " + numpoints; 
     } 
    } 
    } 
    compare(userChoice, computerChoice); 
} 
// $(".greetingPlayer").append("<h4 class='userWeapon'> " + name + "! You win!</h4>"); 
+0

您的代碼沒有意義。爲什麼你將得分與一個甚至沒有初始化的str變量進行比較?另外,你永遠不會打電話給積分功能,所以我不知道你的分數應該如何更新。 – QuantumLicht 2014-12-07 23:11:50

回答

0

我想通了,我範圍都是錯誤的,我不知道爲什麼我試圖在底部添加一個循環。然而,這似乎解決了...

   function getName(){ 
       var name = document.getElementById('fName').value; 
       // console.log(name); 
       $(".greetingPlayer").append("<h3 class='greeting'>Greetings " + name + "!</h3><br><h4>Please choose from the following Weapons...</h4>"); 
       $(".statsA").css("display", "none"); 
      } 

        // Here we declare a variable to represent a User's Choice outside of the function scope 
        var userChoice; 
        var computerChoice; 

        //These variables are created to keep score and are declared w/ zero for addition. 
        var compScore = 0; 
        var userScore = 0; 

      // Here we declare a function to compare a userChoice vs compChoice 
      var choices = function(weapon){ 
       userChoice = weapon; 
        // console.log(userChoice); 

       computerChoice = Math.random(); 
       // console.log(computerChoice); 

       if (computerChoice <= 0.34) { 
       computerChoice = "rock"; 
       } else if(computerChoice <= 0.67) { 
       computerChoice = "paper"; 
       } else { 
       computerChoice = "scissors"; 
       } 

        $(".toolChoice").html(" " + userChoice + "!"); 
       // console.log(computerChoice); 
       $(".toolChoiceB").html(" " + computerChoice + "!"); 

       function compare(choice1, choice2){ 

        if(choice1 === choice2){ 
         console.log("The result is a tie!"); 
        }else if(choice1 === "rock"){ 
         if(choice2 === "scissors"){ 
          console.log("rock wins!"); 
          userScore++; 
         }else{ 
          console.log("paper wins!"); 
          compScore++; 
         } 
        }else if(choice1 === "paper"){ 
         if(choice2 === "rock"){ 
          console.log("paper wins!"); 
          userScore++; 
         }else{ 
          console.log("scissors wins!"); 
          compScore++; 
         } 
        }else if(choice1 === "scissors"){ 
         if(choice2 === "paper"){ 
          console.log("scissors wins!"); 
          userScore++; 
         }else{ 
          console.log("rock wins!"); 
          compScore++; 
         } 
        }else{ 
         console.log("That's not an option! Do it over " 
          + name + " and try again!"); 
        } 
       } 
      // end of compare function 

      // compare function called 
      compare(userChoice, computerChoice); 

       // This consoles the score, use this as a start point for displaying the score. 
       // console.log("human : " + userScore); 
       // console.log("comp : " + compScore); 

       // This jQuery displays the score for each party 

       // This is the score for Humans 
       $('.scoreA').html(" " + userScore); 
      // This is the score for Computers 
       $('.scoreB').html(" " + compScore); 
      }