2D Array Dealing Deck of Cards

I am trying to find a more efficient way of "dealing" the cards (lines 57 - 118). I only need to deal 2 hands, and they need to be in order (1st, 3rd, 5th ... etc. card for first hand, and 2nd, 4th...etc card for second hand). Is there a more efficient way of doing so with a different loop or adjusting the current loop? I tried to dabble with a switch statement, but couldn't figure it out for this situation. It is for a class assignment requiring use of a 2D array, we have not covered classes, objects, or vectors yet so they can't be used for this particular program. Thanks in advance.

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152


#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <ctime>

using namespace std;
const int ROWS(4),
      COLS(13),
      NUM_CARDS(52);
string suit[] = {"Clubs", "Diamond", "Hearts", "Spades"};
string face[] = {"Ace", "Duece", "Three", "Four",
             "Five", "Six", "Seven", "Eight",
             "Nine", "Ten", "Jack", "Queen", "King"};
int deck[ROWS][COLS]; //declared globally, initialized to 0
void shuffleDeck(int shuffleArray[][COLS], int);
void dealHands(int dealArray[][COLS], int);
bool found;

int main()
{
int pos(1);
for (int r=0; ! found && r !=ROWS; r++) //setting deck[] positions to 1-52
    {
        for(int c = 0; ! found && c !=COLS; c++)
            {
                deck[r][c] = pos++;
            }
    }

srand(time(NULL)); //used for random number generator to shuffle


shuffleDeck(deck, ROWS);//shuffle deck



for (int n=0; n <= NUM_CARDS; n++) //display shuffled deck
{
    found = false;
    for (int r=0;  !found && r !=ROWS; r++)
    {
        for(int c=0;  !found && c !=COLS; c++)
        {
            if (deck[r][c] == n)
            {
                cout << deck[r][c] << " " << face[c] << " of " << suit[r] << endl;
                found=true;
            }
        }
    }
}

//display hands
cout << "***** HAND 1 *****" << endl;
for (int r=0; r < ROWS; r++)
for (int c=0; c<COLS; c++)
    {
        if (deck[r][c] == 1 )
            cout << face[c] << " of " << suit[r] << endl;
    }
        for (int r=0; r < ROWS; r++)
for (int c=0; c<COLS; c++)
    {
        if (deck[r][c] == 3 )
            cout <<  face[c] << " of " << suit[r] << endl;
    }
        for (int r=0; r < ROWS; r++)
for (int c=0; c<COLS; c++)
    {
        if (deck[r][c] == 5 )
            cout << face[c] << " of " << suit[r] << endl;
    }
        for (int r=0; r < ROWS; r++)
for (int c=0; c<COLS; c++)
    {
        if (deck[r][c] == 7 )
            cout << face[c] << " of " << suit[r] << endl;
    }
        for (int r=0; r < ROWS; r++)
for (int c=0; c<COLS; c++)
    {
        if (deck[r][c] == 9 )
            cout << face[c] << " of " << suit[r] << endl;
    }
cout << "***** HAND 2 *****" << endl;
for (int r=0; r < ROWS; r++)
    for (int c=0; c<COLS; c++)
    {
        if (deck[r][c] == 2 )
            cout << face[c] << " of " << suit[r] << endl;
    }
for (int r=0; r < ROWS; r++)
    for (int c=0; c<COLS; c++)
    {
        if (deck[r][c] == 4 )
            cout <<  face[c] << " of " << suit[r] << endl;
    }
for (int r=0; r < ROWS; r++)
    for (int c=0; c<COLS; c++)
    {
        if (deck[r][c] == 6 )
            cout << face[c] << " of " << suit[r] << endl;
    }
for (int r=0; r < ROWS; r++)
    for (int c=0; c<COLS; c++)
    {
        if (deck[r][c] == 8 )
            cout << face[c] << " of " << suit[r] << endl;
    }
for (int r=0; r < ROWS; r++)
    for (int c=0; c<COLS; c++)
    {
        if (deck[r][c] == 10 )
            cout << face[c] << " of " << suit[r] << endl;
    }


}//end main

void shuffleDeck(int shuffleArray[][COLS], int numRows)
{
for (int n=0; n <= NUM_CARDS; n++)
{
    for (int row = 0; row < numRows; row++)
    {
        for (int col=0; col < COLS; col++)
        {
            if (deck[row][col] == n)
            {
            int temp(0);
            int cardvalue = rand() % 13 ;
            int cardsuit = rand() % 4 ;

            temp = deck[row][col];
            deck[row][col] = shuffleArray[cardsuit][cardvalue];
            shuffleArray[cardsuit][cardvalue] = temp;
            }
        }
    }
}






}//end shuffleDeck

Last edited on
Bump.
You don't need face values or suits until you display the hands:

Your deck, from which you deal, is an array of 52 int, 0 to 51;

You shuffle them by a single pass through the array, switching the current value with a value from a random index in the array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// initialize deck:
int deck[52]
for (i = 0; i < 52;i++)
{
deck[i] = i;
}
// shuffle deck:
int itemp;
for (i = 0; i < 52; i++)
{
int pointer = rand % 52;
itemp = deck[pointer];
deck[pointer] = deck[i];
deck[i] = itemp;
}
//deal: naturally could do this in a for (i = 0; i < 5; i++ ) loop //
// if a loop, hand1 gets cards[ i * 2]; hand 2 gets cards[ i*2 +1];
hand1[0] = cards[0]
hand2[0] = cards[1] 
hand1[1] = cards[2] //etc. . . 

//now player 1 gets cards 1,3,5 etc, if you want it that way, or he/she could
// hold an array of cards filled with deck[1], deck[3], etc. values.

// the suits are values from cards[] : 0 to 12 == Clubs, 13 to 25 == Diamonds, 26 to 38 == Hearts, 39 to 51 == Spades.
// for the suit, divide card value by 13: i.e. card value 14 = 14/13 = 1 or Diamonds.
// for the face, use card value % 13; i.e. card value 14 = 14 % 13 = 1 or Deuce.

//so if player 1 gets deck[1] card and deck[1] is 14, he gets the 2 of Diamonds.
Topic archived. No new replies allowed.