私は単純な三目並べゲームを作成しています。クリックすると、clickHandler関数の影響を受けるすべての変数をリセットし、テーブルセルから要素をクリアして、ゲームを仮想的に再起動するボタンがあります。ただし、ゲームの終了前に新しいゲームを開始すると、X と O が同じセルに移動し、ターンをスキップしたと言ってゲームが開始されます。このコードの何が問題なのですか?なぜこのような動作をするのですか? 結果を直接テストしたい場合は、ここにフィドルがあります。http://jsfiddle.net/YdRLg/
//Creates the variables needed to be manipulated later
var X = 'X';
var O = 'O';
var currentPlayer;
var turnCount = 0;
var xMoves = [];
var oMoves = [];
var cellTracker;
var winAlert;
var winConditions = [
['c1', 'c2', 'c3'],
['c4', 'c5', 'c6'],
['c7', 'c8', 'c9'],
['c1', 'c4', 'c7'],
['c2', 'c5', 'c8'],
['c3', 'c6', 'c9'],
['c1', 'c5', 'c9'],
['c3', 'c5', 'c7']
];
var button = $('button');
/*Set's the current player to X if turnCount is odd
And to O if turnCount is even*/
var setCurrentPlayer = function () {
if (turnCount % 2 === 0) {
currentPlayer = O;
} else {
currentPlayer = X;
}
};
//Pushes cellTracker's value to the curent player's move variable
var storeMoves = function () {
if (currentPlayer === X) {
xMoves.push(cellTracker);
} else if (currentPlayer === O) {
oMoves.push(cellTracker);
}
};
//Compares players moves with the winConditions to determine a winner
var determineWin = function (pMoves) {
for (var i = 0; i < winConditions.length; i++) {
if (winConditions[i].length > pMoves.length) {
continue;
}
for (var j = 0; j < winConditions[i].length; j++) {
winAlert = false;
for (var k = 0; k < pMoves.length; k++) {
if (pMoves[k] === winConditions[i][j]) {
winAlert = true;
break;
}
}
if (!winAlert) break;
}
if (winAlert) {
alert(currentPlayer + " wins!");
break;
}
}
};
//Determines if the game is over
var determineEnd = function () {
if (turnCount === 9 && winAlert === false) {
alert("Tie game!");
}
if (winAlert === true) {
$('td').off('click.mygame', clickHandler);
}
};
//Calls the above functions to simulate the game
var clickHandler = function () {
turnCount += 1;
setCurrentPlayer();
$(this).text(currentPlayer);
cellTracker = $(this).attr('id');
storeMoves();
determineWin(currentPlayer == 'X' ? xMoves : oMoves);
determineEnd();
console.log(turnCount, xMoves, oMoves, winAlert);
};
//Calls the clickHandler function when a cell is clicked
$('td').one('click.mygame', clickHandler);
//Starts a new game when the New Game button is clicked
$('button').bind('click', function () {
$('td').empty();
turnCount = 0;
xMoves = [];
oMoves = [];
winAlert = false;
$('td').one('click.mygame', clickHandler);
});