-1

I have a poker program and in that program I have method that checks if there is a four of kind in an array.

public boolean isFourOfAKind(Card[] arr){}

I change arr[] to an int array by grabbing each of the cards values, 2-14 and store them in a separate array. How do I check(efficiently) if 4 integers are the same in that array?

*NOTE: I am passing in an array of 7 cards

My Two Pair Method (From Requests)

   /*
     * Returns 0 if no two pairs
     *  else returns the highest card out of the two pairs, 
     *  if two hands have the same highest pair then further inspection is needed
     */
    public static int isTwoPair(Card[] arr){
        int hold[] = new int[5];
        int values[] = new int[arr.length];
        int max = 0;
        boolean total = false;
        for(int i=0;i<arr.length;i++){
            values[i] = arr[i].getValue();
        }
        for(int i = 0;i<values.length;i++){
            for(int j = 0;j<values.length;j++){
                if(i==j){
                    continue;
                }
                if(values[i]==values[j]){
                    if(values[i]>max){
                        max = values[i];
                    }
                    int hold1 =0;
                    int hold2=0;

                    hold1 = j;
                    hold2=i;
                    int counter = 0;
                    for(int ii = 0;ii<values.length;ii++){
                        if(ii==j||ii==i){
                            continue;
                        }
                        hold[counter] = values[ii];
                        counter++;

                    }
                    //                     for(int iii: hold){
                    //                     System.out.print(" , "+ iii);
                    //                     }
                    //                     System.out.println();
                    if(isPair(hold)==0){
                        max = 0;
                        total = false;
                    }else{
                        int holdInt = isPair(hold);
                        if(holdInt>max){
                            max = holdInt;
                        }
                        total = true;
                    }
                }
            }
        }
        return max;

    }
Jonas
  • 121,568
  • 97
  • 310
  • 388
Sam Liokumovich
  • 91
  • 1
  • 2
  • 6

2 Answers2

2

I'd prefer to do it like this:

public boolean isFourOfAKind(Card[] arr){
    int[] counts = new int[15];
    for (Card c: arr) {
        counts[c.cardNum]++;
        if (counts[c.nameOfTheFieldWithCardNumber] == 4) return true;
    }
    return false;
}

also you can easily modify this method to check 2/3 of a kind/full house combinations

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
0

If you sort the array by rank (2,3,...,King,Ace) then the following checks all become a lot easier:

  • Straight flush (incrementing rank, combined with Flush check)
  • Four of a kind (4 of the same rank grouped together)
  • Full house (first 2 are equal and last 2 are equal and middle is equal to one of them)
  • Straight (incrementing rank)
  • Three of a kind (3 of the same rank grouped together)
  • Two pair (2 groups of same rank)
  • One pair (2 of the same rank grouped together)
  • High card (last card)

The only one that doesn't benefit, is Flush, and checking for that doesn't require any sorting, so that's not a problem.

Andreas
  • 154,647
  • 11
  • 152
  • 247