我需要将一个变量的先前值存储在另一个变量中

时间:2018-07-19 22:31:06

标签: javascript

我正在做一个掷骰子游戏,我需要确保如果您连续两次掷骰子两次,那么您将失去所有分。因此,我认为您需要创建一个存储该变量的先前值的变量,但我不知道该怎么做。这是函数,我需要将变量“ dice”的先前值一直存储在函数顶部:

document.querySelector('.btn-roll').addEventListener('click', function() {
    if(gamePlaying) {
        //1.Random number
        var dice = Math.floor(Math.random() * 6) + 1;
        //2. Display the result
        var diceDOM = document.querySelector('.dice');
        diceDOM.style.display = 'block';
        diceDOM.src = 'dice-' + dice + '.png';
    
        //3 Update the round score IF the rolled number was NOT a 1
        if (dice !== 1){
            roundScore += dice;
            document.querySelector('#current-' + activePlayer).textContent = roundScore;
        } else {
            togglePlayer();
        } 
    } else {
        alert("Start a new game!!!");
    }
    
   
});

4 个答案:

答案 0 :(得分:0)

希望以下内容会有所帮助:

var previousDice = 0;
document.querySelector('.btn-roll').addEventListener('click', function() {
	if(gamePlaying) {
		//1.Random number
		var dice = Math.floor(Math.random() * 6) + 1;
		
		if (dice === previousDice === 6) {
			// all your points are gone
		}
		//2. Display the result
		var diceDOM = document.querySelector('.dice');
		diceDOM.style.display = 'block';
		diceDOM.src = 'dice-' + dice + '.png';
    
		//3 Update the round score IF the rolled number was NOT a 1
		if (dice !== 1){
			roundScore += dice;
			document.querySelector('#current-' + activePlayer).textContent = roundScore;
		} else {
			togglePlayer();
		}
		previousDice = dice;
	} else {
		previousDice = 0;
		alert("Start a new game!!!");
	}
});

答案 1 :(得分:0)

您可以创建一个时间变量(previousRoll),如果previouosRoll === 6 && dice === 6(上一卷和当前一卷等于6)在每个卷中进行检查。如果是这样,请遵循逻辑,否则

// mock
var gamePlaying = true;
var roundScore = 0;
var activePlayer = 1;

function togglePlayer() {}
// end mock

var previousRoll;

document.querySelector('.btn-roll').addEventListener('click', function() {
  if (gamePlaying) {
    //1.Random number
    var dice = Math.floor(Math.random() * 6) + 1;
    console.log(dice);
    if (previousRoll === 6 && dice === 6) {
      //if you roll twice 6 in a row you lose all your points
      dice = 0;
      console.log('lost your points')
    }
    previousRoll = dice; // this will reset previousRoll too in case it was a double-six

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

    //3 Update the round score IF the rolled number was NOT a 1
    if (dice === 0) {
      console.log('your logic when points should be reset')
    } else if (dice !== 1) {
      roundScore += dice;
      document.querySelector('#current-' + activePlayer).textContent = roundScore;
    } else {
      togglePlayer();
    }
  } else {
    alert("Start a new game!!!");
  }
});
<button class="btn-roll">Roll</button> <-- click until you get two 6 in a row :) and see the console

<div class="dice"></div>

<div id="current-1"></div>

答案 2 :(得分:0)

这是您可以通过简单方式完成的方法

var role_count = 0
if (dice == 6 && role_count < 3) {
    role_count++;
  console.log("role count: "+role_count);
  if (role_count == 3){
    role_count = 0;
    dice = 0;
    console.log('reset');
    // change player turn
  }
}
else {
    role_count = 0;
}

已更正:JsFiddle

答案 3 :(得分:0)

我找到了!你们真的帮助了我,原来我必须在函数的末尾声明该变量,这样在声明新骰子时就不会刷新!十分感谢大家!这是代码:

var lastDice;

document.querySelector('.btn-roll').addEventListener('click', function() {
    if(gamePlaying) {
        //1.Random number
        var dice = Math.floor(Math.random() * 6) + 1;
        
        if(dice === 6 && lastDice === 6) {
             //Score is lost 
        };
        //2. Display the result
        var diceDOM = document.querySelector('.dice');
        diceDOM.style.display = 'block';
        diceDOM.src = 'dice-' + dice + '.png';
    
        //3 Update the round score IF the rolled number was NOT a 1
        if (dice !== 1){
            roundScore += dice;
            document.querySelector('#current-' + activePlayer).textContent = roundScore;
        } else {
            togglePlayer();
        } 
        lastDice = dice;
        
    } else {
        alert("Start a new game!!!");
    }
    
   
});