CodePen上的jQuery Tic-Tac-Toe无限循环警告

时间:2018-12-18 01:10:39

标签: javascript jquery tic-tac-toe codepen

在这里,我尝试玩类似游戏的 TIC-TAC-TOE
在CodePen游戏中,有时在打破获胜逻辑时会崩溃,并显示此无限循环警告。似乎无法弄清为什么

picture of error on codePen 我似乎无法在脱机计算机上将其断开,也无法想到无限循环将在线出现的原因。

//Tic Tac Toe Simple Game less then 50 lines 
//of code
//simple jquery tic-tac style game with aim 
//at short easy css modifiebly code
//@sanchopanza
var gameOver = false;
var playerTurn = 1;
var playerAMoves = [];
var playerBMoves = [];
for (var i = 1; i < 10; i++) { //generate map
  $(".map").append('<div class="box" id="loadID""></div>');
  $("#loadID").attr("id", i);
}
$(".box").click(function() {
  if (gameOver === false && $("#" + this.id).text() == "") {
    if (playerTurn === 1) {
      $("#" + this.id).append('<div class="crissCross">0</div>');
      playerAMoves.push(this.id);
      playerTurn = 2;
      gameEnd();
      return;
    }
    if (playerTurn === 2) {
      $("#" + this.id).append('<div class="crissCross">X</div>');
      playerBMoves.push(this.id);
      playerTurn = 1;
      gameEnd();
      return;
    }
  };
})

function gameEnd() {
  var player;
  if (playerTurn === 2) {
    player = playerAMoves;
  };
  if (playerTurn === 1) {
    player = playerBMoves;
  };
  if (containsAll(["1", "5", "9"], player) === true) {
    winnerWinner();
    return;
  };
  if (containsAll(["3", "5", "7"], player) === true) {
    winnerWinner();
    return;
  };
  if (containsAll(["1", "2", "3"], player) === true) {
    winnerWinner();
    return;
  };
  if (containsAll(["4", "5", "6"], player) === true) {
    winnerWinner();
    return;
  };
  if (containsAll(["7", "8", "9"], player) === true) {
    winnerWinner();
    return;
  };
  if (containsAll(["1", "4", "7"], player) === true) {
    winnerWinner();
    return;
  };
  if (containsAll(["2", "5", "8"], player) === true) {
    winnerWinner();
    return;
  };
  if (containsAll(["3", "6", "9"], player) === true) {
    winnerWinner();
    return;
  };
  if (playerAMoves.length + playerBMoves.length === 9) {
    $(".map").css("height", "370px").append("PARITY");
    return;
  }
};

function containsAll(winCombi, playerMoves) { //locator
  for (var i = 0, len = winCombi.length; i < len; i++) {
    if ($.inArray(winCombi[i], playerMoves) == -1) return false;
  }
  return true;
}

function winnerWinner() {
  gameOver = true;
  if (playerTurn === 2) {
    $(".map").css("height", "370px").append("CIRCLE WINS");
    return;
  } else {
    $(".map").css("height", "370px").append("CROSS WINS");
    return;
  }
}

0 个答案:

没有答案