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 net.mlw.gfw.event.Event; 24 import net.mlw.gfw.event.EventHandler; 25 import net.mlw.gfw.event.EventHandlerList; 26 import net.mlw.gfw.ext.basic.event.SitEvent; 27 import net.mlw.gfw.ext.card.event.ClearTableEvent; 28 import net.mlw.gfw.ext.card.event.PlayCardEvent; 29 30 /*** 31 * @author Matthew L. Wilson 32 * @version $Revision: 1.9 $ $Date: 2004/06/29 20:32:54 $ 33 */ 34 public class DefaultTableModel implements TableModel 35 { 36 protected int numberOfPlayers; 37 protected String[] player; 38 protected Card[] cards; 39 40 protected EventHandlerList eventHandlers; 41 42 public DefaultTableModel(int numberOfPlayers) 43 { 44 this.numberOfPlayers = numberOfPlayers; 45 player = new String[numberOfPlayers]; 46 cards = new Card[numberOfPlayers]; 47 eventHandlers = new EventHandlerList(); 48 } 49 50 public void clear() 51 { 52 for (int i = 0; i < numberOfPlayers; i++) 53 { 54 cards[i] = null; 55 } 56 } 57 58 /*** 59 * @see net.mlw.gfw.ext.card.TableModel#playCard(net.mlw.gfw.ext.card.Card, int) 60 */ 61 public void playCard(Card card, int seat) 62 { 63 cards[seat] = card; 64 } 65 66 public int getNumberOfCardsPlayed() 67 { 68 int numberOfCardsPlayed = 0; 69 for (int i = 0; i < numberOfPlayers; i++) 70 { 71 if (cards[i] != null) 72 { 73 numberOfCardsPlayed++; 74 } 75 } 76 return numberOfCardsPlayed; 77 } 78 79 /*** 80 * @see net.mlw.gfw.ext.card.TableModel#sit(java.lang.String, int) 81 */ 82 public void sit(String userName, int seat) 83 { 84 player[seat] = userName; 85 } 86 87 /*** 88 * @see net.mlw.gfw.ext.card.TableModel#stand(java.lang.String) 89 */ 90 public void stand(String userName) 91 { 92 int seat = isSeated(userName); 93 player[seat] = null; 94 } 95 96 public int isSeated(String userName) 97 { 98 for (int i = 0; i < numberOfPlayers; i++) 99 { 100 if (userName.equals(player[i])) 101 { 102 return i; 103 } 104 } 105 return -1; 106 } 107 108 /*** 109 * @see net.mlw.gfw.ext.card.TableModel#getCard(int) 110 */ 111 public Card getCard(int seat) 112 { 113 return cards[seat]; 114 } 115 116 /*** 117 * @see net.mlw.gfw.ext.card.TableModel#getPlayersName(int) 118 */ 119 public String getPlayersName(int seat) 120 { 121 return player[seat]; 122 } 123 124 /*** 125 * @return Returns the cards. 126 */ 127 public Card[] getCards() 128 { 129 return cards; 130 } 131 132 /*** 133 * @see net.mlw.gfw.event.EventHandler#onEvent(net.mlw.gfw.event.Event) 134 */ 135 public void onEvent(Event event) 136 { 137 if (event instanceof PlayCardEvent) 138 { 139 PlayCardEvent play = (PlayCardEvent) event; 140 int seat = isSeated(play.getUserName()); 141 if (seat != -1) 142 { 143 playCard(play.getCard(), seat); 144 eventHandlers.onEvent(event); 145 } 146 } 147 else if (event instanceof SitEvent) 148 { 149 SitEvent sit = (SitEvent) event; 150 sit(sit.getUserName(), sit.getLocation()); 151 eventHandlers.onEvent(event); 152 } 153 else if (event instanceof ClearTableEvent) 154 { 155 clear(); 156 eventHandlers.onEvent(event); 157 } 158 } 159 160 /*** 161 * @see net.mlw.gfw.event.EventHandlerContainer#addEventHandler(net.mlw.gfw.event.EventHandler) 162 */ 163 public EventHandler addEventHandler(EventHandler eventHandler) 164 { 165 return eventHandlers.addEventHandler(eventHandler); 166 } 167 168 /*** 169 * @see net.mlw.gfw.event.EventHandlerContainer#removeEventHandler(net.mlw.gfw.event.EventHandler) 170 */ 171 public EventHandler removeEventHandler(EventHandler eventHandler) 172 { 173 return eventHandlers.removeEventHandler(eventHandler); 174 } 175 176 }

This page was automatically generated by Maven