Below is a C++ implementation using the Fisher-Yates shuffle algorithm for a deck of cards, suitable for games like Yu-Gi-Oh or Pokémon.
#include <iostream>
#include <vector>
#include <random>
#include <ctime>
#include <string>
// Card structure
struct Card {
std::string name;
std::string type;
// Add attack, defense, etc. as needed
};
// Fisher-Yates shuffle
std::vector<Card> shuffleDeck(std::vector<Card> deck) {
std::random_device rd;
std::mt19937 g(rd());
for (int i = deck.size() - 1; i > 0; --i) {
std::uniform_int_distribution<> distrib(0, i);
int j = distrib(g);
std::swap(deck[i], deck[j]);
}
return deck;
}
// Draw top card
Card drawCard(std::vector<Card>& deck) {
if (deck.empty()) {
std::cout << "Deck is empty!\n";
return Card{"", ""};
}
Card drawn = deck.back();
deck.pop_back();
return drawn;
}
int main() {
std::vector<Card> deck = {
{"Monster A", "Monster"},
{"Monster B", "Monster"},
{"Spell Card", "Spell"},
// Add more cards here
};
deck = shuffleDeck(deck);
Card drawn = drawCard(deck);
std::cout << "Drew card: " << drawn.name << "\n";
return 0;
}
deck vector in main() with your actual card data.