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 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.
1234567890 A B --- C * D * E F ++++ G H I 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.
So that you can communicate with other programs the following messages must be used:
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.
/u/cs217/bin/submit 8 source-files Makefile READMEOnly 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!!).