#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
/*
A standard deck of 52 playing cards consists of four suits:
spades (♠), hearts (♥), diamonds (♦), and clubs (♣).
Each suit contains 13 ranks:
Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King.
Cards of spades and clubs are black, while hearts and diamonds are red.
Color is informational only — it does not affect shuffling or random selection.
*/
/*
This program performs multiple draws of 5 random cards from a 52‑card deck
WITHOUT duplicates across draws.
It uses:
- Arrays of strings to store ranks, suits, and the deck
- Fisher–Yates shuffle for efficient randomization
- srand(time(NULL)) for randomness
- Functions for clean structure
Algorithm:
1. Build a full deck (52 cards)
2. Shuffle the remaining deck before each draw
3. Draw 5 cards
4. Remove those cards from the deck
*/
#define DECK_SIZE 52
#define DRAW_SIZE 5
void buildDeck(char deck[][20], int *deckCount) {
const char *ranks[] = {
"2","3","4","5","6","7","8","9","10","J","Q","K","A"
};
const char *suits[] = {
"Clubs", "Diamonds", "Hearts", "Spades"
};
int index = 0;
for (int s = 0; s < 4; s++) {
for (int r = 0; r < 13; r++) {
snprintf(deck[index], 20, "%s of %s", ranks[r], suits[s]);
index++;
}
}
*deckCount = DECK_SIZE;
}
void shuffleDeck(char deck[][20], int deckCount) {
for (int i = deckCount - 1; i > 0; i--) {
int j = rand() % (i + 1);
char temp[20];
strcpy(temp, deck[i]);
strcpy(deck[i], deck[j]);
strcpy(deck[j], temp);
}
}
void drawFive(char deck[][20], int *deckCount, int drawNumber) {
printf("Draw #%d:\n", drawNumber);
for (int i = 0; i < DRAW_SIZE; i++) {
printf(" %s\n", deck[i]);
}
printf("\n");
// Remove drawn cards by shifting the deck
for (int i = DRAW_SIZE; i < *deckCount; i++) {
strcpy(deck[i - DRAW_SIZE], deck[i]);
}
*deckCount -= DRAW_SIZE;
}
int main() {
// Number of draws
int draws = 3;
char deck[DECK_SIZE][20];
int deckCount = 0;
srand((unsigned)time(NULL));
// Build the deck
buildDeck(deck, &deckCount);
// Perform multiple draws (no duplicates)
for (int i = 1; i <= draws; i++) {
if (deckCount < DRAW_SIZE) {
printf("Not enough cards left for draw #%d.\n", i);
break;
}
shuffleDeck(deck, deckCount);
drawFive(deck, &deckCount, i);
}
return 0;
}
/*
run:
Draw #1:
5 of Hearts
9 of Diamonds
6 of Diamonds
4 of Hearts
3 of Spades
Draw #2:
2 of Hearts
8 of Diamonds
K of Hearts
A of Hearts
J of Clubs
Draw #3:
3 of Diamonds
2 of Diamonds
10 of Spades
A of Diamonds
10 of Hearts
*/