Determine what kind of hand of poker, the five cards
are. For instance, if I have the following:

Ace of Hearts
Two of Diamonds
Two of Clubs
10 of Spades
10 of Diamonds

You would print out:

Two Pair

In general, a poker hand can be the following:

1 Pair
2 Pair
3 of a kind
4 of a kind
full house (3 of a kind and 1 pair)
straight
flush
straight flush
royal flush

import java.util.Random;

public class cards {
    public static void main(String[] args) {

    int [] deck = new int[52];  // deck of cards

    // initialize to "fresh" deck
    for (int i = 0; i < 52; i++)
        deck[i] = i;

    // create the random number generator
    Random randNumGen = new Random();

    // shuffle the deck by swapping random two cards 10000 times
    for (int i = 0; i < 10000; i++) {

        // generate two numbers in range [0, 51]
        int randPos1 = randNumGen.nextInt(52);
        int randPos2 = randNumGen.nextInt(52);

        // now swap cards at positions randPos1 and randPos2
        int temp = deck[randPos1];   // note three statements
        deck[randPos1] = deck[randPos2];
        deck[randPos2] = temp;
    }
int faceValue = 0;
int suit = 0;
String message; 

    // now we need to print first five cards, we'll use a String
    for (int i = 0; i < 5; i++) {
         faceValue = deck[i] % 13 + 1;  // in range [1,13]
         suit = deck[i] / 13;   // in range [0, 3]

        //String message;   // First look at String object

        // in class we will talk more about OBJECTS and METHODS.
        // We've already seen two examples of objects --- StdIn
        // and Random.  String is another example of an object.

        if (faceValue == 1)
        message = "Ace of ";
        else if (faceValue == 11)
        message = "Jack of ";
        else if (faceValue == 12)
        message = "Queen of ";
        else if (faceValue == 13)
        message = "King of ";
        else
        message = faceValue + " of ";

        if (suit == 0)
        message += "Spades";
        else if (suit == 1)
        message += "Clubs";
        else if (suit == 2)
        message += "Hearts";
        else
        message += "Diamonds";

        System.out.println(message);

}
        for(int i = 1; i<=13; i++){

        if(faceValue == Math.pow(4,i)){
        System.out.println("4 of a kind");
    }
         else if(faceValue == Math.pow(3,i)){
        System.out.println("3 of a kind");
    }
        else if (faceValue == Math.pow(2,i)){
        System.out.println("a pair");
    }
    }

    }
}

Dani AI

Generated

Quick summary: stop trying to detect pairs by comparing faceValue to Math.pow — that approach will never work. Count ranks and suits, then decide the hand by frequency and simple sequence checks. As suggested, a 4x13 grid is one way to record cards; a simpler, easier-to-check approach is two arrays: one to count each rank and one to count each suit.

Algorithm (high level)

  • Build rankCount[1..13] and suitCount[0..3] for the five cards.
  • Flush if any suitCount[s] == 5.
  • Count how many ranks have frequency 4, 3, or 2 (call these quads, trips, pairs).
  • Straight if there are 5 distinct ranks and either maxRank - minRank == 4 or the special Ace-high set {1,10,11,12,13} is present.
  • Decide in this order: straight+flush (royal if 10..A), four of a kind, full house, flush, straight, three of a kind, two pair, one pair, else high card.

Example classifier (Java-style, assumes ranks in 1..13 and suits in 0..3):

static String classify(int[] ranks, int[] suits) {
    int[] rankCount = new int[14]; int[] suitCount = new int[4];
    for (int i=0;i<5;i++){ rankCount[ranks[i]]++; suitCount[suits[i]]++; }
    boolean flush = false; for (int s=0;s<4;s++) if (suitCount[s]==5) flush=true;
    int pairs=0,trips=0,quads=0,distinct=0,min=14,max=0;
    for (int r=1;r<=13;r++) if (rankCount[r]>0){ distinct++; min=Math.min(min,r); max=Math.max(max,r);
        if(rankCount[r]==2) pairs++; else if(rankCount[r]==3) trips++; else if(rankCount[r]==4) quads++;
    }
    boolean straight = (distinct==5 && (max-min==4))
        || (rankCount[1]==1 && rankCount[10]==1 && rankCount[11]==1 && rankCount[12]==1 && rankCount[13]==1);
    if (straight && flush) return (rankCount[1]==1 && rankCount[10]==1) ? "Royal flush" : "Straight flush";
    if (quads==1) return "Four of a kind";
    if (trips==1 && pairs==1) return "Full house";
    if (flush) return "Flush";
    if (straight) return "Straight";
    if (trips==1) return "Three of a kind";
    if (pairs==2) return "Two Pair";
    if (pairs==1) return "One Pair";
    return "High card";
}

Notes and pitfalls

  • Check full house before three-of-a-kind and two pair before one pair.
  • Handle Ace as both low (1) and high (10..A) for straights.
  • Validate you never deal duplicate cards (your shuffle approach already prevents that).
    This approach is compact, easy to test with sample hands, and extends naturally if you later want tie-breaking/kicker logic.

Hi there,

I was looking over your code, and I would use the following method. I would use an array of 4x13 ( for the four suits). When the card is dealt, it will have a certain number and a certain suit. Change the corsponding element in the array ( making sure that you are not dealing two of the same card). After that, you can use a template to go check them. For example:

To check two of a kind:

See if an two cards are in the same column

To check flush

All five cards on the same row

To check a straight

Take the lowest numbered card, and see if there is one proceeding it in any of the columns.

HTH,
Tino

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.