jeudi 15 avril 2021

Optimizing random cards assignment

I have a deck of 21 cards and 6 players. I will label each card with a letter from the alphabet.
I have to randomly assign 3 cards to each player (for a total of 18 cards, 3 are left in the deck) under some conditions, which are divided in two different categories:

  • andlist(x) = [[g, e, h],[k, m, f]], which means: 'g, e, h, k, m, f cannot be assigned to player x'
  • orlist(x) = [[a, l, i], [j, d ,o], [b, n, c]], which means: 'a or l or i, and j or d or o, and b or n or c must be assigned to player x'.

Of course this is just an example; andlist(x) and orlist(x) can contain different numbers of nested triplets for each player.

I wrote a simple code that keeps assigning the 18 cards to the 6 players completely random, until the conditions are met (using a while loop), but this strategy is very unefficient as it takes too long to find an appropriate allocation.

Is there any more efficient way to deal with this kind of conditions in Python?




Aucun commentaire:

Enregistrer un commentaire