Reputation: 25
i am a begginer programmer in c++. I found a very interesting exercise in an old c++ book, which doesn't have the solution for that exercise, so i hope you guys can help me:
It wants me to create a deck of 52 cards, then shuffle the deck and deal the cards to 4 players:
This is an example of the code:
cout << "Player 1: ";
for (int j=0; j<13; j++)
{
card[j].display();
cout << ", ";
}
Player 1: 8, 7, 3, 2, A, K, 5, 4, Q, 9, 3, A, 2
Now it wants me to array those numbers from the biggest to smallest: A, A, K, Q, 9 ,8, 7,5 ,4, 3, 3,2, 2
After having done a research on the internet, i learnt how to find the biggest number of an array, but i still don't know how to array those numbers from the biggest to smallest. I am using GNU Compiler.
Thanks.
For those who want to all the code, here it is:
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;
enum Suit {clubs, diamonds, hearts, spades};
class deck
{
private:
int number;
Suit suit;
public:
void setcards(int n, Suit s)
{ number = n; suit = s; }
void display();
};
void deck::display()
{
if (number >= 2 && number <= 10)
cout << number;
else
switch(number)
{
case 11: cout << "J"; break;
case 12: cout << "Q"; break;
case 13: cout << "K"; break;
case 14: cout << "A"; break;
}
switch(suit)
{
case clubs: cout << static_cast <char> (5); break;
case diamonds: cout << static_cast <char> (4); break;
case hearts: cout << static_cast <char> (3); break;
case spades: cout << static_cast <char> (6); break;
}
}
class game
{
private:
int j;
enum {cardmax = 52};
deck card[cardmax];
public:
game()
{
for (int j=0; j<cardmax; j++)
{
int num = (j % 13) + 2;
Suit su = Suit(j / 13);
card[j].setcards(num, su);
}
}
void shuffle()
{
char ans;
cout << "Would you like to shuffle the deck? (y/n): ";
cin >> ans;
if (ans == 'y')
{
srand(time(NULL));
for (j=0; j<cardmax; j++)
{
int k = rand() % 52;
deck temp = card[j];
card[j] = card[k];
card[k] = temp;
}
}
else if (ans =='n')
deal();
}
void deal();
};
void game::deal()
{
const char esc = 27;
const char enter = '\r';
char ans;
cout << "Press ESCAPE to exit the game or ENTER to deal: ";
ans = getche();
if (ans == esc)
exit(0);
else if (ans == enter)
{
cout << "\n\nPlayer 1: ";
for (j=0; j<13; j++)
{
card[j].display();
cout << ", ";
}
cout << "\nPlayer 2: ";
for (j=13; j<26; j++)
{
card[j].display();
cout << ", ";
}
cout << "\nPlayer 3: ";
for (j=26; j<39; j++)
{
card[j].display();
cout << ", ";
}
cout << "\nPlayer 4: ";
for (j=39; j<52; j++)
{
card[j].display();
cout << ", ";
}
cout << endl;
cout << "\nWould you like to deal again? (y/n): ";
cin >> ans;
if (ans == 'y')
{
shuffle();
deal();
}
else if (ans == 'n')
exit(0);
}
}
int main()
{
game cards;
cards.shuffle();
cards.deal();
}
Upvotes: 1
Views: 2281
Reputation: 4925
Here's how I'd do it without too much refactoring. To go the whole way I'd split the prompts and "deal again" loop outside deal so it wasn't recursive and make the logic so you wouldn't need exit(0)
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <functional>
#include <iomanip>
//If no c++ 11 you may need to remove the next line
#include <tuple>
using namespace std;
enum Suit {clubs, diamonds, hearts, spades};
const char* CardLetters[] = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
const char SuitLetters[] = { 5, 4, 3, 6 };
class deck
{
private:
int number;
Suit suit;
public:
void setcards(int n, Suit s)
{
number = n; suit = s;
}
void display()
{
cout << std::right<< std::setw(2) << CardLetters[number - 2] << SuitLetters[suit];
}
bool operator >(const deck& rhs) const
{
//If you have c++ 11, this is cleaner
//return std::tie(number, suit) > std::tie(rhs.number, rhs.suit);
//otherwise
if(number == rhs.number)
{
return suit > rhs.suit;
}
return number > rhs.number;
}
};
class game
{
private:
enum {cardmax = 52};
deck card[cardmax];
public:
game()
{
for (int j=0; j<cardmax; j++)
{
int num = (j % 13) + 2;
Suit su = Suit(j / 13);
card[j].setcards(num, su);
}
}
void shuffle()
{
char ans;
cout << "Would you like to shuffle the deck? (y/n): ";
cin >> ans;
if (ans == 'y')
{
std::random_shuffle(&card[0], &card[cardmax]);
}
else if (ans =='n')
{
deal();
}
}
void deal();
};
void game::deal()
{
const char esc = 27;
const char enter = '\r';
cout << "Press ESCAPE to exit the game or ENTER to deal: ";
char ans = getche();
if (ans == esc)
{
exit(0);
}
else if (ans == enter)
{
for(int playernum = 0; playernum < 4; ++playernum)
{
const int offset = 13 * playernum;
std::sort(&card[offset], &card[offset + 13], std::greater<deck>());
cout << "\n\nPlayer " << (playernum + 1) << ": ";
for (int j=0; j<13; j++)
{
if(j > 0)
{
cout << ", ";
}
card[offset + j].display();
}
}
cout << "\n\nWould you like to deal again? (y/n): ";
cin >> ans;
if (ans == 'y')
{
shuffle();
deal();
}
else if (ans == 'n')
{
exit(0);
}
}
}
int main()
{
srand(time(NULL));
game cards;
cards.shuffle();
cards.deal();
return 0;
}
Upvotes: 0
Reputation: 57698
Look up std::sort
.
You should be able to apply it to your array:
std::sort(&card[0], &card[cardmax]);
If you want to order the cards in a different manner, you will have to write a function that will change the order.
From http://www.cplusplus.com/reference/algorithm/sort/?kw=sort
comp
Binary function that accepts two elements in the range as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.
Example:
class deck
{
[...] // All your existing code.
public:
static bool descending_order(const deck& first_card, const deck& second_card)
{
return first_card.number > second_card.number;
}
};
Upvotes: 1
Reputation: 1065
First you need to implement the less than operator in your deck class like so:
class deck {
//...
bool operator < (const deck& other) const {return number < other.number;}
//...
}
Then I'd suggest turning that array of cards in a std::vector
std::vector<deck> card;
card.resize(cardmax);
//instead of
deck card[cardmax];
Then you can sort the std::vector like so
std::sort(card.begin(), card.end());
Note that in order to use the std::vector type, you have to add #include <vector>
somewhere in the file where you are using it and in order to use std::sort, you have to add a #include <algorithm>
Upvotes: 3
Reputation: 693
The easiest sorting algorithm to understand in my opinion is the bubblesort.
The algorithm goes like this:(for biggest to smallest)
First it compares elements 0 and 1. If (element 0 less than element 1) THEN swap the elements Then it does the same thing, but with elements 1 and 2.
When you get to the end of the array, if any swaps have been made, you must start back at elements 0 and 1, and work your way across the array again.
I know that may be difficult to understand just reading it, but HERE is a youtube beginner c++ video that should make it very easy to understand.
Upvotes: 1