by admin

Check Poker Hand Java

Hand

Java Poker Game Code

P: 5
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
  1. import java.util.*;
  2. public class Hand
  3. {
  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. //Categories of hands
  8. public static final int HIGH_CARD = 0;
  9. public static final int PAIR = 1;
  10. public static final int TWO_PAIRS = 2;
  11. public static final int THREE_OF_A_KIND = 3;
  12. public static final int STRAIGHT = 4;
  13. public static final int FLUSH = 5;
  14. public static final int FULL_HOUSE = 6;
  15. public static final int FOUR_OF_A_KIND = 7;
  16. public static final int STRAIGHT_FLUSH = 8;
  17. private Card[] thecard;
  18. /**
  19. * Constructor for objects of class Hand
  20. */
  21. public Hand(Card[] cards)
  22. {
  23. if(cards.length CARDS_IN_HAND)
  24. {
  25. thecard = new Card[5];
  26. for(int i = 0; i<cards.length; i++)
  27. {
  28. thecard[i] = cards[i];
  29. }
  30. }
  31. }
  32. public Hand(Deck deck)
  33. {
  34. thecard = new Card[5];
  35. for(int i = 0; i < CARDS_IN_HAND; i++){
  36. thecard[i] = deck.takeTop();
  37. }
  38. }
  39. public Card getCard(int i)
  40. {
  41. return thecard[i];
  42. }
  43. public java.lang.String toString()
  44. {
  45. String handS = ';
  46. for(int i = 0; i<thecard.length; i++){
  47. handS += thecard[i]+' ';
  48. }
  49. return handS.trim();
  50. }
  51. public int getCategory()
  52. {
  53. int returnValue = HIGH_CARD;
  54. if(hasPair() true){
  55. returnValue = PAIR;
  56. }
  57. if(hasTwoPairs() true){
  58. returnValue = TWO_PAIRS;
  59. }
  60. if(hasThreeOfAKind() true){
  61. returnValue = THREE_OF_A_KIND;
  62. }
  63. if(hasStraight() true){
  64. returnValue = STRAIGHT;
  65. }
  66. if(hasFlush() = true){
  67. returnValue = FLUSH;
  68. }
  69. if(hasFUllHouse() = true){
  70. returnValue = FULL_HOUSE;
  71. }
  72. if(hasFourOfAKind() true){
  73. returnValue = FOUR_OF_A_KIND;
  74. }
  75. if(hasStraightFlush() = true){
  76. returnValue = STRAIGHT_FLUSH;
  77. }
  78. return returnValue;
  79. }
  80. private boolean hasThreeOfAKind()
  81. {
  82. int count = 1;
  83. for(int i = 0;i<(CARDS_IN_HAND-2);i++){
  84. for(int j = i+1;j<CARDS_IN_HAND;j++){
  85. if(thecard[i].getRank() thecard[j].getRank()){
  86. count++;
  87. }
  88. }
  89. if(count 3)
  90. {
  91. break;
  92. }else{
  93. count = 1;
  94. }
  95. }
  96. return count 3;
  97. }
  98. private boolean hasTwoPairs()
  99. {
  100. int count = 1;
  101. int pair = 0;
  102. for(int i = 0;i<(CARDS_IN_HAND-1);i++){
  103. for(int j = i + 1;j<CARDS_IN_HAND;j++){
  104. if(thecard[i].getRank() thecard[j].getRank()){
  105. count++;
  106. }
  107. if(count 2){
  108. pair++;
  109. count =1;
  110. }else{
  111. count =1;
  112. }
  113. }
  114. }
  115. return pair 2;
  116. }
  117. private boolean hasPair()
  118. {
  119. int count = 1;
  120. int pos = 1;
  121. for(int i = 0;i<(CARDS_IN_HAND-1);i++){
  122. for(int j = i+1;j<CARDS_IN_HAND;j++){
  123. if(thecard[i].getRank() thecard[j].getRank()){
  124. count++;
  125. }
  126. }
  127. if(count 2)
  128. {
  129. break;
  130. }else{
  131. count = 1;
  132. }
  133. }
  134. return count 2;
  135. }
  136. public static java.lang.String getCategoryName(int category)
  137. {
  138. String[] rank = {'High card','Pair','Two pairs','Three of a kind','Straight','Flush','Full house','Four of a kind','Straight flush'};
  139. return rank[category];
  140. }
  141. }
Check poker hand java games

GitHub Gist: instantly share code, notes, and snippets.

Video Poker Java

HandPoker

Check Poker Hand Java Download

Poker
  1. Poker Hand Frequencies: GitHub Code: https://github.com/amir650/MyCardGame.
  2. The poker odds calculators on CardPlayer.com let you run any scenario that you see at the poker table, see your odds and outs, and cover the math of winning and losing poker hands. Texas Hold'em Omaha.
  3. The problem asked me to find the highest possible hand for a given set of cards, so I tried to keep things simple by writing a checkhand function that checks each hand starting from straight flush down to high card. As soon as a condition for a hand was satisfied, I returned a number that corresponded to the strength of the hand (1 for high.
  4. I'm trying to evaluate a hand to see if they have a pair and I feel like this is right but i keep getting errors. Java - determining is Poker Hand has a Pair.