如何查看 C 中的一手牌中是否有 1 对或 2 对扑克牌

how to see if there are 1 or 2 poker pairs in a hand in C

我正在尝试开发一个 C 程序来检查 5 张扑克牌中是否有 1 对或 2 对。

我正在使用一个 5x3 数组,其中每一行都是一张卡片(第 3 列用于 \0 字符)。每次我执行代码时,它总是显示 "two pairs" 打印。

我想确保代表每一行的每个字母 (i, j, a, b) 都是不同的。有帮助吗?

P.S.: 这是一个 university/college 项目,我几个月前才开始编程,完全是从头开始,所以非常感谢对我的错误的任何详细解释:)

#include <stdio.h>
#include <stdlib.h>

char (cards[5][3])=
{
    "5S", "6D", "4H", "KD", "5C"
};

int main ()
{
    pair (cards[5][3]);
    return 0;
}

void pair (char (arg[n][0]))
{
    int i,j,a,b;

    if (i!=j!=a!=b)
    {
        if ((arg[i][0]==arg[a][0])&&(arg[b][0]!=arg[j][0]))
        {
            printf("2 -> pair");
        }

        if ((arg[i][0]==arg[a][0])&&(arg[b][0]==arg[j][0]));
        {
            printf("3 -> two pairs");
        }

        if ((arg[i][0]!=arg[a][0])&&(arg[b][0]!=arg[j][0]))
        {
            printf("there is no pair");
        }
    }
    else
    {
        printf("there is no pair");
    }
}

可以对配对功能进行重大改进,使其更简洁。但是,这回答了您的问题并解决了几个极端情况:

#include <stdio.h>
#include <stdlib.h>

void pairCheck(char hand[][2])
{
    int pairCount = 0;
    int tmpCount = 0;
    char tmpCard = '0';
    char foundPairs[2] = {0};

    // Check Hand One
    for(int i =0; i < 5; i++)
    {
        tmpCard = hand[i][0];
        for(int j = 0; j < 5; j++)
        {
            if(tmpCard ==  hand[j][0] && i != j)
            {

                tmpCount++;
            }

            if(tmpCount == 1 && (tmpCard != foundPairs[0] && tmpCard != foundPairs[1]))
            {
                foundPairs[pairCount] = tmpCard;
                pairCount++;
            }

            tmpCount = 0;
        }
    }

    printf("Pair Count Hand One: %i\r\n",pairCount);

    //Reset Variables
    foundPairs[0] = 0;
    foundPairs[1] = 0;
    tmpCard = '0';
    pairCount = 0;
        // Check Hand One
    for(int i =0; i < 5; i++)
    {
        tmpCard = hand[i][1];
        for(int j = 0; j < 5; j++)
        {
            if(tmpCard ==  hand[j][1] && i != j)
            {
                tmpCount++;
            }

            if(tmpCount == 1 && (tmpCard != foundPairs[0] && tmpCard != foundPairs[1]))
            {
                foundPairs[pairCount] = tmpCard;
                pairCount++;
            }

            tmpCount = 0;
        }
    }

    printf("Pair Count Hand Two: %i",pairCount);
}

int main ()
{
    char cards[5][2] = { {'5','H'},{'6','D'},{'4','H'},{'K','D'},{'5','C'}};
    pairCheck(cards);
    return 0;
}

此函数会将三个、四个或五个相同的对象视为一对。如果你想要不同的行为,改变应该很容易。

发布的代码有几个问题,包括逻辑和语法方面的问题,其中一些已在评论中指出。

只是挑一个,考虑这条线

if ((arg[i][0]==arg[a][0])&&(arg[b][0]==arg[j][0]));
{
     // This body will never be executed           ^
}

我建议从头开始,循序渐进。例如,请参阅以下最小实现

// Include all the needed header files, not the unneeded ones.
#include <stdio.h>

// Declare the functions prototype before their use, they will be defined after.
int count_pairs(int n, char const cards[][3]);
// Always specify the inner size,         ^  when passing a multidimensional array
void show_score(int n_pairs);
int have_the_same_value(char const *a, char const *b);

int main (void)
{
    char hand[5][3] = {
    //       ^^^^^^ You could omit the 5, here
        "5S", "6D", "4H", "KD", "5C"
    };

    int n_pairs = count_pairs(5, hand);
    // Always pass the size   ^ if there isn't a sentinel value in the array

    show_score(n_pairs);
    return 0;
}

// This is a simple O(n^2) algorithm. Surely not the best, but it's
// a testable starting point. 
int count_pairs(int n, char const cards[][3])
{
    // Always initialize the variables.
    int count = 0;

    // Pick every card...
    for (int i = 0; i < n; ++i)
    {
        // Compare (only once) with all the remaining others.
        for (int j = i + 1; j < n; ++j)
        { //         ^^^^^
            if ( have_the_same_value(cards[i], cards[j]) ) {
                ++count;
            }
        }
    }
    return count;
}

int have_the_same_value(char const *a, char const *b)
{
    return a[0] == b[0];
}

// Interpret the result of count_pairs outputting the score
void show_score(int n_pairs)
{
    switch (n_pairs)
    {
        case 1:
            printf("one pair.\n");
            break;
        case 2:
            printf("two pairs.\n");
            break;
        case 3:
            printf("three of a kind.\n");
            break;
        case 4:
            printf("full house.\n");
            break;
        case 6:
            printf("four of a kind.\n");
            break;
        default:
            printf("no pairs.\n");
    }
}

请注意,我的 count_pairs 函数会计算 所有可能的 对,因此如果您传递三张相同类型的牌,它将 return 3(给定AC、AS、AD,所有可能的对都是AC AS、AC AD、AS AD)。

如何正确计算所有的扑克排名留给reader。