硬币算法优化

时间:2016-02-19 12:47:15

标签: javascript node.js algorithm

如何在不超出调用堆栈大小的情况下处理递归调用?

这个问题已在Maximum call stack size exceeded error得到解答,但对我没有帮助。这对其他人可能有帮助。

算法提示有点长,但对于那些已确定的人:coin algorithm

我的代码可能不会尝试满足所有要求,但无论我是否想要对我所拥有的内容有所了解。

var gameSetup = {
        coins: [2,333,4,11,99,20,10],
        minOdds: 1,
        maxOdds: 100000
}
function game(gs) {
        this.minOdds = gs.minOdds;
        this.maxOdds = gs.maxOdds;

        var coinArray = gs.coins;
        var coinArrayLength = coinArray.length;
        var indicesToSplice = [];

        var gameResults = {
                turns: 0,
                score: 0
        }

        var gameFunctions = {
                getTurns: function() {
                        return gameResults.turns;
                },
                setTurns: function() {
                        ++gameResults.turns;
                },
                setScore: function(lcv,rcv) {
                        var score = lcv * rcv;
                        gameResults.score += score;

                },
                getScore: function() {
                        return gameResults.score;

                },
                generateFlips: function() {
                        return generateFlips.getRandomNumbersInclusive();
                }

        }
        var generateFlips = (function() {
                var flips = [];
                        return {
                                getRandomNumbersInclusive: function() {
                                        flips = [];
                                        for(i=0; i < coinArrayLength; i ++){
                                                var currentFlip = Math.floor(Math.random() * (maxOdds - minOdds + 1)) + minOdds;
                                                flips.splice(0,0,currentFlip);
                                        }
                                return flips;
                                }
                        }
        })();
        (function takeTurn(coinArrayLength) {
                var flips = gameFunctions.generateFlips();


                flips.forEach(function(i, index) {
                        if(i == maxOdds) {
                                var leftOfIndex = flips[index-1];
                                var rightOfIndex = flips[index +1];
                                if(typeof leftOfIndex  === 'undefined' || typeof rightOfIndex  === 'undefined') {

                                } else {
                                        indicesToSplice.splice(0,0,index);
                                        var leftCoinValue = coinArray[index-1];
                                        var rightCoinValue = coinArray[index+1];
                                        gameFunctions.setScore(leftCoinValue,rightCoinValue);
                                }

                        }

                });



                if(indicesToSplice.length > 0) {
                        indicesToSplice.forEach(function(i){
                                coinArray.splice(i,1);
                                coinArrayLength = coinArray.length;
                        });
                }
                indicesToSplice = [];
                gameFunctions.setTurns();

                if(coinArrayLength > 2) {
                        takeTurn(coinArrayLength);
                } else {
                        return;

                }

        })(coinArrayLength);


}
game(gameSetup);

1 个答案:

答案 0 :(得分:1)

  

如何在不超出调用堆栈大小的情况下处理递归调用?

你不要让它递归。除了coinArrayLength之外,没有需要保留的状态,可以使用while循环来完成。

while (coinArrayLength > 2) {

    var flips = gameFunctions.generateFlips();

    flips.forEach(function(i, index) {
        if(i === maxOdds) {
            var leftOfIndex = flips[index-1];
            var rightOfIndex = flips[index +1];
            if(typeof leftOfIndex  === 'undefined' || typeof rightOfIndex  === 'undefined') {


            } else {
                indicesToSplice.splice(0,0,index);
                var leftCoinValue = coinArray[index-1];
                var rightCoinValue = coinArray[index+1];
                gameFunctions.setScore(leftCoinValue,rightCoinValue);
            }
        }
    });

    if(indicesToSplice.length > 0) {
        indicesToSplice.forEach(function(i){
            coinArray.splice(i,1);
            coinArrayLength = coinArray.length;
        });
    }
    indicesToSplice = [];
    gameFunctions.setTurns();
}

您的代码中填充了没有任何意义的IIF。 gameFunctions.generateFlips函数归结为:

function generateFlips() {
    var flips = [];
    for (var i = 0; i < coinArrayLength; i++) {
        var currentFlip = Math.floor(Math.random() * (maxOdds - minOdds + 1)) + minOdds;
        flips.unshift(currentFlip);
    }
    return flips;
}

然而,你在IIF中写了2个返回语句,存储在gameFunctions对象上。保持简单。