View Javadoc

1   //  Open Hold em is a java-based, server and client environment for hosting 
2   //  your own texas hold poker tournaments, and regular tables. It is full 
3   //  customizable and deployable in a number of different java-based hosting 
4   //  environments.
5   //  Copyright (C) 2005 Chris A. Mattmann <chris@baron.pagemewhen.com>
6   //
7   //  This program is free software; you can redistribute it and/or modify
8   //  it under the terms of the GNU General Public License as published by
9   //  the Free Software Foundation; either version 2 of the License, or
10  //  (at your option) any later version.
11  //
12  //  This program is distributed in the hope that it will be useful,
13  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  //  GNU General Public License for more details.
16  //
17  //  You should have received a copy of the GNU General Public License
18  //  along with this program; if not, write to the Free Software
19  //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  //
21  //  $Id: Hand.java,v 1.5 2005/08/11 17:53:08 mattmann Exp $
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      /* the set of cards that a player holds */
43      private List fCards = null;
44      
45      /* the royal flush hand */
46      public static final String ROYAL_FLUSH = "ROYAL_FLUSH";
47      
48      /* the 4 of a kind hand */
49      public static final String FOUR_KIND = "4_KIND";
50      
51      /* the full house hand */
52      public static final String FULL_HOUSE = "FULL_HOUSE";
53      
54      /* the straight flush hand */
55      public static final String STRAIGHT_FLUSH = "STRAIGHT_FLUSH";
56      
57      /* the 3 of a kind hand */
58      public static final String THREE_KIND = "THREE_KIND";
59      
60      /* the flush hand */
61      public static final String FLUSH = "FLUSH";
62      
63      /* the straight hand */
64      public static final String STRAIGHT = "STRAIGHT";
65      
66      /* the 2 pair hand */
67      public static final String TWO_PAIR = "TWO_PAIR";
68      
69      /* the pair hand */
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         // cast o to a hand
137         Hand h = (Hand) o;
138 
139         // iterate through our hand, and make sure that each card in our hand is
140         // represented in o
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         //now go through the hand in sorted order and make sure that the value of the next card is always the next value
154         //if it is, you're a straight
155         int oldVal = -1;
156         
157         //sort the hand
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        //iterate through the list and count the no. of cards
182         //if the max card count at any point is 4, then you're set
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 }