/*
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 picks 5 random cards from a standard 52‑card deck.
It uses:
- string[] arrays to store ranks, suits, and the deck
- Fisher–Yates shuffle for efficient randomization
- Typed functions for clarity and safety
Algorithm:
1. Build the deck (52 strings).
2. Shuffle using Fisher–Yates.
3. Print the first 5 cards.
*/
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[] = [];
// Combine each rank with each suit → 52 unique cards
for (const suit of suits) {
for (const rank of ranks) {
deck.push(`${rank} of ${suit}`);
}
}
return deck;
}
function shuffleDeck(deck: string[]): void {
// Fisher–Yates shuffle: efficient and unbiased
for (let i: number = deck.length - 1; i > 0; i--) {
const j: number = Math.floor(Math.random() * (i + 1));
const temp: string = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
}
function main(): void {
// Step 1: Build the deck
const deck: string[] = buildDeck();
// Step 2: Shuffle the deck
shuffleDeck(deck);
// Step 3: Draw the first 5 cards
console.log("Your 5 random cards:");
for (let i: number = 0; i < 5; i++) {
console.log(deck[i]);
}
}
main();
/*
run:
Your 5 random cards:
7 of Hearts
K of Spades
8 of Spades
4 of Diamonds
8 of Diamonds
*/