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.

Card.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /**
  9. * Represents a single card, consisting of a rank and suit combination.
  10. *
  11. * @author Chris
  12. */
  13. public class Card implements Comparable<Card> {
  14. /** The suit of this card. */
  15. private final Suit suit;
  16. /** The rank of this card. */
  17. private final Rank rank;
  18. /** Whether or not this card is public. */
  19. private boolean isPublic = false;
  20. /**
  21. * Creates a new instance of Card.
  22. *
  23. * @param suit The suit of this card
  24. * @param rank The rank of this card
  25. */
  26. public Card(final Suit suit, final Rank rank) {
  27. this.suit = suit;
  28. this.rank = rank;
  29. }
  30. /**
  31. * Sets the "public" property of this card. Public cards are displayed
  32. * to all players.
  33. *
  34. * @param isPublic The new public property of this card
  35. */
  36. public void setPublic(final boolean isPublic) {
  37. this.isPublic = isPublic;
  38. }
  39. /**
  40. * Determines if this card is public or not.
  41. *
  42. * @return True if the card is public, false otherwise
  43. */
  44. public boolean isPublic() {
  45. return isPublic;
  46. }
  47. /**
  48. * Retrieves the rank of this card.
  49. *
  50. * @return This card's rank
  51. */
  52. public Rank getRank() {
  53. return rank;
  54. }
  55. /**
  56. * Retrieves the suit of this card.
  57. *
  58. * @return This card's suit
  59. */
  60. public Suit getSuit() {
  61. return suit;
  62. }
  63. /**
  64. * Retrieves the file name used to display this card.
  65. *
  66. * @return The file name used to display this card
  67. */
  68. public String getFileName() {
  69. return (suit.toString().charAt(0) + rank.toString()).toLowerCase();
  70. }
  71. /** {@inheritDoc} */
  72. public int compareTo(Card o) {
  73. return compareTo(o, false);
  74. }
  75. public int compareTo(Card o, boolean acesLow) {
  76. return -1 * rank.compareTo(o.getRank(), acesLow);
  77. }
  78. /** {@inheritDoc} */
  79. @Override
  80. public String toString() {
  81. return rank.toString() + "/" + suit.toString().charAt(0);
  82. }
  83. @Override
  84. public boolean equals(Object obj) {
  85. if (obj == null) {
  86. return false;
  87. }
  88. if (getClass() != obj.getClass()) {
  89. return false;
  90. }
  91. final Card other = (Card) obj;
  92. if (this.suit != other.suit) {
  93. return false;
  94. }
  95. if (this.rank != other.rank) {
  96. return false;
  97. }
  98. return true;
  99. }
  100. @Override
  101. public int hashCode() {
  102. int hash = 3;
  103. hash = 89 * hash + (this.suit != null ? this.suit.hashCode() : 0);
  104. hash = 89 * hash + (this.rank != null ? this.rank.hashCode() : 0);
  105. return hash;
  106. }
  107. }