Hey,

I need to create a war card game.
I have a problem with linked list - I read about it but still I have a problem with writing the code.

I made a deck of shuffled card and now I need to sort it out between 2 players... so I want the deck to be a list so i will always be able to compare the cards

Here is part of my code, I would appreciate any help!
Thanks!!

typedef struct card
{
    int rank;
    int suit;
}card;


typedef struct deck {           
    int num_cards;
    card **cards;
    struct deck *next;
} deck;


deck *shuffle(deck *deck1)   //shuffle the deck
{
    int check[4][13] = { 0 };

    for (int i = 0; i < DECKSIZE; i++)
    {
        int getVal = rand() % 13;
        int getSuit = rand() % 4;

        if (check[getSuit][getVal] >= 1)
        {
            i--;
            continue;
        }

        deck1->cards[i] = make_card(getSuit, getVal);  //didn't insert the mark_card function
        check[getSuit][getVal]++;
    }

    return deck1;
}

deck *addPlayer1(deck *shuffledpile, deck *top, deck *bottom)
{
    deck *pile1 = malloc(sizeof(deck));;
    pile1->num_cards = 26;
    pile1->cards = malloc(26 * sizeof(card *));

    int i;

    for (i = 0; i < DECKSIZE / 2; i++)
    {
        pile1->cards[i] = shuffledpile->cards[i];
        pile1->next = bottom;
    }


    top->cards[i];


    return pile1;
}


int main()
{

deck *pile = makeDeck();   //didn't insert the makeDeck fuction- but it works
deck *deckShuffled = shuffle(pile);
    deck *Atop=NULL, *Abot=NULL;
    deck *player1=addPlayer1(deckShuffled,Atop, Abot);
}

It seems you have the deck be any stack of cards, and many decks, so it is hard to tell if all cards are still in some deck. Shuffling is faster/free of dups if you rand times numner of cards left to pick the next card from a deck originally in some normal order. You could track cards as being in the supply pile, discard, or player hand 1 to N, all in one container. While your decks are tied to each other linked list, they are arrays. Since you need to adjust decks a lot, you might make the decks linked lists of cards, and the list of decks an array: 0=pile, 1=discard, 2=played, 3-N = player hands or some pointers and an array. The cards played onto the table might just be a separate list, as you need to know who owns what but they all end up in the discard, for a tricks sort of game.

This may give you some more ideas ...

and some more tools (C utility functions) to use in your code:
(You could use a 'Clist' by including file Clist.h
available at ...

http://developers-heaven.net/forum/index.php/topic,2582.0.html

if your prefer)

/* 52Cards.c */

#include "readLine.h" /* re. myAssert */
#include <time.h>


const char kinds[] = { 'S', 'C', 'H', 'D' };
const char values[] = { '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' };



typedef struct
{
    char kind;
    char val;

} Rec;

void freeVrec( Rec* p )
{
    /* NO dynamic memory was used here, so NONE to free  ... */
}


/* NEED BOTH definitions above: typedef Rec  and  freeVrec ...
   and then can include ... */

#include "Cvec.h"

#define Card Rec /* equate */


void getNewDeck( Cvec* cards )
{
    int i, j;
    for( i = 0; i < 4; ++ i )
    {
        for( j = 0; j < 13; ++ j )
        {
            Card c;
            c.kind = kinds[i];
            c.val = values[j];
            push_backCvec( cards, &c  );
        }
    }
}
void shuffleDeck( Cvec* cards )
{
    int i, j, count = cards->size;
    Card tmp;
    while( count-- )
    {
        i = rand() % cards->size;
        j = rand() % cards->size;

        /* swap */
        tmp = cards->ary[i];
        cards->ary[i] = cards->ary[j];
        cards->ary[j] = tmp;
    }
}

void showDeck( const Cvec* cards )
{
    int i;
    for( i = 0; i < cards->size;  )
    {
        printf( "%c %c  ", cards->ary[i].kind,  cards->ary[i].val ) ;
        if( ++i % 5 == 0 ) putchar('\n');
    }
}


int main()
{
    Cvec cards;
    initCvec( &cards ); /* must initial to work ok ... */

    srand( time(0) );

    getNewDeck( &cards );
    showDeck( &cards );
    putchar('\n');

    printf( "capacity = %d, size = %d\n", cards.cap, cards.size );


    clearCvec( &cards ); /* clear all dynamic memory whe done */
    puts( "After clearCvec..." );
    printf( "capacity = %d, size = %d\n", cards.cap, cards.size );

    printf( "\nPress 'Enter' to continue/exit ... " );
    fflush( stdout );
    return 0;

}

You can find file readLine.h at:

http://developers-heaven.net/forum/index.php/topic,2580.msg2864.html#msg2864

You can find file Cvec.h at:

http://developers-heaven.net/forum/index.php/topic,2580.0.html

link list is a data structure group of node are represent or sequence.
12.->99.->*

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.