I'm looking for a simple way to shuffle my deck of cards. I have finished the deck, what's a simple way to create a function that shuffles the 52 cards? I'm not looking for complex ways to shuffle my deck, I prefer code that I can read with ease. I've tried to put rand() on the cout
part, but it just randomizes both suits and faces and I get the wrong cards.
struct Card
{
int value;
char suit;
};
Card cards[52]; //global variables
int main()
{
int play;
shuffleDeck();
cout << endl<<endl;
cout << "---------------------------" << endl;
cout << "|Welcome to The Game Of 31| " << endl;
cout << "---------------------------" << endl;
cout << "Would you like to play? (1 for 'YES', 0 for 'NO'): ";
cin >> play;
if (play == 1) { shuffleDeck(); }
else { cout << "See yaa!"; exit(0); }
}
void shuffleDeck() {
for (int i = 0; i < 52; i++)
{
cards[i].value = i % 13; // 13 values
if (cards[i].value == 0) {
cards[i].value = 1;
}
else if (cards[i].value == 1) {
cards[i].value = 2;
}
else if (cards[i].value == 2) {
cards[i].value = 3;
}
else if (cards[i].value == 3) {
cards[i].value = 4;
}
else if (cards[i].value == 4) {
cards[i].value = 5;
}
else if (cards[i].value == 5) {
cards[i].value = 6;
}
else if (cards[i].value == 6) {
cards[i].value = 7;
}
else if (cards[i].value == 7) {
cards[i].value = 8;
}
else if (cards[i].value == 8) {
cards[i].value = 9;
}
else if (cards[i].value == 9) {
cards[i].value = 10;
}
else if (cards[i].value == 10) {
cards[i].value = 10;
}
else if (cards[i].value == 11) {
cards[i].value = 10;
}
else if (cards[i].value == 12) {
cards[i].value = 10;
}
cards[i].suit = i / 13;// 4 suits
if (cards[i].suit == 0) {
cards[i].suit = 'D';
}
else if (cards[i].suit == 1) {
cards[i].suit = 'H';
}
else if (cards[i].suit == 2) {
cards[i].suit = 'C';
}
else if (cards[i].suit == 3) {
cards[i].suit = 'S';
}
}
for (int count = 0; count < 52; count++) {
int count2 = 0;
cout << " | " << cards[count].value;
cout << cards[count].suit << " | ";
if (count == 13) { cout << endl; }
else if (count == 26) { cout << endl; }
else if (count == 39) { cout << endl; }
else if (count == 52) { cout << endl; }
}
}
Aucun commentaire:
Enregistrer un commentaire