This was a final year university assignment for our “Network Programming” module. We were required to produce a simple network-capable application with a suitable programming language such as C#. I chose to make Battleships in Java, a language that I was unfamiliar with and therefore had to learn as I went along. I received a “B+” grade overall for my work on this module.
This post continues with the technical details of this project.
The module focus was on back-end implementation rather than front-end application design. My application featured a TCP socket connection that was configured for non-blocking communication. The system used a message-based protocol to pass instructions between the server and clients, and the game logic was all handled by the server, in order to decrease the likelihood that a client could manipulate it maliciously. The main responsibility of the clients was simply displaying the graphics, and they also featured an automatic profanity filter for the consideration of minors using the game. This doubled up as an additional security measure against illegal commands being used to influence the server as well.
The server could support any number of clients, and alternately assigned them to two opposing teams. Gameplay instructions are handled on a first-come-first-served basis. I found it unfortunate that the module format did not cater for a more sophisticated application design than that.
Checking the TCP socket for message events is done with a separate thread that loops continuously . The selector handles incoming events by placing them in a queue, where they are processed sequentially by the main thread. This guarantees that events will always be processed in the same order that they arrive, and that the main thread of execution is never held up by socket operations (hence it is “non-blocking”). The use of TCP also guarantees that those messages will arrive intact, since it is a connection-based protocol (meaning that for every message sent, an acknowledgement must be received back in order for communication to proceed).
The gameplay logic is structured with a state-machine. The states are Waiting, Lobby, Launch, Setup, Playing and Closing, and the server would behave appropriately based on the current state. The rest of the gameplay is exactly how you would expect, a turn-based guessing game implementing the official rules (as described by Hasbro) of Battleships.
Before I release an executable of this application, I would like to add some polish first. For example, the module did not cover much in the way of handling exceptions due to timeouts or other network difficulties. I would also like to improve the front-end’s usability before making this game available to play.