| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.cb.jset.BoardImpl |
|
|
| 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 org.apache.log4j.Logger; |
|
| 23 | ||
| 24 | import java.util.ArrayList; |
|
| 25 | import java.util.List; |
|
| 26 | ||
| 27 | /** |
|
| 28 | * An implementation of a set game board. |
|
| 29 | * |
|
| 30 | * @author jerome@coffeebreaks.org - last modified by $LastChangedBy: jerome $ |
|
| 31 | * @version $Id: BoardImpl.java 129 2004-04-15 05:00:43Z jerome $ |
|
| 32 | */ |
|
| 33 | 0 | public class BoardImpl implements SetGameBoard |
| 34 | { |
|
| 35 | 0 | private final Logger _logger = Logger.getLogger(BoardImpl.class); |
| 36 | /** the listeners. */ |
|
| 37 | 0 | private final List _boardListeners = new ArrayList(); |
| 38 | /** the cards. */ |
|
| 39 | 0 | private final List _board = new java.util.Vector(12); |
| 40 | ||
| 41 | /** |
|
| 42 | * @return the cards on the board. |
|
| 43 | */ |
|
| 44 | public synchronized CardProperties[] getCards() |
|
| 45 | { |
|
| 46 | 0 | CardProperties[] props = new CardProperties[_board.size()]; |
| 47 | 0 | props = (CardProperties[]) _board.toArray(props); |
| 48 | 0 | return props; |
| 49 | } |
|
| 50 | ||
| 51 | /** |
|
| 52 | * Adds a boardListener to this board. Does nothing if <code>null</code> is passed as argument. |
|
| 53 | * @param boardListener the boardListener to add |
|
| 54 | */ |
|
| 55 | public void addBoardListener(final SetGameBoardListener boardListener) |
|
| 56 | { |
|
| 57 | 0 | _boardListeners.add(boardListener); |
| 58 | 0 | } |
| 59 | ||
| 60 | /** |
|
| 61 | * Removes a listener from this board. Does nothing if <code>null</code> is passed as argument. |
|
| 62 | * @param boardListener the listener to remove |
|
| 63 | */ |
|
| 64 | public void removeBoardListener(SetGameBoardListener boardListener) |
|
| 65 | { |
|
| 66 | 0 | _boardListeners.remove(boardListener); |
| 67 | 0 | } |
| 68 | ||
| 69 | /** |
|
| 70 | * Add the specified cards onto the board. |
|
| 71 | * @param cards a non-<code>null</code> array, containing non-<code>null</code> elements. |
|
| 72 | */ |
|
| 73 | public synchronized void addCards(final CardProperties[] cards) |
|
| 74 | { |
|
| 75 | 0 | for (int i = 0; i < cards.length; i++) |
| 76 | { |
|
| 77 | 0 | _board.add(cards[i]); |
| 78 | } |
|
| 79 | 0 | fireCardsAddedEvent(cards); |
| 80 | 0 | } |
| 81 | ||
| 82 | /** |
|
| 83 | * @return the number of cards on the board. |
|
| 84 | */ |
|
| 85 | public int getNbCards() |
|
| 86 | { |
|
| 87 | 0 | return _board.size(); |
| 88 | } |
|
| 89 | ||
| 90 | /** |
|
| 91 | * Tries to match the specified set on this board. |
|
| 92 | * @param set the set to remove |
|
| 93 | * @throws MatchingException if the specified set is not matching |
|
| 94 | * @throws BoardException if one of the set card is not on the board. |
|
| 95 | */ |
|
| 96 | public synchronized void matchSet(final CardSet set) throws MatchingException, BoardException |
|
| 97 | { |
|
| 98 | 0 | _logger.debug(".removeSet(" + set + "):" + CardProperties.toString(set.getCards())); |
| 99 | 0 | if (!set.isMatching()) |
| 100 | { |
|
| 101 | 0 | throw new MatchingException("The given set is not a matching set."); |
| 102 | } |
|
| 103 | ||
| 104 | 0 | final CardProperties[] cards = set.getCards(); |
| 105 | 0 | final byte notFound = -1; |
| 106 | 0 | final byte[] indexes = new byte[cards.length]; |
| 107 | 0 | for (byte i = 0; i < cards.length; i++) |
| 108 | { |
|
| 109 | 0 | indexes[i] = notFound; |
| 110 | 0 | for (byte j = 0; j < _board.size(); j++) |
| 111 | { |
|
| 112 | 0 | _logger.debug(" comparing cards[" + i + "] (" + cards[i] + ") with _board element j=" + j + "(" + (CardProperties) _board.get(j) + ")"); |
| 113 | 0 | if ((_board.get(j)).equals(cards[i])) |
| 114 | { |
|
| 115 | 0 | indexes[i] = j; |
| 116 | 0 | _logger.debug(" found indexes[" + i + "]=" + j); |
| 117 | } |
|
| 118 | } |
|
| 119 | 0 | if (indexes[i] == notFound) |
| 120 | { |
|
| 121 | 0 | throw new BoardException("CardProperties " + cards[i] + " not found."); |
| 122 | } |
|
| 123 | } |
|
| 124 | 0 | for (int i = 0; i < indexes.length; i++) |
| 125 | { |
|
| 126 | 0 | _board.remove(CardProperties.findCard(cards[i])); |
| 127 | } |
|
| 128 | 0 | fireSetRemovedEvent(set); |
| 129 | 0 | } |
| 130 | ||
| 131 | private void fireCardsAddedEvent(final CardProperties[] cards) |
|
| 132 | { |
|
| 133 | 0 | if (_boardListeners != null) |
| 134 | { |
|
| 135 | 0 | for (int i = 0; i < _boardListeners.size(); i++) |
| 136 | { |
|
| 137 | 0 | final org.cb.jset.SetGameBoardListener boardListenerIF = (org.cb.jset.SetGameBoardListener) _boardListeners.get(i); |
| 138 | 0 | boardListenerIF.cardsAdded(cards); |
| 139 | } |
|
| 140 | } |
|
| 141 | 0 | } |
| 142 | ||
| 143 | private void fireSetRemovedEvent(final CardSet set) |
|
| 144 | { |
|
| 145 | 0 | if (_boardListeners != null) |
| 146 | { |
|
| 147 | 0 | for (int i = 0; i < _boardListeners.size(); i++) |
| 148 | { |
|
| 149 | 0 | final org.cb.jset.SetGameBoardListener boardListenerIF = (org.cb.jset.SetGameBoardListener) _boardListeners.get(i); |
| 150 | 0 | boardListenerIF.setRemoved(set); |
| 151 | } |
|
| 152 | } |
|
| 153 | 0 | } |
| 154 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |