Game Development Reference
In-Depth Information
6. The last message to be handled by the class is the FiringResult message.
This calls applyMove on the game object. Some kind of output should be tied
to this message telling the player what happened. This example game uses Sys-
tem.out.println to convey this.
7. Finally, initialize our ClientMessageHandler object in the constructor of
the client class, as follows:
ClientMessageHandler messageHandler = new
ClientMessageHandler(this, game);
client.addMessageListener(messageHandler);
With the received messages handled, we can look at the logic on the client side and the
messages it sends. This is very limited as most of the game functionality is handled by the
server.
The following steps show how to implement the client-side game logic:
1. The placeShip method can be written in many different ways. Normally, you
will have a graphical interface. For this recipe though, we use a command
prompt, which breaks down the input to x and y coordinates and whether the ship
is placed horizontally or vertically. At the end, it should send five instances of
PlaceShipMessages to the server. For each added ship, we also call
thisPlayer.increaseShips() .
2. We also need a method called setMyTurn . This uses the command prompt to
receive x and y coordinates to shoot at. After this, it populates FireAc-
tionMessage , which is sent to the server.
3. For PlaceShipMessage , create a new class and have it extend GameMes-
sage .
4. The class needs to contain the ID of the player placing the ship, coordinates, and
orientation of the ship. The ID of the ship refers to the position in the following
array:
private static Ship[] ships = new Ship[]{new
Ship("PatrolBoat", 2), new Ship("Destroyer", 3), new
Ship("Submarine", 3), new Ship("Battleship", 4), new
Ship("Carrier", 5)};
Search WWH ::




Custom Search