View Javadoc

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.client;
21  
22  import org.cb.jset.JSetGameImpl;
23  import org.cb.jset.SetGamePlayer;
24  import org.cb.jset.SetGameConnection;
25  import org.cb.jset.CardProperties;
26  import org.cb.jset.CardSet;
27  import org.cb.jset.MatchingException;
28  
29  
30  /***
31   * A Game implementation to play localy.
32   *
33   * FIXME GameListener not registered.
34   * @author jerome@coffeebreaks.org - last modified by $LastModifiedBy$
35   * @version $Id: LocalGame.java 130 2004-04-15 05:18:07Z jerome $
36   */
37  public class LocalGame extends AbstractGame
38  {
39    private JSetGameImpl _game;
40    private SetGamePlayer _player;
41    private SetGameConnection _connection;
42    private String _playerName = "Me";
43    private boolean _isBlocked = false;
44  //  private boolean _wasDisconnected = false;
45  
46    public void start()
47    {
48      _game = new JSetGameImpl("Standalone", 1);
49      _game.start();
50      _player = initPlayer();
51      _connection = _game.connect(_player);
52    }
53  
54    SetGamePlayer initPlayer()
55    {
56      return new MyPlayer();
57    }
58  
59    public boolean isBlocked()
60    {
61      return _isBlocked;
62    }
63  
64    public String getPlayerName()
65    {
66      return _playerName;
67    }
68  
69    /***
70     * An implementation of a player for this local game.
71     */
72    class MyPlayer implements SetGamePlayer
73    {
74      /***
75       * @return the name of the player.
76       */
77      public String getName()
78      {
79        return getPlayerName();
80      }
81  
82      /***
83       * Called by a game when the player should be blocked.
84       * @see #isBlocked()
85       */
86      public void block()
87      {
88        _isBlocked = true;
89      }
90  
91      /***
92       * Called by a game when the player should be unblocked.
93       * @see #isBlocked()
94       */
95      public void unblock()
96      {
97       _isBlocked = false;
98      }
99  
100     /***
101      * A blocked player doesn't have the right to play.
102      * @return <code>true</code> is the player is blocked, <code>false</code> otherwise.
103      */
104     public boolean isBlocked()
105     {
106       return _isBlocked;
107     }
108 
109     /***
110      * Called to notify a disconnection from a game.
111      */
112     public void disconnect()
113     {
114   //    _wasDisconnected = true;
115       LocalGame.this.fireGameChange(new GameEvent(GameEvent.END));
116     }
117 
118     /***
119      * @inheritDoc
120      */
121     public void cardsAdded(final CardProperties[] cards)
122     {
123       LocalGame.this.fireCardsAdded(cards);
124     }
125 
126     /***
127      * @inheritDoc
128      */
129     public void setRemoved(final CardSet set)
130     {
131       LocalGame.this.fireSetRemoved(set);
132     }
133   }
134 
135   public void stop()
136   {
137     _connection.close();
138   }
139 
140   public void endTurn()
141   {
142     throw new IllegalStateException("FIXME: not implemented");
143   }
144 
145   public void removeSet(final CardSet set) throws MatchingException
146   {
147     _connection.matchSet(set);
148   }
149 }