1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package com.openholdem.structs;
25
26
27 /***
28 * @author mattmann
29 * @version $Revision: 1.4 $
30 *
31 * <p>A card is one of the 52 cards in the {@link Deck}.</p>
32 *
33 */
34 public class Card implements ICard, Comparable{
35
36
37 private String fSuit = null;
38
39
40 private String fCard = null;
41
42
43 public static final String ANY_SUIT = "ANY_SUIT";
44
45
46 public static final String SUIT_CLUBS = "c";
47
48
49 public static final String SUIT_HEARTS = "h";
50
51
52 public static final String SUIT_SPADES = "s";
53
54
55 public static final String SUIT_DIAMONDS = "d";
56
57 /***
58 * <p>Construct a new Card</p>
59 */
60 public Card() {
61 super();
62
63 }
64
65 public Card(String suit, String card){
66 fSuit = suit;
67 fCard = card;
68
69 }
70
71
72
73
74 public String getCard() {
75 return fCard;
76 }
77
78
79
80
81 public String getSuit() {
82 return fSuit;
83 }
84
85 /***
86 * @param card The fCard to set.
87 */
88 public void setFCard(String card) {
89 fCard = card;
90 }
91
92 /***
93 * @param suit The fSuit to set.
94 */
95 public void setFSuit(String suit) {
96 fSuit = suit;
97 }
98
99 public String toString(){
100 String rStr="";
101
102 rStr+=this.fCard+this.fSuit;
103
104 return rStr;
105 }
106
107
108 public boolean equals(Object o){
109 return (compareTo(o) == 0);
110 }
111
112 /***
113 * <p>Compare this Card to another Card</p>
114 *
115 * @param o The card to compare ourselves to.
116 * @return -1 if this Card is less than the param o, 0 if it is equal and 1 if it is greater
117 */
118 public int compareTo(Object o) throws ClassCastException{
119 if (!(o instanceof Card)) {
120 throw new ClassCastException("Card expected");
121 }
122
123 int compareVal = -1;
124
125 Card c = (Card)o;
126
127 if(c.getCard().equals(this.fCard) && c.getSuit().equals(this.fSuit)){
128 compareVal = 0;
129 }
130 else if(c.getCard().equals(this.fCard) && !c.getSuit().equals(this.fSuit)){
131 if(c.getCardSuitValue() > getCardSuitValue()){
132 compareVal = -1;
133 }
134 else compareVal = 1;
135 }
136 else if(!c.getCard().equals(this.fCard)){
137
138 int theOtherCard = c.getCardValue();
139 int thisCard = getCardValue();
140
141 if(theOtherCard > thisCard){
142 compareVal = -1;
143 }
144 else if(theOtherCard < thisCard){
145 compareVal = 1;
146 }
147 else {
148 System.out.println("can't determine which card is better "+this+", or "+c);
149 return -1;
150 }
151 }
152
153
154 return compareVal;
155 }
156
157 public int hashCode() {
158 return this.getCardValue()+this.fSuit.charAt(0);
159 }
160
161 public int getCardValue(){
162 int val = -1;
163
164 try{
165 val = Integer.parseInt(this.fCard);
166 return val;
167 }
168 catch(NumberFormatException ignore){
169 if(this.fCard.equals("J")){
170 val = 11;
171 }
172 else if(this.fCard.equals("Q")){
173 val = 12;
174 }
175 else if(this.fCard.equals("K")){
176 val = 13;
177 }
178 else if(this.fCard.equals("A")){
179 val = 14;
180 }
181
182
183 return val;
184 }
185 }
186
187 public int getCardSuitValue(){
188
189
190
191
192
193 int retVal = -1;
194
195 if(this.fSuit.equals(Card.SUIT_DIAMONDS)){
196 retVal = 1;
197 }
198 else if(this.fSuit.equals(Card.SUIT_CLUBS)){
199 retVal = 2;
200 }
201 else if(this.fSuit.equals(Card.SUIT_HEARTS)){
202 retVal = 3;
203 }
204 else if(this.fSuit.equals(Card.SUIT_SPADES)){
205 retVal = 4;
206 }
207
208 return retVal;
209 }
210 }