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.io.Serializable;
23  
24  /***
25   * A CardSet of cards in the game.
26   * A set can be {@link #isMatching() matching} or not.
27   * @author jerome@coffeebreaks.org - last modified by $LastChangedBy: jerome $
28   * @version $Id: CardSet.java 129 2004-04-15 05:00:43Z jerome $
29   */
30  public class CardSet implements Serializable
31  {
32    private final CardProperties[] _set;
33  
34    /***
35     * Creates a new instance of CardSet given the specified card properties.
36     * @param set the potential matching set
37     */
38    public CardSet(final CardProperties[] set)
39    {
40      _set = set;
41    }
42  
43    /***
44     * @return the cards in the set
45     * @todo do not expose the array like that. An Immutabble list could do it.
46     */
47    public CardProperties[] getCards()
48    {
49      return _set;
50    }
51  
52    /***
53     * @return <code>true</code> if the set is matching, <code>false</code> otherwise.
54     */
55    public boolean isMatching()
56    {
57      final CardProperties[] cards = getCards();
58      if (cards.length != 3)
59      {
60        return false;
61      }
62  
63      if ((cards[0].getShape() == cards[1].getShape() &&
64           cards[1].getShape() != cards[2].getShape())
65       || (cards[0].getShape() != cards[1].getShape() &&
66           cards[1].getShape() == cards[2].getShape()))
67      {
68        return false;
69      }
70      if ((cards[0].getColor() == cards[1].getColor() &&
71           cards[1].getColor() != cards[2].getColor())
72       || (cards[0].getColor() != cards[1].getColor() &&
73           cards[1].getColor() == cards[2].getColor()))
74      {
75        return false;
76      }
77      if ((cards[0].getNumber() == cards[1].getNumber() &&
78           cards[1].getNumber() != cards[2].getNumber())
79       || (cards[0].getNumber() != cards[1].getNumber() &&
80           cards[1].getNumber() == cards[2].getNumber()))
81      {
82        return false;
83      }
84      if ((cards[0].getFill() == cards[1].getFill() &&
85           cards[1].getFill() != cards[2].getFill())
86       || (cards[0].getFill() != cards[1].getFill() &&
87           cards[1].getFill() == cards[2].getFill()))
88      {
89        return false;
90      }
91      return true;
92    }
93  
94    /***
95     * @return a <code>String</code> representation of that object.
96     */
97    public String toString()
98    {
99      final StringBuffer buf = new StringBuffer("CardSet:");
100     buf.append(CardProperties.toString(_set));
101     return buf.toString();
102   }
103 
104   /***
105    * @todo move to unit test 
106    * @param args
107    */
108   public static void main(final String[] args)
109   {
110     final CardProperties card1 = new CardProperties(1, 1, 1, 1);
111     final CardProperties card2 = new CardProperties(2, 1, 2, 1);
112     final CardProperties card3 = new CardProperties(3, 1, 3, 1);
113     final CardProperties[] cards = {card1, card2, card3};
114 
115     System.out.println(new CardSet(cards).isMatching());
116   }
117 }