I am doing a card dealing and shuffling program.
Here's a part of the code.
Code:
...
int hand1[5][2] = {0}; //initialized player1 hand, an array of zero
int hand2[5][2] = {0}; //same for player2
...
srand(time(0));
initialize(deck);//a function that resets deck
shuffle(deck, 2);//shuffles deck twice
dealFive(deck, hand1, &cardsdealt);//gives player1 5 cards
dealFive(deck, hand2, &cardsdealt);//gives player2 5 cards
int i;
for (i = 0; i < 5; i++)
{
printf( "%5s(%d) of %-8s(%d)\n", face[hand1[i][1]],(hand1[i][1])+1, suit[hand1[i][0]],(hand1[i][0])+1);
}
printf("\n");
for (i = 0; i < 5; i++)
{
printf( "%5s of %-8s\n", face[hand2[i][1]], suit[hand2[i][0]]);
}
The dealing function works fine.
Code:
void dealFive(int wDeck[][13],int hand[5][2], int *cardID){
int row, col;
int card;
int cardfound;
row = 0;
col = 0;
for (card = 0; card <5; card++)
{
cardfound = 0;
row = 0;
//col = 0;
while(!cardfound&&row<4)
{
while(!cardfound&&col<13)
{
if (wDeck[row][col]==(*cardID)+1)
{
hand[(*cardID)][0] = row; //suit
hand[(*cardID)][1] = col; //face
printf("Card %d: %d %d\n",*cardID+1, row+1,col+1);
(*cardID)++;
cardfound=1;
}
col++;
}
col = 0;
row++;
}
}
}
Here's an output of the program:
Code:
Deck initialized.
1 2 3 4 5 6 7 8 9 10 11 12 13
---------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13
14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29 30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49 50 51 52
---------------------------------------
Deck shuffled.
1 2 3 4 5 6 7 8 9 10 11 12 13
---------------------------------------
3 4 46 19 10 8 6 37 14 32 29 34 47
35 52 13 17 5 22 11 20 26 7 44 31 23
18 21 1 33 27 49 30 45 16 38 15 42 24
36 39 40 48 25 2 51 12 50 28 43 9 41
---------------------------------------
Card 1: 3 3
Card 2: 4 6
Card 3: 1 1
Card 4: 1 2
Card 5: 2 5
Card 6: 1 7
Card 7: 2 10
Card 8: 1 6
Card 9: 4 12
Card 10: 1 5
hand1
Seven(7) of Hearts (1)
Ten(10) of Diamonds(2)
Six(6) of Hearts (1)
Queen(12) of Spades (4)
Five(5) of Hearts (1)
Ace of Hearts
Ace of Hearts
Ace of Hearts
Ace of Hearts
Ace of Hearts
I noticed player2's hand was never written by dealFive function and player1's hand contains what should be player2's cards.
Some observations:
Running the code with hand2 uninitialized with zeros, and printing its contents after dealFive(..hand2..) leads to a seg fault.
I would like to ask why is hand2 empty and why is hand1 = hand2?
Thank you.