Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java: Arrays... Each combination/order of elements

Options
  • 07-04-2009 2:00pm
    #1
    Closed Accounts Posts: 27,857 ✭✭✭✭


    Hey folks,

    I'm working on a project at the moment for college. It's a poker game, and one of the things I'm doing is evaluating hands. As it's Texas Holdem, there'll be 2 private cards and 5 community cards, so I was thinking I should create an array of 7 cards, and then from those 7 create another array of 5, and fill it will all the different combinations available from the 7 cards. I can check each combination, and whichever combination has the best hand will be the one I keep.

    Any suggestions on how I could manage this? I'll try it myself shortly and post up what I've got, but if anyone wants to pitch in with thoughts to right ahead!

    Cheers


Comments

  • Registered Users Posts: 4,188 ✭✭✭pH


    OK what you want is combinations ie you want all combinations of 5 picked from 7, but you don't care about the order (if you did you'd be talking about permutations).

    8c1e1ee9f73e84339513977f6492992e.png
    (wikipedia)

    In your case n=7 and k=5, giving (in my head hope it's right!)
    5040/ 240 = 21 possible hands.

    Now to generate these 21 combinations you'll use a combinadic which is an algorithm that can generate the ith combination for the C(n,k) series.

    Here's how to generate it:

    To get the elements of the combinadic M(n,k)(i) from its index i, we first find the largest first element mk−1 so that C(mk−1, k) is less than or equal to i. The second element is found by repeating the procedure in the reduced combinadic system where i is reduced by the previous C(mk−1,k), n is set to the previous mk−1, and k is reduced by one. This is continued until k is reduced to zero. As McCaffrey points out in his MSDN article, efficiently finding the largest mk−1 is the key to calculating a k-combination from its index value (see his discussion of the helper function LargestV()).
    (wikipedia)

    And here's an explanation and presentation of the algorithm:

    http://msdn.microsoft.com/en-us/library/aa289166(VS.71).aspx


  • Closed Accounts Posts: 27,857 ✭✭✭✭Dave!


    Looks like just the ticket :) Cheers lad, you're a gent


  • Registered Users Posts: 4,188 ✭✭✭pH


    That said, if my calculation of 21 is right it would be quicker and faster just to hard code the 21 combinations!


Advertisement