Java 中的扑克手牌

发布于 2024-10-20 14:47:31 字数 55 浏览 1 评论 0原文

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

Is there any easy method to compare two poker hands? I'm going about this pretty naively, so if anybody has experience doing this, it could be helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

妳是的陽光 2024-10-27 14:47:31

没有一种简单(且高效)的方法可以做到这一点,特别是如果您想评估七张牌的五张牌子集,例如德州扑克。

您可能想查看 pokersource ,它有一些 Java 绑定。

编辑 - 另一个资源是pokerai 论坛。我在那里发现了很多 Java 算法。它们定期运行基准测试,并且通常伴随着蒙特卡罗模拟的枚举算法。 似乎是主要帖子。

There isn't an easy (and performant) way to do this, especially if you want to evaluate five-card subsets of seven-card hands, like in Texas Hold 'em.

You might want to check out pokersource which has some Java bindings.

Edit - an additional resource is the pokerai forums. I found quite a few Java algorithms there. They are regularly run through benchmarks and are often accompanied by enumeration algorithms for Monte Carlo simulation. This appears to be the main post.

顾忌 2024-10-27 14:47:31

我发现这个看起来很有希望。

您也可以自己推出。

I found this which seems promising.

You can also roll your own.

音盲 2024-10-27 14:47:31

您可以在此处 附有文档并在此处进一步解释。欢迎通过其中的电子邮件地址提供所有反馈。

An example of a ready made Texas Hold'em 7- and 5-card evaluator can be found here with documentation and further explained here. All feedback welcome at the e-mail address found therein.

无声情话 2024-10-27 14:47:31

看看来自阿尔伯塔大学的免费扑克 Java 库:
http://spaz.ca/poker/doc/ca /ualberta/cs/poker/package-summary.html

Look at this free poker java lib from Alberta university :
http://spaz.ca/poker/doc/ca/ualberta/cs/poker/package-summary.html

明媚殇 2024-10-27 14:47:31

假设您已经拥有某种数据结构来确定玩家拥有的手牌类型,您可以为每种类型的手牌分配某种 int 值并进行比较。任何拥有更高价值的玩家都会获胜。如果值相等,您只需比较两手牌即可。即,如果双方都有对子,则只需比较卡片的数量,或者如果他们都有葫芦,则比较他们拥有 3 张卡片的价值。

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

Assuming you already have some kind of data structure in place for determining the kind of hand that the player has, you could just assign some kind of int value to each type of hand and compare them. Whatever player has the higher value wins. If the values are equal, you'd just have to compare the hands against eachother. I.E if both have pairs, just compare the numbers of the card, or if they have a full house, compare the value of the card that they have 3 of.

EDIT: if they have the same hand, just check the next highest card in their hand (kicker).

在你怀里撒娇 2024-10-27 14:47:31

我为 java 编写了一个扑克牌评估库,它正是您所寻找的。
这并不容易,但它很高效。我在 GitHub 上根据 GNU GPL 发布了它。

在此处获取源代码

I wrote a poker hand evaluation library for java, that does exactly what your looking for.
It wasn't easy but it is performant. I released it under the GNU GPL on GitHub.

get sourecode here

﹏雨一样淡蓝的深情 2024-10-27 14:47:31

我写了这个,如果有帮助请告诉我。本质上,我将每张牌映射到黑桃 2 为 0,黑桃 A 为 12,红心 2 为 3 等等。
我希望在投入工作之前看到这个线程。

  /**
     * 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;
    }

I wrote this, let me know if this helps. Essentially I map each card to a number 2 of spades being 0,and Ace of spades being a 12 and 2 of hearts a 3 etc etc.
I wish I saw this thread before I put in the work.

  /**
     * 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;
    }

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文