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 import java.util.Vector;
24 import java.util.List;
25
26 /***
27 * Helps to find sets of matching cards in a List of cards.
28 * @author jerome@coffeebreaks.org - last modified by $LastChangedBy: jerome $
29 * @version $Id: MatchingSetFinder.java 78 2004-04-13 04:25:57Z jerome $
30 */
31 public class MatchingSetFinder implements Serializable
32 {
33 /***
34 * Find the matching sets in the specified list of cards.
35 * @param cards the list of cards. May contain null elements.
36 * @return an array of matching <code>CardSet</code>s.
37 */
38 public CardSet[] findSets(final List cards)
39 {
40 final Vector v = new Vector();
41
42 JSetCard tmp;
43 CardProperties c1, c2, c3;
44 for (int i = 0; i < cards.size(); i++)
45 {
46 tmp = ((JSetCard) cards.get(i));
47 if (tmp == null)
48 {
49 continue;
50 }
51 c1 = tmp.getProperties();
52 for (int j = i + 1; j < cards.size(); j++)
53 {
54 tmp = ((JSetCard) cards.get(j));
55 if (tmp == null)
56 {
57 continue;
58 }
59 c2 = tmp.getProperties();
60 for (int k = j + 1; k < cards.size(); k++)
61 {
62 tmp = ((JSetCard) cards.get(k));
63 if (tmp == null)
64 {
65 continue;
66 }
67 c3 = tmp.getProperties();
68
69
70
71
72 final byte colors = (byte) (c1.getColor() + c2.getColor() + c3.getColor());
73 final byte numbers = (byte) (c1.getNumber() + c2.getNumber() + c3.getNumber());
74 final byte types = (byte) (c1.getShape() + c2.getShape() + c3.getShape());
75 final byte fills = (byte) (c1.getFill() + c2.getFill() + c3.getFill());
76 if ((colors % 3 == 0) && (numbers % 3 == 0) && (types % 3 == 0) && (fills % 3 == 0))
77 {
78 final CardProperties[] prop = new CardProperties[3];
79 prop[0] = c1;
80 prop[1] = c2;
81 prop[2] = c3;
82 v.add(new CardSet(prop));
83 }
84 }
85 }
86 }
87 final CardSet[] s = new CardSet[v.size()];
88 return (CardSet[]) v.toArray(s);
89 }
90 }