How to implement a dealer class without storing a deck of cards?

前端 未结 8 1299
长发绾君心
长发绾君心 2021-02-09 19:07
  • Question

    Even only 52 cards, the permutationIndex where I describe in Explanations section, would be a huge number; it is

8条回答
  •  自闭症患者
    2021-02-09 19:27

    One way to tackle this is to use (pseudo)random number generator (like a Mersenne Twister), then store only the seed number for each deal. Since you get the same sequence of random numbers each time from the same seed, it serves to represent the whole deal (using the random numbers generated from that seed to drive what cards are dealt).

    [edit...]

    Some pseudo-code for the deal:

    while (#cards < cardsNeed)
        card = getCard(random())
        if (alreadyHaveThisCard(card))
            continue
        [do something with the card...]
    

提交回复
热议问题