CSCI 1933 Project 2: Battleboats


Instructions

Please read and understand these expectations thoroughly. Failure to follow these instructions could negatively impact your grade. Rules detailed in the course syllabus also apply but will not necessarily be repeated here.

•  Due: The project is due on Friday, March 12th by 11:55 PM.

•  Submission: Please submit a .zip or .tar file on Canvas containing your src/ folder with all the .java files. Only one student must submit a project on Canvas. Submissions in the incorrect format may receive a penalty. Submissions with missing code will also be penalized. Make sure you submit all your source code!

•  Partners: You may work alone or with one partner. Please place your partner’s and your names, student IDs, and x500s in a comment in each .java file you submit. Do not share code with students other than your partner.

•  Code Sharing: If you use online resources to share code with your partner, please ensure the code is private. Public repositories containing your code are prohibited because other students may copy your work.

•  Java Version: The TAs will grade your code using the latest version of Java (version 11). Please ensure you have this version of Java installed.

•  Grading: We will not be using unit tests to grade this project. You are free to make any design decisions you want, but your code must be reasonably clean, well-designed, and commented thoroughly. Your code may receive a penalty if it is confusing or demonstrates poor knowledge of Java. Grading will be done by the TAs, so please address grading problems to them privately.

•  Extra Work: If you enjoy this project and want to add extra features, please document the extra features thoroughly and allow them to be disabled for grading purposes. Mystery features we don’t understand could cause grading issues. Extra work will not result in extra credit, but the TAs will be impressed!

•  Questions: Please post questions related to this project on Discord or the Piazza forum.


Introduction

Battleboats is a probability-based board game that challenges the user to locate enemy boats hidden on a rectangular grid. The purpose of the game is to locate and destroy every enemy Boat in the least number of guesses.

You will be modelling this game in Java. There are no requirements for the actual classes and functions you design, but you must implement every part of the description below. Your code may also be judged based on style. This should not be a stressful requirement - it simply means that you must create logically organized, well-named and well-commented code so the TAs can grade it.



Board

The computer will simulate a rectangular m∗n board. You are required to use a 2-dimensional array to represent the board. The type of this array is up to the programmer.

The user will input the dimensions m and n at the beginning of the game, and the program should create a board of m rows by n columns. Errors such as non positive input should be checked, but you can assume the input is always two integers. The minimum board size is 3∗3 and the maximum is 10 ∗ 10. Assume that the points in the board range from (0, 0) to (m m 1, n n 1) inclusive.




Cells

The Battleboats game board is composed of cells. The cell class should contain the following attributes:

•  public int row: Indicates the row value of the Cell

•  public int col: Indicates the column value of the Cell

•  public char status: Character indicating the status of the Cell. There are three different possibilities for this field, as shown in the table below.


The following functions should also be implemented:

•  public char get_status(): Getter method for status attribute

•  public void set_status(char c): Setter method for status attribute

•  public Cell(int row, int col, char status): Cell class constructor



Boats

Each boat is represented by a line of consecutive squares on the board. Boats may not overlap other boats, extend outside the game board, or be placed diagonally. They may be horizontal or vertical. A boat is considered ”sunk” when all the squares of the boat have been ”hit” by the user.



After the game board has been sized, the program should place boats randomly on the board. This requires randomly generating a coordinate (x, y) where the boat will be placed as well as randomly choosing whether the boat should be horizontal or vertical. The number and size of boats are defined by the width and height of the game board. Recall the smallest board is 3 ∗ 3 and the largest board is 10 ∗ 10.



Use the table above to determine what kind of boats to place. Recall that the board may be rectangular, so a board that is 9 ∗ 3 should have just one boat of length 2 (the first case). The user should be told how many boats are on the board when the game begins.



Turns 

Each turn, the user will input a location x, y to attack on the board. You can assume the input is always two integers, but you will need to check if the pair x, y is a valid location on the game board later.

•  If there is no boat at the location, print ”miss”.

•  If the user has already attacked that location or location is out of bounds, print ”penalty”.

•  If there is a boat, print ”hit”. If the Boat sinks, print ”sunk”. Do not print ”hit” again if the user has already ”hit” this location.

If the user receives a penalty (for attacking the same spot twice or for attacking somewhere out of bounds), the user’s next turn will be skipped. The game ends when all the boats have been sunk. The game should report how many turns it took to discover all the boats. Lower scores are better!



Missile

There are two special powers you will implement in this project; the first is the missile attack. At any point in the game, once the board is initialized, the user can type in ”missile” (or whatever way you want them to signify to the program that they wish to use a missile).

The program will then ask the user for a coordinate. The user will type in two numbers (Example: 0 5). The program will then check if the coordinate (0,5) is valid for the board. If it is not, the program will continue to ask the user to type in valid coordinates until the user does so.

Next, the program will fire a missile in that spot, what that means is that it will fire at a 3x3 square, with the chosen spot being in the very center of the square. If the chosen spot is near the edge of the board, the missile will only hit the spots on the board.



In the above case, a missile will launch at coordinates (2,2). This will hit spots (1,1),(1,2),(1,3),(2,1), (2,2),(2,3),(3,1),(3,2), and (3,3).

Another example is if a user launches a missile at coordinates (0,0). In this case, because (0,0) is near the edge of the board, only the spots that are on the board will be hit. In this case spots (0,0) (0,1),(1,0) and (1,1) will be hit. Note: It is fine if the spot to launch the missile has already been fired upon, or even if all the spots the missile will be hit have already been fired upon. The only invalid missile use is firing outside of the board.


Drone

The second power you will add is the drone attack. At any point in the game, once the board is initialized and the mode has been chosen, the user can type in ”drone” (or whatever way you want them to signify to the program that they wish to use a drone).

The program will then ask the user if they want to scan a row or column. The user will respond, for example by typing in ”row”. If the user types in an invalid response, the program should alert them until they type in a valid response.

Next, the program will ask the user which row or column they wish to scan. The user will respond by typing in a number, for example 0. If the user types in a number that is out of boundaries, the program should alert them until they type in a valid number.

The program will then ”scan” that row or column, and determine how many spots are there that contain a boat. It will then print that information out to the user. For instance, in the above example where a user entered ”row” and 0, the program would scan the 0th (or first row) and return the number of boat spots in that row. The drone will count boat spots that have already been hit and sunk.




Debug Mode

The sections above should hide the game data from the user so the user cannot cheat. However, for grading purposes, you are required to add a debug mode. The game should ask the user whether to run in debug mode and set a flag internally.

If the game is in debug mode, print the game board on the screen before every turn. This means printing the locations of boats as well as their identity and which sections have been hit. You can format output as a grid or just provide a list of information, but it should be easy to understand.

If the game is not in debug mode, do not print this information.


Getting Started

There is a lot to do in this project, but we recommend starting by making the Board class that contains your 2D Array or board. You should first set up the functionality to correctly size the array depending on if the user selects a standard or expert game mode. Then, determine how to randomly place the ships on the board without having them overlap. Implement the print command to help you see the board and make sure that your ships are being correctly placed.


Board Class

•  Contains a 2D Array of Cell objects

•  Contains a 1D Array of Boat objects

•  Contains int variables to keep tracks of total shots, turns, and ships remaining.

•  Constructor takes two int variables as arguments and correctly sizes the array upon con-struction.

•  Has placeBoats() function to randomly place boats on board.

•  Has fire(int x, int y) function to handle firing on a coordinate.

•  Has display() function to print out the player board state every turn.

•  Has print() function to print out the fully revealed board if a player types in the print command (This would be used for debugging purposes)

•  Has missile(int x, int y) function to fire a missile on a specified coordinate

•  Has drone(int direction,int index) function to scan a specific row or column


Cell Class

•  Contains int variables for the row and column of the Cell

•  Contains a char variable to keep track of the status of the Cell (whether it has been guessed and/or if there is a boat present)

•  Getters and setters pertaining to the row and column of the Cell

•  Getter and setter pertaining to the status of the Cell


Boat Class

•  Contains an int size to indicate its length

•  Contains a boolean orientation to indicate whether the boat is horizontal or vertical

•  Contains a Cell[] array corresponding to where the boat is on the board

•  Constructor initializing all of the necessary attributes

•  Getters and setters for necessary attributes


Game Class

•  The main function should be in here. Should create and initialize a Board object. Should use Scanner to take in user input until game end.

•  Can also contain any helper functions that you may find helpful.


README

Make sure to include a README.txt file with your submission that contains the following infor-mation:

•  Group member’s names and x500s

•  Contributions of each partner (if working with a partner)

•  How to compile and run your program

•  Any assumptions

•  Additional features that you implemented (if applicable)

•  Any known bugs or defects in the program

•  Any outside sources (aside from course resources) consulted for ideas used in the project, in the format:

-  idea1: source

-  idea2: source

-  idea3: source

There is a five-point reduction if no README is included in your submission.


Honors Component: BattleboatsGUI

This section is only required for students in the honors section. However, students in other sections are still encouraged to work on this if they are interested and time permits.

This section should not be started until the rest of your project is functioning.

Playing Battleboats in the command line can be fun, but interacting with a GUI (Graphical User Interface) is easier. In this section you will be asked to create a fully functioning graphical interface for your Battleboats game. The GUI game should use the Board, Boat, and Cell classes you created earlier in the project.

To begin, download the provided BattleboatsGUI class. This class has the framework for creating the GUI already implemented. You will be responsible for the following:

•  Drawing the rectangles associated with the Cell’s from the Board class in the constructor and repaint method

•  Adding a label that displays the turns, hits, and misses

•  Adding buttons that allow the user to use a missile or drone when clicking on a cell instead of firing

•  Editing the actionPerformed and mouseClicked events to implement firing, missiles, and drones.

•  Updating the message label after each action with the result, such as ”hit”, ”miss”, ”sunk”, or information about using a missile or drone

You do not need to implement any additional features you previously added to your project unless it is crucial for your Board class to function. That said, you are welcome to add more features for fun or to challenge yourself!