I'm trying to write a poker program. Pretty much everything works right now, the one issue is where the program asks the user which cards they want to keep, and which they want to discard. If it worked as intended, the user would enter which cards they want to keep. The cards that weren't selected would then be removed, and replaced with cards from the deck. The player's hand and the deck are two separate linked lists.
This part of the program behaves kind of strangely. Sometimes it works fine. Other times, it discards cards that were meant to be kept, or keeps card that were meant to be discarded. It sometimes also changes the suit of some of the cards (or maybe it's duplicating certain cards, I'm not sure).
This is the function that creates the deck:
card *
createCard(int n)
{
int i = 0;
card *head = (card *) malloc(sizeof(card));
card *tmp = NULL;
card *p = NULL;
p = head;
for (i = 0; i < n - 1; i++) {
tmp = (card *) malloc(sizeof(card));
p->next = tmp;
p = p->next;
p->next = NULL;
}
tmp = head;
for (i = 1; i <= 13; i++) {
for (int j = 3; j <= 6; j++) {
tmp->face = i;
tmp->suit = j;
tmp = tmp->next;
}
}
return (head);
}
This is the function that creates the player's hand(by creating a new linked list from the first five nodes of the deck list):
void
createHand(card ** deck, card ** hand)
{
card *tmp = NULL;
card *card = *deck;
int i;
//while (card != NULL)
for (i = 0; i < 5; i++) {
(*deck) = (*deck)->next;
tmp = card->next;
card->next = *hand;
*hand = card;
card = tmp;
}
(*hand)->next->next->next->next->next = NULL;
return;
}
This is the section of code that's not working(note: playerHand is the player's hand, first is the deck):
i = 1;
// this array keeps track of which cards the user wants to keep
int a[] = { 0, 0, 0, 0, 0 };
while (i <= 5) {
printf("Pick cards (between 1-5) to hold (-1 to stop): ");
scanf("%d", &keep);
// breaks from loop if the user enters -1
if (keep == -1)
break;
if (keep == 0 || keep > 5 || keep <= -2) {
printf("Invalid index. Pick cards (between 1-5) to hold (-1 to stop): ");
scanf("%d", &keep);
if (keep == -1)
break;
}
if (keep == -1) {
break;
}
if (keep != -1) {
// when player wants to keep a card, the corresponding index of that
// card is set to one in the array
a[keep - 1] = 1;
}
i++;
}
card *tmp;
tmp = first;
card *tmp2;
tmp2 = playerHand;
for (i = 0; i < 5; i++) {
// if the corresponding index in the array is 0, that card is replaced
if (a[i] == 0) {
tmp2->face = tmp->face;
tmp2->suit = tmp->suit;
}
first = first->next;
free(tmp);
tmp = first;
tmp2 = tmp2->next;
}
The cards don't change when this section of code is removed, so the error must be here somewhere, I'm just not sure where.
This is what the output would look like while the player is choosing which cards to keep. In this case, the player is choosing to keep the first and third cards and discarding the other three:
Pick cards (between 1-5) to hold (-1 to stop): 1
Pick cards (between 1-5) to hold (-1 to stop): 3
Pick cards (between 1-5) to hold (-1 to stop): -1