2016-06-21 133 views
-1

我正在继续我的项目,即在codepen中制作一个tic tac toe游戏。我遇到的问题是以下功能。 这是假设计算是否有人赢得了比赛,除了一个例外。现在ai只是把它的角色放在下一个可用的位置,如果我把我的角色放在一条直线上(如中间线),那么我应该赢得计算机会在下一回合中赢得胜利,问题就来了返回电脑赢了。无法退出循环es6

我知道这可能很难理解,因为我只是在游戏中采取我自己的方法。我已经制作了控制台日志,告诉我已经达到了获胜区块,但是我相信这个循环会继续运行。我不这样做,如果这与es6和块范围,但我不能得到正确的行动发生在这种情况下。我尝试过的大多数其他情况似乎都能正常工作。

这里是链接到codepen全方面:

CLICK ME.

编辑:我不小心从项目削减的功能和笔耕不辍,所以我固定的。在调用另一个函数之前,函数是否可以完成?我问的原因是因为电话支票不应该返回真正的电脑赢。这就是为什么我很困惑。

hasWon(){ 
 
     let scores = this.scoring(); 
 
     allScores: { 
 
     for(let i = 0; i < scores.length; i++){ 
 
      let win = $('.end'); 
 

 
      let currentScore = scores[i]; 
 
      console.log("The scores here: " + scores); 
 
      console.log("the current score: " + currentScore); 
 
      console.log("the player score: " + (this.player * 3)); 
 
      console.log("the truth: " + (currentScore === (this.player * 3))); 
 
     
 
      if(currentScore === (this.player * 3)){ 
 
      console.log("we got in here but this line isn't counting"); 
 
      win.html("<h4>Player won the Game!</h4>"); 
 
      this.hasWonEnd(); 
 
      break allScores; 
 
      } else if (currentScore === (this.computer * 3)) { 
 
      console.log("we are displaying this line....."); 
 
      win.html("<h4>Computer won the Game!</h4>"); 
 
      this.hasWonEnd(); 
 
      break allScores; 
 
      } else if (this.emptyIndices().length === 0 && i === (scores.length - 1)){ 
 
      win.html("<h4>It's a draw!</h4>"); 
 
      this.hasWonEnd(); 
 
      break allScores; 
 
      } 
 
     } 
 
     } 
 
    }

回答

0

我已经找到了问题。我相信这是es6无关。 (不知道我是否应该改变标题。)

调用函数,检测胜利者被调用后,胜利者已经决定,从而覆盖它。我的解决方案是创建游戏状态变量,以检查是否有赢家。我仍然不明白为什么这些功能在获得新的价值之前不会完成。但它完成,它似乎工作正常。 感谢所有看过它的人。