Hey everyone, i've got myself stuck on a problem of determining a winner.
While doing a battle if a player runs out of cards it will use the last card(card that started the battle).
I cant seem to get all the bugs out of it; and i think im overlooking something.
Any help would be greatly appreciated.

//Brandon 


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


//card structure
struct _Card
{
    int suit;//the suit of the playing card
    int value;//the value of the playing card
    struct _Card * nextCard;
};
typedef struct _Card card;
typedef card * link;

//list structure
struct _List
{
    link front;
    link bottom;
};
typedef struct _List list;
typedef list * linklist;


//function prototypes
link initCard();
void createDeck(linklist deck);
void enqueueCard(linklist deck, int suit, int value);
void dequeueCard(linklist deck);
void printDeck(linklist deck);
void shuffleDeck(linklist deck);
void dealCards(linklist deck, linklist player1, linklist player2);
int battle(linklist player1, linklist player2, int player1Count, int player2Count, linklist player1Play, linklist player2Play);


//main
int main(void)
{
    int status = 0;
    int player1Count;
    int player2Count;
    linklist deck;
    linklist player1;
    linklist player2;
    linklist player1Play;
    linklist player2Play;

    //initialize
    player1 = malloc(sizeof(list));
    player2 = malloc(sizeof(list));
    deck = malloc(sizeof(list));
    player1Play = malloc(sizeof(list));
    player2Play = malloc(sizeof(list));

    //set to null
    player1->front = NULL;
    player1->bottom = NULL;
    player2->front = NULL;
    player2->bottom = NULL;
    deck->front = NULL;
    deck->bottom = NULL;
    player1Play->front = NULL;
    player1Play->bottom = NULL;
    player2Play->front = NULL;
    player2Play->bottom = NULL;

    createDeck(deck);
    printDeck(deck);
    printf("\n\n\n\n\n\n");
    shuffleDeck(deck);
    printDeck(deck);

    dealCards(deck, player1, player2);
    printf("\nplayer 1's deck\n");
    printDeck(player1);
    printf("player 2's deck\n");
    printDeck(player2);
    printf("\n\n\n\n");

    while (player1->front != NULL && player2->front != NULL)
    {
        //counter should be at 26
        player1Count = 26;
        player2Count = 26;

        //start playing the game
        if (player1->front->value > player2->front->value)
        {
            printf("Player One: %d player 2: %d\n", player1->front->value, player2->front->value);
            link temp = player1->front;
            link temp2 = player2->front;

            //player one wins
            printf("Player one wins this round!\n");

            //remove cards from the decks
            dequeueCard(player1);
            dequeueCard(player2);

            //put the cards on the bottom of the winners deck
            enqueueCard(player1, temp->suit, temp->value);
            enqueueCard(player1, temp2->suit, temp2->value);

            //decrement player two counter
            player2Count--;

            //increment counter
            player1Count++;
        }
        else if (player2->front->value > player1->front->value)
        {
            printf("Player One: %d player 2: %d\n", player1->front->value, player2->front->value);
            link temp = player1->front;
            link temp2 = player2->front;

            //player one wins
            printf("Player two wins this round!\n");

            //remove cards from the decks
            dequeueCard(player1);
            dequeueCard(player2);

            //put the cards on the bottom of the winners deck
            enqueueCard(player2, temp->suit, temp->value);
            enqueueCard(player2, temp2->suit, temp2->value);

            //decrement player one
            player1Count--;

            //increment counter
            player2Count++;
        }
        else if (player1->front->value == player2->front->value)
        {
            //war
            printf("Player One: %d player 2: %d\n", player1->front->value, player2->front->value);
            printf("War!!\n");
            status = battle(player1, player2, player1Count, player2Count, player1Play, player2Play);
            if (status == 2)
            {
                continue;
            }
            else
            {
                break;
            }
        }

    }//end of while

    if (player1->front == NULL && player2->front != NULL)
    {
        //player two wins
        printf("Player two is the winner!!!\n");
    }
    else if (player2->front == NULL && player1->front != NULL)
    {
        printf("Player one is the winner!!!\n");
    }
    else
    {
        //error here
        //cleanup
    }

    //free
    free(player1);
    free(player2);
    free(deck);
    free(player1Play);
    free(player2Play);

    //getch();
    return 0;
}


link initCard()
{
    link iCard;
    iCard = malloc(sizeof(card));
    if (iCard == NULL)
    {
        printf("Malloc failed. Exiting...\n");
        exit(1);
    }
    iCard->nextCard = NULL;
    return iCard;
}

void createDeck(linklist deck)
{

    int suit = 0;
    int value = 0;


    for (suit = 1; suit <= 4; suit++)//four different suits in a deck of cards
    {
        for (value = 1; value <= 13; value++)//13 different values for each suit in a deck of cards
        {
            enqueueCard(deck, suit, value);
        }
    }
}

void enqueueCard(linklist deck, int suit, int value)
{
    link card;
    link tmp;
    card = initCard();
    card->suit = suit;
    card->value = value;

    if (deck->front == NULL && deck->bottom == NULL)
    {
        deck->front = card;
        deck->bottom = card;
    }
    else
    {
        tmp = deck->bottom;
        deck->bottom->nextCard = card;
        tmp->nextCard = card;
        deck->bottom = card;
    }
}
void dequeueCard(linklist deck)
{
    link tmp;
    int suit = 0;
    int value = 0;

    if (deck->front == NULL)
    {
        printf("Trying to dequeue from an empty list. Exiting.\n");
        exit(1);
    }
    else if (deck->front == deck->bottom)
    {
        tmp = deck->front;
        suit = tmp->suit;
        value = tmp->value;

        free(tmp);

        deck->front = NULL;
        deck->bottom = NULL;
    }
    else
    {

        tmp = deck->front;
        suit = tmp->suit;
        value = tmp->value;
        deck->front = deck->front->nextCard;
        free(tmp);
    }
}

void printDeck(linklist deck)
{
    link tmp = deck->front;

    if (tmp == NULL)
    {
        printf("Print error. Exiting.");
        exit(1);
    }
    else
    {
        while (tmp != NULL)
        {

            switch (tmp->value) {
            case(1) :
                printf("Ace");
                break;
            case(13) :
                printf("King");
                break;
            case(12) :
                printf("Queen");
                break;
            case(11) :
                printf("Jack");
                break;
            default:
                printf("%d", tmp->value);
            }
            printf(" of ");
            switch (tmp->suit) {
            case(1) :
                printf("Clubs\n");
                break;
            case(2) :
                printf("Diamonds\n");
                break;
            case(3) :
                printf("Hearts\n");
                break;
            case(4) :
                printf("Spades\n");
                break;
            default:
                printf("No such suit %d\n", tmp->suit);
                exit(1);
            }
            tmp = tmp->nextCard;
        }
    }
}
//Shuffle deck function swaps the values in the nodes
void shuffleDeck(linklist deck)
{
    link tmp;
    int suit;
    int value;
    int i;
    int random;

    srand(time(NULL));//seed time

    for (i = 0; i < 1000; i++)//1000 shuffles
    {
        tmp = deck->front;
        random = rand() % (52 + 1);
        if (random == 0)
        {
            tmp = deck->front;
        }
        else
        {
            while (random > 0)
            {
                if (tmp->nextCard)
                    tmp = tmp->nextCard;
                random--;
            }
        }
        suit = deck->front->suit;
        value = deck->front->value;
        deck->front->suit = tmp->suit;
        deck->front->value = tmp->value;
        tmp->suit = suit;
        tmp->value = value;
    }
}

void dealCards(linklist deck, linklist player1, linklist player2)
{
    link tmp;
    int i;

    tmp = deck->front;

    for (i = 0; i < 26; i++)
    {
        if (tmp != NULL)
        {
            enqueueCard(player1, tmp->suit, tmp->value);
            tmp = tmp->nextCard;
            enqueueCard(player2, tmp->suit, tmp->value);
            tmp = tmp->nextCard;
        }
        else
        {
            printf("Dealing error\n");
            exit(1);
        }
    }
}

int battle(linklist player1, linklist player2, int player1Count, int player2Count, linklist player1Play, linklist player2Play)
{
    if (player1->front != NULL && player2->front != NULL)
    {
        if (player1->front->value == player2->front->value)
        {

            //PLAYER ONE
            if (player1Count - 4 != 0 && player1Count - 4 >= 4)
            {
                enqueueCard(player1Play, player1->front->nextCard->suit, player1->front->nextCard->value);
                enqueueCard(player1Play, player1->front->nextCard->nextCard->suit, player1->front->nextCard->nextCard->value);
                enqueueCard(player1Play, player1->front->nextCard->nextCard->nextCard->suit, player1->front->nextCard->nextCard->nextCard->value);
                enqueueCard(player1Play, player1->front->nextCard->nextCard->nextCard->nextCard->suit, player1->front->nextCard->nextCard->nextCard->nextCard->value);
                //tempCard1 = player1->front->nextCard;//face down
                //tempCard2 = player1->front->nextCard->nextCard;//face down
                //tempCard3 = player1->front->nextCard->nextCard->nextCard;//face down
                //tempCard4 = player1->front->nextCard->nextCard->nextCard->nextCard;//face up
                //deq
                dequeueCard(player1);
                dequeueCard(player1);
                dequeueCard(player1);
                dequeueCard(player1);
                player1Count = player1Count - 4;

            }
            else if (player1Count == 1)
            {
                enqueueCard(player1Play, player1->front->suit, player1->front->value);
                dequeueCard(player1);
                player1Count--;
            }
            else if (player1Count == 2)
            {

                enqueueCard(player1Play, player1->front->nextCard->suit, player1->front->nextCard->value);
                enqueueCard(player1Play, player1->front->suit, player1->front->value);
                dequeueCard(player1);
                dequeueCard(player1);
                player1Count = player1Count - 2;
            }
            else if (player1Count == 3)
            {

                enqueueCard(player1Play, player1->front->nextCard->suit, player1->front->nextCard->value);
                enqueueCard(player1Play, player1->front->nextCard->nextCard->suit, player1->front->nextCard->nextCard->value);
                enqueueCard(player1Play, player1->front->suit, player1->front->value);
                dequeueCard(player1);
                dequeueCard(player1);
                dequeueCard(player1);
                player1Count = player1Count - 3;
            }

            //PLAYER TWO
            if (player2Count - 4 != 0 && player2Count - 4 >= 4)
            {
                enqueueCard(player2Play, player2->front->nextCard->suit, player2->front->nextCard->value);
                enqueueCard(player2Play, player2->front->nextCard->nextCard->suit, player2->front->nextCard->nextCard->value);
                enqueueCard(player2Play, player2->front->nextCard->nextCard->nextCard->suit, player2->front->nextCard->nextCard->nextCard->value);
                enqueueCard(player2Play, player2->front->nextCard->nextCard->nextCard->nextCard->suit, player2->front->nextCard->nextCard->nextCard->nextCard->value);
                //tempCard1 = player2->front->nextCard;//face down
                //tempCard2 = player2->front->nextCard->nextCard;//face down
                //tempCard3 = player2->front->nextCard->nextCard->nextCard;//face down
                //tempCard4 = player2->front->nextCard->nextCard->nextCard->nextCard;//face up
                dequeueCard(player2);
                dequeueCard(player2);
                dequeueCard(player2);
                dequeueCard(player2);
                player2Count = player2Count - 4;
            }
            else if (player2Count == 1)
            {
                enqueueCard(player2Play, player2->front->suit, player2->front->value);
                dequeueCard(player2);
                player2Count--;
            }
            else if (player2Count == 2)
            {

                enqueueCard(player2Play, player2->front->nextCard->suit, player2->front->nextCard->value);
                enqueueCard(player2Play, player2->front->suit, player2->front->value);
                dequeueCard(player2);
                dequeueCard(player2);
                player2Count = player2Count - 2;
            }
            else if (player2Count == 3)
            {

                enqueueCard(player2Play, player2->front->nextCard->suit, player2->front->nextCard->value);
                enqueueCard(player2Play, player2->front->nextCard->nextCard->suit, player2->front->nextCard->nextCard->value);
                enqueueCard(player2Play, player2->front->suit, player2->front->value);
                dequeueCard(player2);
                dequeueCard(player2);
                dequeueCard(player2);
                player2Count = player2Count - 3;
            }

            //check for win
            if (player1Play->front->value > player2Play->front->value)
            {
                printf("Player One: %d player 2: %d\n", player1Play->front->value, player2Play->front->value);
                printf("win one\n");
                //player one wins the round
                while (player1Play->front != NULL)
                {
                    enqueueCard(player1, player1Play->front->suit, player1Play->front->value);
                    enqueueCard(player1, player2Play->front->suit, player2Play->front->value);
                    dequeueCard(player1Play);
                    dequeueCard(player2Play);
                    player1Count++;
                    return 1;
                }

            }
            else if (player2Play->front->value > player1Play->front->value)
            {
                printf("Player One: %d player 2: %d\n", player1Play->front->value, player2Play->front->value);
                printf("win two\n");
                //player two wins the round
                while (player2Play->front != NULL)
                {
                    enqueueCard(player2, player1Play->front->suit, player1Play->front->value);
                    enqueueCard(player2, player2Play->front->suit, player2Play->front->value);
                    dequeueCard(player1Play);
                    dequeueCard(player2Play);
                    player2Count++;
                    return 1;
                }
            }
            else if (player1Play->front->value == player2Play->front->value)
            {
                printf("Player One: %d player 2: %d\n", player1Play->front->value, player2Play->front->value);
                printf("War again!!\n");
                //war again
                return 2;
                //battle(player1, player2, player1Count, player2Count,player1Play,player2Play);
            }


        }//end if
    }
    return 1;
}

Posting 500+ lines of code with no indication or description of the errors or problems you are getting just doesn't work.

Sorry about that i thought i posted my problem.

The battle function seems to crash when I try to enq the cards to the player1Play and player2Play linked lists.

It also skips over the last if statements in main when checking for a winner and it seems that "garbage is contained in player1->front and player2-front when debugging with visual studio.

Im pointing towards the battle function; for some reason i cannot get the function right since i added the check "if a player runs out of cards during a battle use the last card that started the battle"

Here is an update: this is what is contained in player1->front and player2->front when the program is ran (this is before freeing the memory)

player1->front = 0x0147b6b0 {suit=-17891602 value=-17891602 
nextCard=0x0147b6f8 {suit=-17891602 value=-17891602 nextCard=...} }

Seems to be just garbage, and its skipping over the if/else statment at the end of main.(line 156)

After the values are equal and it goes into the battle function it seems to break out of the while loop and not put all the cards from player1Play or player2Play into the player1 or player2 linked list. (line 498, 492)

Here is a screenshot of it running.

I figured out it does not break out of the while loop one line 498,492 and it completes like it should.

Maybe it is dequeing the last card in the players deck and whats left in the linked list is garbage memory? If so how would i fix this?

Not sure how to narrow it down now.
error.PNG

This helps. I'll get back to you after I've had a chance to look closer.

Another thing i forgot to mention is i turned int battle to void battle and removed the return statements and

I changed line 144-151 to just
battle(player1, player2, player1Count, player2Count, player1Play, player2Play);

I have updated my code a bit, it seems that i have a problem with my counter and determining what happens if a player runs out of cards.
(if a player runs out of cards during a war it should use the last card that started the war)
If anyone could it help it would be greatly appreciated.

I finally solved this on my own after doing some major debugging and correcting logic errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.