Java Reference
In-Depth Information
272
273 getChildren().add(ellipse); // Add the ellipse to the pane
274 }
275 }
276
277 /* Handle a mouse click event */
278 private void handleMouseClick() {
279 // If cell is not occupied and the player has the turn
280 if (token == ' ' && myTurn) {
281 setToken(myToken); // Set the player's token in the cell
282 myTurn = false ;
283 rowSelected = row;
284 columnSelected = column;
285 lblStatus.setText( "Waiting for the other player to move" );
286 waiting = false ; // Just completed a successful move
287 }
288 }
289 }
290 }
mouse clicked handler
The server can serve any number of sessions simultaneously. Each session takes care of two
players. The client can be deployed to run as a Java applet. To run a client as a Java applet
from a Web browser, the server must run from a Web server. FiguresĀ 31.15 and 31.16 show
sample runs of the server and the clients.
F IGURE 31.15
TicTacToeServer accepts connection requests and creates sessions to
serve pairs of players.
F IGURE 31.16
TicTacToeClient can run as an applet or standalone.
The TicTacToeConstants interface defines the constants shared by all the classes in the
project. Each class that uses the constants needs to implement the interface. Centrally defining
constants in an interface is a common practice in Java.
Once a session is established, the server receives moves from the players in alterna-
tion. Upon receiving a move from a player, the server determines the status of the game. If
the game is not finished, the server sends the status ( CONTINUE ) and the player's move to
 
 
Search WWH ::




Custom Search