001 /*
002 Copyright (c) 2005 Garrett Smith
003 The MIT License
004 
005 Permission is hereby granted, free of charge, to any person obtaining a copy 
006 of this software and associated documentation files (the "Software"), to deal
007 in the Software without restriction, including without limitation the rights
008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
009 copies of the Software, and to permit persons to whom the Software is 
010 furnished to do so, subject to the following conditions:
011 
012 The above copyright notice and this permission notice shall be included in all 
013 copies or substantial portions of the Software.
014 
015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
021 THE SOFTWARE.
022 */
023 
024 // $Id: Card.java.html,v 1.1 2005/06/14 06:50:55 gsmith Exp $
025 
026 package garrettsmith.playingcards;
027 
028 /**
029  * Represents a card in a typical deck of playing cards.
030  * Card is immutable, and, therefore, thread safe.
031  *
032  @author Garrett Smith, gsmith at northwestern dot edu
033  @version Blackjack v1.0
034  @since Blackjack v1.0
035  */
036 public final class Card {
037 
038     private final Suit       _suit;
039     private final Value      _value;
040     private final Color      _color;
041     private       int        _hashCode;
042 
043     /**
044      * Creates a joker.
045      */
046     public
047     Card() {
048         thisValue.JOKER, null );
049     }
050 
051     /**
052      <p>
053      * Creates a new card with the specified suit and value.
054      </p>
055      
056      @param suit the card's suit, for example, hearts
057      @param value the card's value, for example, a ten
058      */
059     public
060     Cardfinal Value value, final Suit suit ) {
061         _value = value;
062         if Value.JOKER.equals_value ) ) {
063             _suit = Suit.NONE;
064             _color = Color.NONE;
065             return;
066         }
067         else {
068             _suit = suit;
069         }
070         if Suit.HEART.equalssuit || Suit.DIAMOND.equalssuit ) ) {
071             _color = Color.RED;
072         }
073         else if Suit.CLUB.equalssuit || Suit.SPADE.equalssuit ) ) {
074             _color = Color.BLACK;
075         }
076         else {
077             _color = Color.NONE;
078         }
079         _hashCode = _suit.hashCode() 37 11 * _value.hashCode() 11;
080     }
081 
082     /**
083      * Returns whether the object referred to in the
084      <code>object</code> parameter is equal to this instance.
085      *
086      @return whether the two cards are the same.
087      */
088     public
089     boolean equalsfinal Object object ) {
090         if object != null && object instanceof Card ) {
091             Card card = Card object;
092             return this.getSuit().equalscard.getSuit() ) &&
093                      this.getValue().equalscard.getValue() ) );
094         }
095         return false;
096     }
097 
098     /**
099      * Returns a string representation of this card's color.
100      *
101      @return a representation of this card's color.
102      */
103     public
104     Color getColor() {
105         return _color;
106     }
107 
108     /**
109      * Returns the card's suit.
110      *
111      @return the card's suit.
112      */
113     public
114     Suit getSuit() {
115         return _suit;
116     }
117 
118     /**
119      * Returns the card's value.
120      *
121      @return the card's value.
122      */
123     public
124     Value getValue() {
125         return _value;
126     }
127 
128     /**
129      * Returns a hash value for this card.
130      *
131      @return a hash value for this object.
132      */
133     public
134     int hashCode() {
135         return _hashCode;
136     }
137 
138     /**
139      * Returns a string representation this card's state.
140      *
141      @return representing this card's state.
142      */
143     public
144     String toString() {
145         return "Card="
146             "[suit=" + _suit
147             ",value=" + _value
148             "]";
149     }
150 
151     /**
152      * Represents the card's color: red, black, or none.  Colors are immutable.
153      */
154     public static class Color {
155 
156         private int    _value       = -1;
157         private int    _hashCode    = -1;
158         private String _stringColor = null;
159 
160         private static final int _RED_VALUE = 1;
161         private static final int _BLACK_VALUE = 2;
162         private static final int _NONE_VALUE = 3;
163 
164         
165         private static final String _RED_STRING = "red";
166         private static final String _BLACK_STRING = "black";
167         private static final String _NONE_STRING = "none";
168 
169         /**
170          * Indicates a red card.
171          */
172         public static final Color RED = new Color_RED_VALUE, _RED_STRING );
173         
174         /**
175          * Indicates a black card.
176          */
177         public static final Color BLACK = new Color_BLACK_VALUE, _BLACK_STRING );        
178 
179         /**
180          * Indicates a card with no color.
181          */
182         public static final Color NONE = new Color_NONE_VALUE, _NONE_STRING );        
183 
184         /**
185        * Returns whether object is a <tt>Color</tt> equal to this one.
186        @return <tt>true</tt> if the objects are equal, <tt>false</tt> otherwise
187        */
188         public
189         boolean equalsObject object ) {
190             return object != null
191                    && object instanceof Card.Color 
192                    && ( ( Card.Color object )._value == this._value;
193         }
194 
195         /**
196          * Returns a hash value for this card.
197          *
198          @return a hash value for this object.
199          */
200         public
201         int hashCode() {
202             return _hashCode;
203         }
204 
205         /**
206          * Returns a string representation.
207          */
208         public
209         String toString() {
210             return _stringColor;
211         }
212 
213         private Colorint value, String color ) {
214             _value = value;
215             _hashCode = _value * 37 17;
216             _stringColor = color;
217         }
218     // class Color
219 
220     /**
221      * Represents a card's suit, such as spades or clubs.
222      */
223     public static class Suit {
224         
225       /**
226        * Returns whether <tt>object</tt> is equal to this suit.
227        */
228         public
229         boolean equalsObject object ) {
230             return object != null
231                    && object instanceof Card.Suit
232                    && ( ( Card.Suit object )._value == this._value;
233         }
234 
235         /**
236          * Returns a hash value for this card.
237          *
238          @return a hash value for this object.
239          */
240         public
241         int hashCode() {
242             return _hashCode;
243         }
244 
245         /**
246          * Returns a string representation of this value.
247          */
248         public
249         String toString() {
250             return _stringSuit;
251         }
252         
253         private static final int _CLUB_VALUE = 1;
254         private static final int _DIAMOND_VALUE = 2;
255         private static final int _HEART_VALUE = 3;
256         private static final int _NONE_VALUE = 4;
257         private static final int _SPADE_VALUE = 5;
258 
259         private static final String _CLUB_STRING = "club";
260         private static final String _DIAMOND_STRING = "diamond";
261         private static final String _HEART_STRING = "heart";
262         private static final String _NONE_STRING = "none";
263         private static final String _SPADE_STRING = "spade";
264 
265         /**
266          * Represents the clubs.
267          */
268         public static final Suit CLUB = new Suit_CLUB_VALUE, _CLUB_STRING );        
269 
270         /**
271          * Represents the diamonds.
272          */
273         public static final Suit DIAMOND = new Suit_DIAMOND_VALUE, _DIAMOND_STRING );        
274 
275         /**
276          * Represents no suit.
277          */
278         public static final Suit NONE = new Suit_NONE_VALUE, _NONE_STRING );        
279 
280         /**
281          * Represents the hearts.
282          */
283         public static final Suit HEART = new Suit_HEART_VALUE, _HEART_STRING );
284 
285         /**
286          * Represents the spades.
287          */
288         public static final Suit SPADE = new Suit_SPADE_VALUE, _SPADE_STRING );
289         
290         protected
291         Object clone()
292             throws CloneNotSupportedException {
293             throw new CloneNotSupportedException();
294         }
295 
296         private Suitint value, String suit ) {
297             _value = value;
298             _hashCode = _value * 37 17;
299             _stringSuit = suit;
300         }
301         
302         private int    _value      = -1;
303         private int    _hashCode   = -1;
304         private String _stringSuit = null;
305         
306     // class Suit
307 
308     /**
309      * Represents a card's value: two, queen, or ace, for example.
310      */
311     public static class Value {
312 
313       /**
314        * Returns whether <tt>object</tt> is equal to this Value.
315        */
316         public
317         boolean equalsObject object ) {
318             return object != null
319                    && object instanceof Card.Value
320                    && ( ( Card.Value object )._value == this._value;
321         }
322 
323         /**
324          * Returns a hash value for this card.
325          *
326          @return a hash value for this object.
327          *
328          @see <a href="http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html#hashCode()">the
329          *      <code>java.lang.Object</code> API specification</a>
330          */
331         public
332         int hashCode() {
333             return _hashCode;
334         }
335 
336         /**
337          * Returns a string repesentation.
338          */
339         public
340         String toString() {
341             return _stringValue;
342         }
343 
344         private static final int _TWO_VALUE = 1;
345         private static final int _THREE_VALUE = 2;
346         private static final int _FOUR_VALUE = 3;
347         private static final int _FIVE_VALUE = 4;
348         private static final int _SIX_VALUE = 5;
349         private static final int _SEVEN_VALUE = 6;
350         private static final int _EIGHT_VALUE = 7;
351         private static final int _NINE_VALUE = 8;
352         private static final int _TEN_VALUE = 9;
353         private static final int _JACK_VALUE = 10;
354         private static final int _QUEEN_VALUE = 11;
355         private static final int _KING_VALUE = 12;
356         private static final int _ACE_VALUE = 13;
357         private static final int _JOKER_VALUE = 14;
358 
359         private static final String _TWO_STRING = "two";
360         private static final String _THREE_STRING = "three";
361         private static final String _FOUR_STRING = "four";
362         private static final String _FIVE_STRING = "five";
363         private static final String _SIX_STRING = "six";
364         private static final String _SEVEN_STRING = "seven";
365         private static final String _EIGHT_STRING = "eight";
366         private static final String _NINE_STRING = "nine";
367         private static final String _TEN_STRING = "ten";
368         private static final String _JACK_STRING = "jack";
369         private static final String _QUEEN_STRING = "queen";
370         private static final String _KING_STRING = "king";
371         private static final String _ACE_STRING = "ace";
372         private static final String _JOKER_STRING = "joker";
373 
374         /**
375          * Represents a two.
376          */
377         public static final Value TWO = new Value_TWO_VALUE, _TWO_STRING );        
378 
379         /**
380          * Represents a three.
381          */
382         public static final Value THREE = new Value_THREE_VALUE, _THREE_STRING );        
383 
384         /**
385          * Represents a four.
386          */
387         public static final Value FOUR = new Value_FOUR_VALUE, _FOUR_STRING );        
388 
389         /**
390          * Represents a five.
391          */
392         public static final Value FIVE = new Value_FIVE_VALUE, _FIVE_STRING );        
393 
394         /**
395          * Represents a six.
396          */
397         public static final Value SIX = new Value_SIX_VALUE, _SIX_STRING );        
398 
399         /**
400          * Represents a seven.
401          */
402         public static final Value SEVEN = new Value_SEVEN_VALUE, _SEVEN_STRING );        
403 
404         /**
405          * Represents a eight.
406          */
407         public static final Value EIGHT = new Value_EIGHT_VALUE, _EIGHT_STRING );        
408 
409         /**
410          * Represents a nine.
411          */
412         public static final Value NINE = new Value_NINE_VALUE, _NINE_STRING );        
413 
414         /**
415          * Represents a ten.
416          */
417         public static final Value TEN = new Value_TEN_VALUE, _TEN_STRING );        
418 
419         /**
420          * Represents a jack.
421          */
422         public static final Value JACK = new Value_JACK_VALUE, _JACK_STRING );        
423 
424         /**
425          * Represents a queen.
426          */
427         public static final Value QUEEN = new Value_QUEEN_VALUE, _QUEEN_STRING );        
428 
429         /**
430          * Represents a king.
431          */
432         public static final Value KING = new Value_KING_VALUE, _KING_STRING );        
433 
434         /**
435          * Represents a ace.
436          */
437         public static final Value ACE = new Value_ACE_VALUE, _ACE_STRING );       
438 
439         /**
440          * Represents a joker.
441          */
442         public static final Value JOKER = new Value_JOKER_VALUE, _JOKER_STRING );       
443         
444         private int _value = -1;
445         private int _hashCode = -1;
446         private String _stringValue = null;
447 
448         private Valueint value, String string ) {
449             _value = value;
450             _hashCode = _value * 37 17;
451             _stringValue = string;
452         }
453         
454     // class Value
455 // class Card