> 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.