How can I generate all unique card pairs from a list of cards?
-
01-07-2021 - |
سؤال
I am implementing a hand strength evaluator, which is to evaluate all possible pairs from the remaining 47 cards, after dealing one hand and the flop.
I have implemented the evaluator, but I'm missing all the possible combinations for which to compare. I am tempted to create a class for Hand, which consists of two cards, and store each combination in a set, HashSet. Which data structure should i choose? If HashSet is best, then how can i force each instantiation of Hand to be unique?
المحلول
HashSet
seems reasonable although since ordering might matter later you might want to consider a TreeSet
. If you implements the equals
and compareTo
/Comparable
methods in Hand
the Set
will force uniqueness.
نصائح أخرى
I would number the cards or place them in a List.
If you use a list you can do
Set<Card> inHand = ...
for(int i=0;i<list.size();i++) {
Card card1 = list.get(i);
if (inHand.contains(card1)) continue;
for(int j=i+1;j<list.size();j++) {
Card card2 = list.get(j);
if (inHand.contains(card2)) continue;
// process card1 and card2