/*
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 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() {
const ranks = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"];
const suits = ["Clubs", "Diamonds", "Hearts", "Spades"];
const deck = [];
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) {
for (let i = 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, drawNumber) {
// Shuffle the remaining deck before this draw
shuffleDeck(deck);
console.log(`Draw #${drawNumber}:`);
for (let i = 0; i < 5; i++) {
console.log(" " + deck[i]);
}
console.log();
// Remove drawn cards
deck.splice(0, 5);
}
function main() {
// Number of draws
const draws = 3;
const deck = buildDeck();
// Perform multiple draws (no duplicates)
for (let i = 1; i <= draws; i++) {
if (deck.length < 5) {
console.log(`Not enough cards left for draw #${i}.`);
break;
}
drawFive(deck, i);
}
}
main();
/*
run:
Draw #1:
8 of Diamonds
J of Spades
6 of Clubs
J of Diamonds
8 of Hearts
Draw #2:
3 of Clubs
4 of Diamonds
5 of Clubs
Q of Spades
10 of Spades
Draw #3:
6 of Hearts
10 of Hearts
K of Hearts
J of Hearts
9 of Spades
*/