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.

Deck.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 java.util.ArrayList;
  9. import java.util.Collections;
  10. import java.util.List;
  11. /**
  12. *
  13. * @author Chris
  14. */
  15. public class Deck extends ArrayList<Card> implements Comparable<Deck> {
  16. private static final long serialVersionUID = 1;
  17. public Deck() {
  18. super();
  19. }
  20. public Deck(final List<Card> parent) {
  21. super(parent);
  22. }
  23. public Deck(final Card ... cards) {
  24. super();
  25. for (Card card : cards) {
  26. add(card);
  27. }
  28. }
  29. public Deck getSuit(final Suit suit) {
  30. final Deck res = new Deck();
  31. for (Card card : this) {
  32. if (card.getSuit() == suit) {
  33. res.add(card);
  34. }
  35. }
  36. return res;
  37. }
  38. public Deck getRank(final Rank rank) {
  39. final Deck res = new Deck();
  40. for (Card card : this) {
  41. if (card.getRank() == rank) {
  42. res.add(card);
  43. }
  44. }
  45. return res;
  46. }
  47. public void removeByRank(final Rank rank, final int count) {
  48. final Deck newDeck = new Deck();
  49. int remaining = count;
  50. for (Card card : this) {
  51. if (card.getRank().equals(rank) && remaining > 0) {
  52. remaining--;
  53. } else {
  54. newDeck.add(card);
  55. }
  56. }
  57. clear();
  58. addAll(newDeck);
  59. }
  60. public Card deal() {
  61. final Card res = get(0);
  62. remove(0);
  63. return res;
  64. }
  65. public void limitTo(final int number) {
  66. Collections.sort(this);
  67. while (size() > number) {
  68. remove(0);
  69. }
  70. }
  71. public void reverseLimitTo(final int number) {
  72. Collections.sort(this);
  73. while (size() > number) {
  74. remove(size() - 1);
  75. }
  76. }
  77. public int compareTo(Deck o) {
  78. return compareTo(o, false);
  79. }
  80. public int compareTo(Deck o, boolean acesLow) {
  81. Collections.sort(this);
  82. Collections.sort(o);
  83. if (size() != o.size()) {
  84. System.err.println("Warning: Deck.compareTo() called with different sized deck:");
  85. System.err.println(" Me: " + this);
  86. System.err.println(" Them: " + o);
  87. new Exception().printStackTrace();
  88. }
  89. for (int i = Math.min(this.size(), o.size()) - 1; i >= 0; i--) {
  90. int res = this.get(i).compareTo(o.get(i), acesLow);
  91. if (res != 0) {
  92. return res;
  93. }
  94. }
  95. return 0;
  96. }
  97. @Override
  98. public String toString() {
  99. final StringBuilder builder = new StringBuilder();
  100. for (Card card : this) {
  101. builder.append(' ');
  102. builder.append(card.toString());
  103. }
  104. return builder.length() == 0 ? "" : builder.substring(1);
  105. }
  106. }