class Ocean Instance variables ships a collection of ships o

class Ocean Instance variables ships a collection of ships on the ocean shotsFired - The total number of shots fired by the user. hitCount The number of times a shot hit a ship. If the user shoots the same part of a ship more than once, every hit is counted, even though the additional \"hits\" don\'t do the user any good. int shipsSunk -- The number of ships sunk (10 ships in all) ocean() The constructor. Creates an \"empty\" ocean (fills the ships array with EmptySeas). Also initializes all game variables void placeAllshipsRandomly() Place all ten ships randomly on the (initially empty) ocean. Place larger ships before smaller ones, or you may end up with no legal place to put a large ship. boolean isoccupied(int row, int column) Returns true if the given location contains a ship, false if it does not. boolean shootAt (int row, int column) Returns true if the given location contains a \"real\" ship, still afloat, (not an EmptySea), false if it does not. In addition, this method updates the number of shots that have been fired, and the number of hits. Note: If a location contains a \"real\" ship, shootAt should

Solution

Note: Include necessary packages and execute.

import java.util.ArrayList;

import java.util.Random;

public class Ocean

{

     //Instance variable declaration

     private int hitCount;

     //Location is been tracked to fire

     private boolean[][] Firedlocation;

     //Identify the ships on the Ocean.

     private Ship[][] ships;

     //identifies how many ships has been sunk into the Ocean

     private int shipsSunk;

     //identifies how many shots have been fired

     private int shotsFired;

    

     //Constructor

     public Ocean()

     {

          this.hitCount = 0;

          this.shipsSunk = 0;

          this.shotsFired = 0;

          this.ships = new Ship[10][10];

          this.Firedlocation = new boolean[10][10];

          for (int r=0; r < this.ships.length; r++)

          {

              for (int c=0; c < this.ships[r].length; c++)

              {

                   this.ships[r][c] = new EmptySea();

                   this.Firedlocation[r][c] = false;

              }

          }

         

          this.placeAllShipsRandomly();

     }

    

     //Constructor used for testing purpose

     public Ocean(Ship[][] shipArray)

     {

          this.hitCount = 0;

          this.shipsSunk = 0;

          this.shotsFired = 0;

          this.ships = shipArray;

          this.Firedlocation = new boolean[10][10];

          for (int r=0; r < this.ships.length; r++)

{

              for (int c=0; c < this.ships[r].length; c++)

              {                 

                   this.Firedlocation[r][c] = false;

              }

          }

     }

    

     //Ships are placed Randomly in the Ocean

     public void placeAllShipsRandomly()

     {

          ArrayList<Ship> shipsToPlace = this.InitialShipList();

          boolean ShipPlaced = false;

          int r = 0;

          int c = 0;

          boolean h = false;

         

          for (Ship ship : shipsToPlace)

          {

              while (!ShipPlaced)

              {

                   Random random = new Random();

                   r = random.nextInt(10);

                   c = random.nextInt(10);

                   h = random.nextBoolean();

                   ShipPlaced = ship.ShipPlacedAt(r,c,h,this);              

              }

             

              ship.setBowColumn(c);

              ship.setBowRow(r);

              ship.setHorizontal(h);

              this.PlaceShipAt(ship);

              ShipPlaced = false;

          }        

     }

    

     //Position occupied by the ship

     public boolean isOccupied(int r, int c)

     {

          return !(this.ships[r][c] instanceof EmptySea);

     }

    

    

     public boolean shootAt(int r, int col)

     {

          this.shotsFired++;

          this.Firedlocation[r][col] = true;

         

          Ship shipAtLocation = this.ships[r][col];      

         

          if (shipAtLocation.isSunk())

          {

              return false;

          }

         

     boolean ShipHitWhenFired = shipAtLocation.shootAt(r, col);

          if (ShipHitWhenFired)

          {

              this.hitCount++;

          }

          if (shipAtLocation instanceof EmptySea)

          {

              return ShipHitWhenFired;

          }

          if (shipAtLocation.isSunk())

          {

              shipsSunk++;

          }

          return ShipHitWhenFired;

     }

    

     //Gets the value of number of shots fired

     public int getShotsFired()

     {

          return this.shotsFired;

     }

    

     //Gets the count that have hit the ship when fired

     public int getHitCount()

     {

          return this.hitCount;

     }

    

     //Gets the value of how many ships have been sunk

     public int getShipsSunk()

     {

          return this.shipsSunk;

     }

    

     //gets 2D array [Rows and Columns] to represent ship in the

     //game

     public Ship[][] getShipArray()

     {

          return this.ships;

     }

    

     //type of ships that can be placed on the game board in the

     //list

     private ArrayList<Ship> InitialShipList()

     {

          ArrayList<Ship> ships = new ArrayList<Ship>();

          ships.add(new Battleship());

          for (int i=0; i<2; i++)

          {

              ships.add(new Cruiser());

          }

          for (int i=0; i<3; i++)

          {

              ships.add(new destroyer());

          }

          for (int i=0; i<4; i++)

          {

              ships.add(new Sub_marine());

          }        

          return ships;

     }

    

     //indicates game is finished when all the ships have sunk

     //else return false

     public boolean isGameOver()

     {

          return (this.shipsSunk == 10);

     }

    

     //Place the ships on to the specified location

     private void PlaceShipAt(Ship ship)

     {

          int r = ship.getBowRow();

          int c = ship.getBowColumn();

         

          if (ship.isHorizontal())

          {

              for (int i=0; i < ship.getLength(); i++)

              {                 

                   this.ships[r][(c+i)] = ship;

              }

          }

          else

          {

              for (int i=0; i < ship.getLength(); i++)

              {                 

                   this.ships[(r+i)][c] = ship;

              }

          }

     }

    

     //print status of the game

     public void print()

     {

          System.out.print(\"|   | \");

          for (int i=0; i < 10; i++)

          {

              System.out.print(i + \" | \");

          }

          System.out.print(\"\ \");

         

          for (int r=0; r < 10; r++)

          {

              System.out.print(\"| \" + r + \" | \");

              for (int c=0; c < 10; c++)

              {

                   String value = \"\";

                   if (this.Firedlocation[r][c])

                   {

                        value = this.ships[r][c].toString();

                   }

                   else

                   {

                        value = \".\";

                   }

                  

                   System.out.print(value + \" | \");

              }

              System.out.print(\"\ \");

          }

     }

}

 class Ocean Instance variables ships a collection of ships on the ocean shotsFired - The total number of shots fired by the user. hitCount The number of times
 class Ocean Instance variables ships a collection of ships on the ocean shotsFired - The total number of shots fired by the user. hitCount The number of times
 class Ocean Instance variables ships a collection of ships on the ocean shotsFired - The total number of shots fired by the user. hitCount The number of times
 class Ocean Instance variables ships a collection of ships on the ocean shotsFired - The total number of shots fired by the user. hitCount The number of times
 class Ocean Instance variables ships a collection of ships on the ocean shotsFired - The total number of shots fired by the user. hitCount The number of times
 class Ocean Instance variables ships a collection of ships on the ocean shotsFired - The total number of shots fired by the user. hitCount The number of times
 class Ocean Instance variables ships a collection of ships on the ocean shotsFired - The total number of shots fired by the user. hitCount The number of times

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site