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 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 public class BoardImpl implements SetGameBoard
34 {
35 private final Logger _logger = Logger.getLogger(BoardImpl.class);
36 /*** the listeners. */
37 private final List _boardListeners = new ArrayList();
38 /*** the cards. */
39 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 CardProperties[] props = new CardProperties[_board.size()];
47 props = (CardProperties[]) _board.toArray(props);
48 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 _boardListeners.add(boardListener);
58 }
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 _boardListeners.remove(boardListener);
67 }
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 for (int i = 0; i < cards.length; i++)
76 {
77 _board.add(cards[i]);
78 }
79 fireCardsAddedEvent(cards);
80 }
81
82 /***
83 * @return the number of cards on the board.
84 */
85 public int getNbCards()
86 {
87 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 _logger.debug(".removeSet(" + set + "):" + CardProperties.toString(set.getCards()));
99 if (!set.isMatching())
100 {
101 throw new MatchingException("The given set is not a matching set.");
102 }
103
104 final CardProperties[] cards = set.getCards();
105 final byte notFound = -1;
106 final byte[] indexes = new byte[cards.length];
107 for (byte i = 0; i < cards.length; i++)
108 {
109 indexes[i] = notFound;
110 for (byte j = 0; j < _board.size(); j++)
111 {
112 _logger.debug(" comparing cards[" + i + "] (" + cards[i] + ") with _board element j=" + j + "(" + (CardProperties) _board.get(j) + ")");
113 if ((_board.get(j)).equals(cards[i]))
114 {
115 indexes[i] = j;
116 _logger.debug(" found indexes[" + i + "]=" + j);
117 }
118 }
119 if (indexes[i] == notFound)
120 {
121 throw new BoardException("CardProperties " + cards[i] + " not found.");
122 }
123 }
124 for (int i = 0; i < indexes.length; i++)
125 {
126 _board.remove(CardProperties.findCard(cards[i]));
127 }
128 fireSetRemovedEvent(set);
129 }
130
131 private void fireCardsAddedEvent(final CardProperties[] cards)
132 {
133 if (_boardListeners != null)
134 {
135 for (int i = 0; i < _boardListeners.size(); i++)
136 {
137 final org.cb.jset.SetGameBoardListener boardListenerIF = (org.cb.jset.SetGameBoardListener) _boardListeners.get(i);
138 boardListenerIF.cardsAdded(cards);
139 }
140 }
141 }
142
143 private void fireSetRemovedEvent(final CardSet set)
144 {
145 if (_boardListeners != null)
146 {
147 for (int i = 0; i < _boardListeners.size(); i++)
148 {
149 final org.cb.jset.SetGameBoardListener boardListenerIF = (org.cb.jset.SetGameBoardListener) _boardListeners.get(i);
150 boardListenerIF.setRemoved(set);
151 }
152 }
153 }
154 }