Java poker implementation
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractHand.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) Chris 'MD87' Smith, 2007. All rights reserved.
  3. *
  4. * This code may not be redistributed without prior permission from the
  5. * aforementioned copyright holder(s).
  6. */
  7. package com.md87.cardgame.hands;
  8. import com.md87.cardgame.Deck;
  9. import com.md87.cardgame.Rank;
  10. import com.md87.cardgame.Suit;
  11. import com.md87.cardgame.interfaces.Hand;
  12. import java.util.Collections;
  13. /**
  14. *
  15. * @author Chris
  16. */
  17. public abstract class AbstractHand implements Hand {
  18. protected Deck cards;
  19. protected Rank high;
  20. protected Rank low;
  21. protected Deck kickers = new Deck();
  22. protected boolean usesLow = false;
  23. protected boolean usesAllCards = false;
  24. public Rank getHigh() {
  25. return high;
  26. }
  27. public Rank getLow() {
  28. return low;
  29. }
  30. public Deck getDeck() {
  31. return cards;
  32. }
  33. public Deck getKickers() {
  34. return kickers;
  35. }
  36. protected boolean isStraightFlush(final Deck deck) {
  37. usesLow = false;
  38. usesAllCards = false;
  39. for (Suit suit : Suit.values()) {
  40. Deck mySuit = deck.getSuit(suit);
  41. if (isStraight(mySuit)) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. protected boolean isFourOfAKind(final Deck deck) {
  48. usesLow = false;
  49. usesAllCards = false;
  50. for (Rank rank : Rank.values()) {
  51. if (deck.getRank(rank).size() == 4) {
  52. high = rank;
  53. kickers = new Deck(deck);
  54. kickers.removeByRank(high, 4);
  55. kickers.limitTo(1);
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
  61. protected boolean isFullHouse(final Deck deck) {
  62. usesLow = true;
  63. usesAllCards = false;
  64. high = null;
  65. low = null;
  66. for (Rank rank : Rank.values()) {
  67. if (high == null && deck.getRank(rank).size() >= 3) {
  68. high = rank;
  69. } else if (low == null && deck.getRank(rank).size() >= 2) {
  70. low = rank;
  71. }
  72. if (high != null && low != null) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. protected boolean isFlush(final Deck deck) {
  79. usesLow = false;
  80. usesAllCards = true;
  81. for (Suit suit : Suit.values()) {
  82. Deck myDeck = deck.getSuit(suit);
  83. if (myDeck.size() >= 5) {
  84. Collections.sort(myDeck);
  85. high = myDeck.get(0).getRank();
  86. kickers.clear();
  87. myDeck.limitTo(5);
  88. deck.clear();
  89. deck.addAll(myDeck);
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. protected boolean isStraight(final Deck deck) {
  96. usesLow = false;
  97. usesAllCards = false;
  98. for (Rank rank : Rank.values()) {
  99. high = rank;
  100. low = rank;
  101. kickers.clear();
  102. boolean fail = false;
  103. for (int i = 0; i < 5; i++) {
  104. if (low == null || deck.getRank(low).size() == 0) {
  105. fail = true;
  106. break;
  107. }
  108. if (low == Rank.DEUCE && i == 3) {
  109. low = Rank.ACE;
  110. } else {
  111. low = low.getLower();
  112. }
  113. }
  114. if (!fail) {
  115. low = null;
  116. return true;
  117. }
  118. }
  119. low = null;
  120. return false;
  121. }
  122. protected boolean isThreeOfAKind(final Deck deck) {
  123. usesLow = false;
  124. usesAllCards = false;
  125. for (Rank rank : Rank.values()) {
  126. if (deck.getRank(rank).size() == 3) {
  127. high = rank;
  128. kickers = new Deck(deck);
  129. kickers.removeByRank(high, 3);
  130. kickers.limitTo(2);
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. protected boolean isTwoPairs(final Deck deck) {
  137. usesLow = true;
  138. usesAllCards = false;
  139. high = null;
  140. low = null;
  141. kickers = new Deck(deck);
  142. for (Rank rank : Rank.values()) {
  143. Deck myDeck = deck.getRank(rank);
  144. if (myDeck.size() == 2) {
  145. if (high == null) {
  146. high = rank;
  147. kickers.removeByRank(rank, 2);
  148. } else {
  149. low = rank;
  150. kickers.removeByRank(rank, 2);
  151. kickers.limitTo(1);
  152. return true;
  153. }
  154. }
  155. }
  156. return false;
  157. }
  158. protected boolean isOnePair(final Deck deck) {
  159. usesLow = false;
  160. usesAllCards = false;
  161. for (Rank rank : Rank.values()) {
  162. if (deck.getRank(rank).size() > 1) {
  163. high = rank;
  164. kickers = new Deck(deck);
  165. kickers.removeByRank(high, 2);
  166. kickers.limitTo(3);
  167. return true;
  168. }
  169. }
  170. return false;
  171. }
  172. }