Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

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

  • 07-04-2009 02:00PM
    #1
    Closed Accounts Posts: 27,856 ✭✭✭✭


    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, Registered Users 2 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,856 ✭✭✭✭Dave!


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


  • Registered Users, Registered Users 2 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