I have taken myself down a rabbit hole that I do not know how to get out of, I have created a deck of random cards. However, I now need to sort them firstly by suit then by rank in the order of my enum's. Firstly I can't figure out how to get elements from within the vector and within the enum, so I can compare them, secondly, I suspect this links to the first I'm not sure how to swap them, finally it would be nice to have a function for the bubble sort that I can call twice to first-order them by suit and then call again to order by card.
Here are the libraries:
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
using std::vector;
These are the structs and enum's:
enum Rank { ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }; // aspects of the card
enum Suit { HEARTS, CLUBS, DIAMONDS, SPADES }; // suits of the card
struct Card { //Card data numbers
Rank rank;
Suit suit;
};
struct Deck { // defines a card
vector<Card> cards; // All the cards
int size = 5; // Max deck size
};
This is what generates a random card deck:
void initialize(Deck& deck, string stdno) {
for (int count=0; count < deck.size; count ) { //creates a deck of cards
Card card;
string rcardstr = stdno.substr(rand() % 8, 1) stdno.substr(rand() % 8, 1);
int rcard = stoi(rcardstr) % 52;
card.suit = static_cast<Suit>(rcard / 13); //cast between the enumerator and the value
card.rank = static_cast<Rank>(1 rcard % 13);
deck.cards.push_back(card); //Adds the card to the deck of cards
}
}
This is my feeble attempts of a bubble sort before I realized my first two problems:
void bubble_sort(Deck& deck ){
bool swapp = true;
while (swapp) {
swapp = false;
for (int i=0; i < deck.size; i ) {
if (deck.rank[0]
}
}
}
...And finally my main function:
int main() {
string stdno="14398132";
Deck my_deck;
initialize(my_deck, stdno);
}
If someone would be willing to give me a few minutes of their time to explain where I need to look/what I need to learn I would be incredibly grateful and make sure to give the same time back to the community once I've become better at c .
Solution: https://pastebin.com/j8ZwUaUX password: MrVolts thankyou Ari
CodePudding user response:
The way to access the members of the deck is to write:
deck.cards[i].rank
or
deck.cards[i].suit
You have multiple options for sorting.
- Simply write two functions:
void bubble_sort_by_rank(Deck& deck ){
bool swapp = true;
while (swapp) {
swapp = false;
for (int i=0; i < deck.cards.size()-1; i ) {
if (deck.cards[i 1].rank > deck.cards[i].rank){
//...
}
}
}
}
and
void bubble_sort_by_suit(Deck& deck ){
bool swapp = true;
while (swapp) {
swapp = false;
for (int i=0; i < deck.cards.size()-1; i ) {
if (deck.cards[i 1].suit > deck.cards[i].suit){
//...
}
}
}
}
Write one bubble sort function, but take the sort criteria as a parameter (which can be enum).
Write a single bubble sort function, but compare each card first by suit and if they were equal, then compare them by rank or just overload the operator< for your Card class as suggested in the comments. This is probably the preferred method and more efficient.
If you don't insist on using bubble sort, you can just use std::sort
with a lambda function that compares your cards first by suit then by rank.
For swapping you should be able to use std::swap or write a simple template function yourself:
template <class T>
void swap(T& x,T& y)
{
T temp;
temp=x;
x=y;
y=temp;
}