Battleshipjava package part3 public class Battleship public
Battleship.java
package part3;
public class Battleship {
public static void main(String[] args) {
// Generate a grid of 15 columns by 10 rows.
BattleshipGrid aGrid = new BattleshipGrid(10,10);
// Print the grid.
aGrid.print();
}
}
Battleship Grid.java
package part3;
import java.util.Random;
import java.util.Scanner;
public class BattleshipGrid {
private char[][] grid;
private int gridRows, gridColumns;
static final int CARRIER_SIZE = 5;
static final int BATTLESHIP_SIZE = 4;
static final int SUB_SIZE = 3;
static final int DESTROYER_SIZE = 3;
static final int PATROL_SIZE = 2;
static final char CARRIER_CHAR = \'C\';
static final char BATTLESHIP_CHAR = \'B\';
static final char SUB_CHAR = \'S\';
static final char DESTROYER_CHAR = \'D\';
static final char PATROL_CHAR = \'P\';
static final char WATER_CHAR = \'.\';
public BattleshipGrid(int columns, int rows){
gridColumns = columns;
gridRows = rows;
// Make a two dimensional array of characters as the grid
grid = new char[gridColumns][gridRows];
// Clear the grid
clear();
// Assign the following ships
assignShip(CARRIER_CHAR, CARRIER_SIZE);
assignShip(BATTLESHIP_CHAR, BATTLESHIP_SIZE);
assignShip(SUB_CHAR, SUB_SIZE);
assignShip(DESTROYER_CHAR, DESTROYER_SIZE);
assignShip(PATROL_CHAR, PATROL_SIZE);
}
// Clear the grid
public void clear(){
for (int row=0; row < gridRows; row++){
for (int column=0; column < gridColumns; column++) grid[column][row] = WATER_CHAR;
}
}
// Print the grid
public void print(){
for (int row=0; row < gridRows; row++){
for (int column=0; column < gridColumns; column++) System.out.print(grid[column][row]);
System.out.println(\"\");
}
}
// Assign a ship horizontally on the grid
private boolean assignShipHorizontally(char shipType, int size){
/*
1.2 If horizontal, select a random row to place the ship.
o Select a random column position to start “growing� your ship. The column position must be less than 10-size of the ship to ensure the ship will fit.
o Look at the next adjacent squares on the grid row to see if they are blank.
o If any of the squares are not blank then the ship will not fit so go back to 1.1.
o If the ship does fit then mark it with its designation.
*/
// Select a row and a starting column to place a ship horizontally
Random randomGenerator = new Random();
int chosenRow = randomGenerator.nextInt(gridRows);
int startingColumn = randomGenerator.nextInt(gridColumns-size);
// Count consecutive grid locations horizontally and see if we can accommodate ship
int countSegments = 0;
for (int c = startingColumn; c < startingColumn+size; c++){
if (grid[c][chosenRow] == WATER_CHAR) countSegments++;
}
// If ship will fit then allocate the ship horizontally
if (countSegments == size){
for (int c = startingColumn; c < startingColumn+size; c++){
grid[c][chosenRow] = shipType;
}
return true;
}
return false;
}
// Assign the ship vertically on the grid
private boolean assignShipVertically(char shipType, int size){
/*
1.3 If vertical, select a random column to place the ship.
o Select a random row position to start “growing� your ship. The row position must be less than 10-size of the ship to ensure the ship will fit.
o Look at the next adjacent squares on the grid column to see if they are blank.
o If any of the squares are not blank then the ship will not fit so go back to 1.1.
o If the ship does fit then mark it with its designation.
*/
// Select a column and a starting row to place a ship vertically
Random randomGenerator = new Random();
int chosenColumn = randomGenerator.nextInt(gridColumns);
int startingRow = randomGenerator.nextInt(gridRows-size);
// Count consecutive grid locations vertically and see if we can accommodate ship
int countSegments = 0;
for (int r = startingRow; r < startingRow+size; r++){
if (grid[chosenColumn][r] == WATER_CHAR) countSegments++;
}
// If ship will fit then allocate the ship vertically
if (countSegments == size){
for (int r = startingRow; r < startingRow+size; r++){
grid[chosenColumn][r] = shipType;
}
return true;
}
return false;
}
// Assign a ship to the grid
private void assignShip(char shipType, int size){
Random randomGenerator = new Random();
boolean status = false;
do{
// Randomly choose between horizontal and vertical allocation.
// Sometimes an allocation isnʻt possible so roll the dice and try again.
if (randomGenerator.nextInt(2) == 0)
status = assignShipHorizontally(shipType, size);
else
status = assignShipVertically(shipType, size);
} while(status != true);
}
public boolean playGame(){
// Print the board
// 1) write a for loop to iterate through the rows
for (){
// 2) write a for loop to iterate through the columns
for (){
// 3) if grid[column][row] is \'X\'
if(){
// 4) print \" \"+grid[column][row] (use System.out.print)
} else{
// 5) else print \" \"+WATER_CHAR (use System.out.print)
}
}
System.out.println(\"\");
}
// Ask user for coordinate
// 6) Create a Scanner called sc instantiate it using new Scanner(System.in)
// Ask user to enter a coordinate
System.out.print(\"Enter a coordinate (col,row): \");
// 7) read in the first integer to an integer called userCol
// 8) read in the second integer to an integer called userRow
// Check if column and row are with in the range of the board
// 9) Check if (userRow is less than 0) or (userRow is greater than or equal to gridRows),
if(){
// 10) use print() method to reveal location of ships
// 11) return false to end game
}
// 12) Check if (userCol is less than 0) or (userCol is greater than or equal to gridColumns),
if(){
// 13) use print() method to reveal location of ships
// 14) return false to end game
}
// 15) Check if grid[userCol][userRow] intersects with a ship,
if(){
// 16) set value at userCol, userRow to \'X\'
}
// return true to continue playing the game
return true;
}
}
Part 3: Modify Battleship Map Creator For this part, you will write a playGame() method that will print the board, ask a user for a coordinate, check if a ship is at this coordinate and mark an X for hit. If a coordinate entered is outside the range of the board, show the ships and end the game. In BattelshipGrid.java: » Locate the playGame() method in the BattelshipGrid.java file · write the code for steps 1-16 In Battelship.java: At line 11, comment out aGrid.print(); At line 12, add the following while loop. o while(aGrid.playGame()); » » Please note You can only have spaces between coordinates when you input them. Otherwise an exception will be thrown » » If you sink all the ships, the game will not end o For extra practice, add code that will end the game when all ships are sunkSolution
CODE:
/*write a for loop to iterate through the rows*/
for(int i=0;i<row;i++){
/*write a for loop to iterate through the columns*/
for(int j=0;j<column;j++){
/*if grid[column][row] is \'X\'*/
if(grid[column][row]==\'X\'){
/* print \" \"+grid[column][row] (use System.out.print)*/
System.out.print(\"Element is \" + grid[column][row]);
}
else{
/*else print \" \"+WATER_CHAR (use System.out.print)*/
System.out.print(\"element is\" + WATER_CHAR);
}
}
System.out.println(\" \");
}
/*Create a Scanner called sc instantiate it using new Scanner(System.in)*/
Scanner sc=new Scanner(System.in);
// Ask user to enter a coordinate
System.out.print(\"Enter a coordinate (col,row): \");
// read in the first integer to an integer called userCol
// read in the second integer to an integer called userRow
int userCol=sc.nextInt();
int userRow=sc.nextInt();
/* Check if column and row are with in the range of the board
Check if (userRow & userCol is less than 0) or (userRow & userCol is greater than or equal to gridRows),
use print() method to reveal location of ships
return false to end game */
if (userRow > 0 || userRow <= Row || userCol > 0 || userCol <= Col){
BattelshipGrid.print(userRow,userCol);
}
else{
return false;
}
// Check if grid[userCol][userRow] intersects with a ship,
// set value at userCol, userRow to \'X\'
if(grid[userCol][userRow]==grid[col][row]){
Sysem.out.print(\"User ship intersects the location of ship \'X\' \");
grid[userCol][userRow]=\'X\';
}
return true;
![Battleship.java package part3; public class Battleship { public static void main(String[] args) { // Generate a grid of 15 columns by 10 rows. BattleshipGrid aG Battleship.java package part3; public class Battleship { public static void main(String[] args) { // Generate a grid of 15 columns by 10 rows. BattleshipGrid aG](/WebImages/22/battleshipjava-package-part3-public-class-battleship-public-1050734-1761547269-0.webp)
![Battleship.java package part3; public class Battleship { public static void main(String[] args) { // Generate a grid of 15 columns by 10 rows. BattleshipGrid aG Battleship.java package part3; public class Battleship { public static void main(String[] args) { // Generate a grid of 15 columns by 10 rows. BattleshipGrid aG](/WebImages/22/battleshipjava-package-part3-public-class-battleship-public-1050734-1761547269-1.webp)
![Battleship.java package part3; public class Battleship { public static void main(String[] args) { // Generate a grid of 15 columns by 10 rows. BattleshipGrid aG Battleship.java package part3; public class Battleship { public static void main(String[] args) { // Generate a grid of 15 columns by 10 rows. BattleshipGrid aG](/WebImages/22/battleshipjava-package-part3-public-class-battleship-public-1050734-1761547269-2.webp)
![Battleship.java package part3; public class Battleship { public static void main(String[] args) { // Generate a grid of 15 columns by 10 rows. BattleshipGrid aG Battleship.java package part3; public class Battleship { public static void main(String[] args) { // Generate a grid of 15 columns by 10 rows. BattleshipGrid aG](/WebImages/22/battleshipjava-package-part3-public-class-battleship-public-1050734-1761547269-3.webp)
![Battleship.java package part3; public class Battleship { public static void main(String[] args) { // Generate a grid of 15 columns by 10 rows. BattleshipGrid aG Battleship.java package part3; public class Battleship { public static void main(String[] args) { // Generate a grid of 15 columns by 10 rows. BattleshipGrid aG](/WebImages/22/battleshipjava-package-part3-public-class-battleship-public-1050734-1761547269-4.webp)