简体   繁体   中英

How can I generate all unique card pairs from a list of cards?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM