Search code examples
cstructshuffleblackjack

I need help shuffling integers in blackjack, C


I have three different functions, one too fill the deck, one too shuffle and one too deal cards. I have implemented my int value in the function fillDeck and that's working fine. But when I shuffling my value in the same function as deck its not working, I don't get any errors but when I debug visual studio says it's unable to read memory. What am I doing wrong?

fillDeck function

void fillDeck(Card * const Deck, const char *suit[], const char *deck[], const int *value[]){

int s;

for (s = 0; s < 52; s++){
    Deck[s].suits = deck[s % 13];
    Deck[s].decks = suit[s / 13];
    Deck[s].value = value[s % 13];
}
return;
}

shuffle function

void shuffle(Card * const Deck, const int *value[]){
int i, j;
Card temp;

for (i = 0; i < 52; i++){
    j = rand() % 52;
    temp = Deck[i];
    Deck[i] = Deck[j];
    Deck[j] = temp;
    value[i] = value[j]; // <-- this is not working
}
return;
}

deal function

void deal(const Card * const Deck, int size, int size_1, int size_2){

int i, j, length;
char anotherCard[2];
char name1[30];
char name2[30];

printf("Name player one > ");
scanf("%s", name1);
printf("Name player two > ");
scanf("%s", name2);

printf("\nWelcome %s and %s, lets begin!\n\n", name1, name2);
getchar();

    printf("%s's card:\n", name1);
    for (i = 0; i < size; i++){
        printf("%5s of %-8s%c", Deck[i].decks, Deck[i].suits, (i + 1) % 2 ? '\t' : '\n');
    }
return;
}

main function

int main(void){

Card allCards[52];
Card cardValue[52];
char *suitname[] = { "spades", "hearts", "diamonds", "clubs" };
char *deckname[] = { "ace", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "jack", "queen", "king" };
int cardvalue[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };

srand(time(NULL));

fillDeck(allCards, suitname, deckname, cardvalue);
shuffle(allCards, cardValue);
deal(allCards, 2, 4, 6);


getchar();
return 0;
}

func.h file

#ifndef FUNC_H
#define FUNC_H

typedef struct card{
    const char *suits;
    const char *decks;
    int value;
};

typedef struct card Card;

void fillDeck(Card * const Deck, char *suit[], char *deck[], const int *value[]);

void shuffle(Card * const Deck, const int *value[]);

void deal(const Card * const Deck, int size, int size_1, int size_2);

#endif

Solution

  • Since value is a part of your struct Card, it is sufficient to shuffle your array of Cards alone. I can't see your full code, but i think there is no need of shuffle any 'valus'. Adapt your function shuffle like this.

    void shuffle( Card * const Deck ){
       for (int i = 0; i < 52; i++){
            int j = rand() % 52;
            Card temp = Deck[i];
            Deck[i] = Deck[j];
            Deck[j] = temp;
        }
        return;
    }
    

    According to this change your function deal like this:

    void deal(const Card * const Deck, int size, int size_1, int size_2) {
        ...
        printf("%s's card:\n", name1);
        for (i = 0; i < size; i++){
            printf to printf("%5s of %-8s %d %d", Deck[i].decks, Deck[i].suits, Deck[i].value, (i + 1) % 2 ? '\t' : '\n'); 
                                                                             // ^^^^^^^
        }
        ...
    }
    

    Apart from this you have to change the type of the 4th parameter of your function fillDeck from const int *value[] to const int value[], because cardvalue is an array of int and not an array of int*

    void fillDeck(Card * const Deck, const char *suit[], const char *deck[], const int value[] );
                                                                                   // ^