1

I've written a blackjack script that I'd like to iterate recursively until a rather large bankroll runs out. I'd like to run analyses on the telemetry. It's a script that lives locally and poses no danger to anything but the browser environment I'm running it in.

Essentially, the script is supposed to be recursive until the cash runs out. It works fine up to around 5k separate hands or so - for bankrolls up to 10k, and then it throws the max call stack error. However, I need way more data; like > 100k hands.

I've searched SO for solutions and I'm gathering it's a browser-specific thing. Any thoughts would be much appreciated!

Code snippet attached:

function main() {
init();
if (bankRoll >= initialBet) {
    determineBet();
}
else {
    alert("Not enough moneyz to play!");
    console.log("telemetry");
    exitFunction();
}
bankRoll -= initialBet;
playTheGame(); // the whole game, betting, receiving cards, strategy etc
}
5
  • Does it have to be recursive? Maybe you can rewrite it to use a stack instead. Commented Jun 2, 2013 at 13:00
  • Can you post a code snippet that has a recursive logic? Commented Jun 2, 2013 at 13:01
  • @FelixKling Nice idea; don't exactly know how to do this as I've just started programming last week. Wrote the game first and then decided I'd use it to test basic bj strategy. Commented Jun 2, 2013 at 13:20
  • Can you post the error you are getting? Commented Jun 2, 2013 at 13:26
  • @Jacob Amerz: "max call stack size exceeded" Commented Jun 2, 2013 at 13:28

1 Answer 1

1

I suggest you use a loop:

function main() {
    init();
    while (bankRoll >= initialBet) {
        determineBet();
        bankRoll -= initialBet;
        playTheGame(); // the whole game, betting, receiving cards, strategy etc
    }
    alert("Not enough moneyz to play!");
    console.log("telemetry");
    exitFunction();
}

It's hard to say if I refactored it correctly since I don't know what functions like playTheGame or determineBet do, but I hope you get the idea.

Sign up to request clarification or add additional context in comments.

5 Comments

This returns the same error at around the same magnitude of hands (>5k). The playTheGame() function holds the bulk of the functions, which is not that complicated, but I think I probably have a lot to learn about coding efficiently.
Well, where and when is the main function called?
It is called in the html script element and in the function that governs the restarting of the game after a hand finishes. That function in turn is called by the evaluating functions that determine the outcome.
Ok, the goal should be to put everything inside of the while loop. The complete logic of dealing and evaluation one hand should be in there, so that you don't need to call the main function recursively and instead just call it once at startup.
Thanks! I dropped the main() function call from the looping function and now it doesn't give me any more trouble! Less is certainly more...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.