Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Trouble assigning cards to players after shuffling using multi-dimensional arrays

0 votes
asked Dec 31, 2018 by Cathal
I have to make the card game 25 in C Programming, im having trouble assigning the cards to the players after i shuffle to cards. i have tried to use 2 single dim arrays and a single 2 dim array and i cant seem to figure out either. any help would be appreciated.

so far i have this:

#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

//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 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");
      
   }

   return 0;

  

}

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]);

}

I dont know how to assign the cards dealt to the players. I have to store the cards to compare them and let the user select them through some means later in order to "play" the cards. Any help would be appreciated

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...