最简单的扑克手评估算法

时间:2012-04-28 13:27:05

标签: java r algorithm data-structures poker

我正在考虑Java中的扑克牌(5张牌)评价。现在我正在寻求简单和清晰,而不是性能和效率。我可能会写一个“天真”的算法,但它需要大量的代码。

我还看到了一些扑克评估库,它们使用散列和按位操作,但它们看起来相当复杂。

什么是“最干净,最简单”的扑克手牌评估算法?

12 个答案:

答案 0 :(得分:31)

这是一个非常简短但完整的基于直方图的5卡扑克评分函数(2.x)。如果转换为Java,它将会变得更长。

def poker(hands):
    scores = [(i, score(hand.split())) for i, hand in enumerate(hands)]
    winner = sorted(scores , key=lambda x:x[1])[-1][0]
    return hands[winner]

def score(hand):
    ranks = '23456789TJQKA'
    rcounts = {ranks.find(r): ''.join(hand).count(r) for r, _ in hand}.items()
    score, ranks = zip(*sorted((cnt, rank) for rank, cnt in rcounts)[::-1])
    if len(score) == 5:
        if ranks[0:2] == (12, 3): #adjust if 5 high straight
            ranks = (3, 2, 1, 0, -1)
        straight = ranks[0] - ranks[4] == 4
        flush = len({suit for _, suit in hand}) == 1
        '''no pair, straight, flush, or straight flush'''
        score = ([1, (3,1,1,1)], [(3,1,1,2), (5,)])[flush][straight]
    return score, ranks

 >>> poker(['8C TS KC 9H 4S', '7D 2S 5D 3S AC', '8C AD 8D AC 9C', '7C 5H 8D TD KS'])
 '8C AD 8D AC 9C'

答案 1 :(得分:11)

查找表是解决问题的最简单,最简单的解决方案,也是最快的解决方案。诀窍是管理表的大小并保持使用模式足够快以便快速处理(space–time tradeoff)。显然,从理论上讲,你可以只编码每个可以持有的手并进行一系列评估,然后--poof--一个表查找,你就完成了。不幸的是,对于大多数机器来说,这样的表会很庞大且难以管理,并且无论如何都会让你颠倒磁盘,因为内存被换掉了。

所谓的两加二解决方案是一个10M的大表,但字面上只涉及一张桌面查找手中的每张卡。您不太可能找到更快更简单的算法。

其他解决方案涉及更多具有更复杂索引的压缩表,但它们易于理解且速度非常快(尽管比2 + 2慢得多)。在这里您可以看到有关散列的语言等等 - 将表格大小缩小到更易于管理的大小的技巧。

在任何情况下,查找解决方案都比直方图排序快几个数量级 - 在头上进行比较 - 特殊情况 - 并且通过这种方式进行一次冲洗解决方案,几乎没有一个值得一瞥。

答案 2 :(得分:5)

你实际上不需要任何高级功能,它可以按位完成:(来源:http://www.codeproject.com/Articles/569271/A-Poker-hand-analyzer-in-JavaScript-using-bit-math

(这个实际上是用JavaScript编写的,但是你可以根据需要从Java中评估JavaScript,所以它不应该是一个问题。而且,它只是它的简短,所以即使是为了说明方法......):

首先你将你的牌分成两个阵列:排名(cs)和套装(ss)并代表套装,你将使用1,2,4或8(即0b0001,0b0010,...):< / p>

var J=11, Q=12, K=13, A=14, C=1, D=2, H=4, S=8;

现在这就是魔术:

function evaluateHand(cs, ss) {
    var pokerHands = ["4 of a Kind", "Straight Flush","Straight","Flush","High Card","1 Pair","2 Pair","Royal Flush", "3 of a Kind","Full House"];

    var v,i,o,s = 1 << cs[0] | 1 << cs[1] | 1 << cs[2] | 1 << cs[3] | 1 << cs[4];
    for (i = -1, v = o = 0; i < 5; i++, o = Math.pow(2, cs[i] * 4)) {v += o * ((v / o & 15) + 1);}
    v = v % 15 - ((s / (s & -s) == 31) || (s == 0x403c) ? 3 : 1);
    v -= (ss[0] == (ss[1] | ss[2] | ss[3] | ss[4])) * ((s == 0x7c00) ? -5 : 1);
    return pokerHands[v];
}

用法:

evaluateHand([A,10,J,K,Q],[C,C,C,C,C]); // Royal Flush

现在它的作用(非常简短)就是它将1加入 s 的第3位,当有2时,将第4位加到第4位,等于3,等等,所以对于上面的例子 s 看起来像这样:

<强> 0b111110000000000

[p,对于[A,2,3,4,5],它看起来像这样

0b100 0000 0011 1100

v 使用四位来记录同一张卡的多个出现次数,所以它长52位,如果你有三个A和两个国王,它的8个MSB位看起来像:

0111 0011 ...

然后最后一行检查同花顺或同花顺或皇家同花(0x7c00)。

答案 3 :(得分:4)

这是一种天真的五卡手比较方法,我用它来帮助最初填充查找表:

我没有尽可能简洁,而是优先考虑类型安全和清晰的自我记录代码。如果您不熟悉我正在使用的番石榴类型,您可以浏览他们的documentation

我会在这里包含代码(减去底部枚举常量的静态导入),虽然它真的太长了,无法在答案中轻松查看。

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Ordering.from;
import static com.google.common.collect.Ordering.natural;
import static java.util.Comparator.comparing;
import static java.util.Comparator.comparingInt;

import java.util.Comparator;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.Set;
import java.util.function.Function;

import com.google.common.collect.EnumMultiset;
import com.google.common.collect.Multiset;
import com.google.common.collect.Multiset.Entry;
import com.google.common.collect.Ordering;

public class Hand implements Comparable<Hand> {
    public final Category category;

    private final LinkedList<Rank> distinctRanks = new LinkedList<>();

    public Hand(Set<Card> cards) {
        checkArgument(cards.size() == 5);
        Set<Suit> suits = EnumSet.noneOf(Suit.class);
        Multiset<Rank> ranks = EnumMultiset.create(Rank.class);
        for (Card card : cards) {
            suits.add(card.suit);
            ranks.add(card.rank);
        }
        Set<Entry<Rank>> entries = ranks.entrySet();
        for (Entry<Rank> entry : byCountThenRank.immutableSortedCopy(entries)) {
            distinctRanks.addFirst(entry.getElement());
        }
        Rank first = distinctRanks.getFirst();
        int distinctCount = distinctRanks.size();
        if (distinctCount == 5) {
            boolean flush = suits.size() == 1;
            if (first.ordinal() - distinctRanks.getLast().ordinal() == 4) {
                category = flush ? STRAIGHT_FLUSH : STRAIGHT;
            }
            else if (first == ACE && distinctRanks.get(1) == FIVE) {
                category = flush ? STRAIGHT_FLUSH : STRAIGHT;
                // ace plays low, move to end
                distinctRanks.addLast(distinctRanks.removeFirst());
            }
            else {
                category = flush ? FLUSH : HIGH_CARD;
            }
        }
        else if (distinctCount == 4) {
            category = ONE_PAIR;
        }
        else if (distinctCount == 3) {
            category = ranks.count(first) == 2 ? TWO_PAIR : THREE_OF_A_KIND;
        }
        else {
            category = ranks.count(first) == 3 ? FULL_HOUSE : FOUR_OF_A_KIND;
        }
    }

    @Override
    public final int compareTo(Hand that) {
        return byCategoryThenRanks.compare(this, that);
    }

    private static final Ordering<Entry<Rank>> byCountThenRank;

    private static final Comparator<Hand> byCategoryThenRanks;

    static {
        Comparator<Entry<Rank>> byCount = comparingInt(Entry::getCount);
        Comparator<Entry<Rank>> byRank = comparing(Entry::getElement);
        byCountThenRank = from(byCount.thenComparing(byRank));
        Comparator<Hand> byCategory = comparing((Hand hand) -> hand.category);
        Function<Hand, Iterable<Rank>> getRanks =
                (Hand hand) -> hand.distinctRanks;
        Comparator<Hand> byRanks =
                comparing(getRanks, natural().lexicographical());
        byCategoryThenRanks = byCategory.thenComparing(byRanks);
    }

    public enum Category {
        HIGH_CARD,
        ONE_PAIR,
        TWO_PAIR,
        THREE_OF_A_KIND,
        STRAIGHT,
        FLUSH,
        FULL_HOUSE,
        FOUR_OF_A_KIND,
        STRAIGHT_FLUSH;
    }

    public enum Rank {
        TWO,
        THREE,
        FOUR,
        FIVE,
        SIX,
        SEVEN,
        EIGHT,
        NINE,
        TEN,
        JACK,
        QUEEN,
        KING,
        ACE;
    }

    public enum Suit {
        DIAMONDS,
        CLUBS,
        HEARTS,
        SPADES;
    }

    public enum Card {
        TWO_DIAMONDS(TWO, DIAMONDS),
        THREE_DIAMONDS(THREE, DIAMONDS),
        FOUR_DIAMONDS(FOUR, DIAMONDS),
        FIVE_DIAMONDS(FIVE, DIAMONDS),
        SIX_DIAMONDS(SIX, DIAMONDS),
        SEVEN_DIAMONDS(SEVEN, DIAMONDS),
        EIGHT_DIAMONDS(EIGHT, DIAMONDS),
        NINE_DIAMONDS(NINE, DIAMONDS),
        TEN_DIAMONDS(TEN, DIAMONDS),
        JACK_DIAMONDS(JACK, DIAMONDS),
        QUEEN_DIAMONDS(QUEEN, DIAMONDS),
        KING_DIAMONDS(KING, DIAMONDS),
        ACE_DIAMONDS(ACE, DIAMONDS),

        TWO_CLUBS(TWO, CLUBS),
        THREE_CLUBS(THREE, CLUBS),
        FOUR_CLUBS(FOUR, CLUBS),
        FIVE_CLUBS(FIVE, CLUBS),
        SIX_CLUBS(SIX, CLUBS),
        SEVEN_CLUBS(SEVEN, CLUBS),
        EIGHT_CLUBS(EIGHT, CLUBS),
        NINE_CLUBS(NINE, CLUBS),
        TEN_CLUBS(TEN, CLUBS),
        JACK_CLUBS(JACK, CLUBS),
        QUEEN_CLUBS(QUEEN, CLUBS),
        KING_CLUBS(KING, CLUBS),
        ACE_CLUBS(ACE, CLUBS),

        TWO_HEARTS(TWO, HEARTS),
        THREE_HEARTS(THREE, HEARTS),
        FOUR_HEARTS(FOUR, HEARTS),
        FIVE_HEARTS(FIVE, HEARTS),
        SIX_HEARTS(SIX, HEARTS),
        SEVEN_HEARTS(SEVEN, HEARTS),
        EIGHT_HEARTS(EIGHT, HEARTS),
        NINE_HEARTS(NINE, HEARTS),
        TEN_HEARTS(TEN, HEARTS),
        JACK_HEARTS(JACK, HEARTS),
        QUEEN_HEARTS(QUEEN, HEARTS),
        KING_HEARTS(KING, HEARTS),
        ACE_HEARTS(ACE, HEARTS),

        TWO_SPADES(TWO, SPADES),
        THREE_SPADES(THREE, SPADES),
        FOUR_SPADES(FOUR, SPADES),
        FIVE_SPADES(FIVE, SPADES),
        SIX_SPADES(SIX, SPADES),
        SEVEN_SPADES(SEVEN, SPADES),
        EIGHT_SPADES(EIGHT, SPADES),
        NINE_SPADES(NINE, SPADES),
        TEN_SPADES(TEN, SPADES),
        JACK_SPADES(JACK, SPADES),
        QUEEN_SPADES(QUEEN, SPADES),
        KING_SPADES(KING, SPADES),
        ACE_SPADES(ACE, SPADES);

        public final Rank rank;

        public final Suit suit;

        Card(Rank rank, Suit suit) {
            this.rank = rank;
            this.suit = suit;
        }
    }
}

答案 4 :(得分:4)

这是dansalmo程序的修改版本,适用于holdem手:

def holdem(board, hands):
    scores = [(evaluate((board + ' ' + hand).split()), i) for i, hand in enumerate(hands)]
    best = max(scores)[0]
    return [x[1] for x in filter(lambda(x): x[0] == best, scores)]

def evaluate(hand):
    ranks = '23456789TJQKA'
    if len(hand) > 5: return max([evaluate(hand[:i] + hand[i+1:]) for i in range(len(hand))])
    score, ranks = zip(*sorted((cnt, rank) for rank, cnt in {ranks.find(r): ''.join(hand).count(r) for r, _ in hand}.items())[::-1])
    if len(score) == 5: # if there are 5 different ranks it could be a straight or a flush (or both)
        if ranks[0:2] == (12, 3): ranks = (3, 2, 1, 0, -1) # adjust if 5 high straight
        score = ([1,(3,1,2)],[(3,1,3),(5,)])[len({suit for _, suit in hand}) == 1][ranks[0] - ranks[4] == 4] # high card, straight, flush, straight flush
    return score, ranks

def test():
    print holdem('9H TC JC QS KC', [
        'JS JD', # 0
        'AD 9C', # 1 A-straight
        'JD 2C', # 2
        'AC 8D', # 3 A-straight
        'QH KH', # 4
        'TS 9C', # 5
        'AH 3H', # 6 A-straight
        '3D 2C', # 7
      # '8C 2C', # 8 flush
    ])

test()

holdem()返回获胜手牌的索引列表。在test()示例中是[1,3,6],因为三只手用a分开底池,或者[8]如果没有注释了冲洗牌。

答案 5 :(得分:3)

如果你只是想了解它是如何工作的,这里是简单的算法:

HandStrength(ourcards,boardcards)
{
    ahead = tied = behind = 0
    ourrank = Rank(ourcards,boardcards)
    /* Consider all two-card combinations
    of the remaining cards. */
    for each case(oppcards)
    {
        opprank = Rank(oppcards,boardcards)
        if(ourrank>opprank)
            ahead += 1
        else if(ourrank==opprank)
            tied += 1
        else /* < */
            behind += 1
    }
    handstrength = (ahead+tied/2) / (ahead+tied+behind)
    return(handstrength)
}

来自Darse Billings的“计算机中的算法和评估”。

答案 6 :(得分:3)

Python 3版本

def poker(hands):
    scores = [(i, score(hand.split())) for i, hand in enumerate(hands)]
    winner = sorted(scores , key=lambda x:x[1])[-1][0]
    return hands[winner]

def score(hand):
    ranks = '23456789TJQKA'
    rcounts = {ranks.find(r): ''.join(hand).count(r) for r, _ in hand}.items()
    score, ranks = zip(*sorted((cnt, rank) for rank, cnt in rcounts)[::-1])
    if len(score) == 5:
        if ranks[0:2] == (12, 3): #adjust if 5 high straight
            ranks = (3, 2, 1, 0, -1)
        straight = ranks[0] - ranks[4] == 4
        flush = len({suit for _, suit in hand}) == 1
        '''no pair, straight, flush, or straight flush'''
        score = ([(1,), (3,1,1,1)], [(3,1,1,2), (5,)])[flush][straight]
    return score, ranks

 >>> poker(['8C TS KC 9H 4S', '7D 2S 5D 3S AC', '8C AD 8D AC 9C', '7C 5H 8D TD KS'])
 '8C AD 8D AC 9C'

基本上必须用(1,)代替1,以避免int至元组比较错误。

答案 7 :(得分:2)

如果你将一个手表示为一个例如Card个对象的数组,那么我会有循环遍历这个数组的方法,并确定它是否有一个2类,刷新等 - 如果是的话,它是什么类型的;所以如果一只手有三个5,你可以让3ofaKind()方法返回5。然后我会建立一个可能性的层次结构(例如,3种类型高于2种)并从那里开始工作。这些方法本身应该非常简单易懂。

答案 8 :(得分:1)

这里是转换为 R 的算法,使用6张卡组进行了测试,对应于以下结果给出的42.504个组合:

C65

扑克手的组合。由于处理限制,未使用13张卡片组进行测试(这将对应2.598.960组合)。

该算法用字符串表示手的,由两部分组成:

  • 按5个字符排序的卡号(例如,“ 31100”表示其中的三个)
  • 卡号由字母'B'(平局)到'N'(王牌)(例如,“ NILH”表示王牌,皇后,九和八)来计算。由于A2345扑克牌的作用是将A放在“ 2”之前(A的值为“ A”),因此它以字母“ B”开头。

例如,“ 32000NB”将是满满的三个A和两个Deuce。

扑克手值字符串很方便用于比较和订购

library(tidyverse)
library(gtools)

hand_value <- function(playerhand) {

  numbers <- str_split("23456789TJQKA", "")[[1]]
  suits <- str_split("DCHS", "")[[1]]

  playerhand <- data.frame(card = playerhand) %>% separate(card, c("number", "suit"), sep = 1)

  number_values <- data.frame(number = numbers, value = LETTERS[2:14], stringsAsFactors = FALSE)

  playerhand_number <- playerhand %>% 
    group_by(number) %>% 
    count(number) %>%
    inner_join(number_values, by = "number") %>%
    arrange(desc(n), desc(value))

  playerhand_suit <- playerhand %>% 
    group_by(suit) %>% 
    count(suit) %>%
    arrange(desc(n))

  if (nrow(playerhand_number) == 5)
    {
      if (playerhand_number[1,1] == 'A' & playerhand_number[2,1] == '5')
        playerhand_number <- data.frame(playerhand_number[,1:2], value = str_split("EDCBA", "")[[1]], stringsAsFactors = FALSE)
      straight <- asc(playerhand_number[1,3]) - asc(playerhand_number[5,3]) == 4
    } else
      straight = FALSE

  flush <- nrow(playerhand_suit) == 1

  if (flush)
    {
    if (straight)
      playerhand_number <- data.frame(playerhand_number[,c(1,3)], n = c(5, 0, 0, 0, 0), stringsAsFactors = FALSE) else
      playerhand_number <- data.frame(playerhand_number[,c(1,3)], n = c(3, 1, 1, 2, 0), stringsAsFactors = FALSE)
    } else
    {
    if (straight)
      playerhand_number <- data.frame(playerhand_number[,c(1,3)], n = c(3, 1, 1, 1, 0), stringsAsFactors = FALSE)
    }  

  playerhand_value <- append(append(c(playerhand_number$n), rep("0", 5 - nrow(playerhand_number))), c(playerhand_number$value))
  playerhand_value <- paste(playerhand_value, collapse = '')

  playerhand_value

}

使用上述示例的相同功能测试功能:

l <- c("8C TS KC 9H 4S", "7D 2S 5D 3S AC", "8C AD 8D AC 9C", '7C 5H 8D TD KS')
t <- as_tibble(l)
t <- t %>% mutate(hand = str_split(value, " ")) %>% select(hand)
t <- t %>% mutate(value = sapply(t[,1]$hand, hand_value)) %>% arrange(desc(value))
paste(t[[1]][[1]], collapse = " ")

哪个返回相同的结果:

[1] "8C AD 8D AC 9C"

希望有帮助。

答案 9 :(得分:1)

这是Kotlin中基于规则的简单实现:

class PokerHand constructor(hand: String) : Comparable<PokerHand> {

companion object {
    const val WIN = 1
    const val TIE = 0
    const val LOSS = -1
}

val cards: List<Card>

val isStraightFlush: Boolean
    get() = isStraight && isFlush

val isFourOfAKind: Boolean
    get() = cards.groupBy { it.weight }.map { it.value }.any { it.size == 4 }

val isFullHouse: Boolean
    get() = cards.groupBy { it.weight }.map { it.value }.size == 2

val isFlush: Boolean
    get() = cards.groupBy { it.suit }.map { it.value }.size == 1

val isStraight: Boolean
    get() = cards.map { it.weight.ordinal } == (cards[0].weight.ordinal..cards[0].weight.ordinal + 4).toList()

val isThreeOfAKind: Boolean
    get() = cards.groupBy { it.weight }.map { it.value }.any { it.size == 3 }

val isTwoPair: Boolean
    get() = cards.groupBy { it.weight }.map { it.value }.filter { it.size == 2 }.count() == 2

val isPair: Boolean
    get() = cards.groupBy { it.weight }.map { it.value }.any { it.size == 2 }

init {
    val cards = ArrayList<Card>()
    hand.split(" ").forEach {
        when (it.length != 2) {
            true -> throw RuntimeException("A card code must be two characters")
            else -> cards += Card(Weight.forCode(it[0]), Suit.forCode(it[1]))
        }
    }
    if (cards.size != 5) {
        throw RuntimeException("There must be five cards in a hand")
    }
    this.cards = cards.sortedBy { it.weight.ordinal }
}

override fun compareTo(other: PokerHand): Int = when {
    (this.isStraightFlush || other.isStraightFlush) ->
        if (this.isStraightFlush) if (other.isStraightFlush) compareByHighCard(other) else WIN else LOSS
    (this.isFourOfAKind || other.isFourOfAKind) ->
        if (this.isFourOfAKind) if (other.isFourOfAKind) compareByHighCard(other) else WIN else LOSS
    (this.isFullHouse || other.isFullHouse) ->
        if (this.isFullHouse) if (other.isFullHouse) compareByHighCard(other) else WIN else LOSS
    (this.isFlush || other.isFlush) ->
        if (this.isFlush) if (other.isFlush) compareByHighCard(other) else WIN else LOSS
    (this.isStraight || other.isStraight) ->
        if (this.isStraight) if (other.isStraight) compareByHighCard(other) else WIN else LOSS
    (this.isThreeOfAKind || other.isThreeOfAKind) ->
        if (this.isThreeOfAKind) if (other.isThreeOfAKind) compareByHighCard(other) else WIN else LOSS
    (this.isTwoPair || other.isTwoPair) ->
        if (this.isTwoPair) if (other.isTwoPair) compareByHighCard(other) else WIN else LOSS
    (this.isPair || other.isPair) ->
        if (this.isPair) if (other.isPair) compareByHighCard(other) else WIN else LOSS
    else -> compareByHighCard(other)
}

private fun compareByHighCard(other: PokerHand, index: Int = 4): Int = when {
    (index < 0) -> TIE
    cards[index].weight === other.cards[index].weight -> compareByHighCard(other, index - 1)
    cards[index].weight.ordinal > other.cards[index].weight.ordinal -> WIN
    else -> LOSS
}

}

实施细节:

  • 用已编码的手实例化,例如2H 3H 4H 5H 6H
  • 评估手是否为“同花顺”,“四类”,“满屋”等方法。这些在Kotlin中易于表达。
  • 使用简单的规则方法来实施Comparable<PokerHand>以对另一只手进行评估,例如,同花顺击败同类的四只,击败满屋等等。

来源are here

答案 10 :(得分:1)

我已经用C ++和Javascript编写了一个扑克手评估器。基本上,程序会将一组随机选择的卡转换为1s和0s的3d数组。通过将卡转换为这种格式,我便能够编写功能,从最高的手开始对每种手型进行测试。

因此,回顾一下,我的程序将生成随机卡,将它们转换为由心,钻石,黑桃和球棒组成的3D阵列,其中1代表我所拥有的卡之一。然后,我将测试3D阵列以查看是否有皇家同花顺,然后是同花顺,然后是4种,直到检测到匹配为止。一旦检测到比赛,比如说在测试同花后,那么我的程序就不必再测试顺子,3等等,因为同花会击败顺子。

以下是我的程序输出的数据:

我的随机卡片:

Table Cards
{ Value: '9', Suit: 'H' }
{ Value: 'A', Suit: 'H' }
{ Value: '9', Suit: 'D' }
{ Value: '7', Suit: 'S' }
{ Value: '6', Suit: 'S' }

代表我的卡片的3D阵列:

 A  2  3  4  5  6  7  8  9  10 J  Q  K  A

Spades
[ 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 ]
Diamonds
[ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 ]
Clubs
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
Hearts
[ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ]

使用上面的值,我可以知道我有一对9,A,7和6的踢脚。

您可以看到阵列包含Aces两次。这是因为您要测试从A开始的同花顺。所以(A,2,3,4,5)。

如果要测试7张卡而不是5张卡,则也可以使用此系统。您可以将用户2张卡和5张卡放在桌子上,并通过我的系统运行它。您也可以在桌上对其他玩家进行相同操作并比较结果。

我希望这会有所帮助。

答案 11 :(得分:0)

 public class Line
{
    private List<Card> _cardsToAnalyse;

    public Line()
    {
        Cards = new List<Card>(5);
    }

    public List<Card> Cards { get; }


    public string PriceName { get; private set; }


    public int Result()
    {
        _cardsToAnalyse = Cards;
        var valueComparer = new CardValueComparer();


        _cardsToAnalyse.Sort(valueComparer);

        if (ContainsStraightFlush(_cardsToAnalyse))
        {
            PriceName = "Straight Flush";
            return PayTable.StraightFlush;
        }

        if (ContainsFourOfAKind(_cardsToAnalyse))
        {
            PriceName = "Quadra";
            return PayTable.FourOfAKind;
        }

        if (ContainsStraight(_cardsToAnalyse))
        {
            PriceName = "Straight";
            return PayTable.Straight;
        }

        if (ContainsFullen(_cardsToAnalyse))
        {
            PriceName = "Full House";
            return PayTable.Fullen;
        }

        if (ContainsFlush(_cardsToAnalyse))
        {
            PriceName = "Flush";
            return PayTable.Flush;
        }

        if (ContainsThreeOfAKind(_cardsToAnalyse))
        {
            PriceName = "Trinca";
            return PayTable.ThreeOfAKind;
        }

        if (ContainsTwoPairs(_cardsToAnalyse))
        {
            PriceName = "Dois Pares";
            return PayTable.TwoPairs;
        }

        if (ContainsPair(_cardsToAnalyse))
        {
            PriceName = "Um Par";
            return PayTable.Pair;
        }

        return 0;
    }

    private bool ContainsFullen(List<Card> _cardsToAnalyse)
    {
        var valueOfThree = 0;

        // Search for 3 of a kind
        Card previousCard1 = null;
        Card previousCard2 = null;

        foreach (var c in Cards)
        {
            if (previousCard1 != null && previousCard2 != null)
                if (c.Value == previousCard1.Value && c.Value == previousCard2.Value)
                    valueOfThree = c.Value;
            previousCard2 = previousCard1;
            previousCard1 = c;
        }

        if (valueOfThree > 0)
        {
            Card previousCard = null;

            foreach (var c in Cards)
            {
                if (previousCard != null)
                    if (c.Value == previousCard.Value)
                        if (c.Value != valueOfThree)
                            return true;
                previousCard = c;
            }

            return false;
        }

        return false;
    }

    private bool ContainsPair(List<Card> Cards)
    {
        Card previousCard = null;

        foreach (var c in Cards)
        {
            if (previousCard != null)
                if (c.Value == previousCard.Value)
                    return true;
            previousCard = c;
        }

        return false;
    }

    private bool ContainsTwoPairs(List<Card> Cards)
    {
        Card previousCard = null;
        var countPairs = 0;
        foreach (var c in Cards)
        {
            if (previousCard != null)
                if (c.Value == previousCard.Value)
                    countPairs++;
            previousCard = c;
        }

        if (countPairs == 2)
            return true;

        return false;
    }

    private bool ContainsThreeOfAKind(List<Card> Cards)
    {
        Card previousCard1 = null;
        Card previousCard2 = null;

        foreach (var c in Cards)
        {
            if (previousCard1 != null && previousCard2 != null)
                if (c.Value == previousCard1.Value && c.Value == previousCard2.Value)
                    return true;
            previousCard2 = previousCard1;
            previousCard1 = c;
        }

        return false;
    }

    private bool ContainsFlush(List<Card> Cards)
    {
        return Cards[0].Naipe == Cards[1].Naipe &&
               Cards[0].Naipe == Cards[2].Naipe &&
               Cards[0].Naipe == Cards[3].Naipe &&
               Cards[0].Naipe == Cards[4].Naipe;
    }

    private bool ContainsStraight(List<Card> Cards)
    {
        return Cards[0].Value + 1 == Cards[1].Value &&
               Cards[1].Value + 1 == Cards[2].Value &&
               Cards[2].Value + 1 == Cards[3].Value &&
               Cards[3].Value + 1 == Cards[4].Value
               ||
               Cards[1].Value + 1 == Cards[2].Value &&
               Cards[2].Value + 1 == Cards[3].Value &&
               Cards[3].Value + 1 == Cards[4].Value &&
               Cards[4].Value == 13 && Cards[0].Value == 1;
    }

    private bool ContainsFourOfAKind(List<Card> Cards)
    {
        Card previousCard1 = null;
        Card previousCard2 = null;
        Card previousCard3 = null;

        foreach (var c in Cards)
        {
            if (previousCard1 != null && previousCard2 != null && previousCard3 != null)
                if (c.Value == previousCard1.Value &&
                    c.Value == previousCard2.Value &&
                    c.Value == previousCard3.Value)
                    return true;
            previousCard3 = previousCard2;
            previousCard2 = previousCard1;
            previousCard1 = c;
        }

        return false;
    }

    private bool ContainsStraightFlush(List<Card> Cards)
    {
        return ContainsFlush(Cards) && ContainsStraight(Cards);
    }
}