CS 1331 Homework 2 - Battleship

Introduction

In this programming project you will practice:

Problem Description

You are to code up a modified Battleship game. This game of Battleship involves two players that are trying to sink each other’s ships. Each player has a grid of their current hits, misses and unguessed squares. Players take turns in guessing locations on the opposing player’s board on where they think a boat is. Depending on that guess and the locations of the ships, the attempt can be a hit or a miss. In our version of Battleship, each player has only one shot (guess) per turn and even if it’s a successful hit, that player’s turn ends. The game ends when a player has successfully hit all of their opponent’s ships.

Solution Description

Download Battleship.java. You will be modifying this file that has many stubbed methods that you will complete along with the main method. The board will have letters representing the columns and numbers representing the rows. So the coordinate ‘a7’ will mean the “a”th row and 7th column. Your code should treat a7 and A7 as the same spot. Rows will start from 1 not 0. Treat the top left corner as ‘a1’.

N.B. The above description has changed to reflect that the letter refers to the row and the number refers to the column. Sorry for the inconvenience!

You will be provided a .txt file that will provide the boards for each player. The first line will have the dimension of each player’s board on the first line (e.g. 3 corresponds with a 3x3 board). The second line will be the positions of player 1’s ships. The third line will be the positions of player 2 ships. The placements of the ships will be guaranteed to be valid, and there can be a varying number of ship locations (however player 1 and 2 will have the same number). You can use these boards as example inputs: game0.txt, game1.txt, game2.txt and game3.txt.

ex.

8                       // board will be 8x8
b4 b3 a4 a6 a7 c3 c5 f3 // player 1’s ship locations
c3 c4 f5 f3 a2 b3 d1 d2 // player 2’ ship locations

The game should start by printing what player 1 sees (a grid of player 2’s board) and how many hits they have remaining. ‘~’ should represent unguessed squares, ‘X’ for hit squares and ‘O’ for missed squares. A Scanner should then record the user’s guess through the command line. If the guess is a successful hit, print “Hit!” and change the board location from a ‘~’ to a ‘X’. If the guess is a miss, print “Miss!” and change the board location from ‘~’ to a ‘O’. Re-print the board after each guess. Repeat the process for player 2. The game should continue shifting turns until one player has hit all of the other’s ships.

At the end of the game, print which player (1 or 2) was the winner.

Methods

IMPORTANT: you may not change the method headers

Sample Output

Player 1 (7 hits left):
~ ~ ~ ~
~ ~ ~ ~
~ ~ ~ ~
~ ~ ~ ~
Enter missile location: b2
Hit!
Player 1 (6 hits left):
~ ~ ~ ~
~ X ~ ~
~ ~ ~ ~
~ ~ ~ ~

----------

Player 2 (7 hits left):
~ ~ ~ ~
~ ~ ~ ~
~ ~ ~ ~
~ ~ ~ ~
Enter missile location: c3
Miss!
Player 2 (7 hits left):
~ ~ ~ ~
~ ~ ~ ~
~ ~ O ~
~ ~ ~ ~

...

Player 2 (1 hits left):
X X X ~
~ ~ ~ X
~ X O O
~ X O ~
Enter missile location: a4
Hit!
Player 2 (0 hits left):
X X X X
~ ~ ~ X
~ X O O
~ X O ~

----------

The winner is Player 2

see ex1.txt for the full example.

Solution Constraints

Tips and Considerations

Grading

Javadocs

You will need to write Javadoc comments and look for checkstyle errors with your submission.

See the CS 1331 Style Guide for details.

Checkstyle

For each of your homework assignments we will run checkstyle and deduct one point for every checkstyle error.

For this homework the checkstyle cap is 20, meaning you can lose up to 20 points on this assignment due to style errors. This limit will increase with each homework.

Collaboration

When completing homeworks for CS1331 you may talk with other students about:

_ What general strategies or algorithms you used to solve problems in the homeworks _ Parts of the homework specification you are unsure of and need more explanation _ Online resources that helped you find a solution _ Key course concepts and Java language features used in your solution _ You may not discuss, show, or share by other means the specifics of your code, including screenshots, file sharing, or showing someone else the code on your computer, or use code shared by others.

Examples of approved/disapproved collaboration:

OKAY: “Hey, I’m really confused on how we are supposed to implement this part of the homework. What strategies/resources did you use to solve it?”

BY NO MEANS OKAY: “Hey… the homework is due in like 20 minutes… Can I see your code? I promise won’t copy it directly!”

In addition to the above rules, note that it is not allowed to upload your code to any sort of public repository. This could be considered an Honor Code violation, even if it is after the homework is due.

Submission

Have fun!