"超出最大呼叫堆栈大小"错误

时间:2016-09-24 14:43:36

标签: javascript jquery

所以我试图在this教程之后制作一个Tic Tac Toe游戏。当我运行它并查看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如果有帮助,它会托管在其他网站上。

谢谢!

3 个答案:

答案 0 :(得分:1)

AIAction方法中有一个拼写错误:

this.oMovesPosition = pos; //the position on the board where the O would be placed
this.minimaxVal = 0; //the minimax value of the state that the action leads to

this.applyTo = function(state) {
    var next = new State(state);

    //if the current turn in the current state is O, increment .oMovesCount
    next.board[this.movePosition] = state.turn;
    if (state.turn === "o") {
        next.oMovesCount++;
    }
    next.advanceTurn();
    return next;
};

注意第一行中的this.oMovesPosition,但是applyTo方法改为引用this.movePosition。

答案 1 :(得分:0)

太递归,在小提琴行395中,你在递归中调用函数minimax。

var nextScore = miniMax(nextState);

必须在内存不足之前停止递归,或将递归转换为循环。

答案 2 :(得分:0)

AIaction = function(pos)中,您有两个拼写相同的movePosition属性:

this.oMovesPosition = pos;

next.board[this.movePosition] = state.turn;

因为它们是不同的属性,所以第二行总是等同于:

next.board[undefined] = state.turn;

......所以董事会从未真正改变过。因此,董事会永远不会被视为终端,您的递归永远不会停止。