| | |
I don't know how to start to determine what hand of the poker, any ideas???
![]() |
•
•
Join Date: Oct 2003
Posts: 2
Reputation:
Solved Threads: 0
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");
}
}
}
}
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");
}
}
}
}
•
•
Join Date: Nov 2003
Posts: 20
Reputation:
Solved Threads: 1
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
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
![]() |
Similar Threads
- Projects for the Beginner (Python)
- HP Pavillion Restarts in Loop but will Start in Safe Mode (Troubleshooting Dead Machines)
- using inheritance to make a poker game (Java)
- Making a 5 hand poker game (C++)
- Can not start my computer (Windows NT / 2000 / XP)
- Windows XP Tips 'n' Tricks (Windows tips 'n' tweaks)
- about:blank Trusted Start Page problem (Viruses, Spyware and other Nasties)
Other Threads in the Java Forum
- Previous Thread: User clicked no on a java applet from IBM
- Next Thread: Visibility of Variable
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card character class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project projects radio recursion replaydirector reporting researchinmotion scanner se server service set sms software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows





