In this programming assignment you will:
- Write a client-server application
- Communicate using HTTP
- Design a messaging standard
In this lab you will write a distributed implementation of the Battleship game. We will use the standard 10×10 variation of the game. Here is an online implementation of the Battleship game. Note that the ships in that implementation have slightly different names.
Our implementation will be based on a symmetric client server architecture, where each player has both a server and a client. The server keeps an internal state of the game and issues replies to the other player’s client.
The first step before the game begins is the setup of the board, according to the rules of the game. We will represent the board with a character array, where
_ represents water and Carrier, Battleship, cRuiser, Submarine, and Destroyer fields are represented by
D respectively. For example, your board might be set up as follows:
CCCCC______x000D_ BBBB_______x000D_ RRR________x000D_ SSS________x000D_ D__________x000D_ D__________x000D_ ___________x000D_ ___________x000D_ ___________x000D_ ___________x000D_
You will save your board as
To play the game, your implementation needs to exchange two types of messages –
fire message needs to communicate the grid location of salvo. The
result message needs to communicate whether the salvo was a hit, a sink, or a miss.
fire message will be represented as an
HTTP POST. The content of the fire message will include the targeted coordinates as a URL formatted string for Web forms, for example 5 and 7, as:
x=5&y=7. Assume that coordinates are 0-indexed. So, assuming that your opponent’s server runs at
fire message is a
POST request sent to
result message will be formatted as an HTTP response. For a correctly formatted
fire request your reply will be an
HTTP OK message with
hit= followed by
1 (hit), or
0 (miss). If the hit results in a sink, then the response will also include
sink= followed by a letter code (
D) of the sunk ship. An example of such a reply is
If the fire message includes coordinates that are out of bounds, the response will be
HTTP Not Found. If the fire message includes coordinates that have been already fired opon, the response will be
HTTP Gone. Finally, if the fire message is not formatted correctly, the response will be
HTTP Bad Request. For your reference here’s a link to the different HTTP response status codes.
Your server process should accept a port parameter, on which a client can connect, and the file containing the setup of your board, eg.
python server.py 5000 own_board.txt.
Your client process should accept the IP address, the port of the server process, and the X and Y coordinates onto which to fire, eg.
python client.py 184.108.40.206 5000 5 7.
The client will be invoked multiple times during the game.
Internal State Representation
fire message the server should update the state of the player’s
own_board.txt (whether a player’s ship has been hit and where). Following each
result message the client should update the record of the player’s shots onto the opponent’s board, represented internally as
opponent_board.txt. A player should be able able to visually inspect their own board and their record of opponent’s board on
http://localhost:5000/opponent_board.html respectively. It is up to you how you visually represent the state of each board.
Take a video less than 5 min to show that how the game work and how the code work.