Thread: having trouble with structs

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    3

    having trouble with structs

    Hey, I have to make the Irish card game "25" in C Programming. i cant figure out how to print my structs as i still have little experience in this language. If anyone could lend me a hand, it would be appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    
    //constants declared
    #define NUMOFCARDS 52
    #define NUMOFPROPERTIES 2
    #define NUMOFSUITS 4
    #define NUMOFFACES 13
    #define PLAYERCARDS 5
    #define FIRSTTWOATATIME  3
    
    
    struct playerCards
    
    
    {
    
    
        int playersCardDeck[PLAYERCARDS][NUMOFPROPERTIES];
    
    
        // Above array denotes number cards assigned to a players where represents "PLAYERCARDS" suit and "NUMOFPROPERTIES" represents faces.
    
    
    };
    
    
    //declaration of arrays
    char* suit[NUMOFSUITS] = { "Hearts","Spades","Clubs","Diamonds" };
    char* face[NUMOFFACES] = { "Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine",
    "Ten","Jack","Queen","King" };
    
    
    //calling functions
    void PrintCard(int deck[NUMOFCARDS][NUMOFPROPERTIES], int i);
    void InitDeck(int deck[NUMOFCARDS][NUMOFPROPERTIES]);
    void SwapCards(int deck[NUMOFCARDS][NUMOFPROPERTIES], int src, int dest);
    void ShuffleDeck(int deck[NUMOFCARDS][NUMOFPROPERTIES]);
    
    
    
    
    int main()
    {
        //declaration of varaiables
        int deck[NUMOFCARDS][NUMOFPROPERTIES];
        int player1Deck[5];
        int src = 0;
        int dest = 0;
        int i;
        int j;
        int row=0;
        int players;
        int numPlayers = 0;
        int playerDeck[30];
        srand(time(NULL));
        InitDeck(deck);
        ShuffleDeck(deck);
        SwapCards(deck, src, dest);
        printf("Please enter the number of players (From 1 to 6): \n");
        scanf("%d", &numPlayers);
    
    
        for (players = 0; players < numPlayers; players++)
        {
            printf("Player %d\n", players + 1);
            for (i = 0; i < PLAYERCARDS; i++)
            {
                PrintCard(deck, i);
    
    
                
    
    
    
    
            }
            printf("\n");
            
        }
    
    
    
    
        struct playerCards *userEnteredPlayerCount = NULL;
    
    
        userEnteredPlayerCount = (struct playerCards *)malloc(numPlayers * sizeof(struct playerCards));
    
    
        {
    
    
            for (players = 0; players < numPlayers; players++) // assign two cards at a time to all players
    
    
            {
    
    
                for (i = 0; i < (PLAYERCARDS-FIRSTTWOATATIME); i++)
    
    
                {
    
    
                    for (j = 0; j < NUMOFPROPERTIES; j++)
    
    
                    {
    
    
                        userEnteredPlayerCount[players].playersCardDeck[i][j] = deck[i][j];
    
    
    
    
                    }
    
    
                    row++; //indicates first rows elements are assigned to a player
    
    
                }
    
    
            }
    
    
            for (players = 0; players < numPlayers; players++) // assign next three cards at a time to all players
            {
    
    
                /* row variable is updated above which keeps updated with which row of elements are assigned to a player and this ensures no duplicat entries are assigned to a player */
    
    
                for (i = row; i < PLAYERCARDS; i++)
    
    
                {
    
    
                    for (j = 0; j < NUMOFPROPERTIES; j++)
    
    
                    {
    
    
                        userEnteredPlayerCount[players].playersCardDeck[i][j] = deck[i][j]; // assign the cards from deck variable
    
    
                    }
    
    
                }
    
    
            }
    
    
        }
    
    
        return 0;
    
    
        printf("%d", userEnteredPlayerCount->playersCardDeck);
    
    
        
    
    
    }
    
    
    void InitDeck(int deck[NUMOFCARDS][NUMOFPROPERTIES])
    {
        int suit;
        int face;
        int row = 0;
        for (suit = 0; suit < 4; suit++)
            for (face = 0; face < 13; face++)
            {
                deck[row][0] = suit;
                deck[row][1] = face;
                row++;
    
    
                
            }
    }
    void ShuffleDeck(int deck[NUMOFCARDS][NUMOFPROPERTIES])
    {
        int src, dest, i;
        srand(time(NULL));
    
    
        for (i = 0; i < NUMOFCARDS; i++)
        {
            src = i;
            dest = rand() % NUMOFCARDS;
            SwapCards(deck, src, dest);
    
    
        }
    }
    
    
    void SwapCards(int deck[NUMOFCARDS][NUMOFPROPERTIES], int src, int dest)
    {
        int temp;
        temp = deck[src][0];
        deck[src][0] = deck[dest][0];
        deck[dest][0] = temp;
        temp = deck[src][1];
        deck[src][1] = deck[dest][1];
        deck[dest][1] = temp;
    
    
    }
    void PrintCard(int deck[NUMOFCARDS][NUMOFPROPERTIES], int i)
    {
        int tempsuit;
        int tempface;
        tempsuit = deck[i][0];
        tempface = deck[i][1];
        printf("Card %d = %s of %s\n", i+1, face[tempface], suit[tempsuit]);
    
    
    }
    Attached Images Attached Images

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble understanding malloc + structs
    By Finoli in forum C Programming
    Replies: 15
    Last Post: 01-03-2016, 07:45 AM
  2. Having trouble with structs
    By C0__0D in forum C Programming
    Replies: 9
    Last Post: 02-05-2013, 11:43 PM
  3. Trouble with reading in data to an array of structs
    By ljgerr93 in forum C Programming
    Replies: 6
    Last Post: 01-22-2012, 01:57 PM
  4. Replies: 7
    Last Post: 06-04-2008, 10:39 PM
  5. trouble with structs..
    By cs32 in forum C Programming
    Replies: 1
    Last Post: 01-22-2008, 11:48 PM

Tags for this Thread