Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i have a code that runs, but it only lets the player to guess where the ships ar

ID: 3940960 • Letter: I

Question

i have a code that runs, but it only lets the player to guess where the ships are(which is a one player game with one board). is there a way some one can help turn this game into a player vs computer game (with two boards one for player and one for computer) Please help me.

BattleShipGame.java

package battleship;

import java.util.ArrayList;
import java.util.Scanner;

public class BattleshipGame {
   private Ocean ocean;
   private boolean[][] availableSpot;
   private Scanner sc;

   public BattleshipGame() {
       // define a new ocean and a new 2D array to store available coordinates
       ocean = new Ocean();
       availableSpot = new boolean[10][10];
       for (int i = 0; i < 10; i++) {
           for (int j = 0; j < 10; j++){
               availableSpot[i][j] = true;
           }
       }
   }

   /**
   * prints the game menu and info
   * //param select
   */
   public void print(int select){
       String info;
       switch (select) {
       case 1: info = "Welcome to the World of Battleship!";
       break;
       case 2: info = "Enter coordinates to fire: ";
       break;
       case 3: info = "Shots fired: "+ocean.getShotsFired()+", Ships sunk: "+ocean.getShipsSunk();
       break;
       case 4: info = "Congratulations! You win!";
       break;
       case 99: info = "Invalid input. Please re-enter:";
       break;
       case 100: info = "--------------------------------------------";
       break;
       case 101: info = " ============================================";
       break;
       default: info = "Error selection";
       break;
       }
       System.out.println(info);
   }

   /**
   * check if the input is valid
   * //param input
   * //return boolean
   */
   public boolean checkValidInput(String input){
       ArrayList<String> numList = new ArrayList<String>();
       for (int i=0;i<10;i++){
           numList.add(""+i);
       }
       String[] coordinates = input.split(" ");
       //returns false if there are not 2 strings
       if (coordinates.length!=2){
           return false;
       }
       //returns false if any of the strings is not a single digit number
       for (String str: coordinates){
           if (numList.contains(str)==false){
               return false;
           }
       }
       //returns false if the coordinates have already been shot at
       int row = Integer.parseInt(coordinates[0]);
       int column = Integer.parseInt(coordinates[1]);
       if (this.availableSpot[row][column]==false){
           return false;
       }
      
       return true;
   }
  
   /**
   * get the coordinates to shoot at from the String input
   * //param input
   * //return int[] coordinates
   */
   public int[] getCoordinates(String input){
       int[] coordinates = new int[2];
       String[] strList = input.split(" ");
       int row = Integer.parseInt(strList[0]);
       int column = Integer.parseInt(strList[1]);
       coordinates[0] = row;
       coordinates[1] = column;
       return coordinates;
   }

   /**
   * play the battleship game
   */
   public void play(){
       print(101);
       print(1);
       ocean.placeAllShipsRandomly();
       boolean isGameOver = ocean.isGameOver();
       sc = new Scanner(System.in);
      
       //print the ocean and start the game
       ocean.print();
       print(3);
       while (!isGameOver){
           print(2);
           String input = sc.nextLine();
          
           //check if input is valid
           while (!checkValidInput(input)){
               print(99);
               input = sc.nextLine();
           }
          
           //get coordinates and fire
           int[] coordinates = getCoordinates(input);
           int row = coordinates[0];
           int column = coordinates[1];
           ocean.shootAt(row, column);
           availableSpot[row][column] = false;
           isGameOver = ocean.isGameOver();
           ocean.print();
           print(3);
           print(100);
       }
       //print info saying you win
       print(4);
   }

   public static void main(String[] args) {
      
       BattleshipGame battleshipGame = new BattleshipGame();
       battleshipGame.play();
       System.out.println("Continue? y/n");
       Scanner sc = new Scanner(System.in);
       String isPlay = sc.next();
       while (isPlay.equals("y")){
           battleshipGame = new BattleshipGame();
           battleshipGame.play();
           System.out.println("Continue? y/n");
           isPlay = sc.next();
       }
       sc.close();
   }

}

Ocean.java

package battleship;

import java.util.*;

public class Ocean {
   private Ship[][] ships;
   private int shotsFired;
   private int hitCount;
   private int shipsSunk;
   Random random = new Random();
   private boolean[][] shadow;
   private Ship battleship;
   private Ship cruiser1, cruiser2;
   private Ship destroyer1, destroyer2, destroyer3;
   private Ship submarine1, submarine2, submarine3, submarine4;
   private ArrayList<Ship> allShips;
   //private boolean[][] shotLocations;


   public Ocean() {
       // TODO Auto-generated constructor stub
       battleship = new Battleship();
       cruiser1 = new Cruiser();
       cruiser2 = new Cruiser();
       destroyer1 = new Destroyer();
       destroyer2 = new Destroyer();
       destroyer3 = new Destroyer();
       submarine1 = new Submarine();
       submarine2 = new Submarine();
       submarine3 = new Submarine();
       submarine4 = new Submarine();

       allShips = new ArrayList<Ship>();
       allShips.add(battleship);
       allShips.add(cruiser1);
       allShips.add(cruiser2);
       allShips.add(destroyer1);
       allShips.add(destroyer2);
       allShips.add(destroyer3);
       allShips.add(submarine1);
       allShips.add(submarine2);
       allShips.add(submarine3);
       allShips.add(submarine4);


       ships = new Ship[10][10];
       shadow = new boolean[10][10];
       //shotLocations = new boolean[10][10];

       for (int i = 0; i < 10; i++) {
           for (int j = 0; j < 10; j++) {
               this.ships[i][j] = new EmptySea();
               this.ships[i][j].setBowRow(i);
               this.ships[i][j].setBowColumn(j);
               this.ships[i][j].setHorizontal(true);
               this.shadow[i][j] = false;
               //this.shotLocations[i][j] = false;
           }
       }
       this.shotsFired = 0;
       this.hitCount = 0;
       this.shipsSunk = 0;

   }

   public void placeAllShipsRandomly() {
       int row;
       int column;
       int trueOrFalse;
       for (Ship ship: allShips){
           row = (int) (Math.random() * 10);
           column = (int) (Math.random() * 10);
           trueOrFalse = (int) (Math.random() * 2);
           boolean horizontal = false;
           if (trueOrFalse == 1) {
               horizontal = true;
           }
           else {
               horizontal = false;
           }

           while (!ship.okToPlaceShipAt(row, column, horizontal, this)) {
               row = (int) (Math.random() * 10);
               column = (int) (Math.random() * 10);
               trueOrFalse = (int) (Math.random() * 2);
               if (trueOrFalse == 1) {
                   horizontal = true;
               }
               else {
                   horizontal = false;
               }
           }
           ship.placeShipAt(row, column, horizontal, this);

       }
   }

   public boolean isOccupied(int row, int column) {
       if (this.ships [row][column].getShipType().equals("empty")) {
           return false;
       }
       return true;
   }

   public boolean shootAt(int row, int column) {
       int hit = 0;
       int sunkNum = 0;
       if (isOccupied(row, column) && !ships[row][column].isSunk()) {
           this.hitCount += 1;
           hit = 1;
       }
       this.shotsFired += 1;
       //this.shotLocations[row][column] = true;
       this.ships[row][column].shootAt(row, column);
       for (Ship ship: this.allShips) {
           if (ship.isSunk()){
               sunkNum += 1;
           }
       }
       this.shipsSunk = sunkNum;
       if (hit == 1) {
           return true;
       }
       return false;
   }

   public int getShotsFired() {
       return this.shotsFired;
   }

   public int getHitCount() {
       return this.hitCount;
   }

   public int getShipsSunk() {
       return this.shipsSunk;
   }

   public boolean isGameOver() {
       if (this.shipsSunk == 10) {
           return true;
       }
       return false;
   }

   public Ship[][] getShipArray() {
       return this.ships;
   }

   public void print() {
       String s = " ";
       int i;
       int j;
       for (i = -1; i < 10; i++) {
           for (j = -1; j < 10; j++) {
               if (i == -1){              
                   if (j > -1){
                       s += " " + j;
                   }
               }
               else if (j == -1) {
                   s += i + " ";
               }
               else if (!this.isHit(i, j)) {
                   s += "." + " ";
               }
               else {
                   s += ships[i][j].toString() + " ";
               }
           }
           s += " ";
       }
       System.out.println(s);
   }


   ////////////////////////////////////////////////additional helper functions//////////////////////////
   public boolean[][] getShadow() {
       return this.shadow;
   }

   /**
   * when put in one ship, shadow all its adjacent sea. Then the okToPrint function can make judgment and forbid ships to place on the shadow.
   */
   public void setShadow() {
       for (int i = 0; i < 10 ; i++){
           for (int j = 0; j < 10; j++) {
               if (this.isOccupied(i,j)) {
                   for (int k = -1; k < 2; k++) {
                       for (int l = -1; l <2; l++ ) {
                           if ((i+k>=0) && (i+k<=9) && (j+l>=0) && (j+l <=9)) {
                               shadow[i+k][j+l] = true;
                           }
                       }
                   }
               }
           }
       }
   }

   /**
   * setter for ship class to place ship in the ocean
   * //param row
   * //param column
   * //param ship
   */
   public void placeShip(int row, int column, Ship ship) {
       this.ships[row][column] = ship;
       //update the shadow(places which don't allow ship to be placed)
       this.setShadow();
   }

   /**
   * all ships list getter for testing
   * //return
   */
   public ArrayList<Ship> getAllShips() {
       return this.allShips;
   }

   public void printTest() {
       String s = " ";
       int i;
       int j;
       for (i = -1; i < 10; i++) {
           for (j = -1; j < 10; j++) {
               if (i == -1){              
                   if (j > -1){
                       s += " " + j;
                   }
               }
               else if (j == -1) {
                   s += i + " ";
               }
               else if (!isOccupied(i,j)) {
                   s += "." + " ";
               }
               else {
                   s += ships[i][j].toString() + " ";
               }
           }
           s += " ";
       }
       System.out.println(s);
   }
  
   public boolean isHit(int row, int column) {
       Ship ship = this.ships[row][column];
       int bowRow = ship.getBowRow();
       int bowColumn = ship.getBowColumn();
       //System.out.println(row + " " + column + " " + ship + " " + bowRow + " " + bowColumn + ship.isHorizontal());
      
       if (ship.getShipType().equals("empty")) {
           return (ship.getHitArray()[0]);
       }
       else if (ship.isHorizontal()) {
           if (ship.getHitArray()[column - bowColumn]) {
               return true;
           }
           return false;
       }
       else {
           if (ship.getHitArray()[row - bowRow]) {
               return true;
           }
           return false;
       }
   }

}

Ship.java

package battleship;

public abstract class Ship {
   private int bowRow;
   private int bowColumn;
   protected int length;
   private boolean horizontal;
   protected boolean[] hit = new boolean[4];

   public Ship() {
       // TODO Auto-generated constructor stub
       super();
   }

   /**
   * returns bowRow
   * //return bowRow
   */
   public int getBowRow() {
       return bowRow;
   }

   public void setBowRow(int bowRow) {
       this.bowRow = bowRow;
   }

   /**
   * returns bowColumn
   * //return bowColumn
   */
   public int getBowColumn() {
       return bowColumn;
   }

   /**
   * sets the value of bowColumn
   * //param bowColumn
   */
   public void setBowColumn(int bowColumn) {
       this.bowColumn = bowColumn;
   }
  
   /**
   * returns the length of this particular ship
   * //return length of the ship
   */
   public int getLength() {
       return length;
   }

   /**
   * returns horizontal as boolean
   * //return isHorizontal
   */
   public boolean isHorizontal() {
       return horizontal;
   }

   /**
   * sets the value of instance variable horizontal
   * //param horizontal
   */
   public void setHorizontal(boolean horizontal) {
       this.horizontal = horizontal;
   }


   abstract String getShipType();

   /**
   * returns true if it is okay to put a ship of certain length with its bow in this location, with the given orientation
   * returns false otherwise
   * //param row
   * //param column
   * //param horizontal
   * //param ocean
   * //return okToPlaceShipAt as boolean
   */
   public boolean okToPlaceShipAt(int row, int column, boolean horizontal, Ocean ocean){
       boolean okToPlace = true;
       boolean[][] shadows = ocean.getShadow();
       if (horizontal){
           for (int i=0; i<this.getLength();i++){
               if (column+i>9){okToPlace = false;}
               else if (shadows[row][column+i]){okToPlace = false;}
           }
       }
       else{
           for (int i=0; i<this.getLength();i++){
               if (row+i>9){okToPlace = false;}
               else if (shadows[row+i][column]){okToPlace = false;}
           }
       }
       return okToPlace;
   }

   /**
   * puts the ship on a certain spot in the ocean
   * //param row
   * //param column
   * //param horizontal
   * //param ocean
   */
   public void placeShipAt(int row, int column, boolean horizontal, Ocean ocean){
       this.setHorizontal(horizontal);
       this.setBowRow(row);
       this.setBowColumn(column);
       if (!this.isHorizontal()){
           for (int i=0;i<this.getLength();i++){
               ocean.placeShip(row+i, column, this);
           }
       }
       else{
           for (int i=0;i<this.getLength();i++){
               ocean.placeShip(row, column+i, this);
           }
       }
   }


   /**
   * returns true if the ship is hit and not sunk. marks that part of the ship as "hit"
   * returns false otherwise
   * //param row
   * //param column
   * //return shootAt as boolean
   */
   public boolean shootAt(int row, int column){
  
       if (this.isHorizontal()){
           for (int i=0; i<this.getLength();i++){
               if ((this.getBowRow() == row)&&(this.getBowColumn()+i==column)){
                   this.hit[i] = true;
                   return true;
               }
           }
       }
       else{
           for (int i=0; i<this.getLength();i++){
               if ((this.getBowRow()+i == row)&&(this.getBowColumn()==column)){
                   this.hit[i] = true;
                   return true;
               }
           }
       }
       return false;
   }

   /**
   * returns the boolean[] that shows which part of the ship is hit
   * //return this.hit
   */
   public boolean[] getHitArray(){
       return this.hit;
   }

   /**
   * returns true if every part of the ship has been hit
   * //return isSunk
   */
   public boolean isSunk(){
       boolean isSunk = true;
       for (int i=0;i<this.getLength();i++){
           isSunk = isSunk&&this.hit[i];
       }
       return isSunk;
   }
  
   /**
   * returns a single-char String to be used in the class 'Ocean'
   * "x" if sunk, "S" if not sunk
   */
   //Override
   public String toString(){
       if (this.isSunk()){
           return "x";
       }
       return "S";
   }
  
  
}

Battleship.java

package battleship;

public class Battleship extends Ship{

   public Battleship() {
       // TODO Auto-generated constructor stub
       super();
       this.length = 4;
   }
  
   //Override
   public String getShipType(){
       return "battleship";
   }
  

}

Cruiser.java

package battleship;

public class Cruiser extends Ship{

   public Cruiser() {
       // TODO Auto-generated constructor stub
       super();
       this.length = 3;
   }
  
   //Override
   public String getShipType(){
       return "cruiser";
   }
  

}

Destroyer.java

package battleship;

public class Destroyer extends Ship{

   public Destroyer() {
       // TODO Auto-generated constructor stub
       this.length = 2;
   }
  
   //Override
   public String getShipType(){
       return "destroyer";
   }

}

Submarine.java

package battleship;

public class Submarine extends Ship{

   public Submarine() {
       // TODO Auto-generated constructor stub
       this.length = 1;
   }
  
   //Override
   public String getShipType(){
       return "submarine";
   }

}

EmptySea.java

package battleship;

public class EmptySea extends Ship{

   public EmptySea() {
       // TODO Auto-generated constructor stub
       super();
       this.length = 1;
   }
  
   //Override
   public boolean shootAt(int row, int column){
       this.hit[0] = true;
       return false;
   }
  
   //Override
   public boolean isSunk(){
       return false;
   }
  
   //Override
   public String toString(){
       return "-";
   }
  
   //Override
   String getShipType(){
       return "empty";
   }


}

Explanation / Answer

import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import java.io.*; import java.lang.Integer; import java.util.Vector; import java.net.*; public class Battleship extends JFrame { private static JButton ok = new JButton("OK"),//closes stats menu done =new JButton("Done");//closes options menu private static JFrame statistics= new JFrame("Statistics"),//holds stats options=new JFrame("Options");//holds opts private static JLabel data,//used for stats menu title;//used for options menu private static JPanel stats=new JPanel(),//used for stats menu opts,//used for options menu inputpanel;//for manually inputting ships private static Container b,c,d;//board and input panel private JPanel input;//input bar private static JMenuItem m,pvp,pvc,cvc;//menu items private static String[] cletters = {" ","A","B","C","D","E","F","G","H","I","J"}, //array of letters used for combo boxes cnumbers = {" ","1","2","3","4","5","6","7","8","9","10"}, //array of numbers used for combo boxes ships = {"Carrier","Battleship","Submarine","Destroyer", "Patrol Boat"},//strings used for ship combo box direction = {"Horizontal","Vertical"},//directions level={"Normal", "Ridiculously Hard"}, layout={"Manual","Automatic"}, colors={"Cyan", "Green", "Yellow", "Magenta", "Pink", "Red", "White"}, first={"Player 1", "Player 2", "Random"};//used for options private JComboBox cshi = new JComboBox(ships),//ships cdir = new JComboBox(direction);//directions private static JComboBox aiLevel=new JComboBox(level), shipLayout=new JComboBox(layout), shipColor=new JComboBox(colors), playsFirst=new JComboBox(first);//used //for options menu private JTextField mbar = new JTextField();//message bar private static int enemy=1, i,j,//counters length=5, you=0, prevcolor=0,//index of previous color prevFirst=0, prevLayout=0, prevLevel=0,//tracks changes in corresponding comboboxes ready=0, sindex=0,//stores index of array dindex=0;//direction private static Player players[]=new Player[2]; private static JButton deploy=new JButton("DEPLOY"); private static int w=0,a=0,s=0,t=0,e=0;//counters to track the use of all ships private static String[][] shiphit=new String[10][10]; private static String user,user2; private static Color[] color={Color.cyan,Color.green,Color.yellow,Color.magenta, Color.pink, Color.red, Color.white}; private static Object selectedValue=" ", gametype; private static BattleshipClient me; private static boolean gameover=false; public Battleship() { setTitle("Battleship"); setDefaultCloseOperation(EXIT_ON_CLOSE); setJMenuBar(createMenuBar()); setResizable(false); //gets user to input name user=JOptionPane.showInputDialog("Enter your name."); int dummy=0; while (((user==null)||(user.equals("")))&&(dummy