-1

So for my class, we're supposed to make a simplified version of poker. For example, an array {1,2,3,4,5} would be a straight, while an array {1,1,1,4,5} would be a three of a kind. I've done everything except for determine what kind of hand the user has and here's what I have so far:

public class handTypes {

public static void main(String[] args) {
    int[] testHand = {1,1,3,3,5}; //Just a test array
    int[] counts = new int [6];
    int counter = 0;

    for(int i=0; i < testHand.length ; i++){
        counts[testHand[i]]++;
    }

    // Determines a pair or 2 pairs
    for(int i =0; i <counts.length; i++){
        if(counts[i] == 2)
            counter++; }

    int option = counter;

    switch(option){
    case 1:
        System.out.println("You have one pair!");
        break;
    case 2:
        System.out.println("You have 2 pairs!");
        break;
    }

   }
}

Of course this doesn't work for for a flush, 4 of a kind, 5 of a kind, etc. But I read online a little algorithm that helps determine that. Lets say the created array "int[] counts" holds the values {4,0,1,0,0}, it would be 4 of a kind while the values {1,1,1,1,1} would be a straight. How would I somehow use this as a statement?

1 Answers1

0

A common general procedure for simple poker-hand evaluators is this:

  1. Sort the hand by rank (high to low, deuces low, aces high).
  2. Starting from the highest hand (five of a kind) and working downward, determine if the hand is of that type and return (e.g. if checking for five of a kind, just check that the first and fifth cards are the same rank--remember that they are sorted--if so, return "five of a kind" and you're done). Your simplified version doesn't seem to have flushes or straight flushes, so skip them.
  3. Work your way down through the hands: for four-of-a-kind, check for XXXXY and XYYYY. For full house, check for XXXYY and XXYYY. For trips, check XXXYZ, XYYYZ, and XYZZZ, etc.
  4. For straights, check five-in-a-row, then special case 2345A (this might not be a special case in your version).
  5. If every check fails, just return "no pair" with the cards sorted high-to-low.
Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55