Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help with shuffle function and give 8 cards to user and computer from shuff

ID: 3694817 • Letter: N

Question

Need help with shuffle function and give 8 cards to user and computer from shuffle deck?

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

typedef struct card_s{
    char suit;
    int face;
    struct card_s *listp;
} card;

void card_create(card* thisNode, char cardSuit, int cardFace, card* nextLoc) {
    thisNode->suit = cardSuit;
    thisNode->face = cardFace;
    thisNode->listp = nextLoc;
    return;
}
void card_insertAfter(card* thisNode, card* newNode) {
    card* tmpNext = NULL;
    tmpNext = thisNode->listp;
    thisNode->listp = newNode;
    newNode->listp = tmpNext;
    return;
}

void card_Print(card* thisNode) {
    if (thisNode->face == -1) {
        printf(" ");
    }
    else if (thisNode->face == 1) {
        printf("A");
    }
    else if (thisNode->face == 11) {
        printf("J");
    }
    else if (thisNode->face == 12) {
        printf("Q");
    }
    else if (thisNode->face == 13) {
        printf("K");
    }
    else {
        printf("%d", thisNode->face);
    }
    if (thisNode->suit == ' ')
        printf("");
    else if (thisNode->suit == 'H')
        printf( " of Hearts ");
    else if (thisNode->suit == 'C')
        printf(" of Clubs ");
    else if (thisNode->suit == 'D')
        printf(" of Diamonds ");
    else if (thisNode->suit == 'S')
        printf(" of Spades ");
    return;
}

void swap(card *a, card *b) {
    card temp = *a;
    *a = *b;
    *b = temp;
}

void delnode(card *head, int coordinate)
{
    card *current = head;
    card *victim = current;
    int count = 1;
  
    if (coordinate == 0) {
        head = current->listp;
        free(victim);
        return;
    }
    while (current != NULL)
    {
        if (count == coordinate) {
            victim = current->listp;
            current->listp = current->listp->listp;
            free(victim);
            return;
        }
        count++;
        current = current->listp;
    }
}
card* card_GetNext(card* thisNode) {
    return thisNode->listp;
}

void printintro() {
    printf("|------- ------- ------- -------| ");
    printf("|------- ------- ------- -------| ");
    printf("     Let's play Crazy Eight ");
    printf("|------- ------- ------- -------| ");
    printf("|------- ------- ------- -------| ");
  
    printf("|Simple Rules                           | ");
    printf("|Card(s) that can be played             | ");
    printf("|-Suit matched with top card or         | ");
    printf("|-Face matched with top card or         | ");
    printf("|------- ------- ------- ------- -------| ");
    printf("|------- ------- ------- ------- -------| ");
  
}
void shuffle_cards(card *h, int n) {
    card *D_1 = h;
    srand(time(NULL));
    int i = n - 1;
    while (i > 0){
        i--;
        int j = rand() % (i + 1);
        swap(&D_1[i], &D_1[j]);
    }
}
int main() {
    int deck[52];
    char username[25];
    int random = 0;
    int i = 0;
    card* headObj = NULL;
    card* currObj = NULL;
    card* lastObj = NULL;
    card userhand[8];
    card comphand[8];
  
    printintro();
    printf("Enter your name: ");
    scanf("%s", &username);
  
    printf(" Main Deck ");
    headObj = (card*)malloc(sizeof(card));
    card_create(headObj, ' ', -1, NULL);
    lastObj = headObj;
    for (int j = 0; j < 4; j++) {
        for (i = 1; i < 14; ++i) {
            if (j == 0) {
                currObj = (card*)malloc(sizeof(card));
                card_create(currObj, 'H', i, NULL);
                card_insertAfter(lastObj, currObj);
                lastObj = currObj;
            }
            else if (j == 1) {
                currObj = (card*)malloc(sizeof(card));
                card_create(currObj, 'D', i, NULL);
                card_insertAfter(lastObj, currObj);
                lastObj = currObj;
            }
            else if (j == 2) {
                currObj = (card*)malloc(sizeof(card));
                card_create(currObj, 'S', i, NULL);
                card_insertAfter(lastObj, currObj);
                lastObj = currObj;
            }
            else if (j == 3) {
                currObj = (card*)malloc(sizeof(card));
                card_create(currObj, 'C', i, NULL);
                card_insertAfter(lastObj, currObj);
                lastObj = currObj;
            }
        }
    }
    currObj = headObj;
    while (currObj != NULL) {
        card_Print(currObj);
        currObj = card_GetNext(currObj);
    }
    printf(" ");
    return 0;
}

Explanation / Answer

void shuffle_cards(card *h, int n) {
    card *D_1 = h;
    srand(time(NULL));
    int i = n - 1;
    while (i > 0){
        i--;
        int j = rand() % (13);
        swap(&D_1[i], &D_1[j]);
    }
}

since we have cards named from 1 to 13 .but you are given 1 to 52. but we are taking card in range betwwen 1 to 13 named cards.

i think this is a the main fault with the function.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote