[英]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. 我正在实施一个手牌力量评估器,用于在处理单手和翻牌后评估其余47张牌中的所有可能对。
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.
我很想为Hand创建一个类,它由两张卡组成,并将每个组合存储在一个集合HashSet中。 Which data structure should i choose?
我应该选择哪种数据结构? If HashSet is best, then how can i force each instantiation of Hand to be unique?
如果HashSet是最好的,那么我如何强制Hand的每个实例化都是唯一的?
HashSet
seems reasonable although since ordering might matter later you might want to consider a TreeSet
. HashSet
看起来很合理,但由于排序可能稍后TreeSet
您可能需要考虑TreeSet
。 If you implements the equals
and compareTo
/ Comparable
methods in Hand
the Set
will force uniqueness. 如果在
Hand
实现equals
和compareTo
/ Comparable
方法,则Set
将强制唯一性。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.