A Poker Game... Sounds easy enough, right?

This is a discussion on A Poker Game... Sounds easy enough, right? within the C++ Programming forums, part of the General Programming Boards category; Well... here I am, making a Card class, thinking I'll make yet another useless poker game or something. Making a ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Godthåb, Grønland
    Posts
    9

    A Poker Game... Sounds easy enough, right?

    Well... here I am, making a Card class, thinking I'll make yet another useless poker game or something. Making a card class is easy. Just put a couple of variables in there, some accessors, and throw in some comparison methods or something.

    What's hard is using it to make a poker game. My thinking was: "Okay, I'll make a Card class, then I'll rank hands in a simple top-down order or something." You know, check for a royal flush first, then 4 of a kind, etc. Sounds simple enough, right?

    Wrong. At least, it's not as simple as I thought it would be. Maybe I'm just too lazy to get down and dirty. Here's my file, card.h (actually card.0.1.h because I added a couple of things). I made a few efforts for my top-down checking method, but none of them work. Any ideas?


    Code:
    #include <string.h>
    
    #define ASCII char
    
    class Card;
    
    void GetCardName (ASCII *, Card*);
    
    class Card{
    	public:
    		Card();
    		Card(int, int);
    		~Card() { }
    
    		int GetSuit() const { return itsSuit;}
    		int GetValue() const { return itsValue; }
    
    		int Compare(const Card&) const;
    		static int cCompare(const Card*, const Card*);
    
    	private:
    		int itsSuit;
    		int itsValue;
    };
    
    Card::Card():
    	itsSuit(1),
    	itsValue(1)
    { }
    
    Card::Card(int suit, int value):
    	itsSuit(suit),
    	itsValue(value)
    { }
    
    int Card::Compare(const Card& rhs) const{
    	int thisValue = this->GetValue();
    	int thatValue = rhs.GetValue();
    
    	if ( (thisValue == 1) && (thatValue != 1) )
    		return 1;
    
    	if ( thisValue > thatValue )
    		return 1;
    
    	if ( thisValue < thatValue )
    		return 2;
    
    	if ( thisValue == thatValue )
    		return 0;
    
    	return -1;
    }
    
    int Card::cCompare(const Card* lhs, const Card* rhs){
    	int thisValue = lhs->GetValue();
    	int thatValue = rhs->GetValue();
    
    	if ( (thisValue == 1) && (thatValue != 1) )
    		return 1;
    
    	if( thisValue > thatValue )
    		return 1;
    
    	if ( thisValue < thatValue )
    		return 2;
    
    	if ( thisValue == thatValue )
    		return 0;
    
    	return -1;
    }
    
    void GetCardName (ASCII * retVal, Card* theCard){
    
    	switch (theCard->GetValue()) {
    		case 1: strcat(retVal, "Ace "); 		break;
    		case 2: strcat(retVal, "Two "); 		break;
    		case 3: strcat(retVal, "Three "); 	break;
    		case 4: strcat(retVal, "Four ");		break;
    		case 5: strcat(retVal, "Five ");		break;
    		case 6: strcat(retVal, "Six "); 		break;
    		case 7: strcat(retVal, "Seven ");	break;
    		case 8: strcat(retVal, "Eight "); 	break;
    		case 9: strcat(retVal, "Nine ");		break;
    		case 10: strcat(retVal, "Ten ");		break;
    		case 11: strcat(retVal, "Jack ");	break;
    		case 12: strcat(retVal,	"Queen ");	break;
    		case 13: strcat(retVal, "King ");	break;
    		default: strcat(retVal, "Ace ");		break;
    	}
    
    	strcat(retVal, "of ");
    
    	switch (theCard->GetSuit()) {
    		case 1: strcat(retVal, "Spades");	break;
    		case 2: strcat(retVal, "Clubs");		break;
    		case 3: strcat(retVal, "Diamonds");	break;
    		case 4: strcat(retVal, "Hearts");	break;
    	}
    }
    Quick edit: Oh, yeah, I suppose it would be helpful to know the Comparison methods are meant to return a zero if the cards are ranked as equal, 1 if the first card (either this or the lhs) is greater than rhs, 2 if rhs is greater than the first card (either this or the lhs), or -1 if none of these conditions are met (which shouldn't happen).
    Last edited by 809AreaCode; 10-21-2011 at 10:54 PM. Reason: Just some extra info...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    So do you have a main(), which calls the compare function with some card values ?

    Does it print the right answer?

    If not, which test cases cause it to break?


    You should also be able to simplify cCompare to just
    Code:
    int Card::cCompare(const Card* lhs, const Card* rhs){
        return lhs->Compare( *rhs );
    }
    Why are these pointers, and not references by the way?

    > void GetCardName (ASCII * retVal, Card* theCard)
    It would be far better if this returned a std::string
    Also, you need to make sure your retVal is initialised to an empty string before trying to strcat stuff into it.
    Eg.
    retVal[0] = '\0';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Godthåb, Grønland
    Posts
    9
    I've already created a main function and set retVal to a null filled string. The whole thing works as expected.

    cCompare accepts pointers... I guess because I just felt like using pointers. It's really just a rough draft right now, though - I'll worry about consistency later. For the record, I prefer pointers to references.

    So yes, it works as it should. What I'm asking is what my next steps should be.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    How about:

    A class called Deck, containing up to 52 cards, which has methods like "shuffle" and "deal".

    A class called Hand, containing up to 5 cards, which has methods like - well what do you want a hand to do?

    So main() creates a deck, shuffles it, deals a few cards into a pair of hands, and goes from there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Quote Originally Posted by 809AreaCode View Post
    I've already created a main function and set retVal to a null filled string. The whole thing works as expected.
    Why not use std::string?

    cCompare accepts pointers... I guess because I just felt like using pointers. It's really just a rough draft right now, though - I'll worry about consistency later. For the record, I prefer pointers to references.
    Why?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Location
    Godthåb, Grønland
    Posts
    9
    Once again, I've done all of these things... well, hand is an array, not a class because I'm too lazy, but... yeah.

    What I want is someone to shed light on how I might determine the value of a hand.

    @Elysia: Probably because I've used C for so long.

  7. #7
    Nor
    Nor is offline
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Quote Originally Posted by 809AreaCode View Post
    What I want is someone to shed light on how I might determine the value of a hand.
    I didn't read through your code so bare with me.
    first you need to know what type of hand.
    then say for 2+ players, you need to know the best hand.

    below is a simple prototype function. with lots missing. but its
    a good way to analize the problem.

    You define the types of hands. and the best wins. thats the base value.
    but when to hands have the same base value, you need to compare the high set.
    the better wins.

    if there equal, then you need to compair the low set.

    I suggest you read up on the rules of poker for more detailed
    information about hands that closely match and which wins.

    Hope this helps!

    Code:
    void GetHandValue( card* array, int& basevalue, int& highset, int& lowset){
        highset = 0;
        lowset = 0;
        if( royalFlush( array ) ){
            basevalue = ROYAL_FLUSH;
            return;
        }
        if( strightFlush( array ) ) {
            basevalue = STRIGHT_FLUSH;
            highset = GetHighCard( array );
            return;
        }
        if( fourOfAKind( array ) ){
            basevalue = FOUR_OF_A_KIND;
            highset = GetPairValue( array );
            return;
        }
        if( fullHouse( array ) ){
            basevalue = FULL_HOUSE;
            highset = GetTripleValue( array );
            lowset  = GetPairValue( array );
            return;
        }
        if( flush( array ) ){
            basevalue = FLUSH;
            highset = GetHighCard( array );
            return;
        }
        if( straight( array ) ){
            basevalue = STRIGHT;
            highset = GetHighCard( array );
            return;
        }
        if( threeOfAKind( array ) ){
            basevalue = THREE_OF_A_KIND;
            highset = GetTripleValue( array );
            return;
        }
        if( pair( array ) ){
            basevalue = PAIR;
            highset = GetPairValue( array );
            return;
        }
        basevalue = NONE;
        return;
    }
    Quote Originally Posted by 809AreaCode View Post
    Well... here I am, making a Card class, thinking I'll make yet another useless poker game or something.
    Its a great way to learn logic, besides, we've all done it. mine was solitary.
    Last edited by Nor; 10-22-2011 at 08:06 PM. Reason: Added Comment
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. This sounds easy but have never done it
    By ralphwiggam in forum C Programming
    Replies: 8
    Last Post: 09-30-2006, 08:05 AM
  2. Poker Game need help...
    By boyracer88 in forum Game Programming
    Replies: 1
    Last Post: 11-25-2005, 12:32 PM
  3. Poker Game
    By egomaster69 in forum C Programming
    Replies: 18
    Last Post: 01-03-2005, 05:24 PM
  4. poker game
    By b00l34n in forum Game Programming
    Replies: 14
    Last Post: 01-03-2005, 11:21 AM
  5. Poker Game
    By Smiley**123 in forum Game Programming
    Replies: 6
    Last Post: 12-14-2003, 08:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21