Sorting cards with text descriptions

Pages: 12
You don't want to just use std::swap? swap(deck[card], deck[firstCard]);
Your personal swap should take vector<int>& for it to match with first argument.

Again, for sorting, why not use std::sort? sort(deck.begin(), deck.end());

As per my earlier comments, I personally would've preferred this way of printing out the cards, so that all the 2's come before all the aces:
1
2
3
4
5
6
7
8
const string RANKS[] = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
                        "Jack", "Queen", "King", "Ace"};
const string SUITS[] = {"Clubs", "Diamonds", "Hearts", "Spades" };

void ShowCard(int i)
{
    cout << RANKS[i/4] << " of " << SUITS[i%4];
}

Unless you like the King of Clubs being the top card...
Using std::swap and std::sort does seem easier, unfortunately I haven't been taught to use those sorts of iterations.

Is it really as simple as simply placing "std::sort" inside of the sort definition? If so, I'll just get the credits taken off for using it, because it makes coding it much easier.

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>

using namespace std;


//Global constants
const int NUM_CARDS = 52;
const int NUM_PLAYERS=4;

const string RANKS[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                        "Jack", "Queen", "King"};
const string SUITS[] = {"Spades", "Hearts", "Diamonds", "Clubs"};


//Create Function prototypes for card loading and shuffling deck of

void unwrap(vector<int> &);
void shuffle(vector<int> &);
void printCards(vector<int>);

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

//Function for sorting cards
void sort(vector<int>);
void swap(vector<int>&);


int main() {

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

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

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

    //Declare 2d vector for dealing the cards to each player
    vector<vector<int>> players(NUM_PLAYERS);

    cout << " Before cards have been dealt " << endl;
    showHands(players);

    cout << " After the cards have been dealt "<<endl;
    deal(players, deck);
    showHands(players);

    return 0;
}

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

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

}

// Print the card's name to cout
void printCard(int card)
{
    cout << RANKS[card % 13] << " of " << SUITS[card / 13];
}


void printCards(vector<int> deck)
{
    for(size_t j=0; j<deck.size(); j++)
    {
        printCard(deck[j]);
        cout << '\n';
    }
}
// deal one card to each player in order, repeat until the entire deck is dealt
void deal(vector<vector<int>>& players, vector<int>& deck)
{
    // clear the players' hands
    for (size_t i = 0; i < players.size(); ++i) {
        players[i].clear();
    }

    // Deal the cards from deck to the hands.
    for (size_t i = 0; i < deck.size(); ++i) {
        players[i%players.size()].push_back(deck[i]);
    }
    //clear deck
    deck.clear();
}

//Show hand after cards have been randomly dealt to each player
void showHands(const vector<vector<int>>& players)
{
    for (size_t i = 0; i < players.size(); ++i) {
        cout << "Player " << i + 1 << ":\n";
        cout << "------" <<endl;
        printCards(players[i]);
    }
}

void sort(vector<int> deck)
{
    sort(deck.begin(), deck.end());

}
Last edited on
you're already including, just not using, std::sort , from <algorithm>

If you have to write your own for credit, then ofc do that instead. The selection sort just looks like O(n2), so it's not ideal, performance-wise.
Thank you. I actually changed the sort function to what you suggested. It's more simple that way and I doubt my instructor would hate easier to read code. lol I posted it above.

Still trying to figure out the swapping.
I noticed in your swap function's first parameter, it has deck as an array. Shouldn't it be a vector?
Yes, I changed it. However, my swap is outputting a bunch of Ace of Spades.


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
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>

using namespace std;


//Global constants
const int NUM_CARDS = 52;
const int NUM_PLAYERS=4;

const string RANKS[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                        "Jack", "Queen", "King"};
const string SUITS[] = {"Spades", "Hearts", "Diamonds", "Clubs"};


//Create Function prototypes for card loading and shuffling deck of

void unwrap(vector<int> &);
void shuffle(vector<int> &);
void printCards(vector<int>);

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

//Function for sorting cards
void sort(vector<int>&);
void swap(vector<int> &deck, int first, int second);


int main() {

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

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

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

    //Declare 2d vector for dealing the cards to each player
    vector<vector<int>> players(NUM_PLAYERS);

    cout << " Before cards have been dealt " << endl;
    showHands(players);

    cout << " After the cards have been dealt "<<endl;
    deal(players, deck);
    showHands(players);

    return 0;
}

void swap(vector<int> &deck, int first, int second)
{
    int temp = deck[first];
    deck[first] = deck[second];
    deck[second] = temp;
}
//Function definitions that load cards and randomly shuffles them
void unwrap(vector<int> &deck)
{
    //Load vector with ints from 0 to 51
    deck.clear();		// ensure that it starts emtpy
    for (int i = 0; i < NUM_CARDS; i++)
    {
        deck.push_back(i);
    }
}

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

}

// Print the card's name to cout
void printCard(int card)
{
    cout << RANKS[card % 13] << " of " << SUITS[card / 13];
}


void printCards(vector<int> deck)
{
    for(size_t j=0; j<deck.size(); j++)
    {
        printCard(deck[j]);
        cout << '\n';
    }
}
// deal one card to each player in order, repeat until the entire deck is dealt
void deal(vector<vector<int>>& players, vector<int>& deck)
{
    // clear the players' hands
    for (size_t i = 0; i < players.size(); ++i) {
        players[i].clear();
    }

    // Deal the cards from deck to the hands.
    for (size_t i = 0; i < deck.size(); ++i) {
        players[i%players.size()].push_back(deck[i]);
    }
    //clear deck
    deck.clear();
}

//Show hand after cards have been randomly dealt to each player
void showHands(const vector<vector<int>>& players)
{
    for (size_t i = 0; i < players.size(); ++i) {
        cout << "PLAYER " << i + 1 << ":\n";
        cout << "------" <<endl;
        printCards(players[i]);
    }
}

void sort(vector<int> &deck)
{
    int firstCard, card;

    for(firstCard = 0; firstCard < (NUM_CARDS * NUM_PLAYERS - 1); firstCard++)
    {
        for(card = firstCard+1; card < (NUM_CARDS * NUM_PLAYERS); card++)
        {
            if( deck[card] < deck[firstCard] )
            {
                swap(deck, card, firstCard);
            }
        }
    }
}
Last edited on
Is the purpose of the sort to lay out the players' cards in their hand by card rank highest to lowest? Otherwise I'm trying to figure out why you'd want to sort the deck.
Last edited on
Yes. That's exactly it. The sort function has to sort through each player's hand before the cards are actually shown.
Then you don't want to pass the deck vector in, you want to pass the 2D player vector in.

also, you can sort one row of the 2D vector simply using
sort(players[0].begin(), players[0].end())

the [0] will point to the first row, [1] second row, etc. begin() starts at [0][0], end stops it at [0][n+1]. The + 1 is the one past the last element of the vector. If you do not explicitly use [0] and just use players.begin() it'll sort through the whole vector basically messing up the cards that the players should've had. You only wanna do one row at a time.

In a for loop you can set it up and just replace [0] with [i] and iterate.
Last edited on
I'm not understanding what you're saying. Sorry.
Which part are you not understanding? Based on the code that has been handed to you so far, you should be able to figure out what I'm telling you. I shouldn't have to write out the whole sort function for you.
Last edited on
Oh, no. That's not what I'm wanting you to do at all. Sorry if my comment came off that way.
No worries, man. Let me know which part/s you'd like me to explain and I'd be glad to help
You mentioned sorting one row of the 2D vector. Basically I'll be doing 4 separate calls for each player and it'll be in a for loop?

I'm sorry for all the questions. I am a beginner beginner, so the jargon is something I'm still getting use to.
Basically, yes. The for loop is basically just doing the same as

1
2
3
4
sort(players[0].begin(), players[0].end())
sort(players[1].begin(), players[1].end())
sort(players[2].begin(), players[2].end())
sort(players[3].begin(), players[3].end())


The advantage of a for loop though is if you change it to have more players, all you'd have to do is redefine your const NUM_PLAYERS and you don't have to delete or add more statements yourself. The for loop conditional i < NUM_PLAYERS will simply take the new definition of NUM_PLAYERS and apply it as such. In your case it's i < 4. At the end of the loop, it checks if i is less than 4. If it is, it'll add 1 to i ++i at the end of the loop. The first time it goes through the loop i = 0. 2nd time i = 1, 3rd time i = 2, 4th time = 3. At the end of the 4th time going through the loop, it'll do the ++i once more and now i = 4. Since 4 < 4 isn't a true statement. It'll not run the loop again and exit out of it.

Also when I said iterate, that's basically shorthand for saying "to go through". In this case... to go through the vector container elements. An iterator is an object that points to the element of whatever container (be it an array, vector, string, etc)

When you use a for loop in your situation, you use an iterator object (in your case, an int) to point to whatever element. In a 2D vector (vec[0][0]), the first [0] is basically the row, the second [0] is the column.
Your loop limits in sort function were off -- you're sorting a deck, which has 52 cards, but you iterated up to 52*4. I tweaked the printCards by printing a few newline every 8 cards so there'd be a bit less vertical output, and fixed your sort. std::sort should have the same output.

Remember, the way you print your cards controls the sort order association (it's currently sorting suit-first).

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>

using namespace std;


//Global constants
const int NUM_CARDS = 52;
const int NUM_PLAYERS=4;

const string RANKS[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
                        "Jack", "Queen", "King"};
const string SUITS[] = {"Spades", "Hearts", "Diamonds", "Clubs"};


//Create Function prototypes for card loading and shuffling deck of

void unwrap(vector<int> &);
void shuffle(vector<int> &);
void printCards(vector<int>);

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

//Function for sorting cards
void sort(vector<int>&);
void swap(vector<int> &deck, int first, int second);


int main() {

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

    cout << "The deck, unwrapped: " << endl;
    unwrap(deck);
    printCards(deck);

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

    //Declare 2d vector for dealing the cards to each player
    vector<vector<int>> players(NUM_PLAYERS);

    cout << " Before cards have been dealt " << endl;
    showHands(players);

    cout << " After the cards have been dealt "<<endl;
    deal(players, deck);
    showHands(players);

    return 0;
}

void swap(vector<int> &deck, int first, int second)
{
    int temp = deck[first];
    deck[first] = deck[second];
    deck[second] = temp;
}
//Function definitions that load cards and randomly shuffles them
void unwrap(vector<int> &deck)
{
    //Load vector with ints from 0 to 51
    deck.clear();		// ensure that it starts emtpy
    for (int i = 0; i < NUM_CARDS; i++)
    {
        deck.push_back(i);
    }
}

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

}

// Print the card's name to cout
void printCard(int card)
{
    cout << RANKS[card % 13] << " of " << SUITS[card / 13];
}


void printCards(vector<int> deck)
{
    for(size_t j=0; j<deck.size(); j++)
    {
        if (j%8==0)
            cout << endl;
        printCard(deck[j]);
        cout << ", ";
    }
    cout << endl << endl;
}
// deal one card to each player in order, repeat until the entire deck is dealt
void deal(vector<vector<int>>& players, vector<int>& deck)
{
    // clear the players' hands
    for (size_t i = 0; i < players.size(); ++i) {
        players[i].clear();
    }

    // Deal the cards from deck to the hands.
    for (size_t i = 0; i < deck.size(); ++i) {
        players[i%players.size()].push_back(deck[i]);
    }
    //clear deck
    deck.clear();
}

//Show hand after cards have been randomly dealt to each player
void showHands(const vector<vector<int>>& players)
{
    for (size_t i = 0; i < players.size(); ++i) {
        cout << "PLAYER " << i + 1 << ":\n";
        cout << "------" <<endl;
        printCards(players[i]);
    }
}

void sort(vector<int> &deck)
{
    int firstCard, card;

    for(firstCard = 0; firstCard<NUM_CARDS-1; firstCard++)
    {
        for(card = firstCard+1; card<NUM_CARDS; card++)
        {
            if( deck[card] < deck[firstCard] )
            {
                swap(deck, card, firstCard);
            }
        }
    }
}
Thank you very much! I was going to ask you what lines 99-102 were doing, but you covered that.
Topic archived. No new replies allowed.
Pages: 12