扑克算法手牌评估器

发布于 2024-12-20 00:23:01 字数 1428 浏览 3 评论 0原文

我正在努力让我的扑克来评估玩家的手牌。我可以使用同花和 J 或更好的组合来工作,但在弄清楚如何完成其​​余部分时遇到了问题。关于如何将卡片评估为适当的类型有什么建议吗?

int Game::HandCheck()
{
    //ROYAL FLUSH
    //return 9;

    //STRAIGHT FLUSH
    //return 8;

    //FOUR OF A KIND
    //return 7;

    //FULL HOUSE
    //return 6;

    //FLUSH
    if( currentHand[ 0 ].GetSuit() == currentHand[ 1 ].GetSuit() && currentHand[ 1 ].GetSuit() == currentHand[ 2 ].GetSuit() &&
        currentHand[ 2 ].GetSuit() == currentHand[ 3 ].GetSuit() && currentHand[ 4 ].GetSuit() == currentHand[ 4 ].GetSuit() ) 
        return 5;

    //STRAIGHT
    //return 4;

    //THREE OF A KIND
    if( currentHand[ 0 ].GetValue() == currentHand[ 2 ].GetValue() && currentHand[ 0 ].GetValue() == currentHand[ 3 ].GetValue()
    //return 3;

    //TWO PAIR
    //return 2;

    //JACKS OR BETTER PAIR
    for( int i = 0; i < 5; i++ )
    {
        if( currentHand[ i ].GetValue() == 11 || currentHand[ i ].GetValue() == 12 || currentHand[ i ].GetValue() == 13 || currentHand[ i ].GetValue() == 1 )
        {
            if( currentHand[ i ].GetValue() == currentHand[ i + 1 ].GetValue() || currentHand[ i ].GetValue() == currentHand[ i + 2 ].GetValue() || //pair
            currentHand[ i ].GetValue() == currentHand[ i + 3 ].GetValue() || currentHand[ i ].GetValue() == currentHand[ i + 4 ].GetValue() )
            return 1;
        }
    }

    return 0;

}

I'm working on getting my Poker to evaluate the player hand. I can get the Flush and the jack or better pair to work but am running into problems figuring out how I would do the rest. Any suggestions on how I can evaluate the cards to the appropriate type?

int Game::HandCheck()
{
    //ROYAL FLUSH
    //return 9;

    //STRAIGHT FLUSH
    //return 8;

    //FOUR OF A KIND
    //return 7;

    //FULL HOUSE
    //return 6;

    //FLUSH
    if( currentHand[ 0 ].GetSuit() == currentHand[ 1 ].GetSuit() && currentHand[ 1 ].GetSuit() == currentHand[ 2 ].GetSuit() &&
        currentHand[ 2 ].GetSuit() == currentHand[ 3 ].GetSuit() && currentHand[ 4 ].GetSuit() == currentHand[ 4 ].GetSuit() ) 
        return 5;

    //STRAIGHT
    //return 4;

    //THREE OF A KIND
    if( currentHand[ 0 ].GetValue() == currentHand[ 2 ].GetValue() && currentHand[ 0 ].GetValue() == currentHand[ 3 ].GetValue()
    //return 3;

    //TWO PAIR
    //return 2;

    //JACKS OR BETTER PAIR
    for( int i = 0; i < 5; i++ )
    {
        if( currentHand[ i ].GetValue() == 11 || currentHand[ i ].GetValue() == 12 || currentHand[ i ].GetValue() == 13 || currentHand[ i ].GetValue() == 1 )
        {
            if( currentHand[ i ].GetValue() == currentHand[ i + 1 ].GetValue() || currentHand[ i ].GetValue() == currentHand[ i + 2 ].GetValue() || //pair
            currentHand[ i ].GetValue() == currentHand[ i + 3 ].GetValue() || currentHand[ i ].GetValue() == currentHand[ i + 4 ].GetValue() )
            return 1;
        }
    }

    return 0;

}

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

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

发布评论

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

评论(2

眼泪也成诗 2024-12-27 00:23:01

我将创建一个直方图数组来进行计数。

填充数组。
如果恰好一个桶的计数为两个或更多,则您有一对、三趟或四趟。
如果两个桶的计数为二或更多,则您有两对或葫芦。

I would create a histogram array to do the counting.

Fill the array.
If exactly one bucket has of count of two or more, you have a pair, trips or four of a kind.
If two buckets have a count of two or more, you have two pair or a full house.

猥︴琐丶欲为 2024-12-27 00:23:01

您的 Flush 算法正在将 4 与自身进行比较:

//FLUSH
if( currentHand[ 0 ].GetSuit() == currentHand[ 1 ].GetSuit() && currentHand[ 1 ].GetSuit() == currentHand[ 2 ].GetSuit() &&
    currentHand[ 2 ].GetSuit() == currentHand[ 3 ].GetSuit() && currentHand[ 4 ].GetSuit() == currentHand[ 4 ].GetSuit() ) 
    return 5;
                                                                            ^^^---------------------------^^^

您的三类算法假设所有三个值都位于前三个位置:

//THREE OF A KIND
if( currentHand[ 0 ].GetValue() == currentHand[ 2 ].GetValue() && currentHand[ 0 ].GetValue() == currentHand[ 3 ].GetValue()
//return 3;

也许三个相同的值存储在手牌的第一张、中间和最后一张牌中。

我不知道传统的扑克牌评估器是如何编写的,但我感觉它们执行了一些额外的计算:创建一个数组,按牌值索引,存储牌数的计数该值的:

int reverse_hand[13] = {0};

for (int i=0; i<HAND_SIZE; i++) {
    reverse_hand[currentHand[i].getValue()] += 1;
}

int pairs[2] = {-1, -1};
int pairs_count = 0;
int triple = -1;
int quad = -1;

int straight_pos = -1;

for (int i=0; i<13; i++) {
    count = reverse_hand[i];
    if (count == 2) {
        pairs[pairs_count++] = i;
    } else if (count == 3) {
        triple = i;
    } else if (count == 4) {
        quad = i;
    } else if (count == 1) {
        if ((i < (13-5)) && (reverse_hand[i] == 1) &&
                            (reverse_hand[i+1] == 1) &&
                            (reverse_hand[i+2] == 1) &&
                            (reverse_hand[i+3] == 1) &&
                            (reverse_hand[i+4] == 1)) {
            straight_pos = i;
    }
}

if (pairs_count == 2) {
    printf("You had two pairs, %d low and %d high\n", pairs[0], pairs[1]);
} else if (pairs_count == 1) {
    printf("YOu had one pair of %d\n", pairs[0]);
}

if (triple >= 0) {
    printf("You had a three of a kind: %d\n", triple);
}

if (quad >= 0) {
    printf("You had a four of a kind: %d\n", quad);
}

if (pairs_count == 1 && triple >=0) {
   printf("Full house, three %d and two %d\n", triple, pairs[0]);
}

/* code here for flush detection */

if (straight_pos >= 0) {
   int p = straight_pos;
   /* if straight flush print a different message */
   printf("Straight, %d %d %d %d %d\n", p, p+1, p+2, p+3, p+4);
}

Your Flush algorithm is comparing 4 against itself:

//FLUSH
if( currentHand[ 0 ].GetSuit() == currentHand[ 1 ].GetSuit() && currentHand[ 1 ].GetSuit() == currentHand[ 2 ].GetSuit() &&
    currentHand[ 2 ].GetSuit() == currentHand[ 3 ].GetSuit() && currentHand[ 4 ].GetSuit() == currentHand[ 4 ].GetSuit() ) 
    return 5;
                                                                            ^^^---------------------------^^^

Your three-of-a-kind algorithm is assuming that all three values are in the first three positions:

//THREE OF A KIND
if( currentHand[ 0 ].GetValue() == currentHand[ 2 ].GetValue() && currentHand[ 0 ].GetValue() == currentHand[ 3 ].GetValue()
//return 3;

Perhaps the three identical values are stored in the first, middle, and last cards in the hand.

I don't know how traditional poker hand evaluators are written, but I have a feeling that they perform some extra computations: create an array, indexed by card value, that stores the count of the number of cards of that value:

int reverse_hand[13] = {0};

for (int i=0; i<HAND_SIZE; i++) {
    reverse_hand[currentHand[i].getValue()] += 1;
}

int pairs[2] = {-1, -1};
int pairs_count = 0;
int triple = -1;
int quad = -1;

int straight_pos = -1;

for (int i=0; i<13; i++) {
    count = reverse_hand[i];
    if (count == 2) {
        pairs[pairs_count++] = i;
    } else if (count == 3) {
        triple = i;
    } else if (count == 4) {
        quad = i;
    } else if (count == 1) {
        if ((i < (13-5)) && (reverse_hand[i] == 1) &&
                            (reverse_hand[i+1] == 1) &&
                            (reverse_hand[i+2] == 1) &&
                            (reverse_hand[i+3] == 1) &&
                            (reverse_hand[i+4] == 1)) {
            straight_pos = i;
    }
}

if (pairs_count == 2) {
    printf("You had two pairs, %d low and %d high\n", pairs[0], pairs[1]);
} else if (pairs_count == 1) {
    printf("YOu had one pair of %d\n", pairs[0]);
}

if (triple >= 0) {
    printf("You had a three of a kind: %d\n", triple);
}

if (quad >= 0) {
    printf("You had a four of a kind: %d\n", quad);
}

if (pairs_count == 1 && triple >=0) {
   printf("Full house, three %d and two %d\n", triple, pairs[0]);
}

/* code here for flush detection */

if (straight_pos >= 0) {
   int p = straight_pos;
   /* if straight flush print a different message */
   printf("Straight, %d %d %d %d %d\n", p, p+1, p+2, p+3, p+4);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文