Due Sunday, Feb27, 2005 before midnight
The Battleship game
In this assignment, you will need to implement a simple object-oriented version of the Battleship game. Hopefully you have all played it sometime, if you haven't you may want to take a look at the game before attempting to write the classes. The following description should give you a rough idea about what the game is about. Since this is a group assignment, I am going to give it in two parts - the first part, due right after the midterm, would be based on standard object-oriented design, and the second part (details TBA) would extend the first part using inheritance.
Description
Battleship is usually a 2 player game. Each player has a two dimensional board, with sections (like a chess game) that represent different portions of a see where a naval battle is taking place. Each player has a fleet containing a number of different types of ships, which are placed in his/her board. Each player only sees his/her board, and tries to guess where the other player's ships are.
The game is played in turns, where the player with the current turn shoots 3 shots at different parts of the opponent's sea. If any of the opponent's ships are hit, they are damaged, and the player earns points. The objective of the game is to win the war by destroying all of your opponent's ships before your opponent destroys yours. The game ends when either your or your opponent's ships are all destroyed. It is possible to end the game earlier after each round as wwell, by asking the player whether the game should continue.
We are going to start with a simple version of this game, but continue through the semester to progressively make it more and more complex. You will do a design for the complete game, but initially our requirements would be simple.
For the assignment, you will assume that there are 4 types of ships (Airplane carrier - A, Destroyer - D, Submarine - S, and Warship - W). To keep things simple, each ship only occupies one cell of the board. Also, to keep things simple, we will assume that one hit on any ship will destroy that ship. However, points are earned differently by distroying different types of ships - 50 points for a destroyer, 75 for submarine, 100 for a warship and 125 for airplane carriers.
You will take turns (play with another team - its more fun!) where you will take your opponent's shots and create your own.
You will need to write the code for Battleship.java, which will
be the main class for your program (and will have the public static void main,
and run methods). You may create as many classes as you need, and submit all
the java files that you have created, but none of the class files that are provided.
Please follow the convention that only class names start with a upper case letter. Points will be deducted if you do not follow the convention.
As a good programmer, you should DOCUMENT your program (i.e. with meaningful variable names, comments, etc.)! Points will be deducted if you do not document your program. Please use the commenting and coding conventions discussed in class as much as possible.
What to submit: You will need to create a word document called Battleship.doc
with the class diagram for your assignment. You only need to have the class
diagram, and not any sequence diagrams. However, the class diagram must be complete,
it should have all the classes, and their attributes and methods, and the associations
between them. This will count for 25% of your points, and even if your code
does not work, a good class diagram will make you elligible for partial credit.
Remember that since this assignment is a group assignment, and the basics of the assignment are fairly simple, I would require a thorough job in the design and implementation. Every team member must participate as equally as possible. At least one class (or a significant number of methods of a class) must be developed by each member, without extensive assistance from other members. Members who feel somewhat lagging in programming skills can help in documentation and testing of the code. Please make only one submission per group. Submission requirements are as follows:
I/O Convention: Since the submission system will explicitly test your program, you will have to stick to the general I/O guidelines described here. This means you will have to accept the input in the format described here. You may choose to use your imagination in generating the output, but you cannot change the type of input the program receives. The following is a sample run of the program as I wrote. Feel free to change the prompts any way you like. However, pay special attention to the user inputs (indicated below in red and bold). You must accept input in exactly this format.
C:\>java Battleship Usage: java Battleship [self|opponent] C:\>java Battleship opponent Setting up my board.. My board is: 01234567 0 S..D....
1 ..W.....
2 ...A.S.A
3 ......AA
4 D.....DS
5 ....S...
6 DWS.A.W.
7 ...D..WW Opponent's turn. Opponent's moves: shot 1 (x): 3 shot 1 (y): 5 shot 2 (x): 3 shot 2 (y): 6 shot 3 (x): 3 shot 3 (y): 7 Opponent's hits: A: 2, D: 0, S:0, W: 0. Opponent's score is 250 My remaining ships: A: 3, D: 5, S: 5, W: 5. My board is: 01234567 0 S..D....
1 ..W.....
2 ...A.S.A
3 ......XX <--- X denotes a shot
4 D.....DS
5 ....S...
6 DWS.A.W.
7 ...D..WW Continue playing? yes My turn. My moves are: (1,1), (1,3), (1, 5) My hits: A: 0 D: 0 S: 0 W: 1 My score is: 100 Opponent's remaining ships: A: 5, D: 5, S: 5, W: 4. Continue playing? yes Opponent's turn. Opponent's moves: .... Opponent's remaining ships: A: 0, D: 0, S: 0, W: 0. Opponent's ships are all destroyed, I win! My score is: .... Opponent's score is: ....
RandomIntGenerator.class documentation The following are the public properties that you need to know about this class for this assignment.
public RandomIntGenerator(int l, int h)
This constructor takes in two integers for lower and uppper bounds. All random numbers generated are in this range.
public int draw()
This method draws a random number from the range that was constructed, and returns it.
RandomIntGenerator mygenerator = new RandomIntGenerator(5, 20); int nextnumber = mygenerator.draw(); nextnumber = mygenerator.draw();
Note that, as shown above, you will only need to allocate two instances of this class, and keep drawing from the two instances. DO NOT instantiate more than twice.
Also, only allocate one instance of the Input class.
Submission method You will need to submit your assignment using the online submission system. To submit, use the following steps: