0

I am coding a poker hand evaluator, and I'm struggling with how unintuitive my naive solution is (obviously!). One of my methods is to evaluate whether the given hand:

int[] handValues = {5,7,8,8,11};

Is of hand-type 'one pair' - that is to say, whether there is one pair within the array.

Those of you who are familiar with poker will know that if two players have the same hand-type, the highest pair wins (and if those pairs are the same value, then it goes to the highest non-pair, and so on...). For this reason, I need to add a number to represent the pair and then the rest of the handValues to a separate array of topValues, with the non-pair values in descending order.

for example, with the example hand above, my topValues array would be

8, 11, 7, 5

Luckily the array is already sorted into ascending order, so the dream scenario is:

1 if(pair exists in handValues) {
2     topValues.add(pair number)
3     handValues.remove(pair number)
4.    topValues.add(handValues.reverse)
5. }

But I'm not sure how to implement that, or if it is even possible. Right now I'm having to work with a really long and silly looking method, shown below. It works, but I'd love something better! Any help would be appreciated.

private boolean isOnePair() {
    // 1st case: 1 & 2
    if (this.handValues[0] == this.handValues[1]) {
        this.topValues.add(this.handValues[0]);
        this.topValues.add(this.handValues[4]);
        this.topValues.add(this.handValues[3]);
        this.topValues.add(this.handValues[2]);
        return true;
    }

    // 2nd case: 2 & 3
    if (this.handValues[1] == this.handValues[2]) {
        this.topValues.add(this.handValues[1]);
        this.topValues.add(this.handValues[4]);
        this.topValues.add(this.handValues[3]);
        this.topValues.add(this.handValues[0]);
        return true;
    }

    // 3rd case: 3 & 4
    if (this.handValues[2] == this.handValues[3]) {
        this.topValues.add(this.handValues[2]);
        this.topValues.add(this.handValues[4]);
        this.topValues.add(this.handValues[1]);
        this.topValues.add(this.handValues[0]);
        return true;
    }

    // 4th case: 4 & 5
    if (this.handValues[3] == this.handValues[4]) {
        this.topValues.add(this.handValues[3]);
        this.topValues.add(this.handValues[2]);
        this.topValues.add(this.handValues[1]);
        this.topValues.add(this.handValues[0]);
        return true;
    }

    return false;
}
j panton
  • 232
  • 4
  • 16
  • You could simplify this by `using a for loop` to cut down on the if statements, and `sorting the array` to get the top values before you find the pair. You can sort the array to get the highest values first, and then do a for loop check to find the pair. This way, you will always find the higher pair – Easton Bornemeier Jul 10 '18 at 16:33
  • I'll give that a go – j panton Jul 10 '18 at 16:41
  • @EastonBornemeier would you mind demonstrating how you would go about that? I've been trying but only getting half-way there. – j panton Jul 10 '18 at 19:26
  • Sorting the cards in the hand by rank is good--I would have gone with descending order, but either is OK. But the most important thing that will simplify your code is to check for hands from HIGH to LOW, i.e., first check for straight flush, then quads, then full house, flush, straight, trips, two pair, one pair. Having failed all those earlier tests will let your later tests make a lot of simplifying assumptions. Also, each of these functions will need a different number of values in your top_value array. – Lee Daniel Crocker Jul 12 '18 at 21:36
  • Yes! I was sure to do that. Note for yourself that the better logic might be to check for triples before a full house. Obviously if a triple isn't obtained, then a full house isn't possible either. I found it a little difficult to check for doubles first too (so I could combine the results to check for a full house), but I'm sure it's possible. – j panton Jul 12 '18 at 21:47
  • Actually it's quicker to check for a full house than trips, because there's only two possibilities: AAABB and AABBB, while trips has 3: AAABC, ABBBC, and ABCCC. Of course you could also just toss the whole by-the-rules type evaluator and grab a library that does the fast DAG method like my own ojcardlib. – Lee Daniel Crocker Jul 12 '18 at 23:51
  • If you're using loops, how would you avoid checking every scenario of triple, even if you're just checking for a full house? But I do see what you're saying - I think its an interesting logical exercise. – j panton Jul 13 '18 at 00:24

2 Answers2

0

Iterate one time over your array if is sorted! It is more clean and easy. Don't use the last element just go last element - 1

    public static void main(String args[]) throws IOException {

        int[] handValues = {5,7,6,11,11};
        System.out.println(isOnePair(handValues));
}

    private static boolean isOnePair(int[] values) {
       for(int i=0; i<values.length-1; i++)
       {
           if(values[i] == values[i+1])
           {
               return true;
           }
       }
        return false;
    }

OutPut:

true
Gatusko
  • 2,503
  • 1
  • 17
  • 25
  • Hi, my problem is that I need to be filling another array at the same time - the `topValue` needs to be filled up according to the example I gave - with the pair first, and then the rest of the numbers in descending order. – j panton Jul 10 '18 at 16:39
0

If you want to remove the pair from the array everytime below logic can be used to do it in any array containing set of certain characters?integers. There are few parts to it:-

  1. Iterate on the given array and Store it in hasmap.
  2. With Condition that key is the integer from the array and value is the no of times its appearing in array.
  3. Now store only those values in a new hashmap which has value pair less than 2 i.e unique 4 Convert that hashmap to array.

so finally you can input the array and as output it will provide a new array with unique set of chars to work on

public static void main(String[] args) {

    int[] st = {1,2,3,4,5,6,7,7};   
    HashMap<Integer,Integer> hm = new HashMap<>();

    for (Integer c : st) {
        Integer count = hm.get(c);
        if(count == null)
          hm.put(c,1);
        else
          hm.put(c,hm.get(c)+1);
    }

    System.out.println(hm);          
    HashMap<Integer, Integer> hm1 = new HashMap<>();
    Set<Entry<Integer,Integer>> es = hm.entrySet();

    for(Entry<Integer,Integer> en : es) {
        if(en.getValue() < 2)
            hm1.put(en.getKey(), n.getValue());
    }

    System.out.println("---------"+hm1);
    Object[] ar = hm1.keySet().toArray();

    for(Object i : ar) {        
        System.out.println(i);      
    }

    System.out.println("Length-"+ar.length);
}
Herb
  • 636
  • 1
  • 15
  • 29