0 Replies - 331 Views - Last Post: 03 September 2009 - 10:10 AM Rate Topic: -----

#1 Nizbel99   User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 117
  • Joined: 19-May 09

Stack Of Cards

Posted 03 September 2009 - 10:10 AM

Description: See Comments - Pretty Simple To UseC Utility Functions To Manage A Deck Of Playing Cards. (Can Easily Be Transformed In A C++ Class If Needed)
/* Implementation Of A Deck Of Cards
By: Zachary Frenette - Utility Functions */

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

/* Declare The Suit Of Each Card */
enum suit {CLUBS, DIAMONDS, HEARTS, SPADES, JOKER};

/* Structure For A Playing Card */
struct Card {
    int rank;
    bool isJoker;
    enum suit cardSuit;
};

/* Structure For A Deck Of Cards */
struct Deck {
    int size;
    struct Card* topCard;
    struct Deck* nextCard;
};

/* Function That Creates A Joker */
struct Card* MakeJoker()
{
    /* Create A New Card */
    struct Card* newCard = malloc(sizeof(struct Card));

    if(newCard == NULL)
    {
        printf("Could not allocate enough memory.n");
        return NULL;
    }

    /* Assign The Joker Values */
    newCard->rank = 0;
    newCard->isJoker = true;
    newCard->cardSuit = JOKER;

    return newCard;
}

/* Function That Makes A Playing Card */
struct Card* MakeCard(int value, enum suit cardSuit)
{
    /* Create A New Card */
    struct Card* newCard = malloc(sizeof(struct Card));

    if(newCard == NULL)
    {
        printf("Could not allocate enough memory.n");
        return NULL;
    }

    /* Assign The Desired Values */
    newCard->rank = value;
    newCard->isJoker = false;
    newCard->cardSuit = cardSuit;

    return newCard;
}

/* Function That Creates A Deck Of Cards (52 Cards) */
struct Deck* MakeDeck()
{
    /* Create A New Deck Of Cards */
    struct Deck* newDeck = NULL;
    struct Card* tempCard = NULL;

    /* Add Each Card To The Deck */
    for(int i = CLUBS; i <= SPADES; i++)
    {
        for(int j = 1; j <= 13; j++)
        {
            tempCard = MakeCard(j, i);
            push_card(&newDeck, tempCard);
        }
    }

    return newDeck;
}

/* Function That Frees The Memory Allocated
From A Playing Card */
void FreeCard(struct Card* c)
{
    /* Free The Memory */
    free(c);
}

/* Function That Frees The Memory Allocated
From A Deck Of Cards */
void FreeDeck(struct Deck* d)
{
    struct Deck* tempDeck = d;

    /* Free The Memory */
    while(d != NULL)
    {
        FreeCard(d->topCard);
        tempDeck = tempDeck->nextCard;
        free(d);
        d = tempDeck;
    }
}

/* Function That Checks If The Deck Is Empty */
bool is_empty(struct Deck* d)
{
    /* Check If The Deck Is Empty */
    if(d == NULL)
    {
        return true;
    }

    else
    {
        return false;
    }
}

/* Function That Checks If The Deck Is Full (52 Cards) */
bool is_full(struct Deck* d)
{
    /* Check If The Deck Is Full */
    if((d != NULL) && (d->size == 52))
    {
        return true;
    }

    else
    {
        return false;
    }
}

/* Function That Checks To See If A Card Is In The Deck
Of The Same Name And Of The Same Suit */
bool find_card(struct Deck* d, struct Card* c)
{
    struct Deck* tempDeck = d;

    /* Check Each Card In The Deck */
    while(tempDeck != NULL)
    {
        if(tempDeck->topCard->rank == c->rank)
        {
            if(tempDeck->topCard->cardSuit == c->cardSuit)
            {
                /* The Card Was Found */
                return true;
            }
        }

        tempDeck = tempDeck->nextCard;
    }

    /* The Card Was Not Found */
    return false;
}

void shuffle(struct Deck** d)
{
    /* Get The Size Of The Original Deck */
    int size = (*d)->size; /* Usually Be 52 For A Full Deck */

    /* Create An Array Of Cards */
    struct Card tempDeck[size];
    struct Card tempCard;

    /* Assign Each Pointer In An Element Of The Array */
    for(int i = 0; i < size; i++)
    {
        tempCard = pop_card(d);
        tempDeck[i] = tempCard;
    }

    /* Seed The Random Numbers, & Create Two Indexes */
    srand((unsigned)time(NULL));
    int index1 = 0;
    int index2 = 0;

    /* Shuffle The Cards */
    for(int i = 0; i < 1000; i++)
    {
        index1 = rand() % size;
        index2 = rand() % size;

        tempCard = tempDeck[index1];

        tempDeck[index1] = tempDeck[index2];
        tempDeck[index2] = tempCard;
    }

    struct Card* newCard = NULL;

    /* Re-Create The Deck */
    for(int i = 0; i < size; i++)
    {
        newCard = MakeCard(tempDeck[i].rank, tempDeck[i].cardSuit);
        push_card(d, newCard);
    }
}

/* Function That Adds A Card To The Top Of The Deck */
void push_card(struct Deck** d, struct Card* c)
{
    /* Check If The Deck Is Full */
    if(is_full(*d))
    {
        printf("The Deck Of Cards Is Full.n");
        return;
    }

    /* Add The New Card, & Modify The Pointer */
    struct Deck* newDeck = malloc(sizeof(struct Deck));

    if(*d == NULL)
    {
        newDeck->topCard = c;
        newDeck->nextCard = *d;
        newDeck->size = 1;
    }

    else
    {
        newDeck->topCard= c;
        newDeck->nextCard = *d;
        newDeck->size = ((*d)->size + 1);
    }

    *d = newDeck;
}

/* Function That Returns The Top Card, And
Removes It From The Deck */
struct Card pop_card(struct Deck** d)
{
    /* Check If The Deck Is Empty */
    if(is_empty(*d))
    {
        printf("The Deck Of Cards Is Empty.n");
        abort();
    }

    /* Create A Card To Return */
    struct Card c;
    c.rank = (*d)->topCard->rank;
    c.isJoker = (*d)->topCard->isJoker;
    c.cardSuit = (*d)->topCard->cardSuit;

    /* Keep Track Of The Top Card */
    struct Deck* oldDeck = *d;
    (*d) = (*d)->nextCard;

    /* Free The Old Memory */
    FreeCard(oldDeck->topCard);
    free(oldDeck);

    d = &(*d);

    return c;
}

/* Function That Peeks At The Nth Card */
struct Card* peek_card(struct Deck* d, int num)
{
    if((num < 1) || (num > d->size))
    {
        printf("Invalid Deck Index.n");
        return NULL;
    }

    if(d == NULL)
    {
        printf("Error: Deck Is Currently Set To NULL.n");
        return NULL;
    }

    struct Deck* tempDeck = d;

    for(int i = num; i > 1; i--)
    {
        tempDeck = tempDeck->nextCard;
    }

    return tempDeck->topCard;
}

/* Function That Prints A Playing Card */
void PrintCard(struct Card* c)
{
    /* Check If The Card Is NULL */
    if(c == NULL)
    {
        printf("Error: Card Is Set To NULL.n");
        return;
    }

    /* Print A Joker */
    if(c->isJoker == true)
    {
        printf("Jokern");
    }

    /* Print The Other Cards */
    else
    {
        /* Print The Rank Of The Card */
        switch(c->rank) {
            case 1:
                printf("Ace of ");
                break;

            case 2:
                printf("Two of ");
                break;

            case 3:
                printf("Three of ");
                break;

            case 4:
                printf("Four of ");
                break;

            case 5:
                printf("Five of ");
                break;

            case 6:
                printf("Six of ");
                break;

            case 7:
                printf("Seven of ");
                break;

            case 8:
                printf("Eight of ");
                break;

            case 9:
                printf("Nine of ");
                break;

            case 10:
                printf("Ten of ");
                break;

            case 11:
                printf("Jack of ");
                break;

            case 12:
                printf("Queen of ");
                break;

            case 13:
                printf("King of ");
                break;
        }

        /* Print The Suit Of The Card */
        switch(c->cardSuit) {
            case CLUBS:
                printf("Clubsn");
                break;

            case DIAMONDS:
                printf("Diamondsn");
                break;

            case HEARTS:
                printf("Heartsn");
                break;

            case SPADES:
                printf("Spadesn");
                break;

            case JOKER:
                break;
        }
    }
}

/* Function That Prints A Deck Of Cards */
void PrintDeck(struct Deck* d)
{
    struct Deck* tempDeck = d;

    /* Print Each Card Of The Deck */
    while(tempDeck != NULL)
    {
        PrintCard(tempDeck->topCard);
        tempDeck = tempDeck->nextCard;
    }

    /* Print A Newline For Readability */
    printf("n");
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1