Итак, я пытаюсь сделать игру Tic Tac Toe после this учебника. Когда я запускаю его и посмотреть на Дев инструментов Chrome, он говорит Uncaught RangeError: Maximum call stack size exceeded
, и указать мне на линии этой функции:Ошибка «Максимальный размер стека вызовов»
var State = function(oldState) {
this.turn = "";
this.oMovesCount = 0;
this.result = "still running";
this.board = [];
//get the information from the previous state to use it for following states
if (typeof oldState !== "undefined") {
var len = oldState.board.length;
this.board = new Array(len);
for (var i = 0; i < len; i++) {
this.board[i] = oldState.board[i];
}
this.oMovesCount = oldState.oMovesCount;
this.result = oldState.result;
this.turn = oldState.turn;
}
//change to X or O accordingly
this.advanceTurn = function() {
//Was it just X's turn? If so, change to O. If not, change to X.
this.turn = this.turn === "x" ? "o" : "x";
};
//checks for victory
this.result = "still running";
this.isTerminal = function() {
var B = this.board;
//check to see if there has been a victory
//check rows
for(var i = 0; i <= 6; i = i + 3) {
if(B[i] !== "E" && B[i] === B[i+1] && B[i+1] == B[i+2]) {
this.result = B[i] + " has won!";
return true;
}
}
//check columns
for(var i = 0; i <= 2 ; i++) {
if(B[i] !== "E" && B[i] === B[i+3] && B[i+3] === B[i+6]) {
this.result = B[i] + " has won!";
return true;
}
}
//check diagonals
for(var i = 0, j = 4; i <= 2 ; i = i + 2, j = j - 2) {
if(B[i] !== "E" && B[i] == B[i+j] && B[i+j] === B[i + 2*j]) {
this.result = B[i] + " has won!";
return true;
}
};
//if there have been no wins, check the number of empty cells
//if there are no empty cells, it's a draw
var available = this.emptyCells();
if (available.length == 0) {
this.result = "draw";
return true;
}
else {
return false;
}
};
//keeps track of how many empty cells there are on the board
this.emptyCells = function() {
var indxs = [];
for (var i = 0; i < 9; i++) {
if (this.board[i] === "E") {
indxs.push(i);
}
}
return indxs;
}
};
Я не понимаю, почему. Here's полный код, ошибка появляется, когда вы нажимаете Play
, затем OK
затем на одной из ячеек. Here он размещен на другом сайте, если это помогает.
Спасибо!
Неверная скрипка, или вы не скопировали фактический код. Получение «TypeError: Невозможно прочитать статус свойства« неопределенного », а не то, что вы говорите. –
@ShadowWizard yep, нажмите play и ok, а затем на любую из ячеек. –