Now that you know how to play' the game, it is time to examine the Source code, for the game Since several of the classes are quite long, we will sprinkle throughout the code rather than leaving the code till the end.


The APPLET tag for this game is simple. Just name the main class and set the size. That's it. There aren't any <param> tags for Scrabblet, Remember, the bigger you make the applet, the nicer the board looks. The  ratio should be a little taller than it is wide.
The main applet class if, found in, At almost 300 lines, this is a fairly
complicated applet class, even though most of the game logic is left to the Board class,  found later in this chapter. We start with the usual collection of import statements, loading almost every standard Java package. Then we declare Scrabbler to be subclass of Applet that implements ActionLISTERNER.

I import java.awt.event
import java. applet
public claGs Scrabblet extends Applet implements ActionListener  . Next comes the declaration of a large collection of instance variables. The server is .our connection to Ute web server running our game server. This machine's name is stored in serverNamc. The bag represents .the shared bag of letters for our game. Our opponent has his or her own copy of the bag, which is initialized with the same
random sequence of tiles so the two bags stay in synch. the board is our copy of ,the
playing board. Our opponent also has a copy of the board, and the game keeps them in synch after each turn. If the network server can't be accessed, the single flag is set, and the apple   players the game in single-player mode. The boolean our turn is true whenever it is our turn to play. If a player can't find a valid word, the player can pass by clicking on pone twice in a row without having !lny tiles on the board. The seen pass variable is used to mark  if the first Done has been clicked on. .• To manage the synchronization of the remote player's board, we keep a copy of the tiles selected in theirs. Seeing what the other person has in his or her tray is cheating. so no hacking this applet to display the contents of theirs! The two strings, name and others_name, hold our name and our opponent's name, respectively.

private Server Connection server;
private String server Name
private Bag bag;
private Board board;
private bool an single'= false;
private Boolean ourturn;
private boof an see pass  false;
private Lett'tlrtheirs[] .=:new Letter[7];
private String name;
private String others-Aame;

Next, we declare eight variables used to manage the user interface. These are all AWT components that must be manipulated by the applet in som~ way. top Panel
. holds. the prompt and the name field for getting the user's name at start-up. The done button is used to signify that you are done with your turn, The chat Tex tField is used to enter chat message id List is used to display available opponents. The challenge button is used to attach us to our opponent. The lean Canvas holds the name and copyright Notice at start-up.

T'he init() method-is called once and simply Sets up the BorderLayo\4t, figures out
what Internet host the applet came from, and.creates the splash screen canvas.

start{ )
'The start() method Is.called whenever the browser redisplays the page in which the
applet is found. The large try block at the beginning is used to catch the case where the network conaection fails. if we succeed in making a new ServerConnection and we've never run "''''art() before, we then sodUP the screen to prompt for the user's name  While we are there, we put the splash screen, ican, in the center of the window. In the case where name is not null,~hat means the user Jeft the page and has now returned. We presume we've already gbt the user's name and jump right to namefinteredl ) the method that is calledwhrn ~!leuser types return in the name entry field. The nlidate( ) at the end makes sure all of the AWT components arc updated properly. Uan exception was thrown, we presume that the net connection failed and go into' single-player mode. The call to start..,.game() gets things 'rolling.

The stopt ) method, is called whenever the user leaves the page with the applet. H'ere, we just tell the server that we've left, We re-create the network connection in start() if the user returns to the page later.

The add() method is called by the ServeJConneclion whenever a new player enters
the game We add the player's name to our Lbt object. Pay special attention to the
formatting of the starting. We use that later to extract certain IDS from the list.

delete( )
The Delete( ) method is called when a player no longer wants to be identified. as available for play. This happens when a player quits or decides to play with someone else, 'Here, we hunt down the id string in our list by extracting the values inside parentheses. U there are no more names on the list (and we aren't playing the game already:  then we display a special message telling the user to hang out .~W someones to make a challenge.

The challenger ) method is called by the ServerConnection whenever another player
challenges  to a game. We could' have made this method more complicated, so that it would prompt the user 11;> accept or refuse the challenge, but instead the challenge is automatically accepted. Notice that the random seed we use to start the game is passed back to the other player in the accept( ) method. This is used by both sides to initialize the random state of the tile bag to ensure a synchronous game. We call server.deletet ) to ensure that we care no longer solicited by other players wanting to play against us. Notice also that we cede the starting turn to the challenger by setting ourtum to false.

accept( )
accept( ) is the method called on the remote side in response to the server.acceptt )
call just mentioned, Just <IS the other player deleted hims,elf .or herself from the list of.available players, so must we call server.delete( ). We take the first turn by setting our turn .

Share This