COS 217 Fall 1998. Final Project - Computer Battleship

COS 217: Introduction to Programming Systems. Spring 1999.

Final Project - Computer Battleship

In this programming assignment, you will work in groups of one (not advised) , two or three to implement a working Battleship program. The primary focus of this project is to learn how to design effective interfaces so that each member of the group will be able to work independently on a component of the project.

Your Battleship program will serve as one player in a game. It must be able to play against another instance of itself, against any other Battleship program created by a group from CS217, or against a human player using a simple command line program. Communcation between the programs will be implemented using a simple sockets library that will be discussed later.

The Game

The game is played on a 10x10 grid that is indexed 1-10 across and A-J down. A legal space on the grid is a row/column pair such as A4 or B9. For simplicity abbreviate 10 as 0. So H10 is simply referred to as H0. Each player has two 10x10 grids, one is called "lower" and the other "upper". The "upper" grid is used to record information regarding shots you have fired, and the "lower" grid is used to record the location of your ships, and shots fired by the opponent.

To setup the game each player must clear the upper grid and place 4 ships on the lower grid. The ships have lengths 2, 3, 4, and 5. They must be placed either horizontally or vertically on the grid and must not intersect. The following is an example of a correct ship placement.

B      ---
C  *	   
D  *	  
F     ++++
J   ===== 

Once both players have placed their ships, the game begins.

In each round of play one player is called the attacker and one the defender. The attacker selects a location and announces it to the defender. The defender must reply "HIT" if that location contains a ship on the defender's lower grid and "MISS" if that location does not contain a ship. The defender must also report "SUNK" if that location represents the last non-hit location of a ship. This attack sequence is recorded by placing a peg a attacker's upper board and one in the defender's lower board. The peg is red if a hit occurred and white if a miss occurred.

Game play continues, alternating who is the attacker and who is the defender until one player has all ships sunk. The player who still has ships remaining is declared the winner.


The battleship program must contain two 10x10 grids that correspond to the upper and lower grids of one player in the game as described above. The program must do the following.

You must allow either the computer to play the game or a human. To support computer play you must design two algorithms, one for placing the ships and one for selecting a location to attack. Both algorithms should be reasonably effective. Simply selecting random attack locations is not sufficent. Hitting Contol-C should switch between computer and human play. There should be an option when queried for a location to switch to computer play. This means that at any time, the play mode can be altered.

At each turn, the program should output the status of both the upper and lower boards of the player. This can be done as text or as graphics. Extra credit will be awarded to an implementation that uses graphics to display the current state of the board.


The Battleship program that you write must be able to play the Battleship program created by any other group in the class. Our advice is that you in fact do this with at least two groups. You will be able to communicate between your Battleship program and another one using Sockets. A simple sockets library, which includes files simpsock.c and simpsock.h, is currently available in /u/cs217/8. It provides you with three functions.

See simpsock.h for a description of these functions. There is no need to copy these files to your directory. Just set up your makefile appropriately.

So that you can communicate with other programs the following messages must be used:

The messages SUNK and DONE should be sent on their own. If a hit results in sinking a ship, and the game is not finished, SUNK should be sent, not HIT followed by SUNK. Similarly, if a hit results in sinking the last ship, only a DONE message should be sent.

Grading: Grading will be out of a possible score of 10. If extra options are implemented, a score above 10 can be acheived. ALL members of the group will get the same grade, so make sure that each person does his/her fair share. An important part of the grading for this assignment will be your use of modules and interfaces. Be sure to include a description of how you chose to divide the problem, and the interfaces. Each person should be able to code their portion using only the header files provided by the other two members of the group, with no knowledge of how these functions are actually implemented. To judge effectiveness, and for fun, a tournament will be held for grading, where different programs will play against each other.


Submit your program electronically with the command
/u/cs217/bin/submit 8 source-files Makefile README
Only one person in the group needs to submit the files. In the README file please designate the people that worked on the project and what part each person implemented.

Due: Dean's Date, May 11, 4:59pm (NOTE TIME!!).

Back to the COS 217 home page