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.

OmahaHighLow.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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.games;
  8. import com.md87.cardgame.Card;
  9. import com.md87.cardgame.Deck;
  10. import com.md87.cardgame.Player;
  11. import com.md87.cardgame.Rank;
  12. import com.md87.cardgame.Suit;
  13. import com.md87.cardgame.hands.AceFiveLowHand;
  14. import com.md87.cardgame.hands.StandardHand;
  15. import com.md87.cardgame.interfaces.Hand;
  16. import java.util.Collections;
  17. import java.util.concurrent.Semaphore;
  18. /**
  19. * Implements a standard (local) Omaha Hold'em game.
  20. *
  21. * @author Chris
  22. */
  23. public class OmahaHighLow extends OmahaHoldEm {
  24. protected static final Deck minDeck = new Deck(
  25. new Card(Suit.CLUBS, Rank.EIGHT),
  26. new Card(Suit.CLUBS, Rank.SEVEN),
  27. new Card(Suit.CLUBS, Rank.SIX),
  28. new Card(Suit.CLUBS, Rank.FIVE),
  29. new Card(Suit.CLUBS, Rank.FOUR)
  30. );
  31. protected boolean isHigh = true;
  32. protected final Semaphore highSemaphore = new Semaphore(1);
  33. public OmahaHighLow(final int numplayers, final int bigblind, final int ante,
  34. final int raises) {
  35. super(numplayers, bigblind, ante, raises);
  36. }
  37. @Override
  38. protected void doWinner() {
  39. if (countPlayers(true, true, false) == 1) {
  40. doWinner(false);
  41. return;
  42. }
  43. highSemaphore.acquireUninterruptibly();
  44. isHigh = false;
  45. boolean isLowPlayer = false;
  46. for (Player player : players) {
  47. if (!player.isOut()) {
  48. if (new AceFiveLowHand(player.getBestDeck()).getBestRank() != AceFiveLowHand.Ranking.NON_QUALIFIER) {
  49. isLowPlayer = true;
  50. }
  51. }
  52. }
  53. isHigh = true;
  54. if (isLowPlayer) {
  55. doWinner(true);
  56. isHigh = false;
  57. doWinner(true);
  58. isHigh = true;
  59. } else {
  60. doWinner(false);
  61. }
  62. highSemaphore.release();
  63. }
  64. @Override
  65. public Hand getHand(Deck deck) {
  66. if (isHigh) {
  67. return new StandardHand(deck);
  68. } else {
  69. return new AceFiveLowHand(deck);
  70. }
  71. }
  72. /** {@inheritDoc} */
  73. @Override
  74. public String getHandText(final Player player) {
  75. highSemaphore.acquireUninterruptibly();
  76. StringBuffer buff = new StringBuffer("High: ");
  77. isHigh = true;
  78. buff.append(getHand(player.getBestDeck()).getFriendlyName());
  79. buff.append("\nLow: ");
  80. isHigh = false;
  81. final AceFiveLowHand theHand = new AceFiveLowHand(player.getBestDeck());
  82. if (theHand.getBestRank() == AceFiveLowHand.Ranking.NO_PAIR) {
  83. final Deck theDeck = theHand.getDeck();
  84. StringBuffer other = new StringBuffer();
  85. boolean addAce = false;
  86. Collections.sort(theDeck);
  87. for (Card card : theDeck) {
  88. if (card.getRank() != Rank.ACE) {
  89. other.append('-');
  90. other.append("" + (14 - card.getRank().ordinal()));
  91. } else {
  92. addAce = true;
  93. }
  94. }
  95. if (addAce) {
  96. buff.append('A');
  97. buff.append(other);
  98. } else {
  99. buff.append(other.substring(1));
  100. }
  101. } else {
  102. buff.append("doesn't qualify");
  103. }
  104. isHigh = true;
  105. highSemaphore.release();
  106. return buff.toString();
  107. }
  108. }