Java中的扑克之手

时间:2011-03-05 18:41:18

标签: java poker

有没有简单的方法来比较两个扑克牌?我很天真地谈论这个,所以如果有人有这方面的经验,那可能会有所帮助。

8 个答案:

答案 0 :(得分:6)

没有一种简单(高效)的方法可以做到这一点,特别是如果你想评估七张牌的五张牌子牌,就像在德州扑克中一样。

您可能想要查看具有一些Java绑定的pokersource

修改 - 其他资源是pokerai forums。我在那里发现了很多Java算法。它们经常通过基准测试运行,并且通常伴随着蒙特卡罗模拟的枚举算法。 This似乎是主要职位。

答案 1 :(得分:3)

我发现this看起来很有希望。

你也可以自己动手。

答案 2 :(得分:2)

看看这个来自艾伯塔大学的免费扑克java lib: http://spaz.ca/poker/doc/ca/ualberta/cs/poker/package-summary.html

答案 3 :(得分:2)

(无耻的插件)你可以使用我的库轻松地完成这项工作

https://github.com/ableiten/foldem

这样做的代码如下所示:

Evaluator evaluator = new DefaultEvaluator();
Board board = board("AcTs4d3h3s");

Hand a = hand("AsTd");
Hand b = hand("TcJh");

if (evaluator.rank(a, board) > evaluator.rank(b, board)) {
     System.out.println("Hand A wins!");
} else {
     System.out.println("Hand B wins (or draws)!");
}

还支持基于模拟的权益计算和基本的翻牌后分析。

答案 4 :(得分:1)

假设您已经有某种数据结构来确定玩家拥有的手牌类型,您可以为每种类型的手分配某种int值并进行比较。无论哪个玩家拥有更高价值的胜利。如果值相等,则只需将双手相互比较即可。 I.E如果两者都有配对,只需比较一下卡的编号,或者如果他们有一个完整的房子,则比较他们有3张卡的价值。

编辑:如果他们有相同的牌,只需检查他们手中的下一张牌(踢球者)。

答案 5 :(得分:1)

可以找到现成的德州扑克7和5卡评估员的示例here,其中包含文档并进一步解释here。所有反馈都欢迎来自其中的电子邮件地址。

答案 6 :(得分:1)

我为java编写了一个扑克手评估库,它正是你想要的。 这并不容易,但它是高效的。我在GitHub上的GNU GPL下发布了它。

get sourecode here

答案 7 :(得分:1)

我写了这个,请告诉我这是否有帮助。基本上我将每张牌映射到2号黑桃为0,黑桃王牌为12和2心三等等。 我希望在我投入工作之前看到这个帖子。



  /**
     * Takes any subset of numberCards and creates an array which directly says
     * how good the cards are This method is long so If it is working try not to
     * tweak it TODO potential re-factor
     *
     * @todo Fix this @fixMe
     * @param cards to analyze
     * @return condensed form of hand in the format(tier, High Card, High Card
     * 2,Kicker(s),sometimes some zeroes)
     */
    public static int[] analyzeCards(int[] cards) {
        int suitFlush = 0;
        int[] unsuitedCards = new int[cards.length];
        for (int i = 0; i < cards.length; i++) {
            unsuitedCards[i] = (cards[i] % 13);
        }
                Arrays.sort(unsuitedCards);
        unsuitedCards = reverse(unsuitedCards);
 //converting cards into just numbers 0-12 in descending order

        
        int[] lookForPairsHere=unsuitedCards.clone();

//        System.out.println(Arrays.toString(unsuitedCards));
        //defining the suits of the cards 0,1,2,3
        // and the suits are in order of the cards
        int[] suitsOfCards = new int[cards.length];
        for (int i = 0; i < cards.length; i++) {
            suitsOfCards[i] = cards[i] / 13;
        }
        int[] numberOfEachValuedCard = {0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0};
        int[] pairs = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

        lookForPairsHere = unsuitedCards.clone();

        int[] originalCards = cards.clone();
        int[] condensedCards = {0, -1, 0, 0, 0, 0};
        int[] suits = {0, 0, 0, 0};
        boolean[] hasQuality = {false, false}; // hasQuality[0] is flush and 
        //hasQuality[1] is a straight
        for (int i = 0; i < suitsOfCards.length; i++) {
            suits[suitsOfCards[i]]++;
        }
        for (int i = 0; i < 4; i++) {
            if (suits[i] > 4) {
                hasQuality[0] = true;
//                System.out.println("flush");
                // there is a flush!
                suitFlush = i;
                }
        }
int[] straightCards = {-2, -2, -2, -2, -2, -2, -2};
        int i = 0;
        int j = 1;
        int card = 0;
//        some iterators I will need to keep track of temporarily
//         i is just an iterator j is the number of consecuetive cards

if(hasQuality[0]){
        for (i = 0; i <originalCards.length; i++) {
                if (originalCards[i]/13 == suitFlush) {
                    straightCards[i] = originalCards[i];
                }
            }
Arrays.sort(straightCards);
straightCards=reverse(straightCards);
//sorting flushCards in order;
        unsuitedCards = removeDuplicates(unsuitedCards);
        // getting rid of duplicates to prevent trouble
condensedCards[1]=straightCards[0]%13;
}
        if (/*there is not a flush already*/!hasQuality[0]) {
                for(i=0;(i<unsuitedCards.length-5);i++){
                    if(unsuitedCards[i]-1==unsuitedCards[i+1]&&
                            unsuitedCards[i]-2==unsuitedCards[i+2]&&
                            unsuitedCards[i]-3==unsuitedCards[i+3]&&
                            unsuitedCards[i]-4==unsuitedCards[i+4]){
                        hasQuality[1]=true;
                        condensedCards[1]=unsuitedCards[i];
                }
                }
            if (hasQuality[1] == false) {
                int b = 0;
                if (unsuitedCards[0] == 12) {
                    //looks for ace-5 flush
                    for (i = 0; i < unsuitedCards.length; i++) {
                        if (unsuitedCards[i] == 3 || unsuitedCards[i] == 2 || unsuitedCards[i] == 1
                                || unsuitedCards[i] == 0) {
                            b++;
                        }
                    }
                    if (b>3) {
                        hasQuality[1] = true;
                        //there is an ace-5 flush
                        condensedCards[1] = 3;
                    }
                }
            }
        } else/*there is a flush already so a straight is suited*/ {
            Arrays.sort(straightCards);
            straightCards = reverse(straightCards);
            //order it
            // look for A normal straight    
            for(i=0;(i<straightCards.length-5);i++){
                    if(straightCards[i]-1==straightCards[i+1]&&
                            straightCards[i]-2==straightCards[i+2]&&
                            straightCards[i]-3==straightCards[i+3]&&
                            straightCards[i]-4==straightCards[i+4]){
                        hasQuality[1]=true;
                        condensedCards[1]=straightCards[i];
                }
            }
            if (hasQuality[1] == false) {
                int b = 0;
                if (straightCards[0] == 12) {
                    //looks for ace-5 flush
                    for (int z = 0; (z< straightCards.length); z++){
                        if (straightCards[z] == 3 || straightCards[z] == 2 || 
                                straightCards[z] == 1
                                ||straightCards[z] == 0) {
                            b++;
                        }
                    }
                    if (b>3) {
                        hasQuality[1] = true;
                        //there is an ace-5 flush
                        condensedCards[1] = 3;
                    }
                }
            }
        }
        if (hasQuality[1] && hasQuality[0]) {
            condensedCards[0] = 8;
            return condensedCards;
        }
        if (hasQuality[1] && !hasQuality[0]) {
            condensedCards[0] = 4;
            return condensedCards;
        }
        if (hasQuality[0] && !hasQuality[1]) {
            condensedCards[0] = 5;
            for(i=2;i<=5;i++){
                condensedCards[i]=straightCards[i-1];
            }
            return condensedCards;
        }
        card = 0;

        for (i = 0; i < lookForPairsHere.length; i++) {
            numberOfEachValuedCard[lookForPairsHere[i]]++;
        }
        for (i = 12; i >= 0 && card < 5; i--) {
            // create an array that has how much of each card
            if (numberOfEachValuedCard[i] > 0) {
                pairs[2 * card] = numberOfEachValuedCard[i];
                pairs[2 * card + 1] = i;
                card++;
            }
        }

//parse the array above
        int[] pairCondensed = {0, 0, 0, 0, 0};
        for (i = 0; i < 5; i++) {
            pairCondensed[i] = pairs[2 * i];
        }
        //highest to lowest
        Arrays.sort(pairCondensed);
        pairCondensed = reverse(pairCondensed);

        if (pairCondensed[0] == 4 && pairCondensed[1] == 1) {
            //four of a kind
            condensedCards[0] = 7;
            for (i = 0; pairs[i] > 0; i += 2) {
                if (pairs[i] == 4) {
                    condensedCards[1] = pairs[i + 1];
                }
                for (j = 0; j < 10; j += 2) {
                    if (pairs[j] == 1) {
                        condensedCards[2] = pairs[j + 1];
                        break;
                    }
                }
            }
            return condensedCards;
        }
        if (pairCondensed[0] == 3 && pairCondensed[1] == 2) {
            //full house
            condensedCards[0] = 6;
            for (i = 0; i < 10; i += 2) {
                if (pairs[i] == 3) {
                    condensedCards[1] = pairs[i + 1];
                }
                if (pairs[i] == 2) {
                    condensedCards[2] = pairs[i + 1];
                }
            }
            return condensedCards;
        }
        if (pairCondensed[0] == 3 && pairCondensed[1] == 1) {
            //three kind
            condensedCards[0] = 3;
            for (i = 0; i < 10; i += 2) {
                if (pairs[i] == 3) {
                    condensedCards[1] = pairs[i + 1];
                }
            }

            //kicker stuff here
            j = 3;
            for (i = 0; j < 5; i++) {
                if (unsuitedCards[i] != condensedCards[1]) {
                    condensedCards[j] = unsuitedCards[i];
                    j++;
                }
            }
        }
        if (pairCondensed[0] == 2 && pairCondensed[1] == 2) {

//two pair
            condensedCards[0] = 2;
            for (i = 0; i < 10; i += 2) {
                if (pairs[i] == 2 && condensedCards[1] == -1) {
                    condensedCards[1] = pairs[i + 1];
                }
                if (pairs[i] == 2 && condensedCards[1] != -1) {
                    condensedCards[2] = pairs[i + 1];
                }
            }
            if (condensedCards[2] > condensedCards[1]) {
                int temp2 = condensedCards[2];
                int temp1 = condensedCards[1];
                condensedCards[1] = temp2;
                condensedCards[2] = temp1;
            }

            //kicker stuff here
            j = 3;
            for (i = 0; j < 4; i++) {
                if (unsuitedCards[i]
                        != condensedCards[1] && unsuitedCards[i]
                        != condensedCards[2]) {
                    condensedCards[j] = unsuitedCards[i];
                    j++;
                }
            }
        }
        if (pairCondensed[0] == 2&&pairCondensed[1]==1) {
            //one pair
            condensedCards[0] = 1;
            for (i = 0; i < 10; i += 2) {

                if (pairs[i] == 2) {
                    condensedCards[1] = pairs[i + 1];
                }
            }
            //kicker stuff here
            j = 3;
            for (i = 0; j < 6 && i < 5; i++) {
                if (unsuitedCards[i] != condensedCards[1]) {
                    condensedCards[j] = unsuitedCards[i];
                    j++;
                }
            }
        }

        if (condensedCards[0] == 0) {
            //high card/ no pair
            for (i = 0,j=1; j <=5 && i < originalCards.length; i++) {
condensedCards[j]=lookForPairsHere[i];
                j++;
            }
        }

        return condensedCards;
    }
&#13;
&#13;
&#13;