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.

HumanPlayer.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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.controllers;
  8. import com.md87.cardgame.Card;
  9. import com.md87.cardgame.Deck;
  10. import com.md87.cardgame.interfaces.GameObserver;
  11. import com.md87.cardgame.ui.GameWindow;
  12. import com.md87.cardgame.Player;
  13. import com.md87.cardgame.Player.CallRaiseFold;
  14. import com.md87.cardgame.Player.OpenCheck;
  15. import com.md87.cardgame.interfaces.Game;
  16. import com.md87.cardgame.interfaces.PlayerController;
  17. import com.md87.cardgame.RaiseWindow;
  18. import com.md87.cardgame.interfaces.Game;
  19. /**
  20. *
  21. * @author Chris
  22. */
  23. public class HumanPlayer implements PlayerController, GameObserver {
  24. public int move = -1;
  25. public int bet = -1;
  26. public Deck discards = null;
  27. private Game game;
  28. private Player player;
  29. private GameWindow window;
  30. public HumanPlayer(final GameWindow window) {
  31. this.window = window;
  32. }
  33. public CallRaiseFold doCallRaiseFold(int callAmount, boolean canRaise) {
  34. move = -1;
  35. window.setHumanPlayer(this, true, canRaise);
  36. synchronized(this) {
  37. while (move == -1) {
  38. try {
  39. wait();
  40. } catch (InterruptedException ex) {
  41. // Do nothing
  42. }
  43. }
  44. }
  45. if (move == 0) {
  46. return CallRaiseFold.CALL;
  47. } else if (move == 1) {
  48. return CallRaiseFold.RAISE;
  49. } else {
  50. return CallRaiseFold.FOLD;
  51. }
  52. }
  53. public OpenCheck doOpenCheck() {
  54. move = -1;
  55. window.setHumanPlayer(this, false, true);
  56. synchronized(this) {
  57. while (move == -1) {
  58. try {
  59. wait();
  60. } catch (InterruptedException ex) {
  61. // Do nothing
  62. }
  63. }
  64. }
  65. if (move == 0) {
  66. return OpenCheck.CHECK;
  67. } else {
  68. return OpenCheck.OPEN;
  69. }
  70. }
  71. public boolean shouldShowCards() {
  72. return true;
  73. }
  74. public void setPlayer(Player player) {
  75. this.player = player;
  76. }
  77. public void setGame(Game game) {
  78. this.game = game;
  79. game.registerObserver(this);
  80. }
  81. public int getRaise(int minimum) {
  82. bet = -1;
  83. new RaiseWindow(this, minimum);
  84. synchronized(this) {
  85. while (bet == -1) {
  86. try {
  87. wait();
  88. } catch (InterruptedException ex) {
  89. // Do nothing
  90. }
  91. }
  92. }
  93. return bet;
  94. }
  95. public Player getPlayer() {
  96. return player;
  97. }
  98. public GameWindow getWindow() {
  99. return window;
  100. }
  101. public void communityCardsUpdated() {
  102. // Do nothing
  103. }
  104. public void playerCardsUpdated() {
  105. // Do nothing
  106. }
  107. public void playersTurn(Player player) {
  108. // Do nothing
  109. }
  110. public void newPlayer(Player player) {
  111. // Do nothing
  112. }
  113. public void newGame() {
  114. // Do nothing
  115. }
  116. public void endGame() {
  117. window.setWaitPlayer(this);
  118. boolean cont = false;
  119. synchronized(this) {
  120. do {
  121. cont = false;
  122. try {
  123. wait();
  124. } catch (InterruptedException ex) {
  125. cont = true;
  126. }
  127. } while (cont);
  128. }
  129. }
  130. public void setDealer(Player player) {
  131. // Do nothing
  132. }
  133. public void placeBlind(Player player, int blind, String name) {
  134. // Do nothing
  135. }
  136. public void raise(Player player, int amount) {
  137. // Do nothing
  138. }
  139. public void fold(Player player) {
  140. // Do nothing
  141. }
  142. public void call(Player player) {
  143. // Do nothing
  144. }
  145. public void check(Player player) {
  146. // Do nothing
  147. }
  148. public void open(Player player, int amount) {
  149. // Do nothing
  150. }
  151. public void winner(Player players) {
  152. // Do nothing
  153. }
  154. public void showdown() {
  155. // Do nothing
  156. }
  157. /** {@inheritDoc} */
  158. public boolean isLocalHuman() {
  159. return true;
  160. }
  161. /** {@inheritDoc} */
  162. public Deck discardCards(final int minimum, final int maximum) {
  163. discards = null;
  164. window.setDiscardPlayer(this, minimum, maximum);
  165. synchronized(this) {
  166. while (discards == null) {
  167. try {
  168. wait();
  169. } catch (InterruptedException ex) {
  170. // Do nothing
  171. }
  172. }
  173. }
  174. return discards;
  175. }
  176. public void discards(Player player, int number) {
  177. // Do nothing
  178. }
  179. public void cardDealt(Player player, Card card) {
  180. // Do nothing
  181. }
  182. }