# Thread: How to join this two together?

1. ## How to join this two together?

I have about 6 players... and calling this function for each player.. Is there a way to joint it together? So i can just call it depending on the player?

like dealplayer(1); or dealplayer(2);

Code:
```void dealplayer1 (){
player1.count++;
double R = (double)rand()/(double)RAND_MAX;
double P;
player1.card = cards[rand() % 52];
while (isalpha(player1.card))
player1.card = cards[rand() % 52];
cards[player1.card] = 'X';
if (player1.card >= 11)
player1.cardnew = 10;
else
player1.cardnew = player1.card;
strcpy(player1a[player1.count], cardnames[player1.card]);
player1.total += player1.cardnew;
P = (double)1 - (((double)21 - (double)player1.total)/(double)21);
if (R >= P)
player1.play = 0;
else
player1.play = 1;
if (player1.total >= 21)
player1.play = 1;
}

void dealplayer2 (){
player2.count++;
double R = (double)rand()/(double)RAND_MAX;
double P;
player2.card = cards[rand() % 52];
while (isalpha(player2.card))
player2.card = cards[rand() % 52];
cards[player2.card] = 'X';
if (player2.card >= 11)
player2.cardnew = 10;
else
player2.cardnew = player2.card;
strcpy(player2a[player2.count], cardnames[player2.card]);
player2.total += player2.cardnew;
P = (double)1 - (((double)21 - (double)player2.total)/(double)21);
if (R >= P)
player2.play = 0;
else
player2.play = 1;
if (player2.total >= 21)
player2.play = 1;
}```

2. Yes, and you'll want to use an array to hold all of the players instead of having individual players like player1 and player2. See Arrays in C - Cprogramming.com to learn about arrays.

3. so i can have
Code:
`char PLAYER [4][7] = {"player1", "player2", "player3", "player4"};`
Code:
```void dealplayer (){
int i=0;
PLAYER[i].count++;
double R = (double)rand()/(double)RAND_MAX;
double P;
PLAYER[i].card = cards[rand() % 52];
while (isalpha(PLAYER[i].card))
PLAYER[i].card = cards[rand() % 52];
cards[PLAYER[i].card] = 'X';
if (PLAYER[i].card >= 11)
PLAYER[i].cardnew = 10;
else
PLAYER[i].cardnew = PLAYER[i].card;
strcpy(PLAYER[i]a[PLAYER[i].count], cardnames[PLAYER[i].card]);
PLAYER[i].total += PLAYER[i].cardnew;
P = (double)1 - (((double)21 - (double)PLAYER[i].total)/(double)21);
if (R >= P)
PLAYER[i].play = 0;
else
PLAYER[i].play = 1;
if (PLAYER[i].total >= 21)
PLAYER[i].play = 1;
}
```

4. Code:
`char PLAYER [4][7] = {"player1", "player2", "player3", "player4"};`
This array just holds the players' names. If you also wanted to hold data, you'd have to use a different approach. Your code indicates the usage of a structure - do you know how to use these? An array of structures can allow you to hold variables of different data types for each "player."

5. Originally Posted by Matticus
Code:
`char PLAYER [4][7] = {"player1", "player2", "player3", "player4"};`
This array just holds the players' names. If you also wanted to hold data, you'd have to use a different approach. Your code indicates the usage of a structure - do you know how to use these? An array of structures can allow you to hold variables of different data types for each "player."
I have a struct that contains values used in the previous function:
player1.count
player1.card
player1.play

etc

6. Originally Posted by SuperMiguel
I have a struct that contains values used in the previous function:
player1.count
player1.card
player1.play

etc
Then you will want to make an array of struct, something like this:
Code:
`struct player players[6];`

7. Originally Posted by christop
Then you will want to make an array of struct, something like this:
Code:
`struct player players[6];`
i have this struct:
Code:
```struct blackjack
{
int card, cardnew, total, play, result, count, countnew, ace;
char playornot;
} you, dealer, player1, player2, player3, player4;```
thats where all the variables are being held

so you are saying to do

Code:
`structblackjack players[6];`
and to access the variables do playernames[i].total ??

8. You don't have to make an array of structs, but it is certainly beneficial and an advisable way to do it.
Alternatively, you can declare an array of pointers to struct and initialise those pointers to point to your individual struct variables.