2
// 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!

4
  • ewwwww indent the code :) Commented Sep 4, 2014 at 3:38
  • It most likely will not be enough to do just 52 card swaps to shuffle a deck. Your most effective shuffle will be to empty the deck, then randomly put the cards back in. Commented Sep 4, 2014 at 3:39
  • Why isn't this a 1-d array of cards? This is much easier: std::string myCards[52] = { "heart two", "heart three", etc ...}. Then all you would then need is std::random_shuffle instead 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. Commented Sep 4, 2014 at 4:13
  • i have to use a 3d array for unshuffled, and a 2d array for shuffled Commented Sep 4, 2014 at 6:14

2 Answers 2

1

I would recommend implementing your Shuffle routine to use the Knuth Shuffle. The kunth shuffle only requires 52 swaps for a 52 card deck.

The Knuth Shuffle can be generalized to multiple dimensions. However it is easier to shuffle and use it as if it were a 1D array, but access it if it were a 3D array.

Wikipedia has some very simple Pseudo code you could use to implement it:

http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm

If you need help learning how to treat a 1D array as if it were a 3D array, look up some material on Row-Major ordering: https://courses.engr.illinois.edu/ece390/books/artofasm/CH05/CH05-2.html#HEADING2-105

Sign up to request clarification or add additional context in comments.

Comments

1

You can make your life a lot easier if you:

  1. Use an integer between 0 - 51 to represent a card.
  2. Given 0 <= N <= 51, you can treat N/13 as the suite of the card and N%13 as the face of the card.
  3. You can represent the string versions of the suites and faces using static arrays and get the name of the suite and the name of the face of a card by indexing into those static arrays.
  4. You can start out with a deck that is ordered and then shuffle them as many times as you wish using std::shuffle.

Here's a program that does that.

#include <cassert>
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>

// Represent cards by numbers 0 - 51.
// Given 0 <= N <= 51,
// suite = N/13
// face  = N%13.

std::string const& getSuiteName(unsigned int index)
{
   static std::string suites[4] = {"Club", "Diamond", "Heart", "Spade"};
   assert(index < 4);
   return suites[index];
}

std::string const& getFaceName(unsigned int index)
{
   static std::string cards[13] =
   {
      "Two", "Three", "Four", "Five",
      "Six", "Seven", "Eight", "Nine",
      "Ten", "Jack", "Queen", "King", "Ace"
   };
   assert(index < 13);
   return cards[index];
}

unsigned int getSuiteIndex(unsigned int card)
{
   return card/13;
}

unsigned int getFaceIndex(unsigned int card)
{
   return card%13;
}

void printCards(std::vector<unsigned int> const& cards)
{
   std::cout << "---------------\n";
   for(auto card : cards)
   {
      std::cout
         << getSuiteName(getSuiteIndex(card)) << " "
         << getFaceName(getFaceIndex(card)) << std::endl;
   }
   std::cout << "---------------\n\n";
}

void shuffleCards(std::vector<unsigned int>& cards)
{
  // obtain a time-based seed:
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

  // Shuffle the deck.
  std::shuffle(cards.begin(), cards.end(), std::default_random_engine(seed));
}

void testShuffle()
{
   std::vector<unsigned int> cards;
   for ( unsigned int i = 0; i < 52; ++i )
   {
      cards.push_back(i);
   }

   printCards(cards);

   shuffleCards(cards);
   printCards(cards);

   shuffleCards(cards);
   printCards(cards);
}

int main()
{
   testShuffle();
   return 0;
}

4 Comments

this would be a great way to go about this, except i am not allowed to use ANY strings and must use 3d/2d character arrays
Are you also expected to write your own shuffling algorithm?
yea i have to write my own algorithm for shuffling, im not sure if im going about it the right way
That will be a lot of code but I guess this must be your homework :) Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.