943,722 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4897
  • Java RSS
Oct 23rd, 2003
1

I don't know how to start to determine what hand of the poker, any ideas???

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

}
}
Similar Threads
Reputation Points: 11
Solved Threads: 0
Newbie Poster
mclam is offline Offline
2 posts
since Oct 2003
Nov 9th, 2003
0

Re: I don't know how to start to determine what hand of the poker, any ideas???

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
Reputation Points: 47
Solved Threads: 1
Newbie Poster
oberoc is offline Offline
20 posts
since Nov 2003

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: User clicked no on a java applet from IBM
Next Thread in Java Forum Timeline: Visibility of Variable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC