Question

I'm developing a card game AI.

A player only knows it's cards and needs to randomly distribute the unseen cards to its opponents.

Some opponents will have 0% chance of having some cards (this is a must).

The opponents will be more likely to have some cards than others (this would be good but my main question is to solve the situation presented before).

Now the problem is that this card distribution must happen lots of times in a very short period of time, so randomly distributing it until the 0% chance cards are not in the hand of the opponents that have 0% chance of having them is not feasible.

What approach would you suggest if I want to solve this problems:

  1. Make sure no opponent has a card that he has 0% chance of having while ensuring the other cards are evenly distributed (and one other opponent will have this card)

  2. (In a situation where some players have 10%, 40%, etc.. chance of having a card)

    Make sure the cards are distrubuted acording to their weights. Eg: A player has to choose 3 cards, he has 2% chance of having A, K or Q so he cannot only have thoose 3 cards to choose from (he may end up with n different highcards from different suits)

If you could help me to solve situation number 1 (in any language, or pointing me to an algorithm), I would realy apreciate it.

Thanks!

EDIT: I should clarify that while some opponents may have 0% chance of having one card other opponents may have a normal chance of having them (and someone definily will have to have it).

EDIT2: This means that if we keep distributing cards at will we may end up with only forbidden cards for the last player to take (which can't happen).

Was it helpful?

Solution 3

I found a similar question on another site altought it doesn't actually get an answer on how to solve the simple part of my question.

If anyone else is interested the link is:

https://math.stackexchange.com/questions/220606/n-piles-of-hidden-cards-of-known-marginal-probability-distribution-then-a-card

Edit: I ended up distributing all the cards using the constraints of some opponents not having some suits. When one opponent had no cards he could choose from he tries to swap a card from the pile he has no use for with another opponent than can take this card. That opponent in exchange gives him a card he can take. It is not the best solution, but works 99.9% of the times.

OTHER TIPS

For #1, if you deal a 'forbidden' card into a hand, just shuffle it back into the deck.
For #2 you might be able to use something like a "Cumulative Distribution Function"

I think this question might be relevant.

  1. Make sure no one has a card that he has 0% chance of having while ensuring the others are evenly distributes

To make a 0% chance for some cards to be dealt, simply remove those cards as options. E.g.

IList<int> cardOptions = Enumerable.Range(1, 52).ToList();
// all 52 cards in this deck represented as integers
// remove some:
cardOptions.Remove(5);
cardOptions.Remove(42);
// when you randomly select from cardOptions,
// each remaining card has a 1/50 probability of being chosen

2. (In a situation where some players have 10%, 40%, etc.. chance of having a card)

Make sure the cards are distrubuted acording to their weights. Eg: A player has to choose 3 cards, he has 2% chance of having A, K or Q and that's the only cards he has left to choose from (from 2 different suits)

This sounds different from a typical weighted random distribution. You could do the following (pseudocode):

double probabilityOfHavingCertainCard = 0.4;
int thatCard = 52;
if (randomDoubleBetween0and1 < probabilityOfHavingCertainCard)
    cardResults.Add(thatCard);

cardOptions.Remove(thatCard);
while (cardResults.Count < 5)
    cardResults.Add(random from cardOptions, not previously chosen);

That is, it treats the weighted card as a special case, not as part of the ordinary shuffle of the hand.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top