The Server

The server is implemented by two classes in the sw4.server package: Server and Player.

Server accepts incoming connections and spawns an instance of Player to represent each. Every two connections are paired off to play a game against each other. There is one game per connection; if a client wishes to play again she must make a new connection to the server.

The Player classes start threads which read messages from the network and place them in Server's message queue. Server then processes and responds to the messages in sequential order.

Each Player has a bit field for permissions. The Server uses this to determine what commands a player may execute at any given time; for example, after connecting a player may only place ships. Once all ships are placed, placing ships is forbidden and other commands are allowed depending on whose turn it is.

The Server maintains the current state of the game world with an instance of the Board class (from the sw4 package). It keeps track of the status of each square (has been shot, is visible by player 2, etc.), where both player's ships are. Server inverts the coordinates coming to and from the second player, so that each player sees the world with their ships starting at the bottom of the board.