Help with Deck Shuffle Program

I'm trying to figure out why my sort function from the Player class doesn't work. Any suggestions will help. My sort function uses an overloaded "<" operator from the card class.

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include "pch.h"
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;
enum Suit { Spade, Heart, Diamond, Club };
class Card {
private:

	int number;
	string description;

public:
	Suit suit;
	void setNumber(int cNumber) {
		number = cNumber;
	}
	int getNumber() {
		return number;
	}
	void setSuit(Suit cSuit) {
		suit = cSuit;
	}
	Suit getSuit() {
		return suit;
	}
	void setDescription() {
		string temp = to_string(number);
		switch (number) {
		case 1:
			temp = "Ace";
			break;
		case 11:
			temp = "Jack";
			break;
		case 12:
			temp = "Queen";
			break;
		case 13:
			temp = "King";
			break;
		default:
			temp = to_string(number);
		}

		switch (suit) {
		case Spade:
			description = "Spade " + temp;
			//cout << description;
			break;
		case Heart:
			description = "Heart " + temp;
			//cout << description;
			break;
		case Diamond:
			description = "Diamond " + temp;
			//cout << description;
			break;
		case Club:
			description = "Club " + temp;
			//cout << description;
			break;
		default:
			break;
		}
	}
	string getDescription() {
		return description;
	}
	bool operator ==(const Card &c) {
		if (suit == c.suit && number == c.number)
			return true;
		return false;
	}
	bool operator <(const Card &c) {
		if (suit < c.suit)
			return true;
		if (suit == c.suit && c.number == 1)
			return true;
		else if (suit == c.suit && c.number != 1 && number < c.number)
			return true;
		return false;
	}
};
class Deck {
private:
	Card deck[52];

public:
	void shuffleDeck(Card deck[]) {
		int seed = time(0);
		srand(seed);
		for (int i = 0; i < 52; i++) {
			int r = rand() % 52;
			Card temp = deck[i];
			deck[i] = deck[r];
			deck[r] = temp;
		}
	}
};
class Player {
private:
	Card hand[13];
	string name;

public:
	void setName(string n) {
		name = n;
	}
	string getName() {
		return name;
	}
	void sort(Card array[], int size) {
		Card temp;
		bool swap;

		do {
			swap = false;
			for (int count = 0; count < (size - 1); count++) {
				if (array[count + 1] < array[count]) {
					temp = array[count];
					array[count] = array[count + 1];
					array[count + 1] = temp;
					swap = true;
				}
			}
		} while (swap);
	}
	int clubTwo(Card array[], int size, Card value) {
		int first = 0;
		int last = size - 1;
		int middle;
		int position = -1;
		bool found = false;

		while (!found && first <= last) {
			middle = (first + last) / 2;
			if (array[middle] == value) {
				found = true;
				position = middle;
			}
			else if (value < array[middle])
				last = middle - 1;
			else
				first = middle + 1;
		}
		return position;
	}
};
int main() {
	int m, i, j, results;
	Deck blackJack;
	Player p1, p2, p3, p4;
	p1.setName("Lady Gaga");
	p2.setName("Albert Einstein");
	p3.setName("Muhammed Ali");
	p4.setName("Diego Maradona");

	Card deck[52];
	Card hand1[13];
	Card hand2[13];
	Card hand3[13];
	Card hand4[13];
	for (int i = 1; i < 14; i++) {
		for (int j = 1; j < 5; j++) {
			int pos = i * 4 + j - 5;
			switch (j) {
			case 1:
				deck[pos].setSuit(Club);
				deck[pos].getSuit();
				break;
			case 2:
				deck[pos].setSuit(Diamond);
				deck[pos].getSuit();
				break;
			case 3:
				deck[pos].setSuit(Heart);
				deck[pos].getSuit();
				break;
			case 4:
				deck[pos].setSuit(Spade);
				deck[pos].getSuit();
				break;
			default:
				break;
			}
			deck[pos].setNumber(i);
			deck[pos].setDescription();
			//cout << left << setw(15) << deck[pos].getDescription() << endl;
		}
	}
	Card clubTwo = deck[4];
	do {
		cout << "Enter 1 to shuffle" << endl << "Enter 2 to deal" << endl << "Enter 3 to end";
		cin >> m;
		switch (m) {
		case 1:
			blackJack.shuffleDeck(deck);
			//for (i = 0; i < 52; i++) {
				//cout << left << setw(15) << deck[i].getDescription() << endl;
			//}
			break;
		case 2:
			j = 0;
			for (i = 0; i < 13; i++) {
				hand1[j] = deck[i];
				j++;
			}
			j = 0;
			for (i = 13; i < 26; i++) {
				hand2[j] = deck[i];
				j++;
			}
			j = 0;
			for (i = 26; i < 39; i++) {
				hand3[j] = deck[i];
				j++;
			}
			j = 0;
			for (i = 39; i < 52; i++) {
				hand4[j] = deck[i];
				j++;
			}
			p1.sort(hand1, 13);
			p2.sort(hand2, 13);
			p3.sort(hand3, 13);
			p4.sort(hand4, 13);
			cout << left << setw(15) << p1.getName() << left << setw(20) << p2.getName() << left << setw(25) << p3.getName() << left << setw(30) << p4.getName();
			cout << endl;
			cout << endl;
			for (i = 0; i < 13; i++) {
				cout << left << setw(15) << hand1[i].getDescription() << left << setw(20) << hand2[i].getDescription() << left << setw(25) << hand3[i].getDescription() << left << setw(30) << hand4[i].getDescription();
				cout << endl;
			}
			/*results = p1.clubTwo(hand1, 13, clubTwo);
			if (results == -1)
				cout << "Not in hand 1";
			else
				cout << "ClubTwo in hand 1";
			cout << endl;
			results = p2.clubTwo(hand2, 13, clubTwo);
			if (results == -1)
				cout << "Not in hand 2";
			else
				cout << "ClubTwo in hand 2";
			cout << endl;
			results = p3.clubTwo(hand3, 13, clubTwo);
			if (results == -1)
				cout << "Not in hand 3";
			else
				cout << "ClubTwo in hand 3";
			cout << endl;
			results = p4.clubTwo(hand4, 13, clubTwo);
			if (results == -1)
				cout << "Not in hand 4";
			else
				cout << "ClubTwo in hand 4";
			cout << endl;
			break;*/
		case 3:
			exit(1);
		}
	} while (m != 3);
	return 0;
}
Last edited on
In what sense does it "not work"? Does it not sort them at all? Does it sort them incorrectly?

BTW, it's very difficult to read unformatted code. Some indentation would be nice. You could try using code tags:

[code]
your code goes here
maintains indentation
adds syntax highlighting
cures gingivitis
[/code]
Last edited on
I'm trying to figure out why my sort function from the Person class doesn't work

1) In the code you posted, there’s no class Person
2) Your code is incomplete (e.g. the funcion main() is not entire); please post a compilable (part of the) code.
3) You Player::sort() method doesn’t sort Player::hand, but another Card*. May I ask you why?
4) Your Deck::shuffleDeck() method doesn’t shuffle Deck::deck, but another Card*. May I ask you why?
The code only sorts them sometimes. Most of the time it seems to halt the program at least thats what it seems like from the console screen. The worst part is that I'm not getting any error message so I can't even tell what to look at.
Also, I mistyped when I put Person I meant Player.
Finally the Player::hand and Deck::deck are really only there because the assignment required them.
The code only sorts them sometimes

What are ‘them’?
If you gave us a compilable example we could try to compile and run it. I’m pretty sure you'd get far better answers.
I can see what's wrong with your sort (and your shuffle), but since you refuse to use code tags ....
I hope nobody else helps you until you repost your code properly.
Last edited on
ok. The code should compile now or at least be easier to read now. The compiler I'm using is Visual Studios.
You have some fundamental problems.

You declare Card arrays in Player and Deck, but you don't use them. Instead you use separate Card arrays in main (hand and deck).

srand should only be called once in the program (usually early in main).

You should initialize the deck in the Deck constructor. You could also use a constructor for Player to set the name.

You don't need to store the description. You can produce it as needed on the fly.

Anyway, here's the idea. I made the cards arrays public for now. I don't know how object-oriented you want to make it.

At the moment your operator< considers the card to be less than the other if it has the same suit and they are both 1's. Maybe they should be equal in that case? (I don't know the game.)

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

enum Suit { Spade, Heart, Diamond, Club };

class Card {
private:
    int number;
    Suit suit;

public:
    void setNumber(int cNumber) { number = cNumber; }
    int  getNumber()            { return number; }
    void setSuit  (Suit cSuit)  { suit = cSuit; }
    Suit getSuit  ()            { return suit; }

    string getDescription() {
        string val;
        switch (number) {
        case  1: val = "Ace";   break;
        case 11: val = "Jack";  break;
        case 12: val = "Queen"; break;
        case 13: val = "King";  break;
        default: val = to_string(number);
        }
        switch (suit) {
        case Spade:   return val + " of Spades";
        case Heart:   return val + " of Hearts";
        case Diamond: return val + " of Diamonds";
        case Club:    return val + " of Clubs";        
        }
        return "";
    }

    bool operator ==(const Card &c) {
        return (suit == c.suit && number == c.number);
    }

    bool operator <(const Card &c) {
        return (suit < c.suit)
            || (suit == c.suit && (c.number == 1 || number < c.number));
    }
};

class Deck {
public:
    Card cards[52];

    Deck() {
        for (int i = 0; i < 13; i++) {
            for (int j = 0; j < 4; j++) {
                int pos = i * 4 + j;
                switch (j) {
                case 0: cards[pos].setSuit(Club);    break;
                case 1: cards[pos].setSuit(Diamond); break;
                case 2: cards[pos].setSuit(Heart);   break;
                case 3: cards[pos].setSuit(Spade);   break;
                }
                cards[pos].setNumber(i + 1);
            }
        }
    }

    void shuffle() {
        for (int i = 52; i > 1; ) {
            int r = rand() % i--;
            swap(cards[i], cards[r]);
        }
    }
};

class Player {
private:
    string name;
public:
    Card cards[13];
    
    Player(string name)      : name(name) { }
    void   setName(string n) { name = n; }
    string getName()         { return name; }

    void sort() {
        for (int i = 1; i < 13; i++) {
            auto save = cards[i];
            int j = i;
            for ( ; j > 0 && cards[j - 1] < save; --j)
                cards[j] = cards[j - 1];
            cards[j] = save;
        }
    }
};

int main() {
    srand(time(nullptr));

    Deck deck;
    Player p[4] = {
        {"Lady Gaga"},
        {"Albert Einstein"},
        {"Muhammed Ali"},
        {"Diego Maradona"}
    };

    for (int choice = 0; choice != 3; ) {

        cout << "Enter 1 to shuffle\n"
             << "Enter 2 to deal\n"
             << "Enter 3 to end\n";
        cin >> choice;

        switch (choice) {
        case 1:
            deck.shuffle();
            break;

        case 2:
            for (int i = 0; i < 13; i++)
                for (int j = 0; j < 4; j++)
                    p[j].cards[i] = deck.cards[j * 13 + i];

            for (int i = 0; i < 4; i++)
                p[i].sort();

            for (int i = 0; i < 4; i++)
                cout << left << setw(18) << p[i].getName();
            cout << '\n';

            for (int i = 0; i < 13; i++) {
                for (int j = 0; j < 4; j++)
                    cout << left << setw(18) << p[j].cards[i].getDescription();
                cout << '\n';
            }
            break;
        }
    }
}

Topic archived. No new replies allowed.