2016-11-28 113 views
0

我構建了一個名爲roll()的函數來處理擲骰子。我試圖讀取卷的價值,並檢查是否連續兩次擊中連續兩次,以便我可以打斷玩家輪流並將其轉向下一個玩家。檢查JavaScript中變量值的兩個連續值匹配

我可以讀取骰子的值好吧,我無法弄清楚如何檢查連續兩次6。

這是我的函數:

roll = function(){ 
    if (gamePlaying){ 
     // 1. Get a random number 
     //var dice = Math.floor(Math.random() * 6) + 1; //1 to 6 randomly 
     var dice = 6; 

     //2. Display the result 
     var diceDOM = document.querySelector('.dice'); 
     diceDOM.style.display = 'block'; 
     diceDOM.src = 'images/' + 'dice-' + dice + '.png'; 

     //3. Update the roundScore IF the rolled number is not 1 

     // for type coersion, we need to use !== and not != 
     if(dice !== 1) { 
      //add score 
      roundScore += dice; // same as roundScore = roundScore + dice 
      //it outputs to a div of ID = #myId 
      document.querySelector('#current-' + activePlayer).textContent = roundScore; 
     } else { 
      alert('Next Player') 
      nextPlayer(); 
     } 

     // Is this right? 
     for(var i = 1; i >= 2; i++){ 
      if (dice == 6){ 
       console.log('sixes'); 
      } 
     } 

    } 
} 

正在通過一個按鈕,這樣的觸發:

document.querySelector('.btn-roll').addEventListener('click', function(){ 
    roll(); 

}); 

我裝遊戲這CODEPEN

附:我在隨機函數下放了一個dice = 6;,所以你不必玩遊戲直到你得到兩個六分。只需取消它的註釋並註釋掉骰子=數學函數,你將只獲得六個。

我還把「這是對的嗎?」評論一個for循環的頂部。我的意思是,「這是正確的做法嗎?」我應該繼續嘗試一個循環還是我已經離開了?

順便說一句,如果2個亂七八糟做上來,整個成績被註銷的時候被傳遞到score[]但我可以做到這一點。我想笑

非常感謝。

+1

任何需要比單次滾動更長的值(得分和計數器上有多少個六進制數)需要存儲在範圍比函數更高的變量中。這樣,您的功能可以檢查這些值,當一個新的捲髮生並相應採取行動。 –

+0

'for(var i = 1; i> = 2; i ++){'=>你想要做什麼?這是從來沒有 – Fefux

+0

@Fefux我試圖。 – LOTUSMS

回答

1

你可以嘗試類似這樣的方法,在這裏你可以使roll()成爲一個自我調用函數。這樣,你可以存儲他們已經擲出六次的次數。

roll = (function(){ 

    var count = 0; 
    var lastRoll = 0; 

    return function() { 

    if (gamePlaying){ 

      // 1. Get a random number 
      var dice  = Math.floor(Math.random() * 6) + 1; //1 to 6 randomly 
      var thisRoll = dice; 

      if(dice === 6) { 
       lastRoll = 6; 
       count += 1; 
      } else { 
       lastRoll = 0; 
       count = 0; 
      } 

      if(thisRoll === 6 && lastRoll === 6 && count === 2) { 
      alert('You rolled a six twice!'); 
      lastRoll = 0; 
      count = 0; 
      // do your stuff for 2 sixes in a row here! 
      return; 
      } 

     //2. Display the result 
     var diceDOM = document.querySelector('.dice'); 
     diceDOM.style.display = 'block'; 
     diceDOM.src = 'http://sitedev.online/repo/' + 'dice-' + dice + '.png'; 

     //3. Update the roundScore IF the rolled number is not 1 

     // for type coersion, we need to use !== and not != 
     if(dice !== 1) { 
      //add score 
      roundScore += dice; // same as roundScore = roundScore + dice 
      //it outputs to a div of ID = #myId 
      document.querySelector('#current-' + activePlayer).textContent = roundScore; 
      console.log(dice); 
     } else { 
      alert('Next Player') 
      nextPlayer(); 
     } 
    } 

    } 

})(); 
+0

這是非常好的,但我認爲有什麼不對。當我用傳入骰子變量的隨機函數嘗試它時,似乎我偶爾會得到2個六位的警報。有時只需點擊一下。難道它會挽救前六個,等待下一個,然後提醒我2個六個?警報只有在六個連續滾動時纔會發生。 – LOTUSMS

+0

我會將您的代碼添加到codepen – LOTUSMS

+0

我知道問題是什麼。它檢查一個六是否滾動兩次,但它也檢查它是否不連續! – user2085143

1

只是爲了它,你不需要任何全球或外部增值稅這樣做。訣竅是,請記住,函數是對象。您可以讀取和寫入屬性;你甚至可以完全改變函數內的函數(這是一個老JS單例模式的技巧)。

下面是一個例子。如果你說喂「真」,它會更新「最後」參考值並返回兩個隨機骰子。如果您輸入「false」,它將不會更新之前的參考值,直到您再次輸入true(但它仍會返回新卷)。這樣,您可以繼續滾動,保持初始值,並將其與所有您想要的新的第二個值進行比較。

<html> 
<head> 
</head> 
<body> 

<script> 
    var rollfunc = function (updateLast) { 
     var d1 = Math.floor((Math.random() * 6) + 1); 
     var d2 = Math.floor((Math.random() * 6) + 1); 
     if (updateLast) { 
      rollfunc.d1 = d1; 
      rollfunc.d2 = d2; 
     } 

     return { 
      dice1 : d1, 
      dice2 : d2, 
      bothsixes : ((d1 + d2 === 6) && (rollfunc.d1 + rollfunc.d2 === 6)) 
     }; 
    } 

    var result = rollfunc (true); 
    // If you pass in true then d1, d2, and rollfunc.d1, rollfunc.d2 will always be the same 
    console.log ("Reference updated: ", result, "d1 = " + rollfunc.d1, ", d2 = " + rollfunc.d2); 
    var result = rollfunc (false); 
    // If you pass in false, the reference won't change, but the new roll will, you can compare the two 
    console.log ("Reference left alone: ", result, "d1 = " + rollfunc.d1, ", d2 = " + rollfunc.d2); 
</script> 
</body> 
</html> 

我得到這可能是過於學術,但它是有用的知道JS可以做到這一點。

+0

蒂姆,謝謝你。不要擔心它過於學術。這不是「大學」的事情。這是來自udemy.com的在線教程。我沒有測試過,看它是否有效,但我會通過它。加一個好友 – LOTUSMS