Results 1 to 20 of 20

Thread: Tricky Logic Challenge

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36

    Tricky Logic Challenge

    I have been working on a problem that I thought I would give you guys a shot at. I have found it much more challenging than it appears.

    You have a normal 52 card playing deck, with no jokers.

    You have between 5 and 7 random cards that make up your hand.
    ex: AS, 5D, 3H, 9H, 9C, 6S, 2C

    The problem is to create a sub that determines, with the random cards, if you have either a straight or a chance to make a straight.

    Draws include:
    X=Card that helps the straight
    0=Card that dosent help the straight

    XXXXX - Straight
    0XXXX0 - Double Chance Straight
    X0XXX0X - Double Chance Straight
    X0XXX - One Chance Straight
    XX0XX - One Chance Straight
    XXX0X - One Chance Straight

    **Additional Challenge**
    Determine if the straight or straight draw is a straight flush draw. (All cards that make the straight are of the same suit.)

    I have been working on this for two days and am stumped.

    Thanks for the help.

  2. #2
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    a bunch of Select Cases?? Lol that's what i woudl do but i doubt it's very effeciant...

    Idk if this would work, but make an array with all the hands (not just winning hands, include how you had it like 2 chance stragith etc, just a 2x# of hands array, in one column would be the hand, and in the other would be the cards, then u'd have to match up what you actually have, and find that hand in the array...it'd be tricky seems like a lot of code, so that usually means there is an easier way...

    sorry i couldnt help more i dont really know what i'm doing either so dont listen to me
    It is like wiping your ass with silk, I love it!

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Two words:

    Regular Expressions

    (.Net has a brilliant regex engine, look in the help)
    I don't live here any more.

  4. #4
    Member
    Join Date
    Jun 2004
    Location
    The Netherlands
    Posts
    37
    First sort your hand, make your own function to include J, Q, K, A.

    After that it should be simple, start with the first card in your hand if it's value + 1 equals your next item then continue.

    If the next value differs 2 from the one you're checking add 1 to variable OneOrDoubleChanceOfStraight.

    If the next value differs 3 or more from the one you're checking now exit, no chance on a straight.

    When the loop or while ends, check OneOrDoubleChanceOfStraight, if it's 0 you have a straight, if it's 1 you have OneChanceOf Straight. If it's 2 you have TwoChanceOfStraight.

    If you have a straight, loop or while again through your hand to check if all types are equal.


    After rereading your post I see I didn't read it right the first time, but main thing stays, sort your hand first, then try to figure it out...
    Last edited by cphoenixc; Jul 11th, 2004 at 10:46 AM.

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    HI,

    It is not safe to comment on the programming code without knowing the precise object of the game. Is it a well known game or one you have invented?

    It looks as if it might be a simple form of Poker (the ability to have scoring hands from different numbers of cards). When you use the word "Straight" do you mean a sequential run of cards, e.g. 2, 3, 4, 5, 6?

    Do the cards in the Straight have to be of the same suit?

    Are Straights the only winning hands?

    Can you have 5 sequential cards making a valid straight out of 7 cards held?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    wossname:
    I looked into using regular expressions. I think I have a rough idea of how to use them, but the syntax seems pretty complex. If I can't find a 'easier' solution I will go back and try to learn Regex.

    Taxes
    I appologize, let me clairfy a bit. The game is a 7 card stud game where you get a total of 7 cards, you may only use 5 of them to make your hand. A straight is a group of 5 sequential cards that may have different suits. I would also like to check for a "straight flush" which contains 5 sequental cards, but each of the cards that makes the straight must be of the same suit.
    For the purpose of this I am only lookng at the straight as a winning hand. Determining if I actually have completed a straight is fairly easy, its trying to figure out if I have a chance to make a straight that makes it a bit more difficult. (ie. the One Chance Straight and Double Chance Straight)

    cphoenixc
    What I have done so far is to convert the face cards (J, Q, K, A) into the numbers (11, 12, 13, 14) to make it easier.
    Your suggestion does help, but there are a few problems with it. For example what happens if the hand is (2c, 9d, 10d, Jc, Qs, Kh) Your code will exit out after the 2 didnt find a card within 2 spaces.

    Some other problems I have run into include:
    If you have a pair the normal checking for + 1 gets messed up. I tried removing all pairs but then that creates a problem when you check for the flush as well. As removing one of the pair cards could prevent you from making the straight flush.

    This problem sure seem trickier than it should be, thanks for the help!

    -Toucan

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    So, one final confirmation please. You have 7 cards in hand from which you want to check if any five (or more) constitute a Straight or could constitute a Straight if one or two matiching cards turn up. Is that right?
    Last edited by taxes; Jul 11th, 2004 at 04:23 PM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    Yes, you have 7 cards in your hand and you want to check and see if any 5 of them make a straight. A straight is "made" once any 5 of them are in consecutive order(3, 4, 5, 6, 7). Beyond that (say if there are 6 consecutive cards) the straight was made when the 5th card was checked and was consecutive, therefore the 6th consecutive card dosent really matter as the straight has already been made.

    -Toucan

    ::Looking at what I posted after "(3, 4, 5, 6, 7)" reads really confusing. Ignore it if it confuses you guys as much as it just did me.
    Last edited by SoonerToucan; Jul 11th, 2004 at 04:30 PM.

  9. #9
    Member
    Join Date
    Jun 2004
    Location
    The Netherlands
    Posts
    37
    Another try, not 100% worked out, but might give you ideas.

    First make two arrays, one with cards you have left, one with cards you choose.

    A
    1 - take first card from arrayLeft put in arrayChoose
    2 - take first card from arrayLeft, compare if it follows the one in array Choose

    3 - it follows, great, put it in arrayChoose, repeat process (2)

    4 - it doesn't follow, put it back in arrayLeft, move point in arrayleft 1 step so the second item is now number one.

    5 - repeat proces (2)

    6 - if you run out of cards, move point in arraychoose to the right and start again (1), till your last card is the one you started with.

    B
    if this didn't bring you a straight follow with this;
    (arraychoose is empty again, arrayleft is back to old state)
    1 - take first card from arrayLeft, put in arrayChoose
    2 - take first card from arrayleft, put it in arraychoose (don't care if it follows or not)

    3 - take first card from arrayleft compare if diff with card1 is 2, if so, put it in array choose, do 2,3,4,5 from A.
    4 - if diff is not 2 then put it back in arrayleft, move point in arrayleft 1 step

    5 - repeat process (3)

    K, this is getting complicated now, but hopefully it helps you along, it's called backtracking and it's a simple calculate till you drop with multiple while loops. In this case, your cards don't need to be ordered.

    Small (hopefully correct) example;

    2 4 5 6 13 10 8

    A

    2
    2 (picks 4 5 6 13 10 8, but no match)

    4
    4 5
    4 5 6
    4 5 6 (picks 2 13 10 8, but no match)

    5
    5 6 (picks 2 4 13 10 8, but no match)

    If you follow this, starting with 6, 12, 10, 8 gives no straight.

    B

    2
    2 4
    2 4 (picks 5 6 13 10 8, no luck, take next one in arrayleft)

    4
    4 2
    4 2 5
    4 2 5 6 (picks 13 10 8, no luck)

    Start again with A

    I know this doesn't do (yet) what you want, but it's a try out which might show you the correct way.

    If you don't exit the loop(s) when you find one of the solutions you want it will find all solutions, also taking care of the problem of pairs etc. Example:

    1c 2h 3h 4h 5h 1h 00 is a different solution from;l
    1h 2h 3h 4h 5h 1c 00 and both will be found.

  10. #10
    Member
    Join Date
    Jun 2004
    Location
    The Netherlands
    Posts
    37
    Originally posted by taxes
    Hi,

    So, one final confirmation please. You have 7 cards in hand from which you want to check if any five (or more) constitute a Straight or could constitute a Straight if one or two matiching cards turn up. Is that right?
    SoonerToucan you say 'Yes' to this, but if I understand correctly it should be;

    'You have 7 cards in hand from which you want to check if any five (or more) constitute a Straight or could constitute a Straight if ONE matiching cards turn up.'

    If not then X00XXX etc. are also options...

  11. #11

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    cphoenixc, your last post is correct. I am only searching for a completed (5 card straight) or an uncompleted (5 card straight) that only needs one card to complete.

    I havent had a chance to read your suggestions yet, I wanted to clarify your question asap. I will post again after I look it over and try some stuff out.

    Thanks,
    -Toucan

  12. #12

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    Ack! am I losing my mind or did taxes post a solution? I was working with it and then had to close the browser. When I came back it was gone.

    Hmm....

    Someone tell me I am not crazy.

    -Toucan

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by SoonerToucan
    Ack! am I losing my mind or did taxes post a solution? I was working with it and then had to close the browser. When I came back it was gone.

    Hmm....

    Someone tell me I am not crazy.

    -Toucan
    Hi,

    Sorry,

    It was 3.00 am here when I found a combination that it did not handle correctly. I could not sort out the error and decided to take a simpler approach which I will post shortly.

    If you got the other one to work totally then let me know, it was not far off.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  14. #14

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    Cool, I was afriad you yanked it for good. I was in the middle of playing with it when you pulled it, so I stopped working with it. I am looking forward to seeing your solution. Would you agree this problem is a bit trickier than it first appears?

    Thanks again,
    -Toucan

  15. #15
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by SoonerToucan
    Cool, I was afriad you yanked it for good. I was in the middle of playing with it when you pulled it, so I stopped working with it. I am looking forward to seeing your solution. Would you agree this problem is a bit trickier than it first appears?

    Thanks again,
    -Toucan
    Hi,

    Once you understand the object of the game it is not difficult logically but because of the large number of theoretical possibilities the programme tends to get out of hand, which is what was likely to happen with my withdrawn approach. - I discovered that with the approach I was using (which I started when I thought you could draw 2 new cards) , having tested the cards in ascending order, you would then have to test them in descending order to cover all possibilities

    The approach I am now using avoids that and it should be available within an hour.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  16. #16
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    I have now decided to use a different appraoch to reduce the complexity.

    First I examine for the possibility of four or five in a sequence.

    Then I examine for the possibility of five with one gap in a sequence.

    I have assumed that Aces can be treated as high or low.

    VB Code:
    1. Dim iCheck, iStartCheck As Integer    'To check for successive unbridgable gaps in sequence
    2.         Dim iCount, iCount1, iMissing As Integer
    3.         Dim iExamined, iDiscards As Integer
    4.         Dim iAdjustCardCount As Integer = 0
    5.         Dim iZeroCount, iStraightCount, iFullStraight As Integer
    6.         Dim arriHand(7) As Integer   ' To hold cards as dealt, sorted
    7.         Dim strMessage As String
    8.         arriHand(1) = 2
    9.         arriHand(2) = 3
    10.         arriHand(3) = 4
    11.         arriHand(4) = 5
    12.         arriHand(5) = 12
    13.         arriHand(6) = 13
    14.         arriHand(7) = 14
    15.         Dim arriHand1(15) As Integer    'Number of array element to indicate value of card dealt
    16.         For iCount = 0 To 7
    17.             arriHand1(arriHand(iCount)) = arriHand1(arriHand(iCount)) + 1
    18.         Next
    19.         If arriHand1(14) > 0 Then
    20.             iAdjustCardCount = 1
    21.             arriHand1(1) = arriHand1(14)  'If there is an Ace, place it in first position as well
    22.         End If
    23.         iMissing = 0
    24.         iCheck = 0
    25.         iStartCheck = 1
    26.         Do
    27.  
    28.             'Check for existing straight 5 or straight 4
    29.             For iCount = iStartCheck To 14
    30.                 If arriHand1(iCount) = 0 Then
    31.                     iCheck = 0
    32.                 Else
    33.                     iExamined = iExamined + arriHand1(iCount)
    34.                     iCheck = iCheck + 1
    35.                 End If
    36.  
    37.                 Select Case iCheck
    38.                     Case Is > 4
    39.                         strMessage = "Straight Five Held"
    40.                         Exit Do
    41.                     Case Is > 3
    42.                         strMessage = "Straight 5 Possible with 1 card"
    43.                         If 7 + iAdjustCardCount - iExamined < 1 Then
    44.                             Exit Do
    45.                         End If
    46.                 End Select
    47.  
    48.             Next
    49.  
    50.  
    51.             'Check for 4 in sequence with 1 gap
    52.             iExamined = 0
    53.             For iCount = iStartCheck To 14
    54.                 If arriHand1(iCount) > 0 Then
    55.                     iExamined = iExamined + 1
    56.                 Else
    57.                     If iExamined > 0 Then
    58.                         iMissing = iMissing + 1
    59.                         If iMissing > 1 Then
    60.                             iStartCheck = iStartCheck + 1
    61.                             iCount = iStartCheck - 1
    62.                             iExamined = 0
    63.                             iMissing = 0
    64.                             iDiscards = 0
    65.                             For iCount1 = 1 To iCount
    66.                                 If arriHand1(iCount1) > 0 Then iDiscards = iDiscards + 1
    67.                             Next
    68.                             If iDiscards - iAdjustCardCount > 3 Then
    69.                                 strMessage = "Straight 5 Not Possible with 1 Card"
    70.                                 Exit Do
    71.                             End If
    72.  
    73.                         End If
    74.                     End If
    75.                 End If
    76.                 If iExamined = 4 Then
    77.                     strMessage = "Straight 5 Possible with I card"
    78.                     Exit Do
    79.                 End If
    80.             Next
    81.             strMessage = "Straight 5 Not Possible with 1 card"
    82.             Exit Do
    83.         Loop
    84.         MessageBox.Show(strMessage)
    Last edited by taxes; Jul 12th, 2004 at 07:55 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  17. #17
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    HI,


    Phew!! Got it.

    I have edited my previous post which has worked on all the variations I can think of.

    I should have stuck to the the programmers Rule 1 ..... Check one thing at a time and not tried to check the whole hand at once

    The final programme took me 1 hour. Now I can have my breakfast (and lunch at the same time).
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  18. #18
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    HI,

    Just in case anyone looked at it, I have just amended my posted code to allow for one sequence I forgot to check.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  19. #19

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    Very nice taxes I am impressed. My last attempt at solving this used the same method, but my was much more verbose and much uglier.... and didnt work. Its nice to know i was on the right track.

    Once again thanks for your time, you have been a great help.

    Cheers,
    -Toucan

  20. #20
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by SoonerToucan
    Very nice taxes I am impressed. My last attempt at solving this used the same method, but my was much more verbose and much uglier.... and didnt work. Its nice to know i was on the right track.

    Once again thanks for your time, you have been a great help.

    Cheers,
    -Toucan
    You're welcome It was interesting.

    The fun really starts when you want to know which card(s) you want and which to discard
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width