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.

Player.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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;
  8. import com.md87.cardgame.interfaces.Game;
  9. import com.md87.cardgame.interfaces.PlayerController;
  10. /**
  11. *
  12. * @author Chris
  13. */
  14. public class Player implements Comparable<Player> {
  15. public static enum OpenCheck {OPEN, CHECK};
  16. public static enum CallRaiseFold {CALL, RAISE, FOLD};
  17. private String name;
  18. private Game game;
  19. private int cash;
  20. private int bet;
  21. private PlayerController controller;
  22. private final Deck cards = new Deck();
  23. private Deck bestDeck = null;
  24. private boolean hasFolded = false;
  25. private boolean isOut = false;
  26. private boolean isAllIn = false;
  27. public Player(final Game game, final String name, final int cash, final PlayerController controller) {
  28. this.name = name;
  29. this.cash = cash;
  30. this.game = game;
  31. this.controller = controller;
  32. controller.setPlayer(this);
  33. controller.setGame(game);
  34. }
  35. public Deck getCards() {
  36. return cards;
  37. }
  38. public Deck getBestDeck() {
  39. calculateBestDeck();
  40. return bestDeck;
  41. }
  42. public Deck doCardDiscard(int min, int max) {
  43. return controller.discardCards(min, max);
  44. }
  45. public void calculateBestDeck() {
  46. bestDeck = game.getBestDeck(cards);
  47. }
  48. public OpenCheck doOpenCheck() {
  49. OpenCheck res = controller.doOpenCheck();
  50. if (cash <= 0 && res == OpenCheck.OPEN) {
  51. res = OpenCheck.CHECK;
  52. }
  53. return res;
  54. }
  55. public CallRaiseFold doCallRaiseFold(final int callAmount, boolean canRaise) {
  56. CallRaiseFold res = controller.doCallRaiseFold(callAmount, canRaise);
  57. if (cash - callAmount <= 0 && res == CallRaiseFold.RAISE) {
  58. res = Player.CallRaiseFold.CALL;
  59. }
  60. return res;
  61. }
  62. public boolean shouldShowCards() {
  63. return controller.shouldShowCards();
  64. }
  65. public void forceBet(final int blind) {
  66. int size = Math.min(cash, blind);
  67. bet += size;
  68. cash -= size;
  69. if (cash == 0) {
  70. isAllIn = true;
  71. }
  72. }
  73. public void resetBet() {
  74. this.bet = 0;
  75. }
  76. public void addCash(final int amount) {
  77. cash += amount;
  78. }
  79. public void setOut() {
  80. isOut = true;
  81. }
  82. public String getName() {
  83. return name + " (" + (isAllIn ? "all in" : cash) + ")";
  84. }
  85. public void setFold() {
  86. hasFolded = true;
  87. }
  88. public void dealCard(final Card card) {
  89. cards.add(card);
  90. }
  91. public void removeCard(final Card card) {
  92. cards.remove(card);
  93. }
  94. public void discardCards() {
  95. synchronized(cards) {
  96. cards.clear();
  97. }
  98. bet = 0;
  99. hasFolded = false;
  100. isOut = cash <= 0;
  101. isAllIn = false;
  102. bestDeck = null;
  103. }
  104. public int getCash() {
  105. return cash;
  106. }
  107. public int getBet() {
  108. return bet;
  109. }
  110. public boolean hasFolded() {
  111. return hasFolded;
  112. }
  113. public boolean isOut() {
  114. return isOut;
  115. }
  116. public boolean isAllIn() {
  117. return isAllIn;
  118. }
  119. public int getRaiseAmount(final int minimum) {
  120. if (minimum > cash) {
  121. return cash;
  122. } else {
  123. return controller.getRaise(minimum);
  124. }
  125. }
  126. public int compareTo(Player o) {
  127. return -1 * game.getHand(getBestDeck()).compareTo(game.getHand(o.getBestDeck()));
  128. }
  129. public boolean isLocalHuman() {
  130. return controller.isLocalHuman();
  131. }
  132. }