Game Development Reference
In-Depth Information
How it works...
When a player launches the client, it will automatically connect to the server defined in the
properties file.
The server will acknowledge this, assign a user ID to the player, and send back Wel-
comeMessage containing the ID. The job of WelcomeMessage is to confirm the con-
nection to the client, and let the client know its given ID. In this implementation, it is used
for future communication from the client. Another way of filtering incoming messages
would be possible using the HostedConnection instance, as it holds a unique address
to the client.
When the first player connects, a new game will be created. The game is put in the
WAITING status until two players have connected, and both have placed their ships. For
each player connecting, it creates a GameStatusMessage letting all players in the game
know the current status (which is WAITING ) and any player information it might have. The
first player, PlayerOne , will receive the message twice (again when PlayerTwo con-
nects), but it doesn't matter as the game will be in the WAITING status until both players
have placed their ships.
The placeShip method is simplified and doesn't contain all the verification that you will
normally have in a full game. Make sure that the the server checks whether a ship is outside
the board, or overlapping, and make sure it's of the right type, length, and so on and send a
message back if it is wrong. This method simply checks that the ship is inside bounds and
skips it if it isn't. Verification can also be done on the client, but to limit exploitation, it has
to be done on the server as well.
The starting player will be selected randomly and sent in a TurnMessage to both players
stating who begins. The player is asked to enter a set of coordinates to fire at and
FireActionMessage is sent to the server.
The server verifies the player and applies it to the board. It then broadcasts a FireRes-
ult message to all players with information about the action, and whether any ships are
hit. If the attacked player still has ships left, it becomes his or her turn to fire.
Once a player has run out of ships, the game ends. The server broadcasts a message to all
the clients and disconnects them.
Search WWH ::




Custom Search