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 have 7 cards you need to determine if there is a 5 card straight there, that is 5 cards ...

  1. #1
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794

    Finding a 'straight' in poker.

    You have 7 cards you need to determine if there is a 5 card straight there, that is 5 cards
    in a sequence eg 45678, of any suit. You cannot have QKA12 for example as the ace can
    only be at start or the end.

    What is the quickest way to do it?

    You can assume the cards are in an array cards[7] where 2
    1=Ace
    2=2
    3=3
    ...
    10=10
    11=Jack
    12=Queen
    13=King

    So you have

    int c[7]; // array of unsorted cards.


    How would you do it?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    sort it
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Sort the cards, then check for 5 consecutive numbers in a row.
    If the 2nd card != 1st card + 1, strike one.
    If the 3rd card != 2nd card + 1, strike two.
    If the 1st card is not an ace or the other 4 cards aren't consecutive, strike three.

  4. #4
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    OK I was thinking I could do something like this:-
    There are two types of straights, ascending ones which start at 10 or lower, the first 'bit',
    and descending ones which start at 5 or above.
    Not sure if it compiles or work yet, but probably not too far off.
    I will try that later.
    Can't be too far off and it does the fiddly testing without sorting it first.
    May be quicker than sorting first.
    I have just realised, there could be several straights eg 1,2,3,4,5 and 2,3,4,5,6 etc...

    Code:
    int c[7];
    int a,b,c,d,e;
    
    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 (c=0; c<7; c++){
    			   if (c[c]]==c[b]+1) {//third
    
    					for (d=0; d<7; d++){
    						if (c[d]]==c[c]+1) {//fourth
    
    							for (e=0; e<7; e++){
    								if ((c[e]]==c[d]+1) || (c[d]==12 && c[d]=1)) {//fifth
    									printf("\n Straight"); //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 (c=0; c<7; c++){
    			   if (c[c]==c[b]-1) {//third
    
    					for (d=0; d<7; d++){
    						if (c[d]==c[c]-1) {//fourth
    
    							for (e=0; e<7; e++){
    								if (c[e]==c[d]-1)  {//fifth
    									printf("\n Straight");
    								}
    							}	
    						}
    					}
    				}
    			}
    		}
    	}
    }

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    of course it will not compile
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    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.

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by vart View Post
    of course it will not compile
    If it compiles first time I know I have compiled the wrong program!!!

  8. #8
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by vart View Post
    of course it will not compile

    No but this does
    Might be the odd bug in it though

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    #include <io.h>
    #include <fcntl.h>
    #include <errno.h>
    int c[7];
    int a,b,x,d,e;
    
    
    main(argc,argv)
    	int  argc;
    	char *argv[]; 
    {
    
    
    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)) {//fifth
    									printf("\n Straight"); //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");
    								}
    							}	
    						}
    					}
    				}
    			}
    		}
    	}
    }
    
    
    }/*main */

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    only #include <stdio.h> is used

    main should be declared as

    int main(int argc, char* argv[])

    variables should be declared locally and initialized

    Code:
    (c[d]==12 && c[d]==1)
    Do you understand your own code?

    etc
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #10
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Sorry ,i am too stupid to be able understand your code.i dont know what these two FOR loop doing in your main.Is some sorting is done as asked before?
    For little i could understand was you should start checking the for loop with previous values.confused!! huh
    Code:
    for (a=0; a<7; a++)
    {
    	    if (c[a]>10) 
             continue;
    	for (b=a+1; b<7; b++)
    Long time no C. I need to learn the language again.
    Help a man when he is in trouble and he will remember you when he is in trouble again.
    You learn in life when you lose.
    Complex problems have simple, easy to understand wrong answers.
    "A ship in the harbour is safe, but that's not what ships are built
    for"

  11. #11
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by vart View Post
    only #include <stdio.h> is used

    main should be declared as

    int main(int argc, char* argv[])

    variables should be declared locally and initialized

    Code:
    (c[d]==12 && c[d]==1)
    Do you understand your own code?

    etc
    Well my working program seems to think so!!
    4 straights found, 2 going up and two going down.
    Which is correct with the data I put in the array.
    Might be a problem with some of the end straights containing aces.
    Not tested yet.
    You don't have my compiler.
    Code:
    (c[d]==12 && c[d]==1)
    Well spotted ty.

    Code:
    (c[d]==12 && c[d]+1==1)


    I think this is not far off!!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    #include <io.h>
    #include <fcntl.h>
    #include <errno.h>
    int c[7];
    int a,b,x,d,e;
    
    
    main(argc,argv)
    	int  argc;
    	char *argv[]; 
    {
    
    
    
    printf("\n Start");
    
    c[0]=3;
    c[1]=7;
    c[2]=3;
    c[3]=12;
    c[4]=5;
    c[5]=4;
    c[6]=6;
    
    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 */
    Output:-
    Code:
     Start
     Straight up
     Straight up
     Straight down
     Straight down

  12. #12
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    It finds this 'tricky one'
    Code:
    c[0]=3;
    c[1]=10;
    c[2]=3;
    c[3]=11;
    c[4]=1;
    c[5]=12;
    c[6]=9;
    
     Start
     Straight down

  13. #13
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    And this one:-
    It correctly misses 12,1,2,3,4. (well I hope so anyway!!)
    Code:
    c[0]=4;
    c[1]=12;
    c[2]=3;
    c[3]=2;
    c[4]=1;
    c[5]=4;
    c[6]=9;
    
     Start
     End
    I quite like doing it this way without a sort, it is more the way the human mind does it.
    I don't know it it is quicker than a sort first, sorts can be very fast.
    This way might be quicker for some arrangements of cards, but I prefer to keep
    then in the original order.

  14. #14
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    I also need to change the 'ace' back to a 1 if I change it to 13.

  15. #15
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    is more the way the human mind does it
    Have you ever played cards? Because if yes - it seems to me you have something different from the regular human mind
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Page 1 of 4 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