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: Card.java,v 1.4 2005/08/11 17:53:08 mattmann Exp $
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      /* the suit of the card */
37      private String fSuit = null;
38      
39      /* the card (e.g., A, K, Q, etc.) */
40      private String fCard = null;
41      
42      /* set the suit to this if you don't care what the suit is, and you only care about the card */
43      public static final String ANY_SUIT = "ANY_SUIT";
44      
45      /* the clubs suit */
46      public static final String SUIT_CLUBS = "c";
47      
48      /* the hearts suit */
49      public static final String SUIT_HEARTS = "h";
50      
51      /* the spades suit */
52      public static final String SUIT_SPADES = "s";
53      
54      /* the diamonds suit */
55      public static final String SUIT_DIAMONDS = "d";
56      
57      /***
58       * <p>Construct a new Card</p>
59       */
60      public Card() {
61          super();
62          // TODO Auto-generated constructor stub
63      }
64      
65      public Card(String suit, String card){
66          fSuit = suit;
67          fCard = card;
68          
69      }
70  
71      /* (non-Javadoc)
72       * @see com.openholdem.structs.ICard#getCard()
73       */
74      public String getCard() {
75          return fCard;
76      }
77  
78      /* (non-Javadoc)
79       * @see com.openholdem.structs.ICard#getSuit()
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             //okay try and parse it as an int
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         //spades the highest
189         //followed by hearts
190         //then clubs
191         //then diamonds
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 }