Insert a card into a deck of cards

Advertisements

So i have this program and i want to implement an insert void function. Basically i want to insert a card into the deck. The output should be a deck of cards and the shuffled version and then insert a card and sort it again. How can i accomplish that?

Deck.cpp

#include "deck.h"
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <random>
#include <chrono> 

using namespace std;

/**
 * Help function to convert Suit enum to string
*/
const string SuitToString(Suit s) {
    switch (s) {
        case Suit::Hearts:
            return "H";
        case Suit::Diamonds:
            return "D";
        case Suit::Spades:
            return "S";
        case Suit::Clubs:
            return "C";
    }
    return "";
}

Card::Card(int rank, Suit suit)
{
    this->rank = rank;
    this->suit = suit;
}

/**
 * Overload of << operator for Card class
*/
ostream & operator << (ostream &out, const Card &c)
{
    string r;
    switch(c.rank) {
        case 1:
            r = 'A';
            break;
        case 11:
            r = 'J';
            break;
        case 12:
            r = 'Q';
            break;
        case 13:
            r = 'K';
            break;
        default:
            r = to_string(c.rank);
            break;
    }

    out << r << SuitToString(c.suit);

    return out;
}

/**
 * Create a deck of 52 cards
*/
Deck::Deck() {
    for (Suit s = Suit::Hearts; s <= Suit::Clubs; s = Suit(s + 1)) {
        for (int r=1; r<14; r++) {
            cards.push_back(Card(r, s));
        }
    }
}

/**
 * Overload of << operator for Deck class
*/
ostream & operator << (ostream &out, const Deck &d)
{
    string separator;
    for (auto i: d.cards) {
        out << separator << i;
        separator = ", ";
    }

    return out;
}

/** 
 * Return number of cards in deck
 * 
*/
int Deck::size()
{
    return this->cards.size();
}

/**
 * Shuffle all the cards in deck in ranom order. 
*/
void Deck::shuffle()
{
    auto rng = std::default_random_engine(std::chrono::system_clock::now().time_since_epoch().count());
    std::shuffle(this->cards.begin(),this->cards.end(), rng);
}

/**
 * Sort the deck according to card rank. All aces, the all two's, then all threes, ...
*/
void Deck::sort()
{
    vector<Card> sorted_cards; // Empty temporary deck

    while (this->size() > 0) {
        // Go through the deck from left to right, insert the deck in the appropriate spot
        Deck::insert(sorted_cards, this->take()); //Take a card from the old deck, insert it into the new one
    }

    this->cards = sorted_cards;
}

/**
 * Take the top card from the deck 
*/
Card Deck::take() {
    Card c = this->cards.back();
    this->cards.pop_back();
    return c;
}

/**
 * Put a card on top of the deck 
*/
void Deck::put(Card card) {
    cards.push_back(card);
}

void Deck::insert(vector<Card> &cardlist, Card card) {

    /**
     * My insert code here!
    */

    return;
}

Deck.h

#include <iostream>
#include <vector>
#include <string>

using namespace std;

enum Suit {
    Hearts,
    Diamonds,
    Spades,
    Clubs
};

class Card
{
private:
    int rank;
    Suit suit;
public:
    Card(int rank, Suit suit);
    
    bool operator == (Card const &other)
    {
        return rank == other.rank;
    }

    bool operator < (Card const &other)
    {
        return rank < other.rank;
    }

    bool operator > (Card const &other)
    {
        return rank > other.rank;
    }

    friend ostream & operator << (ostream &out, const Card &c);
};

class Deck
{
private:
    vector<Card> cards;
public:
    Deck();
    friend ostream & operator << (ostream &out, const Deck &d);
    int size();
    void shuffle();
    void sort();
    Card take();
    void put(Card card);

    static void insert(vector<Card> &cardlist, Card card);
};

main.cpp


#include "deck.h"
#include <iostream>

using namespace std;

main() {
    Deck deck = Deck();
    cout << "Fresh deck: " << deck << endl;
    deck.shuffle();
    cout << "Shuffled deck: " << deck << endl;
    deck.sort();
    cout << "Sorted deck: " << deck << endl;
}

For now i just have written the function itself.

>Solution :

Since Deck::insert is being called from the Deck::sort method, I think the aim is to insert the new card in order with respect to the previously inserted (and therefore already sorted) cards.

So it seems you need something like this

void Deck::insert(vector<Card> &cardlist, Card card) {
    // starting from zero
    size_t i = 0;
    // find the index of the first card in the list that is >= to the card to insert
    for (; i < cardList.size(); ++i)
    {
         if (card < cardList[i])
             break;
    }
    // insert the new card before that index
    cardList.insert(cardList.begin() + i, card);
}

This is bad code, for a couple of reasons (efficiency and design) but the above is my take on what you are being asked to do.

Leave a ReplyCancel reply