2016-06-21 132 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無關。 (不知道我是否應該改變標題。)

調用函數,檢測勝利者被調用後,勝利者已經決定,從而覆蓋它。我的解決方案是創建遊戲狀態變量,以檢查是否有贏家。我仍然不明白爲什麼這些功能在獲得新的價值之前不會完成。但它完成,它似乎工作正常。 感謝所有看過它的人。