Thread: Need help with this code

  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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,540
    > I compelete like 70% from the code ..
    I'd give it 30% finished.

    You've made a reasonable job of shuffle.

    The next task would be a function called 'deal'.
    One thing you need to keep track of is how many cards you've dealt so far.


    You also have a ton of unnecessary blank lines in your post.
    About 200 too many.
    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]);
    }
    The whole 'NUMOFPROPERTIES' thing is a bodge when the homework is 'do this without using a struct' kind of pointless exercise.

    Given that you already know what a struct is, perhaps you can start with
    Code:
    struct card {
      int face;
      int suit;
    };

    From which it follows naturally
    Code:
    struct card deck[52];  // the whole deck
    struct card hand[13];  // one player's hand
    
    // or even
    struct card players[4][13];
    Given the small size of the data ranges, using malloc is an overkill and a complication you can well do without. Just make an array that's large enough.


    Oh, and you only need to call
    srand(time(NULL));
    exactly once in a program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2020
    Posts
    7
    Just give me the right code and i can pay for that if u want

  4. #4
    Registered User
    Join Date
    Nov 2020
    Posts
    7
    I really need the solution today please.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,277
    Quote Originally Posted by abodyabody5111
    Just give me the right code and i can pay for that if u want
    Since this is obviously an academic project rather than a real world project for which outsourcing is a valid option, paying for the code is academic dishonesty, so nope.

    Quote Originally Posted by abodyabody5111
    I really need the solution today please.
    Then you should immediately start by taking Salem's advice in post #2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2020
    Posts
    7
    Dude I will lost my grades if u don't help me. I did my best in the code, and this is the last day to submit.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,277
    Quote Originally Posted by abodyabody5111
    Dude I will lost my grades if u don't help me.
    If someone here helps you by simply doing the work for you, especially given that you offered to pay so it is more serious than simple plagiarism, and your teacher finds out about it, you might not only score zero for this particular assignment rather than the grade that you would have achieved, but possibly fail the entire class for academic dishonesty.

    Quote Originally Posted by abodyabody5111
    I did my best in the code, and this is the last day to submit.
    What I would do in your shoes is this:
    • Make the formatting improvements suggested by Salem in post #2 and ensure that the resulting code compiles.
    • Put this code aside safely, then work on the other suggestions concerning the use of a struct. This will involve redoing your code a bit, which is why I suggest putting your code aside so that if you do fail to complete, you can submit your earlier compilable code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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