Search code examples
cpoker

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


I am trying to develop a C program that checks if there are 1 or 2 pairs in a 5 card poker hand.

I am using a 5x3 array where every line is a card (the 3rd column being for the \0 character). Every time I execute the code it always shows the "two pairs" print.

I want to make sure that each letter (i, j, a, b) representing each line is different. Any help?

P.S.: This is for a university/college project, I have only started programming a few months ago from absolute scratch, so any detailed explanations on my mistakes would be very much appreciated :)

#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");
    }
}

Solution

  • The posted code has several issues, both logical and syntactical, some have been pointed out in the comments.

    Just to pick one, consider this line

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

    I'd suggest to restart from scratch and to proceed in small steps. See, for instance, the following minimal implementation

    // 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");
        }
    }
    

    Note that my count_pairs function counts every possible pair, so if you pass three cards of the same kind, it will return 3 (given AC, AS, AD, all the possible pairs are AC AS, AC AD, AS AD).

    How to correctly calculate all the poker ranks is left to the reader.