Question
First, make a class to store the location of your battleships. 1. Create a class named Location with only 2 attributes, row and column. Add
First, make a class to store the location of your battleships.
1. Create a class named Location with only 2 attributes, row and column.
Add getters and setters for both attributes and
a constructor that accepts the row and column values as arguments and assigns them appropriately
written in java
Then, add a driver class (either in the same le or separately) to drive your game. In this class:
2. Build your game board. This is a 5 x 5 two-dimensional char array.
Initialize the board to store Os (capital letter o) for the open water.
3. Create an ArrayList capable of storing Location objects (see step 1).
4. Using an object of the Random class, generate random row and column values for 3 battle ships. Create corresponding Location objects and append these objects to the ArrayList.
5. The user gets 4 attempts to guess where your battleships are. For each attempt, do the following:
(a) Tell them how many attempts remain
(b) Tell them how many battleships remain
(c) Allow the user to make a guess, then:
i. Check their guess against the boundaries of the board, if it is outside of the boundaries, display an appropriate message
ii. Check the guess against cells already marked with an asterisk (indicating a battleship) or an X (indicating a miss), display a message to tell the user that theyve already guessed that location
iii. Check the guess against all battleship locations in the ArrayList. If they guess correctly:
Remove that location from the ArrayList
Mark the location on the board with an asterisk
Display an appropriate message
iv. Otherwise, mark the guessed location with an X and display a message telling the user they have missed your battleship
(d) Print the board
(e) Decrement the number of turns
After all guesses have been made, if there are still battleships remaining, mark all of them on the board with an asterisk, display a message stating the game is over and reprint the board
Test your game!
Make sure correct and incorrect guesses work appropriately according to the above instructions. Take a screenshot of your successful game play
written in java.
Heres what i have so far:
1. Location class
class Location {
private int row;
private int column;
public Location(int row, int column) { this.row = row;
this.column = column; } public int getRow() { return row; }
public int getColumn() { return column; }
public void setRow(int row) { this.row=row; }
public void setColumn(int column) { this.column=column; }
}
2. the driver:
public class Driver2 { public static void main(String [] args) { char [][] board = new char [5] [5];
for(int rows=0; rows { for(int columns=0; columns { board[rows][columns]='O'; } } for(int rows=0; rows { for(int columns=0; columns { System.out.print(board[rows][columns]+ " "); } System.out.println(); } ArrayListLocationObjList = new ArrayList();
Random rand = new Random (); Scanner in = new Scanner(System.in); for (int i = 0; i < 3; i++) { int row = rand.nextInt(4) + 1; int column = rand.nextInt(4) + 1; Location locationObj = new Location(row, column); LocationObjList.add(locationObj); } Driver driverObj = new Driver(); int shipCount = 4; int temp_count = shipCount;
for (int i = 0; i < temp_count; i++) {
System.out.println("you have "+(temp_count-i)+" Attempts remaining");
System.out.println("The Remaining Battle Ships Are "+shipCount);
System.out.println("Guess the Location"); int usr_Row = in.nextInt();
int usr_Colm = in.nextInt();
if ((usr_Row > 0 && usr_Row < 5) && (usr_Colm > 0 && usr_Colm < 5)) {
} } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
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