OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / CORBA / swing / x5 / PlayerImpl.java
1 /* PlayerImpl.java --
2  Copyright (C) 2005 Free Software Foundation, Inc.
3
4  This file is part of GNU Classpath.
5
6  GNU Classpath is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2, or (at your option)
9  any later version.
10
11  GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with GNU Classpath; see the file COPYING.  If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20
21  Linking this library statically or dynamically with other modules is
22  making a combined work based on this library.  Thus, the terms and
23  conditions of the GNU General Public License cover the whole
24  combination.
25
26  As a special exception, the copyright holders of this library give you
27  permission to link this library with independent modules to produce an
28  executable, regardless of the license terms of these independent
29  modules, and to copy and distribute the resulting executable under
30  terms of your choice, provided that you also meet, for each linked
31  independent module, the terms and conditions of the license of that
32  module.  An independent module is a module which is not derived from
33  or based on this library.  If you modify this library, you may extend
34  this exception to your version of the library, but you are not
35  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */
37
38
39 package gnu.classpath.examples.CORBA.swing.x5;
40
41 import java.awt.Color;
42 import java.awt.Point;
43
44 import java.rmi.RemoteException;
45
46 /**
47  * The implementation of the PlayerCommunicator, providing the local
48  * functionality. Apart remote methods, the class also defines some local
49  * methods, needed for the co-ordinated work with the game user interface.
50  * 
51  * @author Audrius Meskauskas (AudriusA@Bioinformatics.org) 
52  */
53 public class PlayerImpl
54   implements Player, State
55 {
56   /**
57    * The playing table.
58    */
59   PlayingDesk desk;
60
61   /**
62    * The state of this player (one of the constants, defined in the player
63    * interface.
64    */
65   private int state = DISCONNECTED;
66
67   /**
68    * The other player.
69    */
70   Player partner;
71
72   /**
73    * Called when the local player refuses to continue the game.
74    */
75   public void leave()
76   {
77     try
78       {
79         if (state == I_THINK || state == I_WAIT_FOR_YOUR_MOVE)
80           {
81             partner.receive_chat(ChatConstants.REMOTE_PLAYER,
82               "Your partner has left the game.");
83             partner.disconnect();
84           }
85         else if (state == State.QUEUED)
86           {
87             if (desk.manager != null)
88               desk.manager.unregister(desk.player);
89             receive_chat(ChatConstants.SYSTEM,
90               "Do not be so pessimistic, try to play first!");
91           }
92         set_current_state(State.DISCONNECTED);
93
94         desk.frame.bChat.setEnabled(false);
95         desk.frame.bLeave.setEnabled(false);
96         desk.frame.bConnect.setEnabled(true);
97         desk.frame.taUrl.setText(desk.frame.mior);
98       }
99     catch (RemoteException ex)
100       {
101         // We will print the exception because this is a demo application that
102         // may be modified for learning purposes.
103         ex.printStackTrace();
104       }
105   }
106
107   /**
108    * Called when we make the move. The PlayingTable is responsible for checking
109    * the correctness of the move and detecting the victory.
110    * 
111    * @param x x position of the new dot.
112    * @param y y position of the new dot.
113    * 
114    * @param victory array of two memebers, representing the endpoints of the
115    * drawn line (victory detected) or null if no such yet exists.
116    */
117   public void we_move(int x, int y, Point[] victory)
118   {
119     try
120       {
121         set_current_state(I_WAIT_FOR_YOUR_MOVE);
122         partner.receive_move(x, y, victory);
123       }
124     catch (RemoteException ex)
125       {
126         // We will print the exception because this is a demo application that
127         // may be modified for learning purposes.
128         ex.printStackTrace();
129
130         state = ERROR;
131       }
132   }
133
134   /**
135    * Set the current state.
136    */
137   public void set_current_state(int new_state)
138   {
139     state = new_state;
140
141     if (state == DISCONNECTED)
142       {
143         setStatus("Disconnected");
144       }
145     else if (state == I_THINK)
146       {
147         setStatus("Our move");
148       }
149     else if (state == I_WAIT_FOR_YOUR_MOVE)
150       {
151         setStatus("Partner's move");
152       }
153     else if (state == ERROR)
154       {
155         setStatus("Error.");
156       }
157     else if (state == I_HAVE_LOST)
158       {
159         setStatus("We lost");
160       }
161     else if (state == I_HAVE_WON)
162       {
163         setStatus("Victory");
164       }
165     else if (state == QUEUED)
166       {
167         setStatus("Queued");
168       }
169     else
170       {
171         setStatus("State " + state);
172       }
173
174     boolean connected = state != State.DISCONNECTED;
175
176     desk.frame.bConnect.setEnabled(!connected && state != State.QUEUED);
177     desk.frame.bReset.setEnabled(connected);
178     desk.frame.bLeave.setEnabled(connected);
179     desk.frame.bChat.setEnabled(connected);
180   }
181
182   /**
183    * Show the state in the status line.
184    */
185   public void setStatus(String status)
186   {
187     desk.frame.lbState.setText(status);
188   }
189
190   /**
191    * Receive the invitation to play from the patner or the game manager.
192    * 
193    * @param address the address (host and port) of the remote partner.
194    * @param youStart if true, the game manager instructs to start the game first
195    * (another side is instructed to start the game second).
196    * 
197    * Game server may also chat a little bit with both players, saying that the
198    * game has started.
199    * 
200    * @return true on success.
201    */
202   public boolean start_game(Player otherPlayer, boolean youStart)
203     throws RemoteException
204   {
205     partner = otherPlayer;
206     desk.reset();
207
208     if (youStart)
209       {
210         set_current_state(I_THINK);
211       }
212     else
213       {
214         set_current_state(I_WAIT_FOR_YOUR_MOVE);
215       }
216
217     desk.frame.taUrl.setText("");
218
219     return true;
220   }
221
222   /**
223    * Get the state of the local player (one of the constants, defined in this
224    * interface).
225    */
226   public int get_current_state()
227     throws RemoteException
228   {
229     return state;
230   }
231
232   /**
233    * Receive the chat message from the friend or challenge server (remote).
234    * Possible at any state, always remote.
235    * 
236    * @param color the color code, used to highlight the message.
237    * @param text the message text.
238    */
239   public void receive_chat(byte color, String text)
240     throws RemoteException
241   {
242     if (color >= ChatConstants.colors.length)
243       color = ChatConstants.REMOTE_PLAYER;
244
245     desk.frame.talk(ChatConstants.colors[color], text);
246   }
247
248   /**
249    * Indicated that the remote side leaves the game (capitulating).
250    */
251   public void disconnect()
252     throws RemoteException
253   {
254     desk.frame.talk(Color.red, "The partner leaves the game.");
255     partner = null;
256     set_current_state(DISCONNECTED);
257
258     desk.frame.taUrl.setText(desk.frame.mior);
259   }
260
261   /**
262    * Receive friends move (possible at I_WAIT_FOR_YOUR_MOVE).
263    * 
264    * @param x grid position.
265    * @param y grid position.
266    * @param victory if not a null, the friend thinks that it has won, the
267    * parameter containing the ends of the builded line.
268    */
269   public void receive_move(int x, int y, Point[] victory)
270     throws RemoteException
271   {
272     // The state changes are handled by the PlayingTable
273     desk.friendsMove(x, y, victory);
274   }
275 }