# Thread: Need help with this code

1. ## 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]);

}```

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

3. Just give me the right code and i can pay for that if u want

4. I really need the solution today please.

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

Originally Posted by abodyabody5111
I really need the solution today please.
Then you should immediately start by taking Salem's advice in post #2.

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

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.

Popular pages Recent additions