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
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.
Board
The computer will simulate a rectangular mn 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 have 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 33 and the maximum is 10 10. Assume that the points in the board range from (0, 0) to (m 1, n 1) inclusive.
Note: The Board class is also in charge of placing boats. In a regular game, boats should be placed randomly on the board, but if you are running into issues with this method of placement, you may place the boats manually to get the other parts of the game working. In that case you will wait until the rest of the game works to go back and change the placement to random.
Important: You are required to make a distinct Board class as well as classes for the entities Cell and Boat. Another class Game which will contain your main method is also required. We recommend using these names, but you can name these classes whatever you like as long as it is easy to understand.
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
'-' | Has not been guessed, no boat present |
'B' | Has not been guessed, boat present |
'H' | Has been guessed, boat present |
'M' | Has been guessed, no boat present |
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.
Note: Examples: Valid coordinates for a boat of size 3 are {(0, 0),(0, 1),(0, 2)} and {(1, 1),(2, 1),(3, 1)}. Examples of invalid coordinates are {(0, 0),(0, 1)}, which is invalid because there are not enough points. {(0, 0),(0, 2),(0, 3)} is invalid because the points are not consecutive. {(1, 0),(0, 0),(1, 0)} is invalid because the first coordinate is out of bounds. Finally, two boats cannot contain the same point because they cannot overlap.
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.
Smallest Dimension | Boat Sizes |
width == 3 or height == 3 | 2 |
3 < width <= 4 or 3 < height <= 4 | 2, 3 |
4 < width <= 6 or 4 < height <= 6 | 2, 3, 3 |
6 < width <= 8 or 6 < height <= 8 | 2, 3, 3, 4 |
8 < width <= 10 or 8 < height <= 10 | 2, 3, 3, 4, 5 |
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.
Hint: To randomly place a boat, consider the coordinate in the upper left. If the upper left corner was (0, 0), consider how the boat looks if it is horizontal or vertical. What upper left corner coordinates are invalid? The placing of the boats may be the most challenging aspect of this project: see what assumptions you can make to simplify it. To generate random numbers, the Math.random() method can be used. However, this method returns a double in the range 0 to 1. We will need to scale this and then round it to a whole. To achieve this, use the Math.floor(x) function, which takes a double x and rounds it down to the nearest integer. For example, Math.floor(2.9) is 2.
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!
Example:
Suppose the board is 3 3 and just one boat is placed with the coordinates (0, 0),(0, 1),(0, 2).
Turn 1: the user selects (1, 0) and "miss" is printed.
Turn 2: the user selects (0, 0) and "hit" is printed.
Turn 3: the user selects (0, 0) again and is penalized by losing turn 4.
Turn 4: skipped.
Turn 5: the user selects (0, 1) and "hit" is printed.
Turn 6: The user selects (1, 0) which is out of bounds. Penalty.
Turn 7: skipped.
Turn 8: the user selects (0, 2) and "sunk" is printed. The game ends because the last boat has sunk.
Missile
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. Example: User: Types in missile Terminal: Where would you like to launch your missile? User: 2 2 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
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.
Example:
User: Types in drone
Terminal: Would you like to scan a row or column? Type in r for row or c for column.
User: row
Terminal: Invalid input. Please type in r for row or c for column.
User: r
Terminal: Which row or column would you like to scan?
User: -1
Terminal: Invalid Input. Please type in a number within the boundaries of the board.
User: 0
Terminal: Drone has scanned 2 targets in the specified area.
User: drone
Terminal: Drone has been used the max amount of times
Scanner
At any point in the game, once the board is initialized and the mode has been chosen, the user can type in "scanner" (or whatever way you want them to signify to the program that they wish to use a scanner). The program will then "scan" a set of coordinates and determine whether the boat placed there is vertical or horizontal as well as return the size of the boat. It will then print out that information to the user. Boat spots that have already been hit can also be scanned. If they enter the coordinates, then it should also fire there.
Example
User: Types in scanner
Terminal: What coordinate would you like to scan?
User: 2 3
Terminal: Hit! The boat is horizontally placed and is size 5.
Note: If the input coordinate is out of bounds, the program should skip the users turn and continue the game.
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 based on user input. 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 construction.
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
Has scanner(int x,int y) function to scan a specific coordinate to see if the boat is horizontal or vertical and returns the size of the boat.
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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Here is a basic implementation of the Battleboats game in Java java import javautilRandom import javautilScanner public class Battleboats public static void mainString args Board board ...See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started