468,736 Members | 1,972 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,736 developers. It's quick & easy.

Problem with a Poker hand class

At the moment im trying to write a hand class for a game poker patientnce
But when i get to the part having to catergorise the difference of full house straight flush flush four of a kind and straight i got stuck.I need to write boolean methods to return these (stright flush , four of a kind..etc) I can only do a pair and 2 pairs and three of a kind. The following is my code please someone if possible help me thanks

Expand|Select|Wrap|Line Numbers
  1.  import java.util.*; 
  2.  
  3. public class Hand 
  4. public static final int CARDS_IN_HAND = 5; 
  5. public static final int CARDS_IN_SUIT = 13; 
  6. public static final int NUM_RANKS = 4; 
  7.  
  8. //Categories of hands 
  9. public static final int HIGH_CARD = 0; 
  10. public static final int PAIR = 1; 
  11. public static final int TWO_PAIRS = 2; 
  12. public static final int THREE_OF_A_KIND = 3; 
  13. public static final int STRAIGHT = 4; 
  14. public static final int FLUSH = 5; 
  15. public static final int FULL_HOUSE = 6; 
  16. public static final int FOUR_OF_A_KIND = 7; 
  17. public static final int STRAIGHT_FLUSH = 8; 
  18. private Card[] thecard; 
  19.  
  20. /** 
  21. * Constructor for objects of class Hand 
  22. */ 
  23.  
  24. public Hand(Card[] cards) 
  25. if(cards.length == CARDS_IN_HAND) 
  26. thecard = new Card[5]; 
  27. for(int i = 0; i<cards.length; i++) 
  28. thecard[i] = cards[i]; 
  29.  
  30.  
  31. public Hand(Deck deck) 
  32. thecard = new Card[5]; 
  33. for(int i = 0; i < CARDS_IN_HAND; i++){ 
  34. thecard[i] = deck.takeTop(); 
  35.  
  36. public Card getCard(int i) 
  37. return thecard[i]; 
  38.  
  39. public java.lang.String toString() 
  40. String handS = ""; 
  41. for(int i = 0; i<thecard.length; i++){ 
  42. handS += thecard[i]+" "; 
  43. return handS.trim(); 
  44.  
  45. public int getCategory() 
  46. int returnValue = HIGH_CARD; 
  47.  
  48. if(hasPair() == true){ 
  49. returnValue = PAIR; 
  50. if(hasTwoPairs() == true){ 
  51. returnValue = TWO_PAIRS; 
  52. if(hasThreeOfAKind() == true){ 
  53. returnValue = THREE_OF_A_KIND; 
  54. if(hasStraight() == true){ 
  55. returnValue = STRAIGHT; 
  56. if(hasFlush() = true){ 
  57. returnValue = FLUSH; 
  58. if(hasFUllHouse() = true){ 
  59. returnValue = FULL_HOUSE; 
  60. if(hasFourOfAKind() == true){ 
  61. returnValue = FOUR_OF_A_KIND; 
  62. if(hasStraightFlush() = true){ 
  63. returnValue = STRAIGHT_FLUSH; 
  64. return returnValue; 
  65.  
  66.  
  67. private boolean hasThreeOfAKind() 
  68. int count = 1; 
  69. for(int i = 0;i<(CARDS_IN_HAND-2);i++){ 
  70. for(int j = i+1;j<CARDS_IN_HAND;j++){ 
  71. if(thecard[i].getRank() == thecard[j].getRank()){ 
  72. count++; 
  73. if(count == 3) 
  74. break; 
  75. }else{ 
  76. count = 1; 
  77. return count == 3; 
  78.  
  79. private boolean hasTwoPairs() 
  80. int count = 1; 
  81. int pair = 0; 
  82. for(int i = 0;i<(CARDS_IN_HAND-1);i++){ 
  83. for(int j = i + 1;j<CARDS_IN_HAND;j++){ 
  84. if(thecard[i].getRank() == thecard[j].getRank()){ 
  85. count++; 
  86. if(count == 2){ 
  87. pair++; 
  88. count =1; 
  89. }else{ 
  90. count =1; 
  91.  
  92. return pair == 2; 
  93.  
  94. private boolean hasPair() 
  95. int count = 1; 
  96. int pos = 1; 
  97. for(int i = 0;i<(CARDS_IN_HAND-1);i++){ 
  98. for(int j = i+1;j<CARDS_IN_HAND;j++){ 
  99. if(thecard[i].getRank() == thecard[j].getRank()){ 
  100. count++; 
  101. if(count == 2) 
  102. break; 
  103. }else{ 
  104. count = 1; 
  105. return count == 2; 
  106.  
  107.  
  108. public static java.lang.String getCategoryName(int category) 
  109. String[] rank = {"High card","Pair","Two pairs","Three of a kind","Straight","Flush","Full house","Four of a kind","Straight flush"}; 
  110. return rank[category]; 
  111.  
  112. }
  113.  
Jun 2 '07 #1
15 5906
r035198x
13,262 8TB
1.) Look how better the code looks under code tags! Remember them next time.
2.) You don't have to write java.lang.String for return type. Just use String(remember the java.lang package is automatically imported for you)
3.) I don't know a thing about poker (they could be others like me here as well) so why don't you explain the rules of the methods that you want to implement. You don't have to explain the whole game. Just the stright flush e.t.c
Jun 2 '07 #2
JosAH
11,448 Expert 8TB
Think of a card as a rank [0, 12] and a suit [0, 3]. Create two int arrays, one for
the ranks and one for the suits. Given the hand (five cards) add the corresponding
entries in the rank and suit arrays. If you find five consecutive ones (1) in the
rank array you've got a flush; if on top of that one of the suit elements in the
suit array equals four, you've got a royal flush. If you find a four in the rank array
you've got a carre etc. etc.

kind regards,

Jos
Jun 2 '07 #3
r035198x
13,262 8TB
Think of a card as a rank [0, 12] and a suit [0, 3]. Create two int arrays, one for
the ranks and one for the suits. Given the hand (five cards) add the corresponding
entries in the rank and suit arrays. If you find five consecutive ones (1) in the
rank array you've got a flush; if on top of that one of the suit elements in the
suit array equals four, you've got a royal flush. If you find a four in the rank array
you've got a carre etc. etc.

kind regards,

Jos
Ah, looks like Jos has played a few hands.
Jun 2 '07 #4
1.) Look how better the code looks under code tags! Remember them next time.
2.) You don't have to write java.lang.String for return type. Just use String(remember the java.lang package is automatically imported for you)
3.) I don't know a thing about poker (they could be others like me here as well) so why don't you explain the rules of the methods that you want to implement. You don't have to explain the whole game. Just the stright flush e.t.c
Yip it does look better under the code tags i will remember use it next time.
About poker in my code there should be 5 cards in the hand and you want to check if there is flush straight etc in it.So a flush is where all 5 cards are the same suit eg all 5 cards are spades all 5 cards are diamonds. 4of a kind is where out of the 5 cards 4 cards are the same eg ace ace ace ace and a king tat is a four of a kind.Straight Flush is where those 5 cards are one after the other doesnt matter the order and with the same suit. Eg the 5 cards are spade ace spade 3 spade5 spade 2 spade 4 this is a straight flush. A full house is where there is a three of a kind with a pair eg the cards are ace ace ace 2 2. I m struggling with writing boolean methods for straight flush, flush,4 of a kind,straight in this class.
Jun 2 '07 #5
JosAH
11,448 Expert 8TB
I m struggling with writing boolean methods for straight flush, flush,4 of a kind,straight in this class.
Read my reply #3 for starters.

kind regards,

Jos
Jun 2 '07 #6
JosAH
11,448 Expert 8TB
Ah, looks like Jos has played a few hands.
I'm quite good at cheating and counting too ;-)

kind regards,

Jos (<--- miserable specimen)
Jun 2 '07 #7
Read my reply #3 for starters.

kind regards,

Jos
Ok i get the idea of what ur saying i can do the part about the same suit but i dont know how to find consecutive number in the array where bout can i look up tat kinda info i checked in my book cant find similar problem
Jun 2 '07 #8
r035198x
13,262 8TB
Ok i get the idea of what ur saying i can do the part about the same suit but i dont know how to find consecutive number in the array where bout can i look up tat kinda info i checked in my book cant find similar problem
how about
Expand|Select|Wrap|Line Numbers
  1. if((array[i + 1] + 1) == array[i] ) 
?
Jun 2 '07 #9
JosAH
11,448 Expert 8TB
how about
Expand|Select|Wrap|Line Numbers
  1. if((array[i + 1] + 1) == array[i] ) 
?
It can be much simple than that; have a look:
Expand|Select|Wrap|Line Numbers
  1. char[] rank= "0000000000000".toCharArray(); // Thirteen zeros
  2. // add 1 to the rank and suit arrays for all cards;
  3. int index= new String(rank).indexOf("11111");
  4. if (index >= 0)
  5.    // five consecutive cards starting at 'index'
kind regards,

Jos (<--- bag of old tricks)
Jun 2 '07 #10
It can be much simple than that; have a look:
Expand|Select|Wrap|Line Numbers
  1. char[] rank= "0000000000000".toCharArray(); // Thirteen zeros
  2. // add 1 to the rank and suit arrays for all cards;
  3. int index= new String(rank).indexOf("11111");
  4. if (index >= 0)
  5.    // five consecutive cards starting at 'index'
kind regards,

Jos (<--- bag of old tricks)
umm i dont quite get this method is it possible if u explain it a little bit?
I understand tat array one but u said this one is simplier so im wanna understand it. Also is it possible to use a for loop will it be similar ideas to this.

Thanks
Jun 2 '07 #11
JosAH
11,448 Expert 8TB
umm i dont quite get this method is it possible if u explain it a little bit?
I understand tat array one but u said this one is simplier so im wanna understand it. Also is it possible to use a for loop will it be similar ideas to this.

Thanks
Char arrays are the building blocks of Strings; you can easily convert one to
another and vice versa (see my litle code snippet). If I initialize a String with all
character '0's, convert the String to an array and add 1 to them for every card
rank in my hand, I get a 'rank score'. If I have five consecutive cards in my hand
I end up with a String that has five consecutive '1' values in it. The indexOf()
method will find them. If I'd used regular expressions I could even have found a
full house easily: "(2.*3)|(3.*2)" or two pairs or whatever. Check the API docs.

You don't need any loops except for updating the 'rank' and 'suit' arrays given
your hand of cards:
Expand|Select|Wrap|Line Numbers
  1. for (Card card : hand) {
  2.    rank[card.getRank()]++;
  3.    suit[card.getSuit()]++;
  4. }
kind regards,

Jos
Jun 2 '07 #12
r035198x
13,262 8TB
It can be much simple than that; have a look:
Expand|Select|Wrap|Line Numbers
  1. char[] rank= "0000000000000".toCharArray(); // Thirteen zeros
  2. // add 1 to the rank and suit arrays for all cards;
  3. int index= new String(rank).indexOf("11111");
  4. if (index >= 0)
  5. // five consecutive cards starting at 'index'
kind regards,

Jos (<--- bag of old tricks)
I like this one.
Lesson learnt: Never leave a couple of string literals to Jos.
Jun 2 '07 #13
JosAH
11,448 Expert 8TB
I like this one.
Lesson learnt: Never leave a couple of string literals to Jos.
SImilar trickery-dickery applies equally well to that other "Hangman" thread.
Strings can come in handy when abused well ;-)

kind regards,

Jos
Jun 2 '07 #14
Thanks for the tips but i think im still stuck could somoene point out my mistakes or where i be confused with here my codes.Im still not too sure with rour method also what is it mean by regular expressions

Thank You


[PHP] public Hand(Card[] cards)
{
cardrank= new int[12];
if(cards.length == CARDS_IN_HAND)
{
thecard = new Card[5];
for(int i = 0; i<cards.length; i++)
{
thecard[i] = cards[i];
}
}

}

public int getCategory()
{
if(hasStraight() == true){
returnValue = STRAIGHT;
}

return returnValue;

private boolean hasStraight()
{
cardrank= new int[12];
char[] cardrank= "0000000000000".toCharArray();
int index= new String(cardrank).indexOf("11111");
if (index >= 0){
return true;
}
return false;
}[/PHP]
Jun 3 '07 #15
JosAH
11,448 Expert 8TB
Thanks for the tips but i think im still stuck could somoene point out my mistakes or where i be confused with here my codes.Im still not too sure with rour method also what is it mean by regular expressions

Thank You


[PHP] public Hand(Card[] cards)
{
cardrank= new int[12];
if(cards.length == CARDS_IN_HAND)
{
thecard = new Card[5];
for(int i = 0; i<cards.length; i++)
{
thecard[i] = cards[i];
}
}

}

public int getCategory()
{
if(hasStraight() == true){
returnValue = STRAIGHT;
}

return returnValue;

private boolean hasStraight()
{
cardrank= new int[12];
char[] cardrank= "0000000000000".toCharArray();
int index= new String(cardrank).indexOf("11111");
if (index >= 0){
return true;
}
return false;
}[/PHP]
1) Why do you tag your code as if it were PHP?

2) Your code doesn't make sense at all now, sorry. Reread my replies #10 and #12
again and try to understand them. Don't just mix up pieces of code and hope for the best.

You need two char arrays; you can construct and initialize them like this:
Expand|Select|Wrap|Line Numbers
  1. char[] rank= "0000000000000".toCharArray();
  2. char[] suit= "0000".toCharArray();
  3.  
Next update those arrays as I showed in my previous reply #12. Finally check
for five consecutive cards (or whatever) using the code snippet in my reply #10.

kind regards,

Jos
Jun 3 '07 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Mike Hofer | last post: by
27 posts views Thread by Simon Biber | last post: by
5 posts views Thread by althafexcel | last post: by
9 posts views Thread by teejayem | last post: by
2 posts views Thread by blitz1989 | last post: by
5 posts views Thread by vtp82 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.