GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Jan-2007, 21:25
ineedcodinghelp ineedcodinghelp is offline
New Member
 
Join Date: Jan 2007
Posts: 10
ineedcodinghelp is on a distinguished road
Exclamation

Poker program code problem


I must write a program that will figure out the value of a hand of 3 cards. I can use if statements, and if else, and other things of that nature. I must generate three random numbers, for the 3 cards, while making sure they are not duplicates....then i must find a way to label the cards by rank and suit somehow, from 1 - 52. The program should function like a poker game, the user is dealt three random cards, and it should tell the user what type of hand they have (straight, flush, etc.)....And I must loop the whole thing so the user can elect to play again.

I am a beginner so i do not kno much about c++, and im struggling with this one greatly, i have to get it done within 24 hours, and im lost right now.

What i have been able to do:
write if statements that print to the screen saying, card 1 is this suit, and then later saying, card 1 is this rank. i dont know how to combine them saying, card1 is a 2 of hearts, etc.
I thought i would have trouble with determining the different hands, such as straights and stuff, but i havent even got that far...i just have a mess of code testing the waters to see wut i kno how to do....

if necessary, i will post my code, but i wont unless asked because it is not close in any manner to being what i want it to be.

Please, some advice? clues? Im really lost, and time is of the essence. Thanks in advance for all those who will be helping me here.
  #2  
Old 09-Jan-2007, 22:04
davis
 
Posts: n/a

Re: Poker program code problem


Quote:
Originally Posted by ineedcodinghelp
I must write a program that will figure out the value of a hand of 3 cards. I can use if statements, and if else, and other things of that nature. I must generate three random numbers, for the 3 cards, while making sure they are not duplicates....then i must find a way to label the cards by rank and suit somehow, from 1 - 52. The program should function like a poker game, the user is dealt three random cards, and it should tell the user what type of hand they have (straight, flush, etc.)....And I must loop the whole thing so the user can elect to play again.

I am a beginner so i do not kno much about c++, and im struggling with this one greatly, i have to get it done within 24 hours, and im lost right now.

What i have been able to do:
write if statements that print to the screen saying, card 1 is this suit, and then later saying, card 1 is this rank. i dont know how to combine them saying, card1 is a 2 of hearts, etc.
I thought i would have trouble with determining the different hands, such as straights and stuff, but i havent even got that far...i just have a mess of code testing the waters to see wut i kno how to do....

if necessary, i will post my code, but i wont unless asked because it is not close in any manner to being what i want it to be.

Please, some advice? clues? Im really lost, and time is of the essence. Thanks in advance for all those who will be helping me here.

I recently wrote a PlayingCard class implementation for representing a playing card. For brevity and simplicity, I combined everything into a single file, but it may be helpful for you to see it.

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

const static char FACE_VALUES[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
const static char* FACE_NAMES[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
const static char SUIT_VALUES[] = { 0x10, 0x20, 0x30, 0x40 };
const static char* SUIT_NAMES[] = { "Spades", "Hearts", "Clubs", "Diamonds" };
const static char MIN_CARD_VALUE = 1 | 0x10;
const static char MAX_CARD_VALUE = 13 | 0x40;

class PlayingCard
{
public:
    const static char SPADES	= 0x10;
    const static char HEARTS	= 0x20;
    const static char CLUBS	= 0x30;
    const static char DIAMONDS	= 0x40;
    
    PlayingCard() : m_value( 0 ){}
    PlayingCard( const char value ) : m_value( value ){}
    PlayingCard( PlayingCard const& rhs ){ m_value = rhs.m_value; }
    const char getValue() const { return m_value; }
    void setValue( const char value ){ m_value = validateValue( value ) ? value : m_value; }
    std::string toString()
    {
	std::string value;
	if( validateValue( m_value ) )
	{
	    std::string face( FACE_NAMES[ (m_value % 0x10) -1 ] );
	    std::string suit( SUIT_NAMES[ (m_value / 0x10) -1 ] ); 
	    value = face;
	    value += " ";
	    value += suit;
	}
	else
	{
	    value = "Invalid Playing Card!";
	}
	return value;
    }
    const char getFaceValue() const { return FACE_VALUES[ (m_value % 0x10) -1 ]; }
    const char getSuitValue() const { return SUIT_VALUES[ (m_value / 0x10) -1 ]; }
    static char makeValue( const char face, const char suit ) { return validateValue( face | suit ) ? ( face | suit ) : 0; }
private:
    char m_value;
    static bool validateValue( const char value )
    {
	bool bIsValid = false;
	if( value >= MIN_CARD_VALUE && value <= MAX_CARD_VALUE )
	{
	    bIsValid = true;
	}
	return bIsValid;
    } 
};

typedef std::vector<PlayingCard> vPlayingCardDeck;

void spew_deck( vPlayingCardDeck& v )
{
    vPlayingCardDeck::iterator it;
    for( it = v.begin(); it != v.end(); it++ )
    {
	cout << (*it).toString() << endl;
    }
}

int main()
{
    vPlayingCardDeck v;

    for( int i = 0x10; i < 0x50; i += 0x10 )
    {
	for( int j = 1; j < 14; j++ )
	{
	    PlayingCard card( i | j );
	    v.push_back( card );
	}
    }

    spew_deck( v );

    PlayingCard card;
    cout << card.toString() << endl;

    card.setValue( 12 | PlayingCard::SPADES );
    cout << card.toString() << endl;

    cout << "\t" << static_cast<int>( card.getFaceValue() ) << endl;
    cout << setfill( '0' ) << showbase << hex << internal;
    cout << "\t" << static_cast<int>( card.getSuitValue() ) << endl;

    card.setValue( PlayingCard::makeValue( 11, PlayingCard::DIAMONDS ) );
    cout << card.toString() << endl;

    return 0;
}    


Comparing cards in a "HandEvaluator" class is not difficult. Let me know if you need more help. In this code, if you want to add a random hand generator, simply:

CPP / C++ / C Code:
// add these lines to the includes
#include <algorithm>
#include <cstdlib>
#include <ctime>
// rest of PlayingCard class goes here...

int main()
{
    vPlayingCardDeck v;

    for( int i = 0x10; i < 0x50; i += 0x10 )
    {
	for( int j = 1; j < 14; j++ )
	{
	    PlayingCard card( i | j );
	    v.push_back( card );
	}
    }

    for( int i = 0; i < 17; i++ )
    {
	srand( time(0) );
	random_shuffle( v.begin(), v.end() );
	for( int j = 0; j < 3; j++ )
	{
	    if( j == 0 )
	    {
		cout << "Next Hand: " << endl;
	    }
	    cout << "\t" << v[j].toString() << endl;
	}
    }

    return 0;
}    

Output:

Code:
Next Hand: K Hearts 4 Clubs 9 Diamonds Next Hand: Q Spades Q Hearts 5 Hearts Next Hand: 3 Hearts 8 Clubs A Diamonds Next Hand: 4 Hearts 10 Spades K Spades Next Hand: 4 Diamonds 10 Diamonds 4 Spades Next Hand: 5 Clubs A Hearts 7 Diamonds Next Hand: 6 Clubs 8 Hearts 7 Hearts Next Hand: K Diamonds 6 Diamonds K Clubs Next Hand: 9 Clubs J Spades 5 Spades Next Hand: 8 Diamonds Q Clubs 2 Clubs Next Hand: 6 Spades 2 Spades 2 Diamonds Next Hand: 3 Clubs 4 Clubs 7 Spades Next Hand: J Clubs Q Hearts 5 Diamonds Next Hand: J Hearts 8 Clubs Q Diamonds Next Hand: J Diamonds 10 Spades 6 Hearts Next Hand: 2 Hearts 10 Diamonds 3 Diamonds Next Hand: 9 Hearts A Hearts 7 Clubs


:davis:
  #3  
Old 10-Jan-2007, 11:07
ineedcodinghelp ineedcodinghelp is offline
New Member
 
Join Date: Jan 2007
Posts: 10
ineedcodinghelp is on a distinguished road

Re: Poker program code problem


the second coding sections was more my speedin terms of technique. the first part is way too advanced for me. Thats part of the problem, its this huge complicated program, and all we can use is the very basic stuff like if or if else statements. And on top of that, once we get the hands like you did, we have to tell what type of hand it is somehow, like a straight, or 3 of a kind, etc.

thanks for your initial post....any advice?
  #4  
Old 10-Jan-2007, 15:03
davis
 
Posts: n/a

Re: Poker program code problem


Quote:
Originally Posted by ineedcodinghelp
the second coding sections was more my speedin terms of technique. the first part is way too advanced for me. Thats part of the problem, its this huge complicated program, and all we can use is the very basic stuff like if or if else statements. And on top of that, once we get the hands like you did, we have to tell what type of hand it is somehow, like a straight, or 3 of a kind, etc.

thanks for your initial post....any advice?

I understand the requirements. My advice is to plan better so that you have more time (than 24h) to solve the problem...I can write the code easily using nothing but simple coding techniques and just a single character per card. The logic for evaluating a hand is relatively simple, but you can not have a straight in 3 cards, since a straight is 5 cards...unless you want to modify the rules of poker to accept that only 3 cards are used for all hand qualifications.

An easy approach is to decide whether or not a hand is a straight, flush, straight-flush, three of a kind (no four of a kind, full house or two pair possible in 3 cards), pair or high card or tie.

The key is in setting up your "filtering" so that you assign a value to each hand that corresponds to a possible hand. Then you compare whether or not the hands of the other players exceed, are equal to or are less than the value of one of the player hands working through until you get the best hand (or a tie) between all of the players/possible hands that must be evaluated. It isn't very difficult. I can easily write it, but probably not within the next few hours or maybe not even today as I have other things that I need to do very soon.

I would expect such a simple implementation to start out something like:

CPP / C++ / C Code:

char PlayingCardDeck[52];
char Player1Hand[3];
char Player2Hand[3];
char Player...Hand[3];

initialize_playingcard_deck(...);
shuffle_deck(...);
assign_cards_to_players(...);
evaluate_hands(...);
report_results(...);


...though an multi-dimensional array for the number of players would also work well and probably make the evaluation operation easy to write.


:davis:
  #5  
Old 10-Jan-2007, 19:58
ineedcodinghelp ineedcodinghelp is offline
New Member
 
Join Date: Jan 2007
Posts: 10
ineedcodinghelp is on a distinguished road

Re: Poker program code problem


thanks davis, i got it done (hopefully its correct haha)..
i appreciate the response
  #6  
Old 11-Jan-2007, 10:05
davis
 
Posts: n/a

Re: Poker program code problem


Quote:
Originally Posted by ineedcodinghelp
thanks davis, i got it done (hopefully its correct haha)..
i appreciate the response

Well then, let's see it!

:davis:
  #7  
Old 11-Jan-2007, 15:14
ineedcodinghelp ineedcodinghelp is offline
New Member
 
Join Date: Jan 2007
Posts: 10
ineedcodinghelp is on a distinguished road

Re: Poker program code problem


keep in mind, some things could have been completed much easier using other techniques, but i was forced to use if statements haha


Code:
#include <iostream> #include <ctime> #include <cmath> #include <cstdlib> #include <cstring> #include <fstream> using namespace std; int main() { int card_value1, card_value2, card_value3; int suit1, suit2, suit3; double rank1, rank2, rank3; int ans; srand((unsigned) time(NULL)); //loop to repeat whole program do{ //generates random number for the card value //loops until there is not an illegal hand do { card_value1=((rand()%52)+1); card_value2=((rand()%52)+1); card_value3=((rand()%52)+1); }while(card_value1==card_value2||card_value1==card_value3||card_value2==card_value3); //determines rank of card rank1=(card_value1%13); rank2=(card_value2%13); rank3=(card_value3%13); suit1=(card_value1%4); suit2=(card_value2%4); suit3=(card_value3%4); //prints the rank of the card to the screen if(rank1==0) { cout<<"Card1 is an ace"; } else if(rank1==1) { cout<<"Card1 is a two"; } else if(rank1==2) { cout<<"Card1 is a three"; } else if(rank1==3) { cout<<"Card1 is a four"; } else if(rank1==4) { cout<<"Card1 is a five"; } else if(rank1==5) { cout<<"Card1 is a six"; } else if(rank1==6) { cout<<"Card1 is a seven"; } else if(rank1==7) { cout<<"Card1 is an eight"; } else if(rank1==8) { cout<<"Card1 is a nine"; } else if(rank1==9) { cout<<"Card1 is a ten"; } else if(rank1==10) { cout<<"Card1 is a jack"; } else if(rank1==11) { cout<<"Card1 is a queen"; } else { cout<<"Card1 is a king"; } //Determines the suit for card1 if(suit1==0) { cout<<" of hearts"<<endl; } else if(suit1==1) { cout<<" of spades"<<endl; } else if(suit1==2) { cout<<" of diamonds"<<endl; } else if(suit1==3) { cout<<" of clubs"<<endl; } //same as above for card2 if(rank2==0) { cout<<"Card2 is an ace"; } else if(rank2==1) { cout<<"Card2 is a two"; } else if(rank2==2) { cout<<"Card2 is a three"; } else if(rank2==3) { cout<<"Card2 is a four"; } else if(rank2==4) { cout<<"Card2 is a five"; } else if(rank2==5) { cout<<"Card2 is a six"; } else if(rank2==6) { cout<<"Card2 is a seven"; } else if(rank2==7) { cout<<"Card2 is an eight"; } else if(rank2==8) { cout<<"Card2 is a nine"; } else if(rank2==9) { cout<<"Card2 is a ten"; } else if(rank2==10) { cout<<"Card2 is a jack"; } else if(rank2==11) { cout<<"Card2 is a queen"; } else { cout<<"Card2 is a king"; } //Use modulus to determine suit for card1, above. if(suit2==0) { cout<<" of hearts"<<endl; } else if(suit2==1) { cout<<" of spades"<<endl; } else if(suit2==2) { cout<<" of diamonds"<<endl; } else if(suit2==3) { cout<<" of clubs"<<endl; } //same procedure as above for card 3 if(rank3==0) { cout<<"Card3 is an ace"; } else if(rank3==1) { cout<<"Card3 is a two"; } else if(rank3==2) { cout<<"Card3 is a three"; } else if(rank3==3) { cout<<"Card3 is a four"; } else if(rank3==4) { cout<<"Card3 is a five"; } else if(rank3==5) { cout<<"Card3 is a six"; } else if(rank3==6) { cout<<"Card3 is a seven"; } else if(rank3==7) { cout<<"Card3 is an eight"; } else if(rank3==8) { cout<<"Card3 is a nine"; } else if(rank3==9) { cout<<"Card3 is a ten"; } else if(rank3==10) { cout<<"Card3 is a jack"; } else if(rank3==11) { cout<<"Card3 is a queen"; } else { cout<<"Card3 is a king"; } //Use modulus to determine suit for card1, above. if(suit3==0) { cout<<" of hearts"<<endl; } else if(suit3==1) { cout<<" of spades"<<endl; } else if(suit3==2) { cout<<" of diamonds"<<endl; } else if(suit3==3) { cout<<" of clubs"<<endl; } //end of card outcomes //hand values //hand is illegal if(card_value1==card_value2||card_value1==card_value3||card_value2==card_value3) { cout<<"Illegal Hand"<<endl; } //three of a kind else if(rank1==rank2&&rank2==rank3) { cout<<"You have three of a kind"<<endl; } //pair else if(rank1==rank2||rank1==rank3||rank2==rank3) { cout<<"You have a pair"<<endl; } //straight else if((abs(rank1-rank2)==1)&&(abs(rank2-rank3)==1)) { cout<<"You have a straight"<<endl; } else if((abs(rank1-rank2)==1)&&(abs(rank3-rank1)==1)) { cout<<"You have a straight"<<endl; } else if((abs(rank2-rank3)==1)&&(abs(rank3-rank1)==1)) { cout<<"You have a straight"<<endl; } //Determining if hand is a straight flush, or royal straight flush else if(suit1==suit2&&suit2==suit3) { if((abs(rank1-rank2)==1)&&(abs(rank2-rank3)==1)) { if(rank1>=9&&rank2>=9&&rank3>=9) { cout<<"You have a royal straight flush"<<endl; } else { cout<<"You have a straight flush"<<endl; } } else if((abs(rank1-rank2)==1)&&(abs(rank3-rank1)==1)) { if(rank1>=9&&rank2>=9&&rank3>=9) { cout<<"You have a royal straight flush"<<endl; } else { cout<<"You have a straight flush"<<endl; } } else if((abs(rank2-rank3)==1)&&(abs(rank3-rank1)==1)) { if(rank1>=9&&rank2>=9&&rank3>=9) { cout<<"You have a royal straight flush"<<endl; } else { cout<<"You have a straight flush"<<endl; } } else { cout<<"You have a flush"<<endl; } } //high card else if(rank1>rank2&&rank1>rank3) { cout<<"First card the high card"<<endl; } else if(rank2>rank1&&rank2>rank3) { cout<<"Second card is the high card"<<endl; } else if(rank3>rank1&&rank3>rank2) { cout<<"Third card is the high card"<<endl; } cout<<endl<<"Play again? (Type 1 for yes)"<<endl; cin>>ans; cout<<endl; }while(ans==1); }
  #8  
Old 11-Jan-2007, 20:35
davis
 
Posts: n/a

Re: Poker program code problem


Quote:
Originally Posted by ineedcodinghelp
keep in mind, some things could have been completed much easier using other techniques, but i was forced to use if statements haha

Card1 is an ace of clubs
Card2 is a jack of clubs
Card3 is a nine of hearts
Second card is the high card

Play again? (Type 1 for yes)


...oh well, close


:davis:
  #9  
Old 11-Jan-2007, 20:48
ineedcodinghelp ineedcodinghelp is offline
New Member
 
Join Date: Jan 2007
Posts: 10
ineedcodinghelp is on a distinguished road

Re: Poker program code problem


o jeez, i forgot...thats bad....i forgot to code in there that ace can be high or low...
  #10  
Old 11-Jan-2007, 21:09
davis
 
Posts: n/a

Re: Poker program code problem


Quote:
Originally Posted by ineedcodinghelp
o jeez, i forgot...thats bad....i forgot to code in there that ace can be high or low...

Don't be put-off by such a bug...I have a hand-held Texas Hold'em device that constantly out-ranks:

A-2-3-4-5
..over:
2-3-4-5-6

That is, a "baby straight" is higher than a higher-card straight due to the ace. It is an incredibly annoying bug considering that this is a commercial product, but then again, so is Windoze!


:davis:
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to sort random access file? wmmccoy0910 C Programming Language 12 04-Sep-2006 03:40
Bloodshed Dev C++ Project Options JdS C++ Forum 6 11-Nov-2005 17:23
Problem with int mixed with char,... leitz C++ Forum 17 07-Dec-2004 20:56
problem with the program under Borland awmp-jansen C Programming Language 3 01-Jul-2004 17:05

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:21.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.