struct - I need help shuffling integers in blackjack, C -
i have 3 different functions, 1 fill deck, 1 shuffle , 1 deal cards. have implemented int value in function filldeck , that's working fine. when shuffling value in same function deck not working, don't errors when debug visual studio says it's unable read memory. doing wrong?
filldeck function
void filldeck(card * const deck, const char *suit[], const char *deck[], const int *value[]){ int s; (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; (i = 0; < 52; i++){ j = rand() % 52; temp = deck[i]; deck[i] = deck[j]; deck[j] = temp; value[i] = value[j]; // <-- 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 1 > "); scanf("%s", name1); printf("name player 2 > "); scanf("%s", name2); printf("\nwelcome %s , %s, lets begin!\n\n", name1, name2); getchar(); printf("%s's card:\n", name1); (i = 0; < 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
since value part of struct card, sufficient shuffle array of cards alone. can't see full code, think there no need of shuffle 'valus'. adapt function shuffle this.
void shuffle( card * const deck ){ (int = 0; < 52; i++){ int j = rand() % 52; card temp = deck[i]; deck[i] = deck[j]; deck[j] = temp; } return; } according change function deal this:
void deal(const card * const deck, int size, int size_1, int size_2) { ... printf("%s's card:\n", name1); (i = 0; < size; i++){ printf printf("%5s of %-8s %d %d", deck[i].decks, deck[i].suits, deck[i].value, (i + 1) % 2 ? '\t' : '\n'); // ^^^^^^^ } ... } apart have change type of 4th parameter of function filldeck const int *value[] const int value[], because cardvalue array of int , not array of int*
void filldeck(card * const deck, const char *suit[], const char *deck[], const int value[] ); // ^
Comments
Post a Comment