Project 7: Battleship

  < Previous

Note: This project is optional and will only improve your grade in the projects category.

Assignment id: project7
Required Files: project7.py, rubric7.txt

Due Date

See the calendar for due date.
Note: Late projects will not be grade.

Objectives:

Description:

For this project, you get to write a Python script that will simulate a one-sided version of the game Battleship. Battleship has five different kinds of ships:
Name Number of hits
Battleship 4
Carrier 5
Destroyer 3
Frigate 2
Submarine 3
Tender 2

Your script needs to request the name of a file with the location of each ship. The format of this input file is as follows: (each item is separate by a TAB-character)
shipName	startRowIndex	startColIndex	endRowIndex	endColIndex
...
You can assume that all six ships will be specified in a file and that they will start at the indicated coordinate and end AT the specified coordinate. Track the location of all of the ships using a two dimensional list (the "ocean"). When displaying the "ocean", use just the first letter of each ship. For example, given the locations file project7-input-shipLocationsA.tab:
Battleship	0	0	0	3
Carrier		0	9	4	9
Destroyer	9	7	9	9
Frigate		8	0	9	0
Submarine	2	2	2	4
Tender		2	7	3	7
your script should display the "ocean" as follows:
  0123456789
0 BBBB     C
1          C
2   SSS  T C
3        T C
4          C
5           
6           
7           
8 F         
9 F      DDD
Additionally, your script should request the user to also input the name of a file with the shots for a single player. Each shot is recorded on it's own line by specifying the row and column of the shot: (each item is separate by a TAB-character)
rowIndex	colIndex
...
Your script needs to display the "ocean" after accounting for each shot. If the shot hit a ship, indicate this by using a lowercase letter for the ship at that location. If the shot sunk a ship, display that information as well.
Additionally, you need to use a main() function and at least one single dimensional list.

Examples

Example input files: Example output files:

Submission

Submit your python script and rubric using the handin program. For handin, for this project, type the following in a terminal window exactly as it appears:
handin  project7  project7.py  rubric7.txt
To verify your submission, type the following in a terminal window:
handin  project7

Rubric:

Points       Item
----------   --------------------------------------------------------------
_____ / 13   Meaniful comments (at the beginning of the file, before each function, etc.)
_____ / 15   General good programming practice:
             + Properly handles input file (e.g., handles IO exceptions, close the file)
             + main() function
             + Single and two-dimensional lists (specific to this project)
_____ / 15   Accurately displays the ocean
_____ / 35   Accurately executes the commands in the shots file
_____ /  2   Completed rubric (estimates for each line including hours spent)

_____ / 80   Total


_____  Approximate number of hours spent

Helps

  1. Before writing any code, write the comments first!
  2. Write the code iteratively. For example, write the code to get the ships stored to their respective positions on the "ocean". ...

Notes

  1. Optionally, you can replace typing from the keyboard with the contents of a file. For example, try on ranger:
    (echo "project7-input-shipLocationsA.tab"; echo "project7-input-shotsA.tab" ) | python3 project7.py