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  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      // CardProperties c1, c2, c3;
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            // not the most beautifull implementation but one of the fastest
69            // here the implementation assumes that for each property the sum of the different values
70            // is a multiple of 3
71            // Works pretty well with 1, 2, 3
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  }