1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package com.openholdem.structs;
24
25 import java.util.List;
26 import java.util.Vector;
27 import java.util.Collections;
28 import java.util.Iterator;
29 import java.util.HashMap;
30
31 /***
32 * @author mattmann
33 * @version $Revision: 1.5 $
34 *
35 * <p>
36 * A Hand is a set of {@link Card}s.
37 * </p>
38 *
39 */
40 public class Hand {
41
42
43 private List fCards = null;
44
45
46 public static final String ROYAL_FLUSH = "ROYAL_FLUSH";
47
48
49 public static final String FOUR_KIND = "4_KIND";
50
51
52 public static final String FULL_HOUSE = "FULL_HOUSE";
53
54
55 public static final String STRAIGHT_FLUSH = "STRAIGHT_FLUSH";
56
57
58 public static final String THREE_KIND = "THREE_KIND";
59
60
61 public static final String FLUSH = "FLUSH";
62
63
64 public static final String STRAIGHT = "STRAIGHT";
65
66
67 public static final String TWO_PAIR = "TWO_PAIR";
68
69
70 public static final String PAIR = "PAIR";
71
72
73 /***
74 *
75 */
76 public Hand() {
77 fCards = new Vector();
78 }
79
80 public Hand(int size) {
81 fCards = new Vector();
82 }
83
84 /***
85 * @return Returns the fCards.
86 */
87 public List getFCards() {
88 return fCards;
89 }
90
91 /***
92 * @param cards
93 * The fCards to set.
94 */
95 public void setFCards(List cards) {
96 fCards = cards;
97 }
98
99 public String toString() {
100 StringBuffer rStr = new StringBuffer("[");
101
102
103
104 for (Iterator i = fCards.iterator(); i.hasNext();) {
105 Card c = (Card) i.next();
106 rStr.append(c.toString() + ",");
107 }
108
109 rStr.deleteCharAt(rStr.length() - 1);
110 rStr.append("]");
111
112 return rStr.toString();
113 }
114
115 public boolean sameSuit() {
116 boolean allSame = true;
117
118 String lastSuit = null;
119
120 for (Iterator i = fCards.iterator(); i.hasNext();) {
121 Card c = (Card) i.next();
122
123 if (lastSuit != null && lastSuit.equals(c.getSuit())) {
124 lastSuit = c.getSuit();
125 } else if (lastSuit != null && !lastSuit.equals(c.getSuit())) {
126 allSame = false;
127 } else if (lastSuit == null) {
128 lastSuit = c.getSuit();
129 }
130 }
131
132 return allSame;
133 }
134
135 public boolean equals(Object o) {
136
137 Hand h = (Hand) o;
138
139
140
141 for (Iterator i = fCards.iterator(); i.hasNext();) {
142 Card c = (Card) i.next();
143
144 if (!h.getFCards().contains(c)) {
145 return false;
146 }
147 }
148
149 return true;
150 }
151
152 public boolean isStraight(){
153
154
155 int oldVal = -1;
156
157
158 Collections.sort(this.fCards);
159
160 for(Iterator i = this.fCards.iterator(); i.hasNext(); ){
161 Card c = (Card)i.next();
162
163 if(oldVal == -1){
164 oldVal = c.getCardValue();
165 }
166 else{
167 if(c.getCardValue() != oldVal+1){
168 return false;
169 }
170 else{
171 oldVal = c.getCardValue();
172 }
173 }
174
175 }
176
177 return true;
178 }
179
180 public boolean isFourOfAKind(){
181
182
183
184 HashMap counts = new HashMap();
185 int maxCount = 0;
186
187 for(Iterator i = this.fCards.iterator(); i.hasNext(); ){
188 Card c = (Card)i.next();
189
190 int count = -1;
191
192 if(counts.get(c.getCard()) != null){
193 count = Integer.parseInt((String)counts.get(c.getCard()));
194 count++;
195
196 if(count > maxCount){
197 maxCount = count;
198 }
199 }
200 else{
201 count = 1;
202 }
203
204 counts.put(c.getCard(),String.valueOf(count));
205
206 if(maxCount == 4){
207 return true;
208 }
209 }
210
211 return false;
212 }
213
214 }