0

My code deals a card to all players at once, then there is an interval before dealing again. I want to deal 3 cards to each player, 1 card at a time with interval.

function dealPlayers() {

  var counter = 1;

  var timer = setInterval(function () {

    for (var i = 0; i < gameDB.plySeatArray.length; i++) {

      gameDB.plySeatArray[i].addCard(getNextCard(), false);

    };

    if (counter >= 3) {
      clearInterval(timer);
    }

    counter++;

  }, 1000);

}
3
  • 2
    what/where exactly the problem is? Commented Nov 29, 2016 at 13:46
  • 1
    FWIW, you should really be separating the logic and the presentation here. The logic should happen immediately; how that is presented in the UI should be independent of this. Commented Nov 29, 2016 at 13:48
  • the problem is in the timeout. It deals the players accordingly, but does not wait before dealing each card. Commented Nov 29, 2016 at 14:05

1 Answer 1

1

You dont really want intervals, you want a recursive function that constantly waits and deals to the next player.

function dealCard(playerIndex) {
    gameDB.plySeatArray[playerIndex].addCard(getNextCard(), false);
    if ((playerIndex + 1) == gameDB.plySeatArray.length) {
        //end of the queue, reset to the first player
        playerIndex = 0;
    } else {
        playerIndex++;
    }

    //Check the next playerIndex's card
    if (/*playerIndex doesnt have 3 cards, deal him in in a second*/) {
        setTimeout(function() {
            dealCard(playerIndex);
        }, 1000);
    }
}

dealCard(0);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks tymeJV. am getting an unexpected token in line ==. "if (/*playerIndex doesnt have 3 cards, deal him in in a second*/) { "
there can be 2 - 4 players and they can occupy any seat, leaving some vacant.
@Dayo -- Ahh didn't know about the seating, some more if checks can solve that! Happy to help!

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.