Comparing the rank of cards in a hand

Viewed 2399

I have a homework assignment that was two parts. The first part was to make a program that puts a vector of 52 card objects when you create an object for the class Deck. I did this by making the deck first give each card the number 1-4 for suit and 2-14 for card. It then changes the numbers to strings so it can output the cards in the form of "Ace of spades, two of hearts" etc. Now I am trying to figure out how to make it draw five cards and evaluate it for

pair, two pair, three of a kind, four of a kind, and full house.

Not sure if this is possible with my strings or if I would have to change my whole code to do it a different way. Here is the code.

edit: Also, the main.cpp was provided as a template and we were forced to build around it so even if there are better ways to do it we have to do it in this form >.<

editedit: here is a hint we were given "Hint: Create a map where each pair stores a unique rank and the number of times that rank occurs in the hand. You can also use the count_if function from to determine how many pairs or threes are in the set." but to be honest I don't really get what he wants us to do here...

//main.cpp 

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

using namespace std; 



int main()
{
    Deck deck;                  // created the object called 'deck'
    deck.shuffleCards();        // puts cards in the deck and shuffles them
    while (not deck.empty())    // if the deck isn't empty loop will continue
    {
        cout << deck.draw().toString() << endl;    // first it draws a card from the deck                                                                                                                                                           
    }                                              // of the form '000' and then puts those three
                                                   // numbers into the toString function which 
}                                                  // converts them into a string of words
                                                   // in the form 'Card' of 'Suit'. Keeps drawing
                                                   // cards run out. 

and here is the functions

#include "Deck.h"

// Constructor for cards that are created

inline Card::Card(int s, int r)
{
    suit = s;
    rank = r;
};

// This function turns three int 'Cards' into three word
// strings that get returned when you call the function.

std::string Card::toString() 
{

    std::string oldS = std::to_string(suit); // this creates a string called oldS(uit) 
                                             // and changes the int into a string
    std::string oldR = std::to_string(rank); // this creates a string called oldR(ank) 
                                             // and changes the int into a string

    std::string SR = oldS + oldR;       // turns the two strings into one 
                                        // and puts them into a new string
    std::string newS;
    std::string newR;                   // These will be the new suit and rank 

    // this code turns the numbers (which are already strings) into words.
    // 'substr' lets you search any length of the string. We have a string of
    // two to three numbers but we need to analyze the first character and the
    // second / third seperately. With 'substr' you can do this. 

    if(SR.substr(0, 1) == "1") // if starting at character 0 and reading one character
        newS = "Hearts";       // is equal to '1' then make 'newS' equal to ' Hearts'
    if(SR.substr(0, 1) == "2")
        newS = "Diamonds";
    if(SR.substr(0, 1) == "3")
        newS = "Spades";
    if(SR.substr(0, 1) == "4")
        newS = "Clubs";

    if(SR.substr(1, 2) == "2") // if starting at character 1 and reading 2 characters
        newR = "Two";          // is equal to '2' then make 'newR' equal to 'Three'
    if(SR.substr(1, 2) == "3")
        newR = "Three";
    if(SR.substr(1, 2) == "4")
        newR = "Four";
    if(SR.substr(1, 2) == "5")
        newR = "Five";
    if(SR.substr(1, 2) == "6")
        newR = "Six";
    if(SR.substr(1, 2) == "7")
        newR = "Seven";
    if(SR.substr(1, 2) == "8")
        newR = "Eight";
    if(SR.substr(1, 2) == "9")
        newR = "Nine";
    if(SR.substr(1, 2) == "10")
        newR = "Ten";
    if(SR.substr(1, 2) == "11")
        newR = "Jack";
    if(SR.substr(1, 2) == "12")
        newR = "Queen";
    if(SR.substr(1, 2) == "13")
        newR = "King";
    if(SR.substr(1, 2) == "14")
        newR = "Ace";

    SR = newR + " of " + newS; // this string had the numbers in it but now we can  
                               // reassign it the string 'Card of suit'

    return SR; // returns the string which is outputted to the console when you call
               // the 'toString' function
};

// This function draws top object of the vector then pops it from 
// the vector and returns it to the call. It is of return type 'Card'.

Card Deck::draw() 
    {
        int a = Cards.size(); 
        int b = a - 1;        // -1 because the vector has 52 cards but you 
        Cards.pop_back();     // want to access [0] - [51] not [52]
        return Cards[b];
    };

// This is the function that creates the cards in the vector.
// It uses two loops and assigns a number to 'a' and 'b'. The first number is 
// in the range 1 - 4 and the second is 2 - 14. It then creates an object using 
// 'a' and 'b' which is then pushed back onto the vector. All vector objects have
// the same name as of now (but not the same data). Shuffles the objects at the end.

void Deck::shuffleCards()
{
    int a;
    int b;
    for(a = 1; a < 5; a++) // 1 - 4
    {
        for(b = 2; b < 15; b++ ) // 2 - 14
        {
            Card newCard(a, b);
            Cards.push_back(newCard);
        }
    }

    std:: mt19937 seed(rd());                        // this creates the seed
    std::shuffle(Cards.begin(), Cards.end(), seed);  // this shuffles the deck with the 
};                                                   // random seed 

// This function checks if the deck is empty 
// if it is not it will return false and when it is empty 
// it will return true which breaks the loop in main.cpp

bool Deck::empty()
{
    if(Cards.size() < 1)
        return true;
    else 
        return false;
};

// This function will reset the deck if called. It will purge 
// the vector and then repopulate it with the original contents.
// but will not shuffle them. 

void Deck::reset()
{
    Cards.clear();
    int a;
    int b;
    for(a = 1; a < 5; a++)
    {
        for(b = 2; b < 15; b++ )
        {
            Card newCard(a, b);
            Cards.push_back(newCard);
        }
    }
};

and header

#ifndef DECK_H
#define DECK_H
#include <vector> 
#include <string> 
#include <random>
#include <algorithm>

class Card
{
public:
    inline Card(int s, int r);
    int rank;
    int suit;
    std::string toString();
};

class Deck 
{
private:
    std::vector<Card> Cards;
    std::random_device rd;
public:
    void shuffleCards();
    void reset();
    Card draw();
    bool empty();
};

class Hand
{
    public: 
        std::vector<std::string> Hand;
        void fillHand()
        {
            Deck deck;
            std::string C1 = deck.draw().toString();
            std::string C2 = deck.draw().toString();
            std::string C3 = deck.draw().toString();
            std::string C4 = deck.draw().toString();
            std::string C5 = deck.draw().toString();
            Hand.push_back(C1);
            Hand.push_back(C2);
            Hand.push_back(C3);
            Hand.push_back(C4);
            Hand.push_back(C5);
        }
};
#endif

Thank you in advance :D

at this point I basically create a new object from the hand class which fills the hand up with 5 random cards and in each card is a string in the form of "card of suit". Since I only need to worry about the "card" should I change the code to throw away the "of" and "suit"? Eventually we are gonna need to also use the suit which is why I did not want to do that.

3 Answers
Related