| | |
Need help writing a program to classify a poker hand
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2004
Posts: 2
Reputation:
Solved Threads: 0
I'm writing a program that generates 5 cards, and I need it to tell me what type of poker hand it is.
I really need help with this as soon as possible, so any suggestions that you could give me would be appreciated SO SO SO SO SO much. I saw that someone posted a very similar question, but that person got no responses.
This is my code, some of it is commented out because I wasn't sure about those parts.
public class Poker {
public static void main(String ars[]) throws IOException {
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
int i, temp, pos1, pos2;
int card, cardsuit, cardrank;
int deck[] = new int [52];
String suit[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
String rank[] = {"Ace", "Deuce", "Trey", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
int hand[] = new int [7];
int temptotals[] = new int [0];
int flush[] = new int [4];
boolean swap = true, flushtest = false;
int n = 0;
// int rank[] = new int [10];
for(i = 0; i < 52; i++){
deck[i] = i;
}
//shuffling deck
for(i = 1; i <= 2515; i++){
pos1 = (int) (Math.random() * 52);
pos2 = (int) (Math.random() * 52);
temp = deck[pos1];
deck[pos1] = deck[pos2];
deck[pos2] = temp;
}
//deal 5 cards
for(i = 0; i <= 4; i++) {
card = deck [i];
cardrank = card % 13;
cardsuit = card / 13;
System.out.println ("Card # " + (i + 1) + " is a " + rank [cardrank] + " of " + suit[cardsuit]);
}
//sort hand
while (swap) {
swap = false;
for (i = 0; i < 4; i++) {
if (hand[i] > hand [i + 1]) {
temp = hand[i];
hand[i] = hand[i + 1];
hand[i + 1] = temp;
swap = true;
}
}
}
//flush test
/* for (i = 0; i < 4; i++) {
flush[i] = 0;
}
for (i = 0; i < 7; i++) {
n = hand[i] / 13; //suit of each card in array
flush[n]++;
// for (i = 0; i < 4; i++) { //5 cards with same suit = flush
if (flush[i] > 4) {
flushtest = true;
System.out.print("FLUSH \n");
} else if (flush[i] <= 4) {
flushtest = false;
System.out.print("not \n");
}*/
}
}
I really need help with this as soon as possible, so any suggestions that you could give me would be appreciated SO SO SO SO SO much. I saw that someone posted a very similar question, but that person got no responses.
This is my code, some of it is commented out because I wasn't sure about those parts.
public class Poker {
public static void main(String ars[]) throws IOException {
BufferedReader stdin = new BufferedReader (new InputStreamReader (System.in));
int i, temp, pos1, pos2;
int card, cardsuit, cardrank;
int deck[] = new int [52];
String suit[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
String rank[] = {"Ace", "Deuce", "Trey", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
int hand[] = new int [7];
int temptotals[] = new int [0];
int flush[] = new int [4];
boolean swap = true, flushtest = false;
int n = 0;
// int rank[] = new int [10];
for(i = 0; i < 52; i++){
deck[i] = i;
}
//shuffling deck
for(i = 1; i <= 2515; i++){
pos1 = (int) (Math.random() * 52);
pos2 = (int) (Math.random() * 52);
temp = deck[pos1];
deck[pos1] = deck[pos2];
deck[pos2] = temp;
}
//deal 5 cards
for(i = 0; i <= 4; i++) {
card = deck [i];
cardrank = card % 13;
cardsuit = card / 13;
System.out.println ("Card # " + (i + 1) + " is a " + rank [cardrank] + " of " + suit[cardsuit]);
}
//sort hand
while (swap) {
swap = false;
for (i = 0; i < 4; i++) {
if (hand[i] > hand [i + 1]) {
temp = hand[i];
hand[i] = hand[i + 1];
hand[i + 1] = temp;
swap = true;
}
}
}
//flush test
/* for (i = 0; i < 4; i++) {
flush[i] = 0;
}
for (i = 0; i < 7; i++) {
n = hand[i] / 13; //suit of each card in array
flush[n]++;
// for (i = 0; i < 4; i++) { //5 cards with same suit = flush
if (flush[i] > 4) {
flushtest = true;
System.out.print("FLUSH \n");
} else if (flush[i] <= 4) {
flushtest = false;
System.out.print("not \n");
}*/
}
}
Well, you've already broken your hand up into suits and ranks, that's a good start. Now you just need to retain that info after you do print your output.
For a flush, by far the easiest solution is to take a histogram of the suits. If the entry for any one suit is 5, you have a flush.
I don't know java, but the c++ would be something like:
Furthermore, in addition to having the normally ordered rank histogram, I'd also have a 2nd one that is sorted in descending order so that you can easily find what the highest numbers of the same card number are in the hand. I'd do the same with the suits, because then testing for a flush is cake.
So, lets start at the bottom:
Two of a kind: index 0 of sorted ranks is 2, index 1 is 1, and index 0 of the sorted suits is any rank is 2, index 1 is 1
Two pair: indices 0 and 1 of the sorted ranks is 2
Three of a kind: index 0 of the sorted ranks is 3, index 1 is 1
Straight: using the unsorted rank histogram,
1) any run of 5, starting between ace and 9, is all 1 or
2) the run from 10 to K are all 1, and ace is 1
Flush: index 0 of the sorted suits is 5
Full House: index 0 of the sorted ranks is 3 and index 1 is 2
Four of a kind: index 0 of sorted ranks is 4
Straight Flush: Straight and Flush
Royal Flush: Straight and Flush, in ordered ranks 10 and K are both 1
An example:
After running your histogram, you have this:
Rank
Ace 0
2 0
3 0
4 2
5 0
6 0
7 0
8 0
9 0
10 3
J 0
Q 0
K 0
Then, your second, sorted version of the ranks would be like this:
3
2
0
0
0
0
0
0
0
0
0
0
0
The test for a full house is just:
Good luck!
For a flush, by far the easiest solution is to take a histogram of the suits. If the entry for any one suit is 5, you have a flush.
I don't know java, but the c++ would be something like:
Java Syntax (Toggle Plain Text)
// vRanks is your array of enum ESuit { eS_HEARTS, eS_DIAMONDS, eS_CLUBS, eS_SPADES, eS_LAST }; size_t vSuitCount[eS_LAST]; memset(vSuitCount, 0, sizeof vSuitCount); for (unsigned i = 0; i < 5; ++i) ++vSuitCount[vRanks[i]]; bool bFlush = false; for (unsigned i = 0; i < eS_LAST; ++i) { if (vSuitCount[i] == 5) { bFlush = true; break; } }
Furthermore, in addition to having the normally ordered rank histogram, I'd also have a 2nd one that is sorted in descending order so that you can easily find what the highest numbers of the same card number are in the hand. I'd do the same with the suits, because then testing for a flush is cake.
So, lets start at the bottom:
Two of a kind: index 0 of sorted ranks is 2, index 1 is 1, and index 0 of the sorted suits is any rank is 2, index 1 is 1
Two pair: indices 0 and 1 of the sorted ranks is 2
Three of a kind: index 0 of the sorted ranks is 3, index 1 is 1
Straight: using the unsorted rank histogram,
1) any run of 5, starting between ace and 9, is all 1 or
2) the run from 10 to K are all 1, and ace is 1
Flush: index 0 of the sorted suits is 5
Full House: index 0 of the sorted ranks is 3 and index 1 is 2
Four of a kind: index 0 of sorted ranks is 4
Straight Flush: Straight and Flush
Royal Flush: Straight and Flush, in ordered ranks 10 and K are both 1
An example:
After running your histogram, you have this:
Rank
Ace 0
2 0
3 0
4 2
5 0
6 0
7 0
8 0
9 0
10 3
J 0
Q 0
K 0
Then, your second, sorted version of the ranks would be like this:
3
2
0
0
0
0
0
0
0
0
0
0
0
The test for a full house is just:
Java Syntax (Toggle Plain Text)
bool bFullHouse = (vSortedRanks[0] == 3) && (vSortedRanks[1] == 2);
Good luck!
-don't listen to me-
![]() |
Similar Threads
- writing a program (Python)
- writing a program (Python)
- Ranking multiple poker hands / multiple decks (Geeks' Lounge)
- Hi i'm having trouble with writing a program (C)
- Writing a program that uses a Character Queue (C++)
Other Threads in the Java Forum
- Previous Thread: Comenzando con Java
- Next Thread: How do i verify a string using hashtables
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp draw eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor





