Finding a 'straight' in poker.

This is a discussion on Finding a 'straight' in poker. within the C Programming forums, part of the General Programming Boards category; You are welcome to report any futher bugs...

  1. #16
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    You are welcome to report any futher bugs

  2. #17
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,457
    Quote Originally Posted by esbo View Post
    You are welcome to report any futher bugs
    Why to bother if you do not bother to fix the already found?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #18
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Final version:-
    Code:
    #include <stdio.h>
    int c[7];
    int a,b,x,d,e;
    main(argc,argv)	int  argc;	char *argv[]; {
    	c[0]=4;c[1]=12;c[2]=3;c[3]=2;c[4]=1;c[5]=5;c[6]=9;
    	for (a=0; a<7; a++){
    		if (c[a]>10) continue;
    		for (b=0; b<7; b++){//first card in straight
    			 if (c[b]==c[a]+1) {//second  
    				 for (x=0; x<7; x++){
    				   if (c[x]==(c[b])+1) {//third
    						for (d=0; d<7; d++){
    							if (c[d]==c[x]+1) {//fourth
    								for (e=0; e<7; e++){
    									if ((c[e]==c[d]+1) || (c[d]==12 && c[d]+1==1)) {//fifth
    										printf("\n Straight up"); //fifth includes 9,10,11,12,1
    									}
    								}	
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    	for (a=0; a<7; a++){
    		if ((c[a]<5 ) && (c[a]!=1)) continue;
    		if (c[a]==1) c[a]=13;
    		for (b=0; b<7; b++){//first
    			 if (c[b]==c[a]-1) {//second  
    				 for (x=0; x<7; x++){
    				   if (c[x]==c[b]-1) {//third
    						for (d=0; d<7; d++){
    							if (c[d]==c[x]-1) {//fourth
    								for (e=0; e<7; e++){
    									if (c[e]==c[d]-1)  {//fifth
    										printf("\n Straight down");
    									}
    								}	
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    }/*main */

  4. #19
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by vart View Post
    Why to bother if you do not bother to fix the already found?
    Well I have fixed 90% of them

  5. #20
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by vart View Post
    Have you ever played cards? Because if yes - it seems to me you have something different from the regular human mind

    Yes I play poker a lot. I used to make a good profit but it gets harder as the players get better. You have to be 5% better than the opposition to make a profit, which is not easy
    when a large part of the game is luck.
    Also you might actually be playing against a computer program.
    Futhermore players at the table may be cheating or 'insiders' maybe cheating.

    http://www.associatedcontent.com/art..._cheating.html

    http://www.youtube.com/watch?v=FczbS7FiWSM

    And he is just the guy who got caught, how many other sites are cheating??

    At the moment I can just about break even depending upon how hard I try.

    If you are playing against other good players you might as well forget it, you will never
    be more than 5% better than them, the poker site is the only winner then as it takes
    5% of every pot.

    I used to make $200 a month regular as clockwork - not so easy now!!!

    Most of the bad player have probaly realised they are wasting their money and quit!!

  6. #21
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,457
    c[d]==12 && c[d]+1==1
    you still do not understand what you are doing. if c[d] is 12 then c[d]+1 is 13 and cannot be 1
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #22
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by vart View Post
    c[d]==12 && c[d]+1==1
    you still do not understand what you are doing. if c[d] is 12 then c[d]+1 is 13 and cannot be 1
    OK, thank you, point taken, I will sort it out later.

    Should be
    c[d]==12 && c[e]=1
    or something like that I guess.
    It did seem to work though!!

    You can never do enough testing

    Thanks!!

  8. #23
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by vart View Post
    Have you ever played cards? Because if yes - it seems to me you have something different from the regular human mind
    Actually I think when the human mind looks for a straight what if does first is to look
    for a card between 5 an 10.
    If there is no such card there is no straight!!!

    Which is really clever don't you think!!

    I mean I can see almost instantly if there is a straight but I know my mind does not
    actually sort the cards first.
    It is amazing what your mind can do without you realising it!!

    Then it works outwards from those cards I think.

    I would also say the way the mind does it is in the most effiecient way possible, although
    I cannot prove that (yet!).

    It probabaly has a few more tricks up it's sleeve that I am not aware of yet!!!

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868

    Wink

    Quote Originally Posted by esbo View Post
    Actually I think when the human mind looks for a straight what if does first is to look
    for a card between 5 an 10.
    If there is no such card there is no straight!!!

    Which is really clever don't you think!!

    I mean I can see almost instantly if there is a straight but I know my mind does not
    actually sort the cards first.
    It is amazing what your mind can do without you realising it!!

    Then it works outwards from those cards I think.

    I would also say the way the mind does it is in the most effiecient way possible, although
    I cannot prove that (yet!).

    It probabaly has a few more tricks up it's sleeve that I am not aware of yet!!!
    To find those mind tricks, you have to add more double, triple, and quadruple exclamation points, to your posts. That's the secret.

  10. #25
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by esbo View Post
    Probably would be better to sort if first but then I also have to consider the suit of each card
    held in a seperate array to test for straight flushs, that would also have to sorted in the same
    fashion. Hence it might be easier overall if I fo it this way.
    Rather than using two arrays for the number & suit, another option is you could define the deck as 0-51, then to find the value of one of the cards do:
    Code:
    value = (card % 4) + 1;
    and to find the suit do:
    Code:
    suit = card / 13;
    Either that or create a struct that contains the card value & suit...

  11. #26
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,406
    Man, that is some of the most convoluted 'feo' code i ever saw, those indents boggle the mind, good job is only short,
    i think you have been overworking this code + problem, you will get a cleaner and more expandable result if you have a good think about the actual algorithm, try just using 2loops to compare the contents of the array with the remaining numbers one at a time, or just use qsort function

  12. #27
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by rogster001 View Post
    Man, that is some of the most convoluted 'feo' code i ever saw, those indents boggle the mind, good job is only short,
    i think you have been overworking this code + problem, you will get a cleaner and more expandable result if you have a good think about the actual algorithm, try just using 2loops to compare the contents of the array with the remaining numbers one at a time, or just use qsort function

    You are welcome to post you own better version........ (holds breath)

  13. #28
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    You are welcome to post you own better version........ (holds breath)
    How about something like this?


    Code:
    #include <stdio.h>
    
    #define NUMBER_OF_FACES 13
    #define CARDS_IN_HAND 7
    
    int Rank[NUMBER_OF_FACES];
    
    void CheckForStraight(void)
    {
    	int iNumberConsecutive = 0;
    	int iRank  = 0;
    	while (Rank[iRank] == 0) iRank++;
    	for (; iRank < NUMBER_OF_FACES && Rank[iRank]; iRank++)
    		iNumberConsecutive++;
    	if (iNumberConsecutive == CARDS_IN_HAND)
    		printf("Straight up!\n");
    	else printf("NO straight!\n");
    }
    
    int main(void)
    {
    // Where iIndex is rank value,  
    // Zero is an ace, 1 is a deuce, 2 is trey... 12 is a king
    	for( int iIndex = 0; iIndex < 7; iIndex++)
    		Rank[iIndex]++;
    	CheckForStraight();
    	return 0;
    }

  14. #29
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by BobS0327 View Post
    How about something like this?


    Code:
    #include <stdio.h>
    
    #define NUMBER_OF_FACES 13
    #define CARDS_IN_HAND 7
    
    int Rank[NUMBER_OF_FACES];
    
    void CheckForStraight(void)
    {
    	int iNumberConsecutive = 0;
    	int iRank  = 0;
    	while (Rank[iRank] == 0) iRank++;
    	for (; iRank < NUMBER_OF_FACES && Rank[iRank]; iRank++)
    		iNumberConsecutive++;
    	if (iNumberConsecutive == CARDS_IN_HAND)
    		printf("Straight up!\n");
    	else printf("NO straight!\n");
    }
    
    int main(void)
    {
    // Where iIndex is rank value,  
    // Zero is an ace, 1 is a deuce, 2 is trey... 12 is a king
    	for( int iIndex = 0; iIndex < 7; iIndex++)
    		Rank[iIndex]++;
    	CheckForStraight();
    	return 0;
    }
    That does not seem to work at all.
    All you are doing is seting the first 7 cards to ace
    then counting the number you set and seeing if
    still 7, which it is unsurprisingly.

  15. #30
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How about this one? As a bonus you get to know where in the array the straight starts so you can look up the card value and - for example - compare the result against another hand containing a straight. The function returns the highest value if there are several straights.

    In addition your O(n!) or whatever complexity and gazillion nested loops are reduced nicely to O(n).

    Sorting the hand before is trivial.

    Code:
    #include <stdio.h>
    #include <assert.h>
    
    #define CARD_COUNT 7
    #define ACE 1
    #define MAX_CARD 13
    /*
    expects cards to be in ascending order
    returns the index from which the straight starts
    or -1 if no straight
    */
    int check_for_straight(const int* cards, int straight_size)
    {           
        int i = CARD_COUNT - 2;
        int consequtives = 1;
        
        /*sanity checks for debugging*/
        assert(straight_size > 1);
        assert(CARD_COUNT >= straight_size);
        
        for (; i >= 0; --i ) {
            if (cards[i] == cards[i+1] - 1) {
                ++consequtives;
            }
            else if (cards[i] != cards[i+1]) {
                /* i.e difference is more than 1 */
                consequtives = 1;
            }
            if ( 
                /* if upper straight(?) - 
                special case: ace is considered largest */
                (consequtives == straight_size - 1 
                && cards[0] == ACE 
                && cards[i] == MAX_CARD - straight_size + 2)
                /* or simple straight */
                || consequtives == straight_size) {
                return i;
            }
       }
       return -1; /*no success*/
    }
    
    int main(void)
    {
        int cards[CARD_COUNT] = {3, 3, 4, 5, 6, 7, 12};
        int straight = check_for_straight(cards, 5);
        if (straight == -1) {
            printf("No straight\n");
        }
        else {
            printf("Straight starting from card "
                "with value &#37;d and index %d\n", cards[straight], straight);
        }
        return 0;       
    }
    By the way, how comes you have 7 cards in poker?
    Last edited by anon; 01-08-2008 at 02:36 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making a Poker Game
    By krazyxazn in forum C Programming
    Replies: 4
    Last Post: 04-07-2009, 06:45 PM
  2. Help!For poker game simulation
    By tx1988 in forum C++ Programming
    Replies: 24
    Last Post: 05-25-2007, 09:59 PM
  3. Poker bad beats
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-15-2005, 10:42 PM
  4. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM

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