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.

StandardHand.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.interfaces.Hand;
  10. /**
  11. *
  12. * @author Chris
  13. */
  14. public class StandardHand extends AbstractHand {
  15. public static enum Ranking {
  16. STRAIGHT_FLUSH,
  17. FOUR_OF_A_KIND,
  18. FULL_HOUSE,
  19. FLUSH,
  20. STRAIGHT,
  21. THREE_OF_A_KIND,
  22. TWO_PAIR,
  23. ONE_PAIR,
  24. NO_PAIR
  25. };
  26. protected Ranking bestRank;
  27. public StandardHand(final Deck cards) {
  28. this.cards = cards;
  29. if (isStraightFlush(cards)) {
  30. bestRank = StandardHand.Ranking.STRAIGHT_FLUSH;
  31. } else if (isFourOfAKind(cards)) {
  32. bestRank = StandardHand.Ranking.FOUR_OF_A_KIND;
  33. } else if (isFullHouse(cards)) {
  34. bestRank = StandardHand.Ranking.FULL_HOUSE;
  35. } else if (isFlush(cards)) {
  36. bestRank = StandardHand.Ranking.FLUSH;
  37. } else if (isStraight(cards)) {
  38. bestRank = StandardHand.Ranking.STRAIGHT;
  39. } else if (isThreeOfAKind(cards)) {
  40. bestRank = StandardHand.Ranking.THREE_OF_A_KIND;
  41. } else if (isTwoPairs(cards)) {
  42. bestRank = StandardHand.Ranking.TWO_PAIR;
  43. } else if (isOnePair(cards)) {
  44. bestRank = StandardHand.Ranking.ONE_PAIR;
  45. } else {
  46. high = null;
  47. low = null;
  48. usesLow = false;
  49. usesAllCards = false;
  50. kickers = new Deck(cards);
  51. kickers.limitTo(5);
  52. bestRank = StandardHand.Ranking.NO_PAIR;
  53. }
  54. }
  55. public Ranking getBestRank() {
  56. return bestRank;
  57. }
  58. public int compareTo(final Hand ob) {
  59. final StandardHand o = (StandardHand) ob;
  60. if (o.getBestRank() == bestRank) {
  61. if (o.getHigh() == high) {
  62. if (usesLow && low != o.getLow()) {
  63. return -1 * low.compareTo(o.getLow());
  64. }
  65. if (usesAllCards) {
  66. Deck myCards = new Deck(cards);
  67. myCards.removeAll(kickers);
  68. Deck theirCards = new Deck(o.getDeck());
  69. theirCards.removeAll(o.getKickers());
  70. int result = myCards.compareTo(theirCards);
  71. if (result != 0) {
  72. return result;
  73. }
  74. }
  75. return kickers.compareTo(o.getKickers());
  76. } else {
  77. return -1 * high.compareTo(o.getHigh());
  78. }
  79. } else {
  80. return -1 * bestRank.compareTo(o.getBestRank());
  81. }
  82. }
  83. public String getFriendlyName() {
  84. switch (bestRank) {
  85. case FLUSH:
  86. return "Flush";
  87. case FOUR_OF_A_KIND:
  88. return "Four of a kind: " + high.capitalise() + "s";
  89. case FULL_HOUSE:
  90. return "Full house: " + high.capitalise() + "s full of "
  91. + low.capitalise() + "s";
  92. case NO_PAIR:
  93. return "No pair";
  94. case ONE_PAIR:
  95. return "One pair: " + high.capitalise() + "s";
  96. case STRAIGHT:
  97. return "Straight: " + high.capitalise() + " high";
  98. case STRAIGHT_FLUSH:
  99. return "Straight flush: " + high.capitalise() + " high";
  100. case THREE_OF_A_KIND:
  101. return "Three of a kind: " + high.capitalise() + "s";
  102. case TWO_PAIR:
  103. return "Two pair: " + high.capitalise() + "s over "
  104. + low.capitalise() + "s";
  105. default:
  106. return "Unknown hand";
  107. }
  108. }
  109. }