Thread: texas holdem game help

  1. #1
    new to c++
    Join Date
    Feb 2009
    Posts
    53

    texas holdem game help

    Hello
    I am writing a program that will be just like texas holdem.
    I have a question about how to score the hands. I was wondering if there was an easier way to make sure you have the right cards in your hand to be able to score it.
    I mean if you have a royal flush you have a 10,j,q,k,a in the same suit. And for a flush you have any five card sequence in the same suit. (eg. 8,9,10,J,Q and A, 2,3,4,5 of same suit). All the cards are of the same suit, and all are consecutive i dont even know if this is right or not I guess what i am asking is, is there a way to shorten my if statment?
    Code:
    void playercardscorer(int card1,int card2,int potcard1,int potcard2,int potcard3,char suite,char potsuite)
    {
    	int playerhandscore;
    	if((card1==10,11,12,13,1)||(card2==10,11,12,13,1)||(potcard1==10,11,12,13,1)||(potcard2==10,11,12,13,1)||(potcard3==10,11,12,13,1)||(suite=='d','c','s','h')||(potsuite=='d','c','s','h'))
    	{
    		playerhandscore=10;
    		cout<<"You have a Royal Flush"<<endl;
    	}
    	if((suite=='d','c','s','h')&&(potsuite=='d'||potsuite=='h'||potsuite=='s'||potsuite=='c'))
    	{
    		playerhandscore=9;
    	}
    
    	
    }

  2. #2
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    here...this is what I was thinking, instead of what I have up top. Is there an easier way to write the outcome instead of having to write out every single outcome there could be.
    this is what im going off from:


    Royal Flush:
    Five card sequence from 10 to the Ace in the same suit (10,J,Q,K,A).


    Straight Flush:
    Any five card sequence in the same suit. (eg. 8,9,10,J,Q and A, 2,3,4,5 of same suit). All the cards are of the same suit, and all are consecutive. Ranking between straights is determined by the value of the high end of the straight.


    Four of a Kind: All four cards of the same index (eg. J,J,J,J).


    Full House:
    Three of a kind combined with a pair (eg. A,A,A,5,5). Ties on a full house are broken by the three of a kind, as you cannot have two equal sets of three of a kind in any single deck.


    Flush:
    Any five cards of the same suit, but not in sequence. Don't be tricked into thinking that all five cards are the same color. The high card determines the winner if two or more people have a flush.


    --------------------------------------------------------------------------------


    Straight:
    Five cards in sequence, but not in the same suit. A straight cannot wrap, meaning it is not a straight if you have a Queen, King, Ace, Two, Three. The higher straight wins if two or more people have a straight. In case of straights that tie, the pot is split.


    Three of a Kind:
    Three cards of the same value. The highest set of three cards wins.


    Two Pair:
    Two seperate pairs (eg. 4,4,Q,Q). As usual the pair with the higher value is used to determine the winner of a tie.


    Pair:
    One pair of two equal value cards constitutes a pair.


    High Card:
    If no one has any of the above winning hands, the tie is determined by the highest value card in the hand. If the highest cards are a tie then the tie is broken by the second highest card. Suits are not used to break ties.
    Code:
    void playercardscorer(int card1,int card2,int potcard1,int potcard2,int potcard3,char suite,char potsuite)
    {
    	int playerhandscore;
    
    	if()//not sure of how to write the if statements
    	{
    		cout<<"You have a Royal Flush"<<endl;
    		playerhandscore=10;
    	}
    	if()
    	{
    		cout<<"You have a Flush"<<endl;
    		playerhandscore=9;
    	}
    	if()
    	{
    		cout<<"You have four of a kind"<<endl;
    		playerhandscore=8;
    	}
    	if()
    	{
    		cout<<"You have a full house"<<endl;
    		playerhandscore=7;
    	}
    	if()
    	{
    		cout<<"You have a flush"<<endl;
    		playerhandscore=6;
    	}
    	if()
    	{
    		cout<<"You have a straight"<<endl;
    		playerhandscore=5;
    	}
    	if()
    	{
    		cout<<"You have three of a kind"<<endl;
    		playerhandscore=4;
    	}
    	if()
    	{
    		cout<<"You have two pair"<<endl;
    		playerhandscore=3;
    	}
    	if()
    	{
    		cout<<"You have a pair"<<endl;
    		playerhandscore=2;
    	}
    	if()
    	{
    		cout<<"Nobody has anything, you have a high card"<<endl;
    		playerhandscore=1;
    	}
    	
    }

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Code:
    card1==10,11,12,13,1
    card1 is only an interger per your argument. I assume you're trying to compare arrays. If so, compare them element by element; and use "int * card1" instead of "int card1" to pass in the array.

    Before you attempt to assign values to winning hands, why don't you write a program to sort the cards first.

    here's one function to check for a straight, maynot be the best algorithm:
    Code:
    bool straight(int * hand)
    {
    // hand needs to be in sorted order: smallest to largerst.
        bool check;
        
        for( i = 0; i < 4;i++)
           {
              if (hand[i+1] == hand[i]+1) check = true;
              else
                 {
                    check = false;
                    break;
                 }
             }
    return check;
    }
    }
    Last edited by nimitzhunter; 12-08-2010 at 08:16 PM.

  4. #4
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    Right now I am not using arrays. But i made another program that would generate random cards using arrays but it didnt work. I dont know whats wrong with it.
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main ()
    {
    	char cardvalue[]={'A','2','3','4','5','6','7','8','9','10','J','Q','K'};
    	char* cardsuit[]={"of Spades","of Hearts","of Diamonds","of Clubs"};
    
    	for(int i=13;i>0;i++)
    	{
    		for(int j=4; j>0;j--)
    		{
    			cout<<"Card:"<<cardvalue[i]<<cardsuit[j]<<endl;
    		}
    	}
    
    	system("pause");
    	return(0);
    }

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by rfoor View Post
    Right now I am not using arrays. But i made another program that would generate random cards using arrays but it didnt work. I dont know whats wrong with it.
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main ()
    {
    	char cardvalue[]={'A','2','3','4','5','6','7','8','9','10','J','Q','K'};
    	char* cardsuit[]={"of Spades","of Hearts","of Diamonds","of Clubs"};
    
    	for(int i=13;i>0;i++)
    	{
    		for(int j=4; j>0;j--)
    		{
    			cout<<"Card:"<<cardvalue[i]<<cardsuit[j]<<endl;
    		}
    	}
    
    	system("pause");
    	return(0);
    }
    that code was not shuffling the deck. all you are doing is printing out the deck in the order dictated by the for loop. Look up rand().

  6. #6
    Registered User
    Join Date
    Dec 2010
    Location
    Hawai'i
    Posts
    2
    Quote Originally Posted by rfoor View Post
    Right now I am not using arrays. But i made another program that would generate random cards using arrays but it didnt work. I dont know whats wrong with it.
    Code:
    #include<iostream>
    
    using namespace std;
    
    int main ()
    {
    	char cardvalue[]={'A','2','3','4','5','6','7','8','9','10','J','Q','K'};
    	char* cardsuit[]={"of Spades","of Hearts","of Diamonds","of Clubs"};
    
    	for(int i=13;i>0;i++)
    	{
    		for(int j=4; j>0;j--)
    		{
    			cout<<"Card:"<<cardvalue[i]<<cardsuit[j]<<endl;
    		}
    	}
    
    	system("pause");
    	return(0);
    }
    There are several other problems with this code besides not giving you random cards.
    Maybe you are aware of these issues and they're just caused by carelessness (doing things too quickly or something like that).

    You have an infinite loop because you used:

    for(int i=13;i>0;i++) // i = 13 sets i to a number larger then 0, adding to it never makes it
    // less then or equal to 0

    changing i++ to i-- fixes this

    Your 'A' char isn't returned, and your '10' char is returned as 0

    Changing char to char* and using " instead of ' fixes the return of 10.
    Adding a "", before "A" fixes the return of A.

    Your "of Spades" char isn't returned.

    Adding a "", before "of Spades" fixes the return of of Spades.

    after all those changes your code would look like this (added some spaces here and there to make things more readable for me):

    Code:
    #include<iostream>
    using namespace std;
    
    int main ()
    {
    	char* cardvalue[] = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    	char* cardsuit[] = {""," of Spades"," of Hearts"," of Diamonds"," of Clubs"};
    
    	for(int i=13; i>0; i--)
    	{
    		for(int j=4; j>0; j--)
    		{
    			cout << "Card: " << cardvalue[i] << cardsuit[j] << endl;
    		}
    	}
    
    	system("pause");
    	return(0);
    }
    Greetz
    More
    Last edited by Moreander; 12-10-2010 at 04:11 AM. Reason: my own carelessness :)

  7. #7
    Registered User
    Join Date
    Dec 2010
    Location
    Hawai'i
    Posts
    2
    The empty "" for both arrays is unnecessary.

    You can avoid it by using:

    cout << "Card: " << cardvalue[i-1] << cardsuit[j-1] << endl; // just so there's no confusion, do not change anything else if you use this option.

    or by changing i>0 and j>0 to i>=0 and j>=0 respectively.
    for this one to work correctly you will also have to lower the initial value of both i and j by 1 (to 12 and 3 respectively)

    Greetz
    More

    sorry, bit of a noob here..
    Last edited by Moreander; 12-10-2010 at 04:56 AM. Reason: more carelessness

  8. #8
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    I understand rand(), but i dont see how it would be used to shuffle the deck. If i made the loop random it would just pick all those cards for that number picked at random...Any hints would be helpful
    Code:
    number=1+ rand() %13;
    
    	for(int i=number; i>0; i--)
    	{
    		number= 1+ rand() % 4;
    		for(int j=number; j>0; j--)
    		{
    			cout << "Card: " << cardvalue[i-1] << cardsuit[j-1] << endl;
    		}
    	}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  4. Final compilation of texas holdem poker game
    By System_159 in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 12:12 PM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM