CSCE 2004 - Programming Assignment
Optional Extra Credit
Due: Friday, April 15, 2011 at 11:59pm

1. Problem Statement:

In this assignment, you will implement the well-known board game Battleship in C++. The version that you will develop will be a modified version of the standard Milton Bradley game -- modified both to suit our purposes, and to be simpler in some cases. If you are unfamiliar with the way the game is played in general, please click on the above link to read more. The following description will only identify modifications of the standard game (of course, you may ask your TA any questions about how the game works as you need.)

To begin, download the source code template from the front page of the class website. You may either write this entire program from scratch, or you may use the template to assist you. The template is a fully working program of a simpler version of the game than you are submitting. If you choose to use the template, you will need to modify it heavily (however you see fit) to meet all of the objectives described below.

The version of Battleship that you are designing will have the following features:

  1. If this is the first time a user plays the game, your program will generate a randomized ship placement. In other words, for each new game, the user should experience new, unknown locations for each ship. If the user is returning to the program, they will have the option to load an old, partially completed game (see below for more details).
  2. The game will be a single player game. That means, the player will not be waiting for another party to take a turn. Instead, they will take consecutive shots until they either win or quit.
  3. In your program, seven ships will be deployed. One ship of length 5, one ship of length 4, one ship of length 3, one ship of length 2, and three ships of length 1.
  4. The world the ships are deployed in is an 8x8 grid. To support different types of ships, you should use different characters to represent each type. (For example, use 'B' to represent squares that the Battleship occupies, 'C' for the Carrier, etc.)
  5. Note: When placing ships randomly, first randomly select a "starting" location for the ship. Then, randomly select whether to orient the ship Up, Down, Left, or Right, and check whether it can be placed in that direction successfully. If not, try the whole process again.
  6. When the user wins a game, the program will ask if the user wants to start a new game.
  7. The user has the option to end the current game after any shot fired. If the user chooses to end the game before achieving victory, the program will ask the user if they would like to save the current game. If so, your program will use File I/O to save the entire state of the current game in a format of your choosing for later use.
  8. When the user starts the program, the program will ask for the user's name, and then check for any previously stored games. If a game file associated with that user is found, the program will ask if the user wants to continuing playing the game. Then, based on the user's response, the program will either load the game and begin, or randomly generate a fresh game and begin.

When a game is in progress, the program will take inputs from the user as coordinates between 0 and 7 for the x-axis and 0 and 7 for the y-axis. The program will then check the current game board and return one of four possible responses:

  1. HIT: The user hit a new ship location.
  2. MISS: The user chose a new location that did not contain a ship.
  3. DUPLICATE SHOT: The user chose a location that had been previously tried.
  4. SUNK: The user HIT with their choice of location AND the location hit was the final location for a given ship, so the ship is now SUNK. The displayed message should also identify to the user which ship was sunk (e.g. "You sunk my Battleship!").
The game will end when all the ships have been sunk and it will output the number of rounds the user took until he/she was able to complete the game (including any rounds taken before a game save).

2. Design:

In order to effectively design this assignment you need to build on knowledge attained since the beginning of the semester. You may use any design you like (you will be graded on your choices), but in particular, your program should make excellent use of modular, function-based design. Here are some additional, suggested hints to get you started:

  1. No two ships are allowed to overlap.
  2. Similarly, no ship is allow to exist either fully or partially outside the 8x8 grid world.
  3. Start by placing the largest ship first.
  4. Keep track of each ship's hits separately so that you can report when a particular ship has been sunk.
Finally you, as the programmer, must think of every possible error. You should also perform enough testing that you are confident your program is bug-free. For example, do not let ships be placed over one another, or off the side of the world, etc.

3. Implementation:

Start your program with comments based on your design and (the following cannot be emphasized enough..) add portions of code a little at a time. Compile and run your program on a regular basis, so you always have something that runs, even if it only does part of the job. Make sure you have completed the first task, e.g., printing your name to the screen, before you start writing code for the second task.

4. Testing and Debugging:

Test your program to check that it operates correctly for all of the requirements listed above. Also check for the error handling capabilities of the code. For debugging purposes, it is useful to "echo" any variables you input, and to print intermediate results as they are calculated.

You should comment out your debugging code before you hand in your final version of the program.

5. Documentation:

When you have completed your C++ program, write a short report (less than half of a page long; a few sentences will suffice) describing what the objectives were, what you did, and the status of the program. Does it work properly for all test cases? Are there any known problems?

Include this report in the comments at the top of your source code file.

Finally, when you are finished, run your full program from start to finish. Copy/paste the entire screen output from at least one full run of your program at the bottom of your source code. Use comments around this output (that is, /* and */) to ensure your program still compiles as-submitted after this addition.

Note: Since an entire game of Battleship can be quite long, you may just take a screenshot of the game in action (include as much as you can in the picture), and submit it as an attachment rather than copy/pasting screen output as described above, if you choose.

6. Project Submission:

To Submit Extra Credit Homework:

  1. Begin an email to your TA with the exact title "CSCE 2004 EXTRA CREDIT".
  2. Copy/paste the contents of your source code file into the body of the email.

    (Note: Do not add anything extra to the body of the email! And make sure your FULL name and uark ID number are printed out at the beginning of your program...)

  3. Send the email!

The date/time-stamp on your email will be used to verify that you met the due date above. Since this is an extra credit opportunity, all late projects will ZERO credit. You MUST submit your assignment on time to receive any points, so hand in your best effort on the due date.