Thread: Need help with this code

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2020
    Posts
    7

    Exclamation Need help with this code

    Hello.. I need help with this codes with these arguments:
    should be able to do the following:
    1. Shuffling the deck of playing card.
    2. Distributing the card among four players, one card to each player then second card to each and so on.
    3. The player having maximum similar cards ( like all 13 hearts) consider as winner.
    I compelete like 70% from the code ..
    *Only in C language
    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 13
    #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 = 4;
        int playerDeck[30];
        srand(time(NULL));
        InitDeck(deck);
        ShuffleDeck(deck);
        SwapCards(deck, src, dest);
        printf("Please enter the number of players (From 1 to 4: \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]);
    
    
    
    
    }
    Last edited by Salem; 11-30-2020 at 02:07 PM. Reason: Pointless centering removed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread