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 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)
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");
}
}

}
}
``````

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, learning, and sharing knowledge.