2016-11-28 60 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