So i made an array of [312] because that is the max of cards a player can have (52 cards * 6 decks)
but when i run it it, it says it succeeds but then it corrupts !... what am i doing wrong?
Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//struct datatype
struct card{
char *face;
char *suit;
};
typedef struct card Card; //typedef keyword allows us to create a synonym for a previously defined data type
struct shoe{
struct card Cards[312];
int totalCards;
};
typedef struct{
Card cards[312];
int is_shuffled;
}Deck;
void shuffle(Card d[312], int numdecks){
int totalCards = numdecks * 52;
int i, num;
struct card temp;
srand(time(NULL));
for(i = 0; i<totalCards; i++){
num = rand() % totalCards;
temp = d[i];
d[i] = d[num];
d[num] = temp;
}
}
void print_deck(Card d[312], int numdecks){
//printf("%d\n", numdecks);
int i, j;
int totalCards = numdecks * 52;
for(j = 0; j < numdecks; j++){
for(i = 0; i < 52; i++){
printf("%s of %s\n", d[i].face, d[i].suit);
}
}
printf("\n\n");
//printf("Dealer up-card\n");
//printf(" %s of %s\n", d[0].face, d[0].suit); // dealer 1 card
//printf("%d\n", numplayers);
printf("\n\n");
}
void main(){
Deck my_deck;
int i,j;
char *suits[4] = {"Spades", "Clubs", "Hearts", "Diamonds"};
char *faces[13] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
my_deck.is_shuffled = 0;
for(i=0; i<4; i++){
for(j=0; j<13; j++){
my_deck.cards[(i*13) + j].face = faces[j];
my_deck.cards[(i*13) + j].suit = suits[i];
}
}
//print_deck(my_deck.cards);
int numplayers;
int numdecks;
char outputfile[30];
printf("What is the name of the output file: ");
gets(outputfile);
printf("How many players are at the table (between 1 and 6): ");
scanf("%d", &numplayers);
while(numplayers > 6 || numplayers < 0){
printf("How many players are at the table (between 1 and 6): ");
scanf("%d", &numplayers);
}
printf("How many decks are in the shoe (between 1 and 6): ");
scanf("%d", &numdecks);
while(numdecks > 6 || numdecks < 0){
printf("How many decks are in the shoe (between 1 and 6): ");
scanf("%d", &numdecks);
}
//print_deck(my_deck.cards, numdecks);
shuffle(my_deck.cards, numdecks);
print_deck(my_deck.cards, numdecks);
}

New Topic/Question
Reply




MultiQuote






|