Hey guys, noob programmer here. Over the last week or so we've been learning about classes and inheritance in my latest progamming class. I've recieved a variation of the ever so popular poker program and am having some problems putting it all together.

I haven't had the opportunity to test the logic because the program obviously isnt in a running state. The parts I KNOW I have a problem with are dealing from the vector, I just dont know the syntax to transfer it to myHand[]. I am also having problems with calling the functions in main another syntax problem for me im affraid.

****on another note im not sure if the clearHand() function has a purpose, I didn't know if it would be necesscary, and if it is, could use some help on that.

Card.h

#pragma once
#include<string>
#include<cstdlib>

using namespace std;

class Card
{
public:

    Card();
    string getSuit();
    char suit[4];
    int value[13];
    int getRank();


private:

    int rank;

Card.cpp

#include "Card.h"
#include <string>

Card::Card()
{
    suit[4]=('c', 'h', 'd', 's');
    value[13]=('1','2','3','4','5','6','7','8','9','10', '11','12','13');
}

string Card::getSuit()
{
return suit;
}

int Card::getRank()
{
return rank;
}

Hand.h

#pragma once
#include"Deck.h"
#include"Card.h"
#include <vector>

using namespace std;

class Hand
{
public:

    vector<Card> myHand[4];
    int totalFlush;
    void clearHand();
    bool checkHand(vector<Card>);


private:



};

Hand.cpp

#include "Hand.h"
#include "Card.h"

void Hand::clearHand()
{

}
bool Hand::checkHand(vector<Card>myHand)
{
     // Check for a flush (all the same suit)
    if(myHand[0].getSuit() == myHand[1].getSuit() && myHand[1].getSuit()==myHand[2].getSuit() && myHand[2].getSuit()==myHand[3].getSuit() && myHand[3].getSuit()==myHand[4].getSuit())
    {
        totalFlush++;
    }


}

Deck.h

#pragma once
#include <vector>
#include"Card.h"



using namespace std;

    class Deck
{
public:

    Deck();
    void createDeck(vector<Card>myDeck);
    void createValue(vector<Card>myDeck);
    void createSuit(vector<Card>myDeck);
    void shuffleDeck(vector<Card>myDeck);
    void dealCards();
    vector<Card> myDeck;



private:


};

Deck.cpp

#include "Deck.h"
#include "Card.h"
#include<vector>
#include<algorithm>


    Deck::Deck()
    {
    vector<Card> myDeck(52);
    createDeck(myDeck);
    }


    void Deck::createValue(vector<Card>myDeck)
    {
        int x=0;
        do
        {


        for(int i=0;i<13;i++)
            {
            myDeck[i].getRank=i;
            }
        x++;
        }
        while(x<3);


    }
    void Deck::createSuit(vector<Card>myDeck)
    {
        for(int i = 0; i<52; i++)
            if (i<=12)
            {
            myDeck[i].getSuit='c';
            }
            else if (i<=25)
            {
            myDeck[i].getSuit='d';
            }
            else if (i<=38)
            {
            myDeck[i].getSuit='h';
            }
            else if (i<=51)
            {
            myDeck[i].getSuit='s';
            }
    }

    void Deck::shuffleDeck(vector<Card>myDeck)
    {
    random_shuffle(myDeck.begin(), myDeck.end());
    }


    void Deck::dealCards()
    {

    }

Main.cpp

include <iostream>
#include <stdlib.h>
#include <time.h>
#include "Deck.h"
#include "Card.h"
#include "Hand.h"

using namespace std;


int main()
{
    cout<<"Welcome to the Poker Experiment."<<endl;
    int totalHands =0;
    float flushPercent=0, totalFlush=0;

    Card card1;
    Deck deck1;
    Hand hand1;


            do
            {
            //Create Deck
            deck1.createDeck();

            //Shuffle Deck
            deck1.shuffleDeck();

                for(int i = 52; i > 5; i-5)
                {
            //Deal Hand
            deck1.dealCards();

            //Check Hand
            hand1.checkHand();

            totalHands++;

            //Clear Hand
            hand1.clearHand();
                }
            }
            while(totalHands <16000);

            flushPercent=totalFlush/totalHands*100;

        cout<<"The probability of you getting a flush over 16000 hands: "<<flushPercent;





    return 0;
}

Recommended Answers

All 10 Replies

Some of your objects don't seem to make a lot of sense.

I see you have a card class. Let's look at that. Here are some of its internals:

char suit[4];
int value[13];

So one card has an array of 4 chars, named suit, and also an array of 13 ints, called value. Is this object meant to represent a single card? If it is, why does it contain four different suits and 13 different values? When I think of a single card, it has one suit and one value.

Some of the functions just plain don't work. Your function getSuit says it returns a string, but you're returning an array of four char. Your card constructor looks like you're trying to give every card every suit and every value. It makes no sense. Shouldn't a card have one suit and one value?

Let's look at the hand class. Here is one of its internals:

vector<Card> myHand[4];

This is an array of four vectors. So your class hand contains four vectors. Is this meant to represent a single hand of cards? That would be done with one vector. Why are there four vectors?

As for your issue with dealing cards from the deck, there are many solutions. For example, you could just shuffle the deck each time and then just take the first five cards from the deck.

I've fixed up my syntax today but now i am having problems with calculating the correct flush probability it keeps showing up .15 and the more hands I deal makes the probability better(by alot), I ran it 900000 times and ended up with something like 11.8%.

Anyways new code

Card.h

#pragma once
#include<string>
#include<cstdlib>

using namespace std;

class Card
{
public:

    int suit;
    int rank;


private:


};

Hand.h

#pragma once

#include"Card.h"
#include <vector>

using namespace std;

class Hand
{
public:

    void addCard(Card c);
    bool isFlush();




private:

    vector<Card> myHand;
    int suit;
};

Deck.h

#pragma once
#include <vector>
#include"Card.h"
#include"Hand.h"



using namespace std;

    class Deck
{
public:

    Deck();
    void shuffleDeck();
    Hand dealCards();




private:

    vector<Card> myDeck; 
};

Hand.cpp

#include "Hand.h"
#include "Card.h"


bool Hand::isFlush()
{
    bool result;

     // Check for a flush (all the same suit)
    if(myHand[0].suit == myHand[1].suit && myHand[0].suit== myHand[2].suit && myHand[0].suit==myHand[3].suit && myHand[0].suit==myHand[4].suit)
    {
        result = true;
    }
    else
    {
        result = false;
    }

    return result;
}


void Hand::addCard(Card c)
{
    myHand.push_back(c);
}

Deck.cpp

#include "Deck.h"
#include "Card.h"
#include<vector>
#include<algorithm>


    Deck::Deck()
    {
        for(int i=0; i<52;i++)
        {
            Card c;
            c.suit=i/13;
            c.rank=(i%13)+2;
            myDeck.push_back(c);

        }
    }

    void Deck::shuffleDeck()
    {
      random_shuffle(myDeck.begin(), myDeck.end());
    }

    Hand Deck::dealCards()
    {
        Hand result;
        for(int i=0;i<5;i++)
        {
            result.addCard(myDeck[i]);
        }

        return result;
    }

Main.cpp

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

using namespace std;


int main()
{
    cout<<"Welcome to the Poker Experiment."<<endl;
    Deck deck;
    int flushCount=0;
    int numberOfHands=16000;
    for(int i=0; i<16000;i++)
    {
        deck.shuffleDeck();
        Hand hand=deck.dealCards();
        if(hand.isFlush())
        {
            flushCount++;
            deck.shuffleDeck();
        }
        else
        {
            deck.shuffleDeck();
        }
        hand = deck.dealCards();

    }
    cout<<"Flush percentage = "
        << ((double)flushCount/(double)numberOfHands)*100.0<<endl;




    return 0;
}

There are about two and a half million possible combinations, so you need to test a statistically significiant subset. So long as you get something close to 0.19 percent (i.e. a bit under twice per every thousand hands dealt) it's fine. The random number generator isn't truly random (it's worth learning a bit about the available random number generators in C++).

It'd be a good idea to swap out that 16000 in your loop with numberOfHands.

It'd be a good idea to swap out that 16000 in your loop with numberOfHands.

Thats what was in the for loop, I was just testing different numbers and forgot to change it back.

And the program keeps repeating values for example, if I shuffle it once, it's .135, If I shuffle it twice, .15. These are the only two values I can get, no matter how many times I shuffle the deck its the same two percentiles...

As previously mentioned, I ran a simulation on 1 million hands and my result was something outrageous like 11.7%.

In theory my flush logic is sound, so I really dont know where it is going wrong.

Beats me. When I run your code (with 2 million iterations) it comes out with 0.194 percent - the right answer, near enough. What compiler are you using?

MSVS 2012 is what I use to create the code im not sure which compiler.

You're using the Microsoft C++ compiler, then.

Here's your code with 500000 iterations; comes out pretty much correct.

http://ideone.com/y7C9qk

You're using the Microsoft C++ compiler, then.

Here's your code with 500000 iterations; comes out pretty much correct.

I'm beyond baffled. I tried it on a different computer, same compiler MSVS2012
same result.

Neat website, good resource. Well at this point I dont know what is causing mine to run differently. I removed the using namespace from the .h files just for some house cleaning. But to no avail.

random_shuffle using an implementation defined random number generator. Maybe yours isn't very good. It probbaly uses rand() internally, so try seeding rand with the time, by stickingsrand(time(0)); in once at the start.

Thank you my friend. problem solves program submitted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.