Game Development Reference
In-Depth Information
startMessage.setGameId(game.getId());
startMessage.setGameStatus(Game.GAME_STARTED);
server.broadcast(Filters.in(connsForGame),
startMessage);
3. After this, we decide which player will have the first move and send TurnMes-
sage to the players, as follows:
int startingPlayer = FastMath.nextRandomInt(1, 2);
TurnMessage turnMessage = new TurnMessage();
server.broadcast(Filters.in(connsForGame),
turnMessage);
return game;
}
4. Now, we need to define TurnMessage . It is another simple message, only con-
taining the ID of the player whose turn it currently is and extending GameMes-
sage .
5. Back in ServerMessageListener , we make it ready to receive FireAc-
tionMessage from a player. We begin by verifying that the playerId of the
incoming message matches with the current player on the server side. It can be
implemented as follows:
if(m instanceof FireActionMessage){
FireActionMessage fireAction = (FireActionMessage)
m;
int gameId = fireAction.getGameId();
Game game = gameServer.getGame(gameId);
if(game.getCurrentPlayerId() ==
fireAction.getPlayerId()){
6. Then, we call applyMove on the game, letting it decide whether it's a hit or not.
If it's a hit, the ship will be returned. It can be implemented by typing the follow-
ing code:
Ship hitShip = game.applyMove(fireAction);
7. We go on and create a FiringResult message. This is an extension of
FireActionMessage with additional fields for the (possible) ship being hit. It
Search WWH ::




Custom Search