Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / examples / gnu / classpath / examples / CORBA / swing / x5 / PlayerImpl.java
blobc30f7d51a42cd6c7b9561e74d09d0055ea497df4
1 /* PlayerImpl.java --
2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package gnu.classpath.examples.CORBA.swing.x5;
41 import java.awt.Color;
42 import java.awt.Point;
44 import java.rmi.RemoteException;
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.
51 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
53 public class PlayerImpl
54 implements Player, State
56 /**
57 * The playing table.
59 PlayingDesk desk;
61 /**
62 * The state of this player (one of the constants, defined in the player
63 * interface.
65 private int state = DISCONNECTED;
67 /**
68 * The other player.
70 Player partner;
72 /**
73 * Called when the local player refuses to continue the game.
75 public void leave()
77 try
79 if (state == I_THINK || state == I_WAIT_FOR_YOUR_MOVE)
81 partner.receive_chat(ChatConstants.REMOTE_PLAYER,
82 "Your partner has left the game.");
83 partner.disconnect();
85 else if (state == State.QUEUED)
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!");
92 set_current_state(State.DISCONNECTED);
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);
99 catch (RemoteException ex)
101 // We will print the exception because this is a demo application that
102 // may be modified for learning purposes.
103 ex.printStackTrace();
108 * Called when we make the move. The PlayingTable is responsible for checking
109 * the correctness of the move and detecting the victory.
111 * @param x x position of the new dot.
112 * @param y y position of the new dot.
114 * @param victory array of two memebers, representing the endpoints of the
115 * drawn line (victory detected) or null if no such yet exists.
117 public void we_move(int x, int y, Point[] victory)
121 set_current_state(I_WAIT_FOR_YOUR_MOVE);
122 partner.receive_move(x, y, victory);
124 catch (RemoteException ex)
126 // We will print the exception because this is a demo application that
127 // may be modified for learning purposes.
128 ex.printStackTrace();
130 state = ERROR;
135 * Set the current state.
137 public void set_current_state(int new_state)
139 state = new_state;
141 if (state == DISCONNECTED)
143 setStatus("Disconnected");
145 else if (state == I_THINK)
147 setStatus("Our move");
149 else if (state == I_WAIT_FOR_YOUR_MOVE)
151 setStatus("Partner's move");
153 else if (state == ERROR)
155 setStatus("Error.");
157 else if (state == I_HAVE_LOST)
159 setStatus("We lost");
161 else if (state == I_HAVE_WON)
163 setStatus("Victory");
165 else if (state == QUEUED)
167 setStatus("Queued");
169 else
171 setStatus("State " + state);
174 boolean connected = state != State.DISCONNECTED;
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);
183 * Show the state in the status line.
185 public void setStatus(String status)
187 desk.frame.lbState.setText(status);
191 * Receive the invitation to play from the patner or the game manager.
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).
197 * Game server may also chat a little bit with both players, saying that the
198 * game has started.
200 * @return true on success.
202 public boolean start_game(Player otherPlayer, boolean youStart)
203 throws RemoteException
205 partner = otherPlayer;
206 desk.reset();
208 if (youStart)
210 set_current_state(I_THINK);
212 else
214 set_current_state(I_WAIT_FOR_YOUR_MOVE);
217 desk.frame.taUrl.setText("");
219 return true;
223 * Get the state of the local player (one of the constants, defined in this
224 * interface).
226 public int get_current_state()
227 throws RemoteException
229 return state;
233 * Receive the chat message from the friend or challenge server (remote).
234 * Possible at any state, always remote.
236 * @param color the color code, used to highlight the message.
237 * @param text the message text.
239 public void receive_chat(byte color, String text)
240 throws RemoteException
242 if (color >= ChatConstants.colors.length)
243 color = ChatConstants.REMOTE_PLAYER;
245 desk.frame.talk(ChatConstants.colors[color], text);
249 * Indicated that the remote side leaves the game (capitulating).
251 public void disconnect()
252 throws RemoteException
254 desk.frame.talk(Color.red, "The partner leaves the game.");
255 partner = null;
256 set_current_state(DISCONNECTED);
258 desk.frame.taUrl.setText(desk.frame.mior);
262 * Receive friends move (possible at I_WAIT_FOR_YOUR_MOVE).
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.
269 public void receive_move(int x, int y, Point[] victory)
270 throws RemoteException
272 // The state changes are handled by the PlayingTable
273 desk.friendsMove(x, y, victory);