Community Server

The platform that enables you to build rich, interactive communities
Welcome to Community Server Sign in | Join | Help
in Search

Code to detect a poker straight

Last post 07-24-2006, 8:39 AM by Random Walker. 3 replies.
Sort Posts: Previous Next
  •  07-17-2006, 12:45 PM 20

    Code to detect a poker straight

     

     I'm looking for coding  help. Trying to get the probability of a straight.

     

    How can resampling code detect the presence of 5 consecutive integers , in any order, in a sample of 7 taken from a population?  The population has the integers from 1 to 13 repeated four times for a total of 52.  This the essence of detecting a poker hand that has a 5-card straight, given 7 non-unique card values to chose from. So for example you could have a vector (12,9,9,2,11,13,10) and the straight would be there (9,10,11,12,13) or given a vector (10,5,7,9,8,6,6),  2 straights are present. My coding attempts at this are pretty crude.

     

    Anybody got a reasonable way to code for that? 

    Thanks

     

  •  07-17-2006, 5:42 PM 21 in reply to 20

    Re: Code to detect a poker straight

    Here's a way. I used it in a program to find the probability of a straight in a 5-card hand. I show the computation for a 7-card hand using one of the example hands you mentioned in your post.

    'Solution uses the fact that if an ascending sequence of consecutive
    'numbers is shifted one place and subtracted from the original, then
    'the middle numbers will all be one. For example:
    '    4  5 6 7 8 -   original sequence
    '   (0) 4 5 6 7 8   shifted sequence
    '    4  1 1 1 1 0   difference

    COPY (10 5 7 9 8 6 6)  hand
    SORT hand sortedHand
    COPY 0 sortedHand shiftedSortedHand
    SUBTRACT sortedHand shiftedSortedHand diffVector
    TAKE diffVector 2,7 diffVectorCenter
    COUNT diffVectorCenter =1 sequenceCount
    IF sequenceCount >= 4
       SCORE 1 straightFound
    END
    PRINT sortedHand shiftedSortedHand diffVector diffVectorCenter sequenceCount

  •  07-23-2006, 7:18 PM 23 in reply to 21

    Re: Code to detect a poker straight

    I just saw your reply today. Very nice code. I will try to use it. My try at the code works but it is clumsy.  Alos, I  realized later that I must also allow the number 13 (an Ace, in effect) to be either  13 or 1  in finding straights.  So it is more tricky still.  BTW I'm doing this for a poker article. I am not a student and this isn't a school project or anything like that.  

    Thanks for the help.

  •  07-24-2006, 8:39 AM 24 in reply to 23

    Re: Code to detect a poker straight

    Here's my whole program that computes the probability of a straight for a five-card hand. You can modify it or use the techniques for your seven-card hand problem.

    The check for the straight where the Ace has the value 13 is performed by comparing the sortedHand to the literal vector (1 9 10 11 12). Since the hand is sorted, that's the only configuration that must be tested for. In the seven card sorted hand, you would have to check for (1 X X 9 10 11 12), where the Xs represent any other card. One way to handle that is

       SUBTRACT sortedSevenCardHand (1 0 0 9 10 11 12) aceWild

    then count the number of zeros in aceWild as my program does. Since no card has the value zero, those two positions will always result in non-zero after the subtraction and will therefore not be counted.


    'Compute probability of a straight:
    'A Straight consists of 5 cards with consecutive values with the
    'addition that Ace can be high (13) or low (1) as needed
    'to complete the sequence.
    'Solution uses the fact that if an ascending sequence of consecutive
    'numbers is shifted one place and subtracted from the original, then
    'the middle numbers will all be one. For example:
    '    4  5 6 7 8 -   original sequence
    '   (0) 4 5 6 7 8   shifted sequence
    '    4  1 1 1 1 0   difference
    '
    COPY 100000 repeatCount
    COPY 1,13 1,13 1,13 1,13 deck
    REPEAT repeatCount
       SHUFFLE deck shuffledDeck
       TAKE shuffledDeck 1,5 hand
       SORT hand sortedHand
       COPY 0 sortedHand shiftedSortedHand
       SUBTRACT sortedHand shiftedSortedHand diffVector
       TAKE diffVector 2,5 diffVectorCenter
       COUNT diffVectorCenter = 1 diffVectorSum
       IF diffVectorSum = 4   '<--<< for 7-card hand this should test for >=4.
          SCORE 1 straightCountScore
       END
       '  Handle special case where ace = 13:
       SUBTRACT sortedHand (1 9 10 11 12) aceWild
       COUNT aceWild  =0 aceWildSum
       IF aceWildSum = 5 'we have a match
          SCORE 1 straightCountScore
       END
    END
    SUM straightCountScore straightCount
    DIVIDE straightCount repeatCount probability
    PRINT probability

     

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems