Help with deck of cards program

Hi. I'm at a complete loss I'm trying to make a deck of cards that can be shuffled and distributed to 4 players. I have to use the three classes and I have to code out the shuffle and search functions. I don't see why my sort function in the Player class isn't working. It relies on the overloaded "<" operator from the card class. Any suggestions would be nice.


#include "pch.h"
#include<iomanip>
#include<iostream>
#include<string>
using namespace std;
enum Suit { Club, Diamond, Heart, Spade };
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;
}
Last edited on
1
2
3
4
int main()
{
	int m;
	Player p1, p2, p3, p4;

Have you considered a vector/array of Player?

1
2
3
4
5
6
	Card dek[52];
	Deck deck;
	Player hand1[13];
	Player hand2[13];
	Card hand3[13];
	Card hand4[13];

What is all this about?

Doesn't a Deck already have an array of cards?

Doesn't a Player already have an array of cards called hand?

cout << dek << endl;
What exactly are you trying to print here? Where is the overloaded operator<< for this to happen?

By the way you may want to consider creating overloads for each of your classes for the ostream operator<<, they may help you better understand what each class is doing?

The goal is to shuffle an array (deck) of 52 card objects and distribute them to 4 hands. Then print the four hands to the console
Okay so then what is the problem? You have a Deck that contains a vector/array of cards, and a Player that has a vector/array of cards, take cards from a shuffled Deck and give them to the various players.

Also why does Player have a function named "Deal"? Isn't dealing part of the general game? Does it really belong to the Player or the Deck?

I really think you need to go back to the design phase and rethink the purposes of your classes. And remember in C++ not everything must be contained inside a class.

I have to use the three classes

Were the names and purposes supplied by the assignment or are all the details your responsibility?

Are you allowed to use some of the standard C++ features like std::vector, std::sort, std::shuffle, etc? Or are you required to "roll your own" versions of those functions/classes?



Thanks I'm redesigning the program now and no I'm not allowed to use the sor t or shuffle features of c++.
ok I only have a C4244 warning in class deck shuffleDeck(). Any ideas on how to fix that.
You'd be surprised how few of us have memorized the entire list of error code numbers from the specific compiler you happen to be using. Is it really too much effort to post the entire error message you're getting?

And, of course, post the code that's giving you that error. Properly formatted.
Any ideas on how to fix that.


Read the warning message, don't do what it's telling you not to do. Sounds obvious, I know, but just try it.
Topic archived. No new replies allowed.