/*
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:
- string[] arrays for ranks, suits, and the deck
- Fisher–Yates shuffle for efficient randomization
- Functions for clean structure
Algorithm:
1. Build a full deck (52 cards)
2. For each draw:
a. Shuffle the remaining deck
b. Draw 5 cards
c. Remove them from the deck
*/
function buildDeck(): string[] {
const ranks: string[] = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"];
const suits: string[] = ["Clubs", "Diamonds", "Hearts", "Spades"];
const deck: string[] = [];
for (const suit of suits) {
for (const rank of ranks) {
deck.push(`${rank} of ${suit}`);
}
}
return deck;
}
// Fisher–Yates shuffle (idiomatic and efficient)
function shuffleDeck(deck: string[]): void {
for (let i: number = deck.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[deck[i], deck[j]] = [deck[j], deck[i]];
}
}
function drawFive(deck: string[], drawNumber: number): void {
// Shuffle the remaining deck before this draw
shuffleDeck(deck);
console.log(`Draw #${drawNumber}:`);
for (let i: number = 0; i < 5; i++) {
console.log(" " + deck[i]);
}
console.log();
// Remove drawn cards
deck.splice(0, 5);
}
function main(): void {
// Number of draws
const numberOfDraws: number = 3;
const deck: string[] = buildDeck();
// Perform multiple draws (no duplicates)
for (let drawIndex: number = 1; drawIndex <= numberOfDraws; drawIndex++) {
if (deck.length < 5) {
console.log(`Not enough cards left for draw #${drawIndex}.`);
break;
}
drawFive(deck, drawIndex);
}
}
main();
/*
run:
Draw #1:
J of Spades
J of Diamonds
4 of Diamonds
J of Clubs
7 of Hearts
Draw #2:
6 of Hearts
9 of Spades
2 of Spades
2 of Hearts
10 of Spades
Draw #3:
3 of Hearts
K of Clubs
A of Hearts
7 of Clubs
6 of Spades
*/