Dealing cards

Hi,

So I have a three-part project. I've already done the first part which entailed unwrapping the deck to show that it was in ascending order and a shuffle function that shuffled the deck. I thankfully aced that, according to my instructor.

Now I have to add a deal function to the program that is suppose to deal 13 different cards to each of the four players from the deck of 52 cards. As you can see, I'm having a bit of trouble with this. My output looks something like this for the deal function:

0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10 11 12
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  #include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>

using namespace std;

//Global constants
const int COLS=13;
const int ROWS=4;

//Create Function prototypes for card loading and shuffling deck of cards
void unwrap(vector<int> &);
void shuffle(vector<int> &);
void printCards(vector<int>);

//Function for dealing cards to players
void deal(const int [][COLS], int);


int main() {

    //Declare vector for the number of cards
    vector <int> deck;

    //Declare our 2D Array for dealing cards to players
    int player1[ROWS][COLS] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    int player2[ROWS][COLS] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    int player3[ROWS][COLS] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    int player4[ROWS][COLS] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

    cout << "The deck before shuffling: " << endl;
    unwrap(deck);
    printCards(deck);

    cout << "The deck after shuffling: " << endl;
    shuffle(deck);
    printCards(deck);

    //The contents of our deal cards function are
    deal(player1, ROWS);
    deal(player2, ROWS);
    deal(player3, ROWS);
    deal(player4, ROWS);

    return 0;
}

//Function definitions that load cards and randomly shuffles them
void unwrap(vector<int> &deck)
{
    //Load vector with ints from 0 to 51
    for (int i = 0; i <= 51; i++)
    {
        deck.push_back(i);
    }

}

// Randomize the cards in the deck
void shuffle(vector<int> &deck)
{
    random_shuffle(deck.begin(), deck.end());

}
void printCards(vector<int> deck)
{
    for(int j=0; j<deck.size(); j++)
    {
        cout<< deck[j] << endl;
    }
}
void deal(const int numbers[][COLS], int rows)
{
    for (int x = 0; x < rows; x++)
    {
        for (int y = 0; y < COLS; y++)
        {
            cout<<setw(4) << numbers [x][y] << " ";
        }
        cout << endl;
    }
}
Last edited on
Do you need to use a 2D array for this?
closed account (E0p9LyTq)
You are creating 4 2D arrays in line 27-30. You need only one.

You are already using vectors, use a 2D vector to hold the players' hands.

random_shuffle() will always sort the numbers the same way, you are not seeding the C library rand() function by using srand().

random_shuffle() was deprecated in C++14, and removed in C++17. Using shuffle() instead.

shuffle() requires using C++ random engines.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <random>
#include <chrono>

//Global constants
const int NUM_COLS  = 13;
const int NUM_ROWS  = 4;
const int NUM_CARDS = 52;

//Create Function prototypes for card loading and shuffling deck of cards
void unwrap(std::vector<int>&);
void shuffle(std::vector<int>&);
void printCards(const std::vector<int>&);

//Function for dealing cards to players
void deal(std::vector<std::vector<int>>&, const std::vector<int>&);
void showHands(const std::vector<std::vector<int>>&);


int main()
{
   //Declare vector to hold a deck of cards
   std::vector<int> deck;

   std::cout << "The deck before shuffling:\n";
   unwrap(deck);
   printCards(deck);

   std::cout << "\nThe deck after shuffling:\n";
   shuffle(deck);
   printCards(deck);

   //Declare our 2D Array for dealing cards to players
   std::vector<std::vector<int>> players(NUM_ROWS, std::vector<int>(NUM_COLS, 0));

   std::cout << "\nbefore the cards have been dealt:\n";
   showHands(players);

   //The contents of our deal cards function are
   std::cout << "\nAfter the cards have been dealt:\n";
   deal(players, deck);
   showHands(players);
}

//Function definitions that load cards
void unwrap(std::vector<int> &deck)
{
   //Load vector with ints from 0 to 51
   for (int i = 0; i < NUM_CARDS; i++)
   {
      deck.push_back(i);
   }
}

// shuffles the deck
void shuffle(std::vector<int> &deck)
{
   // if you have to use random_shuffle, seed the C random generator using the current time
   // to get random numbers each run
   // std::srand(std::time(NULL));

   // without initializing the C library rand function random_shuffle will always give the same numbers
   // random_shuffle was deprecated in C++14 and removed in C++17
   // random_shuffle(deck.begin(), deck.end());

   // learn to use the C++ random engines and algorithm
   
   // get a seed based on the current time
   unsigned seed = static_cast<unsigned> (std::chrono::system_clock::now().time_since_epoch().count());
   
   // create a random engine
   std::default_random_engine URNG;
   
   // seed the engine
   URNG.seed(seed);
   
   // shuffle the cards
   std::shuffle(deck.begin(), deck.end(), URNG);
}

void printCards(const std::vector<int>& deck)
{
   /*for (int j = 0; j<deck.size(); j++)
   {
      cout << deck[j] << '\t';
   }*/
   
   // ranged based for loops makes it harder to go out of bounds
   for (const auto itr : deck)
   {
      std::cout << itr << '\t';
   }
   std::cout << '\n';

   // another for loop that is hard to go out of bounds/
   /*for (auto itr = deck.cbegin(); itr != deck.cend(); itr++)
   {
      std::cout << *itr << '\t';
   }
   std::cout << '\n';*/
}

void deal(std::vector<std::vector<int>>& players, const std::vector<int>& deck)
{
   int card_count = 0;
   
   // deal one card to each player in order, repeat until the entire deck is dealt
   for (int cols = 0; cols < NUM_COLS; cols++)
   { 
      for (int rows = 0; rows < NUM_ROWS; rows++)
      {
         players[rows][cols] = deck[card_count];
         card_count++;
      }
   }
}

void showHands(const std::vector<std::vector<int>>& players)
{
   for (int rows = 0; rows < NUM_ROWS; rows++)
   {
      std::cout << "Player " << rows + 1 << ": ";
      for (int cols = 0; cols < NUM_COLS; cols++)
      {
         std::cout << players[rows][cols] << ' ';
      }
      std::cout << '\n';
   }
}

The deck before shuffling:
0       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

The deck after shuffling:
42      19      5       23      0       12      11      22      46      40
43      36      17      30      16      41      4       32      18      24
39      15      13      47      48      21      7       33      20      10
50      27      26      9       49      51      6       1       38      45
44      28      34      8       37      14      3       2       25      29
31      35

before the cards have been dealt:
Player 1: 0 0 0 0 0 0 0 0 0 0 0 0 0
Player 2: 0 0 0 0 0 0 0 0 0 0 0 0 0
Player 3: 0 0 0 0 0 0 0 0 0 0 0 0 0
Player 4: 0 0 0 0 0 0 0 0 0 0 0 0 0

After the cards have been dealt:
Player 1: 42 0 46 17 4 39 48 20 26 6 44 37 25
Player 2: 19 12 40 30 32 15 21 10 9 1 28 14 29
Player 3: 5 11 43 16 18 13 7 50 49 38 34 3 31
Player 4: 23 22 36 41 24 47 33 27 51 45 8 2 35


using namespace std; can cause you problems later, especially if you have all your custom function names lower-case.
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
Last edited on
closed account (E0p9LyTq)
Initializing a 2D array on creation:
1
2
3
4
   int array_players[NUM_ROWS][NUM_COLS] = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, },
                                             { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, },
                                             { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, },
                                             { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 } };
You should teach this, because you do a really great job of breaking everything down to me in a way that helps me to actually understand what is going on. I cannot thank you enough.

What is the zero doing at the end of declaration for the 2d vector?
Last edited on
Topic archived. No new replies allowed.