JavaScript游戏未按预期运行

JavaScript游戏未按预期运行,javascript,jquery,recursion,Javascript,Jquery,Recursion,我正在建立一个像西蒙一样的记忆游戏。我的问题是,在第一次旋转后,它会将所有其他内容都视为不正确,即使在将其视为正确后也是如此。它还会在控制台中重复多次。我不明白为什么会这样。我相信问题出在球员转身和检查移动功能上 我的所有JavaScript代码如下所示: var sqrArray =['one', 'two', 'three', 'four']; var game = { count: 0, sequence: [], is_strict: false } //

我正在建立一个像西蒙一样的记忆游戏。我的问题是,在第一次旋转后,它会将所有其他内容都视为不正确,即使在将其视为正确后也是如此。它还会在控制台中重复多次。我不明白为什么会这样。我相信问题出在球员转身和检查移动功能上

我的所有JavaScript代码如下所示:

var sqrArray =['one', 'two', 'three', 'four']; 

var game = {
    count: 0,
    sequence: [],
    is_strict: false
}

// get random array element
function random_sqr() {
  return sqrArray[Math.floor(Math.random() * 4)];
} 

// adds and remove green background
function makelight(n) {
  $('#' + n).addClass('light');
  setTimeout(function() {
  $('#' + n).removeClass('light');
  }, 300);
}

// assignes makelight() in order of array elements
function play() {
var i = 0;
var sequenceInter = setInterval(function() {
    makelight(game.sequence[i]);
    i++;
    if(i>=game.sequence.length) {
        clearInterval(sequenceInter);
    }
   }, 600);
}

// adds random sqr to game
function addToSequence() {
  game.sequence.push(random_sqr());
}

function play_game() {
  game.count++
  game.sequence.push(random_sqr());
  play();
}
var checkCount = 0;

//player picks sqr
function player_pick() {
  play_game();
  player_turn();
}

function player_turn() {
  if(checkCount < game.sequence.length) {
    check_move();
  } else {
    checkCount = 0;
    player_pick();
    console.log('The end!');
    console.log('check: ' + checkCount);
  }
}

// Checks player choice
function check_move() {
  $('.sqr').click(function() {
    if(this.id === game.sequence[checkCount]) {
      console.log('Correct!');
      checkCount++;
      console.log('after increment ' + checkCount);
      player_turn();
    } else if(this.id !== game.sequence[checkCount]) {
      console.log('incorrect');
      console.log('Array ' + game.sequence[checkCount]);
    }
  })
}

player_pick();
var sqrArray=['1','2','3','4'];
var博弈={
计数:0,
顺序:[],
严格吗:错
}
//获取随机数组元素
函数random_sqr(){
返回sqrArray[Math.floor(Math.random()*4)];
} 
//添加和删除绿色背景
函数makelight(n){
$('#'+n).addClass('light');
setTimeout(函数(){
$('#'+n).removeClass('light');
}, 300);
}
//按数组元素的顺序分配makelight()
函数播放(){
var i=0;
var sequenceInter=setInterval(函数(){
makelight(游戏序列[i]);
i++;
如果(i>=game.sequence.length){
clearInterval(sequenceInter);
}
}, 600);
}
//将随机sqr添加到游戏中
函数addToSequence(){
game.sequence.push(random_sqr());
}
函数播放游戏(){
游戏。计数++
game.sequence.push(random_sqr());
play();
}
var检查计数=0;
//玩家选择sqr
函数播放器_pick(){
玩游戏;
球员转身();
}
函数播放器_turn(){
if(支票计数<游戏序列长度){
检查_move();
}否则{
支票计数=0;
player_pick();
console.log('end!');
console.log('check:'+checkCount);
}
}
//检查玩家选择
函数检查_move(){
$('.sqr')。单击(函数(){
if(this.id==game.sequence[checkCount]){
console.log('Correct!');
支票计数++;
console.log('增量后'+检查计数);
球员转身();
}else if(this.id!==game.sequence[checkCount]){
console.log(“不正确”);
console.log('Array'+game.sequence[checkCount]);
}
})
}
player_pick();
以下是HTML:

<div class="sqr" id="one" ></div>
<div class="sqr" id="two"></div>
<div class="sqr" id="three"></div>
<div class="sqr" id="four"></div>

您在代码中看到的问题是由多次注册单击处理程序的
check\u move
函数引起的:每次调用该函数时,都会注册一个额外的处理程序,并在单击发生时调用所有处理程序

要解决此问题,只需调用
$('.sqr')。单击一次