// deck of cards
// below are initializations
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
int main()
{
ofstream myfile; //setup for copy to text file
const char usdeck[4][13][14] = //create 3d array of 52 cards
{
{"heart two", "heart three", "heart four",
"heart five", "heart six", "heart seven",
"heart eight","heart nine", "heart ten",
"heart jack","heart queen", "heart king",
"heart ace"},
{"diamond two", "diamond three", "diamond four",
"diamond five", "diamond six", "diamond seven",
"diamond eight", "diamond nine", "diamond ten",
"diamond jack", "diamond queen", "diamond king",
"diamond ace"},
{"club two", "club three", "club four", "club five",
"club six", "club seven", "club eight", "club nine",
"club ten", "club jack", "club queen", "club king",
"club ace"},
{"spade two", "spade three", "spade four",
"spade five", "spade six", "spade seven",
"spade eight", "spade nine", "spade ten",
"spade jack", "spade queen", "spade king",
"spade ace"}
};
for(int row=0;row<4; row++)
{
for(int column=0;column<13;column++)
{
for(int element=0;element<14;element++)
{
cout << usdeck[row][column][element] << " ";
}
cout <<endl;
}
}
myfile.open("UnshuffledDeck.txt");//creates a text file to place unshuffled deck into
for(int row=0;row<4; row++)
{
for(int column=0;column<13;column++)
{
for(int element=0;element<14;element++)
{
myfile << usdeck[row][column][element] << " ";
//this creates the unshuffled deck text file
}
myfile <<endl;
}
}
myfile.close(); //closes unshuffled deck text file
return 0;
}
void Shuffle()
{
int temp;
char theDeck[4][13];
srand(time(0));
for (int i=0; i<=51; i++)
{
int j = 1 + rand()%52;
int k = 1 + rand()%52;
temp = theDeck[j];
theDeck[j]=theDeck[k];
theDeck[k]=temp;
}
}
I am trying to shuffle the cards in my deck.. I wrote the below function Shuffle which I believe will shuffle a deck of cards but I am not sure on how to implement it..My "shuffled" deck needs to be implemented in a 2D array.. please help!
std::string myCards[52] = { "heart two", "heart three", etc ...}. Then all you would then need isstd::random_shuffleinstead of all of this code. Your requirements of shuffling a 2d deck makes no sense in the real world of cards, since no one shuffles just the clubs, hearts, spades, and diamonds. You shuffle the entire deck.