View Javadoc
1 /***
2 * Copyright (c) 2003 held jointly by the individual authors.
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; with out even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * > http://www.gnu.org/copyleft/lesser.html
19 * > http://www.opensource.org/licenses/lgpl-license.php
20 */
21 package net.mlw.gfw.ext.card;
22
23 import java.util.Arrays;
24 import java.util.Comparator;
25
26 import net.mlw.gfw.event.Event;
27 import net.mlw.gfw.event.EventHandler;
28 import net.mlw.gfw.event.EventHandlerList;
29 import net.mlw.gfw.ext.card.event.DealEvent;
30 import net.mlw.gfw.ext.card.event.PlayCardEvent;
31
32 /***
33 * @author Matthew L. Wilson
34 * @version $Revision: 1.6 $ $Date: 2004/06/29 20:32:54 $
35 */
36 public class DefaultHandModel implements HandModel
37 {
38 protected int size;
39 protected Card[] cards;
40 protected EventHandlerList eventHandlers;
41
42 private CardSelectionBehavior cardSelectionBehavior;
43
44 public DefaultHandModel(int size)
45 {
46 cards = new Card[this.size = size];
47 eventHandlers = new EventHandlerList();
48 }
49
50 /***
51 * @see net.mlw.gfw.ext.card.HandModel#addCard(net.mlw.gfw.ext.card.Card)
52 */
53 public void addCard(Card card)
54 {
55 for (int i = 0; i < cards.length; i++)
56 {
57 if (cards[i] == null)
58 {
59 cards[i] = card;
60 if (cardSelectionBehavior != null)
61 {
62 cardSelectionBehavior.setSelected(card, true);
63 }
64 break;
65 }
66 }
67
68 Arrays.sort(cards, new Comparator()
69 {
70 public int compare(Object c1, Object c2)
71 {
72 if (c1 == null || c2 == null)
73 {
74 return 0;
75 }
76
77 return c1.hashCode() - c2.hashCode();
78 }
79
80 public boolean equals(Object obj)
81 {
82 return false;
83 }
84 });
85 }
86
87 /***
88 * @see net.mlw.gfw.ext.card.HandModel#removeCard(net.mlw.gfw.ext.card.Card)
89 */
90 public void removeCard(Card card)
91 {
92 for (int i = 0; i < cards.length; i++)
93 {
94 if (cards[i] != null && cards[i].equals(card))
95 {
96 cards[i] = null;
97 return;
98 }
99 }
100 }
101
102 /***
103 * @see net.mlw.gfw.ext.card.HandModel#getCards()
104 */
105 public Card[] getCards()
106 {
107 return cards;
108 }
109
110 /***
111 * @see net.mlw.gfw.ext.card.HandModel#getCard(int)
112 */
113 public Card getCard(int index)
114 {
115 return cards[index];
116 }
117
118 /***
119 * @see net.mlw.gfw.ext.card.HandModel#getMaxNumberOfCards()
120 */
121 public int getMaxNumberOfCards()
122 {
123 return size;
124 }
125
126 /***
127 * @see net.mlw.gfw.ext.card.HandModel#isValid(int)
128 */
129 public boolean isValid(int index)
130 {
131 return cards[index] != null;
132 }
133
134 /***
135 * @see net.mlw.gfw.event.EventHandler#onEvent(net.mlw.gfw.event.Event)
136 */
137 public void onEvent(Event event)
138 {
139 if (event instanceof DealEvent)
140 {
141 Card[] cards = ((DealEvent) event).getCards();
142 for (int i = 0; i < cards.length; i++)
143 {
144 addCard(cards[i]);
145 }
146 eventHandlers.onEvent(event);
147 }
148 else if (event instanceof PlayCardEvent)
149 {
150 PlayCardEvent play = (PlayCardEvent) event;
151 removeCard(play.getCard());
152 eventHandlers.onEvent(event);
153 }
154 }
155
156 /***
157 * @see net.mlw.gfw.event.EventHandlerContainer#addEventHandler(net.mlw.gfw.event.EventHandler)
158 */
159 public EventHandler addEventHandler(EventHandler eventHandler)
160 {
161 return eventHandlers.addEventHandler(eventHandler);
162 }
163
164 /***
165 * @see net.mlw.gfw.event.EventHandlerContainer#removeEventHandler(net.mlw.gfw.event.EventHandler)
166 */
167 public EventHandler removeEventHandler(EventHandler eventHandler)
168 {
169 return eventHandlers.removeEventHandler(eventHandler);
170 }
171
172 /***
173 * @return Returns the cardSelectionBehavior.
174 */
175 public CardSelectionBehavior getCardSelectionBehavior()
176 {
177 return cardSelectionBehavior;
178 }
179
180 /***
181 * @param cardSelectionBehavior The cardSelectionBehavior to set.
182 */
183 public void setCardSelectionBehavior(CardSelectionBehavior cardSelectionBehavior)
184 {
185 this.cardSelectionBehavior = cardSelectionBehavior;
186 }
187
188 /***
189 * @see net.mlw.gfw.ext.card.HandModel#isSelected(Card)
190 */
191 public boolean isSelected(Card card)
192 {
193 return (cardSelectionBehavior == null) ? false : cardSelectionBehavior.isSelected(card);
194 }
195
196 /*** (non-Javadoc)
197 * @see net.mlw.gfw.ext.card.HandModel#fireChange()
198 */
199 public void fireChange()
200 {
201 eventHandlers.onEvent(null);
202 }
203
204 }
This page was automatically generated by Maven