View Javadoc

1   // START LICENSE
2   // JSet - a Java JSet card board game implementation
3   // Copyright (C) 2004 Jerome Lacoste
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or (at
8   // your option) any later version.
9   //
10  // This program is distributed in the hope that it will be useful, but
11  // WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // General Public License for more details.
14  //
15  // You should have received a copy of the GNU General Public License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  // END LICENSE
19  
20  package org.cb.jset;
21  
22  // import java.util.HashSet;
23  
24  import java.io.Serializable;
25  import java.util.List;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  
29  /***
30   * Represents a card used in the game.
31   * <p>
32   * A card has 4 properties: a type, a color, a number of shapes and a number of fills.
33   * Each property has 3 different values, making for 81 different cards in the game.
34   *
35   * @author jerome@coffeebreaks.org - last modified by $LastChangedBy: jerome $
36   * @version $Id: CardProperties.java 129 2004-04-15 05:00:43Z jerome $
37   */
38  public class CardProperties implements Serializable
39  {
40    /***
41     * @todo it is not such a good idea to hardcode the type with the name.
42     * What if we change the implementation?
43     */
44  
45    public static final int COLOR_RED        = 1;
46    public static final int COLOR_GREEN      = 2;
47    public static final int COLOR_BLUE       = 3;
48  
49    public static final int SHAPE_RECTANGLE  = 1;
50    public static final int SHAPE_OVALE      = 2;
51    public static final int SHAPE_ODD        = 3;
52  
53    public static final int FILL_EMPTY       = 1;
54    public static final int FILL_DOTTED      = 2;
55    public static final int FILL_FULL        = 3;
56  
57    private final byte _shape;
58  
59    private final byte _color;
60  
61    private final byte _number;
62  
63    private final byte _fill;
64  
65    // for test purposes
66    /***
67     * Creates a new instance of CardProperties, given the specified shape, color number and fill.
68     * Each property has a value between <code>1</code> and <code>3</code>.
69     * @param shape the card shape type
70     * @param color the card color type
71     * @param number the card shape number type
72     * @param fill the card shape fill type
73     */
74    CardProperties(final int shape, final int color, final int number, final int fill)
75    {
76      this((byte) shape, (byte) color, (byte) number, (byte) fill);
77    }
78  
79    /***
80     * Creates a new instance of CardProperties, given the specified shape, color number and fill.
81     * Each property has a value between <code>1</code> and <code>3</code>.
82     * @param shape the card shape type
83     * @param color the card color type
84     * @param number the card shape number type
85     * @param fill the card shape fill type
86     */
87    public CardProperties(final byte shape, final byte color, final byte number, final byte fill)
88    {
89      _shape = shape;
90      _color = color;
91      _number = number;
92      _fill = fill;
93    }
94  
95    /***
96     * @return the shape property identifier
97     */
98    public byte getShape()
99    {
100     return _shape;
101   }
102 
103   /***
104    * @return the color property identifier
105    */
106   public byte getColor()
107   {
108     return _color;
109   }
110 
111   /***
112    * @return the shape number property identifier
113    */
114   public byte getNumber()
115   {
116     return _number;
117   }
118 
119   /***
120    * @return the shape fill property identifier
121    */
122   public byte getFill()
123   {
124     return _fill;
125   }
126 
127   private static final List CARDS = createCardSet();
128   /*** Number of cards. **/
129   public static final int CARDSNB = 81;
130 
131   private static List createCardSet()
132   {
133     // HashSet cards = new HashSet(CARDSNB);
134     final List cards = new ArrayList(CARDSNB);
135     for (byte t = 1; t < 4; t++)
136     {  // type
137       for (byte c = 1; c < 4; c++)
138       {  // color
139         for (byte n = 1; n < 4; n++)
140         {  // number
141           for (byte f = 1; f < 4; f++)
142           {  // fill
143             // cards.add(new CardProperties(t, c, n, f));
144             cards.add(new CardProperties(t, c, n, f));
145           }
146         }
147       }
148     }
149     return cards;
150   }
151 
152   /***
153    * Get the card given the specified index.
154    * @param idx the index of the card.
155    * @return the card matching the index.
156    * @throws IllegalStateException if index is invalid.
157    */
158   public static CardProperties getCard(final int idx)
159   {
160     if (idx < 0 || idx > CARDSNB)
161     {
162       throw new IllegalStateException("CardProperties not found: invalid index");
163     }
164     return (CardProperties) CARDS.get(idx);
165   }
166 
167   /***
168    * @todo this should not be needed if equals is implemented correctly.
169    * @param card
170    * @return the CardProperties
171    */
172   public static CardProperties findCard(final CardProperties card)
173   {
174     /*
175     Iterator i = CARDS.iterator();
176     while (i.hasNext()) {
177         CardProperties prop = (CardProperties) i.next();
178         if (prop.equals(card)) {
179             return prop;
180         }
181     }*/
182     for (int i = 0; i < CARDSNB; i++)
183     {
184       final CardProperties prop = (CardProperties) CARDS.get(i);
185       if (prop.equals(card))
186       {
187         return prop;
188       }
189     }
190     throw new IllegalStateException(card + " not found in CARDS");
191   }
192 
193   /*
194   static CardProperties findCard(int idx) {
195       Iterator i = CARDS.iterator();
196       while (i.hasNext()) {
197           CardProperties prop = (CardProperties) i.next();
198           if (prop.equals(card)) {
199               return prop;
200           }
201       }
202       throw new IllegalStateException (card + " not found in CARDS");
203   }
204   */
205 
206   /***
207    * Indicates whether some other object is "equal to" this one.
208    * @param o the other object
209    * @return <code<>true</code> if both instances are equal.
210    */
211   public boolean equals(final Object o)
212   {
213     if (this == o) return true;
214     if (!(o instanceof CardProperties)) return false;
215 
216     final CardProperties cardProperties = (CardProperties) o;
217 
218     if (_color != cardProperties._color) return false;
219     if (_fill != cardProperties._fill) return false;
220     if (_number != cardProperties._number) return false;
221     if (_shape != cardProperties._shape) return false;
222 
223     return true;
224   }
225 
226   /***
227    * @return a hash code value for the object.
228    */
229   public int hashCode()
230   {
231     int result;
232     result = (int) _shape;
233     result = 29 * result + (int) _color;
234     result = 29 * result + (int) _number;
235     result = 29 * result + (int) _fill;
236     return result;
237   }
238 
239   /***
240    * An id for that card.
241    * @return
242    */
243   private int getID()
244   {
245     return 27 * (getShape() - 1)
246           + 9 * (getColor() - 1)
247           + 3 * (getNumber() - 1)
248           + 1 * (getFill() - 1);
249   }
250 
251   /***
252    * @return a <code>String</code> representation of that object.
253    */
254   public String toString()
255   {
256     return "" + getID();
257   }
258 
259   /***
260    * A helper function that generates a <code>String</code> representation for the
261    * specified array of <code>CardProperties</code>.
262    * @param cards the cards for which we want a <code>String</code> representation.
263    * @return the <code>String</code> representation
264    */
265   public static String toString(final CardProperties[] cards)
266   {
267     return toString(Arrays.asList(cards));
268   }
269 
270   /***
271    * A helper function that generates a <code>String</code> representation for the
272    * specified list of <code>CardProperties</code>.
273    * @param cards the cards for which we want a <code>String</code> representation.
274    * @return the <code>String</code> representation
275    */
276   public static String toString(final List cards)
277   {
278     final StringBuffer buf = new StringBuffer("[");
279     if (cards != null)
280     {
281       for (int i = 0; i < cards.size(); i++)
282       {
283         final org.cb.jset.CardProperties cardProperties = (org.cb.jset.CardProperties) CARDS.get(i);
284         if (i != 0)
285         {
286           buf.append(",");
287         }
288         if (cardProperties != null)
289         {
290           buf.append(cardProperties.getID());
291         }
292         else
293         {
294           buf.append("null");
295         }
296       }
297     }
298     buf.append("]");
299     return buf.toString();
300   }
301 }