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

#define SUITS 4
#define FACES 13

void shuffel(int deck[][FACES]);
void deal(int deck[][FACES], const char* face[], const char* suit[], int handRow[], int handCol[]);
int containsPair(int handCol[]);
int containsTwoPairs(int handCol[]);
int containsThreeOfKind(int handCol[]);
int containsFourOfKind(int handCol[]);
int containsFlush(int handRow[]);
int containsStraight(int handCol[]);


int main(void)
{
    int deck[SUITS][FACES] = { 0 };

    srand((unsigned)time(NULL));

    const char* suit[SUITS] = {
        "Hearts", "Diamonds", "Clubs", "Spades"
    };

    const char* face[FACES] = {
        "Ace", "Deuce", "Three", "Four", "Five",
        "Six", "Seven", "Eight", "Nine", "Ten",
        "Jack", "Queen", "King"
    };

    int handRow[5];
    int handCol[5];

    shuffel(deck);
    deal(deck, face, suit, handRow, handCol);

    
}

void shuffel(int deck[][FACES])
{
    for (int card = 1; card <= 52; card++)
    {
        int row, col;

        do
        {
            row = rand() % SUITS;
            col = rand() % FACES;
        } while (deck[row][col] != 0);

        deck[row][col] = card;
    }
}

void deal(int deck[][FACES], const char* face[], const char* suit[], int handRow[], int handCol[])
{
    for (int card = 1; card <= 5; card++)
    {
        for (int i = 0; i < SUITS; i++)
        {
            for (int j = 0; j < FACES; j++)
            {
                if (deck[i][j] == card)
                {
                    printf("%5s of %-8s  %c", face[j], suit[i],
                        card % 5 == 0 ? '\n' : ' ');
                    handRow[card - 1] = i;
                    handCol[card - 1] = j;
                }
            }
        }
       
    }
}

int containsPair(int handCol[])
{
    for (int card = 0; card < 5; card++)
    {
        for (int i = card + 1; i < 5; i++)
        {
            if (handCol[card] == handCol[i])
            {
                return 1;
            }
        }
    }
    return 0;
}

int containsTwoPairs(int handCol[])
{
    int count[13] = { 0 };
    int pairs = 0;

    for (int i = 0; i < 5; i++)
    {
        count[handCol[i]]++;
    }

    for (int i = 0; i < 13; i++)
    {
        if (count[i] == 2)
        {
            pairs++;
        }
    }

    return pairs == 2;
}

int containsThreeOfKind(int handCol[])
{
    int count[13] = { 0 };

    for (int i = 0; i < 5; i++)
    {
        count[handCol[i]]++;
    }

    for (int i = 0; i < 13; i++)
    {
        if (count[i] == 3)
        {
            return 1;

        }
    }

    return 0;
}

int containsFourOfKind(int handCol[])
{
    int count[13] = { 0 };

    for (int i = 0; i < 5; i++)
    {
        count[handCol[i]]++;
    }

    for (int i = 0; i < 13; i++)
    {
        if (count[i] == 4)
        {
            return 1;

        }
    }

    return 0;
}

int containsFlush(int handRow[])
{
    
    for (int i = 1; i < 5; i++)
    {
        if (handRow[i] != handRow[0])
        {
            return 0;
        }
    }
    return 1;
}


int containsStraight(int handCol[])
{
    int temp[5];

    
    for (int i = 0; i < 5; i++)
    {
        temp[i] = handCol[i];
    }

    
    for (int i = 0; i < 4; i++)
    {
        for (int j = i + 1; j < 5; j++)
        {
            if (temp[i] > temp[j])
            {
                int swap = temp[i];
                temp[i] = temp[j];
                temp[j] = swap;
            }
        }
    }

    
    for (int i = 0; i < 4; i++)
    {
        if (temp[i + 1] != temp[i] + 1)
        {
            return 0;
        }
    }

    return 1;
}
