| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.cb.jset.MatchingSetFinder |
|
|
| 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 | 3 | 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 | 18 | final Vector v = new Vector(); |
| 41 | // CardProperties c1, c2, c3; |
|
| 42 | JSetCard tmp; |
|
| 43 | CardProperties c1, c2, c3; |
|
| 44 | 542 | for (int i = 0; i < cards.size(); i++) |
| 45 | { |
|
| 46 | 524 | tmp = ((JSetCard) cards.get(i)); |
| 47 | 524 | if (tmp == null) |
| 48 | { |
|
| 49 | 3 | continue; |
| 50 | } |
|
| 51 | 521 | c1 = tmp.getProperties(); |
| 52 | 7996 | for (int j = i + 1; j < cards.size(); j++) |
| 53 | { |
|
| 54 | 7475 | tmp = ((JSetCard) cards.get(j)); |
| 55 | 7475 | if (tmp == null) |
| 56 | { |
|
| 57 | 25 | continue; |
| 58 | } |
|
| 59 | 7450 | c2 = tmp.getProperties(); |
| 60 | 76733 | for (int k = j + 1; k < cards.size(); k++) |
| 61 | { |
|
| 62 | 69283 | tmp = ((JSetCard) cards.get(k)); |
| 63 | 69283 | if (tmp == null) |
| 64 | { |
|
| 65 | 98 | continue; |
| 66 | } |
|
| 67 | 69185 | 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 | 69185 | final byte colors = (byte) (c1.getColor() + c2.getColor() + c3.getColor()); |
| 73 | 69185 | final byte numbers = (byte) (c1.getNumber() + c2.getNumber() + c3.getNumber()); |
| 74 | 69185 | final byte types = (byte) (c1.getShape() + c2.getShape() + c3.getShape()); |
| 75 | 69185 | final byte fills = (byte) (c1.getFill() + c2.getFill() + c3.getFill()); |
| 76 | 69185 | if ((colors % 3 == 0) && (numbers % 3 == 0) && (types % 3 == 0) && (fills % 3 == 0)) |
| 77 | { |
|
| 78 | 824 | final CardProperties[] prop = new CardProperties[3]; |
| 79 | 824 | prop[0] = c1; |
| 80 | 824 | prop[1] = c2; |
| 81 | 824 | prop[2] = c3; |
| 82 | 824 | v.add(new CardSet(prop)); |
| 83 | } |
|
| 84 | } |
|
| 85 | } |
|
| 86 | } |
|
| 87 | 18 | final CardSet[] s = new CardSet[v.size()]; |
| 88 | 18 | return (CardSet[]) v.toArray(s); |
| 89 | } |
|
| 90 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |