Exercise 5: Play with friends
(20 minutes)
The game we have created allows you to compete against the default player mode that runs on your mobile phone.
In this exercise we will extend the game to allow multiple players using a web service that will let you exchange
moves with an opponent on another mobile device. The Java ME Web Service API (JSR 172) support in NetBeans will
help us to add this extended functionality to our application.
Steps to Follow
In this step we will deploy and start a Web Service to enable multi-player gaming. For testing purposes you will
use your own Web Service, running locally.
-
We will start by opening a WebApplication project which contains the web service used to enable
multiplayer gaming on the web.
Open the project by choosing File > Open Project. A filechooser dialog appears.
Select the BrickGameWebApplication project in the <lab_root>/solutions/exercise5/.
The opened project appears in the Projects window of the IDE.

-
Right-click the Project node and choose Deploy from the contextual menu.

The Application Server should be started automatically and the Web Application project should be deployed.
An error message dialog and error message in the Glassfish output window are displayed if there is a problem.
In this exercise you need some additional files we prepared for this lab.
To use these files, we are going to open the project that contains them.
-
Open the project by choosing File > Open Project. A filechooser dialog appears.
Select the MobileGame project in the <lab_root>/exercises/exercise5/.
The opened project appears in Projects window of the IDE.
-
Note that a New Player implementation appeared in logic.players package:
WebPlayer - Player implementation that will use the Web service client to
send your moves to the web service and load the opponent's moves.
Note that the code is not yet compilable because it stilll needs the web service
we are going to create.

In this step you create a new project configuration that allows you to have two versions of the same project:
- The online version of the game
- The offline version of the game
-
Right click the MobileGame project node and cheoose Properties from the contextual menu.

-
In the Project Properties dialog, select
Add Configuration... from Project Configuration drop down menu.

The Add Configuration dialog opens.
-
Name the new configuration OnlineGame and Click OK.

-
The new project configuration is created and currently the active configuration.

-
In the Mobile Game configuration dialog select Abilities from the Category menu.
Deselect the Use Values from DefaultConfiguration option.
-
Click the Add button.
In the Add Ability dialog type ONLINE as the name and true as the value and
Click OK.

-
Click OK to close project properties.
We will generate a Web Service Client using the wizard provided by NetBeans Mobility.
-
Right-click th eproject node and choose New > Java ME Web Service Client... from the contextual menu.

-
In the WSDL URL field type http://localhost:8080/BrickGameWebApplication/BricksWebServiceService?wsdl and click the
Retrieve WSDL button.

After loading and validating the WSDL file some of the the Java ME Web Service Client Information fields are filled and the Local filename is displayed.

-
In the Client Name field type BricksWebService and name the Package wsclient.

-
Press Finish and Web Service client stub is generated. Note that the logic.player.WebPlayer.java is no
longer broken. If it's still broken, there may be a typo in the Web Service Client name or package.

We will use WebPlayer in this lab. It generates a Web Service Client that displays player moves with the opponent playing on the other device.
First we have to initialize the game.
-
Open the logic.BricksLayerManager.java file in the editor.
-
Add the following attribute and method code to the BricksLayerManager class
private BricksWebServiceService client = null;
public BricksWebServiceService getMobileClient() {
if (client == null) {
client = new BricksWebServiceService_Stub();
}
return client;
}
-
Find the initPlayers() method.
Update the method so that it uses a different behaviour depending on the selected project configuration by
replacing the four lines where the players are initialized.

Replace the existing code with the following:
try {
String id = getMobileClient().initPlayer("");
int order = 0;
while ((order = getMobileClient().startGame(id)) == 0){
myWaitingForOpponent = true;
Thread.sleep(500);
}
myWaitingForOpponent = false;
if (order == 1) {
myPlayer1 = new OwnerPlayer(dukeSprite, dukeSpriteAnimator, myGameInfo);
myPlayer2 = new WebPlayer(jamesSprite, jamesSpriteAnimator, myGameInfo,
getMobileClient(), id);
} else {
myPlayer1 = new WebPlayer(dukeSprite, dukeSpriteAnimator, myGameInfo,
getMobileClient(), id);
myPlayer2 = new OwnerPlayer(jamesSprite, jamesSpriteAnimator, myGameInfo);
}
} catch (RemoteException ex) {
myCanvas.errorMsg(ex);
} catch (InterruptedException ex) {
myCanvas.errorMsg(ex);
}
The initPlayers method should now look as follows:

Please note the following conditional attributes in the pasted code:
- If the
ONLINE ability exists and it's value equals true, the first part will be used.
In this part the Player is registered in the service using the getMobileClient().initPlayer("") method.
The game is then started with the getMobileClient().startGame(id) invoked in case there is no opponent.
Typically it is better to initialize the game in a separate thread to avoid conflict with the game UI. But in order
to simplify this exercise and reduce the amount of code in the lab this proceedure has been skipped.
After the opponent appears and the game is initialized the Players are connected to Sprites depending on the playing order chosen.
-
Notice that that code in the else clause is the same as it was before.
If the game application is not online this part of the code will be compiled and used. This means that you
must Fix Imports using the contextual menu for this use case.
-
You can now run the project with two "remote" players.
Right-click the MobileGame project and choose Run from the contextual menu. Repeat this action
to launch a second instance of the game. Two emulators should have started.
You can play for both players by switching between the emulators

The solution for this exercise is provided as a "ready to build and run" NetBeans project,
<lab_root>/solutions/exercise5/MobileGame_solution5.
Troubleshoting tips
- If you are not able to access the web service WSDL, check your network settings. Sometimes a firewall
or antivirus shield can prevent you from connecting to a web service.
- If cannot reach the webservice URL, you can deploy the webservice to an application server on your own machine.
The project with the webservice already configured is in the <lab_root>/solutions/exercise5/ directory.
The name of the pre-configured project is BrickGameWebApplication.
Back to top
To summary