1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 }