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.

TexasHoldEm.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.Deck;
  9. import com.md87.cardgame.Player;
  10. /**
  11. * Implements a standard (local) Texas Hold'em game.
  12. *
  13. * @author Chris
  14. */
  15. public class TexasHoldEm extends AbstractGame {
  16. protected final Deck community = new Deck();
  17. protected boolean doneFlop = false;
  18. protected boolean doneTurn = false;
  19. protected boolean doneRiver = false;
  20. public TexasHoldEm(final int numplayers, final int bigblind, final int ante,
  21. final int raises) {
  22. super(numplayers, bigblind, ante, raises);
  23. }
  24. protected void dealPlayerCards() {
  25. dealCard(players.get((dealer + 1) % numplayers), false);
  26. dealCard(players.get((dealer + 1) % numplayers), false);
  27. }
  28. protected void startGame() {
  29. notifyNewGame();
  30. discardCards();
  31. shuffle();
  32. community.clear();
  33. doAntes();
  34. doBlinds();
  35. doneFlop = false;
  36. doneTurn = false;
  37. doneRiver = false;
  38. dealPlayerCards();
  39. for (int i = 0; i < 5; i++) {
  40. community.add(deck.deal());
  41. }
  42. waitForBets();
  43. if (countPlayers(true, true, false) > 1) {
  44. doneFlop = true;
  45. doBettingRound();
  46. }
  47. if (countPlayers(true, true, false) > 1) {
  48. doneTurn = true;
  49. doBettingRound();
  50. }
  51. if (countPlayers(true, true, false) > 1) {
  52. doneRiver = true;
  53. doBettingRound();
  54. }
  55. if (countPlayers(true, true, false) > 1) {
  56. doShowDown();
  57. } else {
  58. doWinner();
  59. }
  60. for (Player player : players) {
  61. if (player.getCash() <= 0) {
  62. player.setOut();
  63. }
  64. }
  65. notifyEndGame();
  66. doDealerAdvance();
  67. }
  68. /** {@inheritDoc} */
  69. public Deck getCommunityCards() {
  70. if (!doneFlop || community.size() < 3) {
  71. return new Deck();
  72. } else if (!doneTurn || community.size() < 4) {
  73. return new Deck(community.subList(0, 3));
  74. } else if (!doneRiver || community.size() < 5) {
  75. return new Deck(community.subList(0, 4));
  76. } else {
  77. return new Deck(community);
  78. }
  79. }
  80. /** {@inheritDoc} */
  81. public int holeCardCount() {
  82. return 2;
  83. }
  84. protected boolean canDoBringIns() {
  85. return true;
  86. }
  87. }