Card Shuffling and Dealing Simulation

In my reference book i studied the sample program in which the problem is that Write a program to randomly shuffle the deck of cards and to deal it out.
Some Facts of Card Games:
There are 4 suits in one deck: Hearts, Spades, Diamonds and Clubs.

Each suit has 13 cards: Ace, Deuce, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen and King.
As obvious from the problem statement, we are dealing with the deck of cards, required to be identified. A card is identified by its suit i.e. it may be one of the Hearts, Spades, Diamonds or Clubs. Also every card has one value in the range starting from Ace to King. So we want to identify them in our program and our requirement is to use English like ‘five of Clubs’. We will declare one array of suit like:

const char *suite[4] = {“Hearts”, “Diamonds”, “Clubs”, “Spades” };

The second array is of values of cards:

const char *face[13] = { “Ace”, “Deuce”, “Three”, “Four”, “Five”, “Six”, “Seven”, “Eight”, “Nine”, “Ten”, “Jack”, “Queen” and “King”};



A deck has 13 * 4 = 52 cards in total.
Below is the complete code of this program.
#include <iostream.h>
#include <stdlib.h>
#include <time.h>

void shuffle( int [] [ 13 ]);
void deal( const int [][ 13 ], const char *[], const char *[]);

int main()
{
const char *suite[ 4 ] = {"Hearts", "Diamonds", "Clubs", "Spades" };

const char *face[ 13 ] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
int deck[ 4 ][ 13 ] = { 0 };

srand( time( 0 ) );

shuffle( deck );
deal( deck, face, suite );

return 0;
}

void shuffle( int wDeck[][13] )
{
int row, column, card;

for ( card = 1; card <= 52; card++){
do{
row = rand() % 4;
column = rand() % 13;
} while( wDeck [ row ][ column ] != 0 );
wDeck[ row ][ column ] = card;
}
}

void deal( const int wDeck[][ 13 ], const char *wFace[], const char *wSuit[])
{
int card, row, column;
const char *space;
for ( card = 1; card <= 52; card++ )
for( row = 0; row <= 3; row++)
for( column = 0; column <= 12; column++)
if( wDeck[ row ][ column ] == card )
cout << card << ". " <<wFace[ column ] << " of " << wSuit
[row ] << '\n';

}
It is also mentioned in the book that one can also see the condition inside the ‘while statement, ‘wDeck[ row ][ column ] != 0 ‘. This is to ensure that we don’t overwrite row and column, which has already been occupied by some card. How we can't overwrite row and column which has already been occupied by some card through this conditional statement: ‘wDeck[ row ][ column ] != 0 ‘?
hassan236 wrote:
#include <iostream.h>

How old is that reference book? <iostream.h> hasn't been legal C++ for years, not since C++ was standardized with C++98.
A deck of cards is a one-dimensional structure.
I suggest you stop trying to learn using that book and look at an online C++ tutorial.

http://www.cplusplus.com/doc/tutorial/
http://www.learncpp.com/

For reference needs, C and C++: https://en.cppreference.com/w/
A bit of modern C++ code for creating, shuffling and displaying a deck of cards:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <array>
#include <random>
#include <numeric>

int main()
{
   // create a random engine and randomize it
   // https://en.cppreference.com/w/cpp/numeric/random
   std::default_random_engine rng(std::random_device {} ());

   // create a type alias
   // https://en.cppreference.com/w/cpp/language/type_alias
   using card = unsigned;

   // manufacture a deck of cards
   // https://en.cppreference.com/w/cpp/container/array
   std::array<card, 52> deck;

   // create the cards, 0 (zero) to 51
   // https://en.cppreference.com/w/cpp/algorithm/iota
   std::iota(deck.begin(), deck.end(), 0);

   // lambdas to display the card in text representation
   // https://en.cppreference.com/w/cpp/language/lambda
   auto rank = [] (card c) { return "AKQJT98765432"[c % 13]; };
   auto suit = [] (card c) { return "SHDC"[c / 13]; };

   // declare a variable and set the value using "uniform initialization"
   // https://mbevin.wordpress.com/2012/11/16/uniform-initialization/
   card count { };

   // display the original deck
   // https://en.cppreference.com/w/cpp/language/range-for
   for (card c : deck)
   {
      std::cout << rank(c) << suit(c) << ' ';
      count++;

      if (0 == (count % 13)) { std::cout << '\n'; }
   }
   std::cout << '\n';

   // shuffle the deck
   // https://en.cppreference.com/w/cpp/algorithm/random_shuffle
   std::shuffle(deck.begin(), deck.end(), rng);

   // display the shuffled deck
   // this "for" statement requires C++20
   for (card count { }; card c : deck)
   {
      std::cout << rank(c) << suit(c) << ' ';
      count++;

      if (0 == (count % 13)) { std::cout << '\n'; }
   }
}
Based upon original code perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <cstdlib>
#include <ctime>

void shuffle(unsigned[4][13]);
void deal(const unsigned[4][13], const char* const [13], const char* const [4]);

int main()
{
	constexpr const char* const suite[4] {"Hearts", "Diamonds", "Clubs", "Spades"};
	constexpr const char* const face[13] {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
	unsigned deck[4][13] {0};

	srand(static_cast<unsigned>(time(nullptr)));

	shuffle(deck);
	deal(deck, face, suite);
}

void shuffle(unsigned wDeck[4][13])
{
	for (unsigned card = 1; card <= 52; ++card) {
		unsigned row {}, column {};

		do {
			row = rand() % 4;
			column = rand() % 13;

		} while (wDeck[row][column]);

		wDeck[row][column] = card;
	}
}

void deal(const unsigned wDeck[4][13], const char* const wFace[13], const char* const wSuit[4])
{
	for (unsigned card = 0; card < 52; ++card) {
		const auto& drawn {wDeck[card % 4][card % 13]};

		std::cout << card + 1 << ". " << wFace[drawn % 13] << " of " << wSuit[drawn % 4] << '\n';
	}
}

Topic archived. No new replies allowed.