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

Write a Java program from scratch. Simple geometrical reasoning is at the core o

ID: 3742046 • Letter: W

Question

Write a Java program from scratch.

Simple geometrical reasoning is at the core of Battleships. One important notion is adjacency, with two sub-types: edge adjacency and corner adjacency. For example, the square (4,3) is edge-adjacent to the square (3,3), it is corner-adjacent to the square (3,2), and it is not adjacent to the square (6,7).

Write a program that reads a square from the user, and prints out three lists(each with a seperate method):

1) a list of all edge-adjacent squares,

2) a list of all corner-adjacent squares, and

3) a list of all squares which are not adjacent at all.

Our version of battleships is played on a 9 by 9 board. The lower-left square is (0,0). As noted, the program will read from the console a shot specification. You can decide which formats to handle. They may include, for example, specifications like

(5, 5)

3 0

4,1

a7

b 2

An A-level program will handle all the input formats illustrated, output correct answers, be clearly written, and be well designed.

I need help printing the adjacent corners and edges and the not adjacent

This is my code so far. I need help making it better:

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

public class battleShip{
private static String[][] game;
public static void main(String[] args){

    int[][] bord = new int[9][9];
    int[][] ships = new int[5][4];
    int[] shoot = new int[4];
    int attempt = 0,
      shoothit = 0;
    initBoard(bord);
    initShips(ships);
    System.out.println();
    do{
      showBoard(bord);
      shoot(shoot);
      attempt++;
      if (hit(shoot, ships)){
        hint(shoot, ships, attempt);
        shoothit++;
      }
      else
        hint(shoot, ships, attempt);
      changeboard(shoot, ships, bord);
    }
    while (shoothit != 5);
    System.out.println(" Battleship Java game finished! You hit 5 ships in " + attempt + " attempt");
    showBoard(bord);
}

public static void initBoard(int[][] bord){
    for (int row = 0; row < 9; row++)
      for (int column = 0; column < 9; column++)
      bord[row][column] = -1;
}

public static void showBoard(int[][] bord){
    System.out.println(" 1 2 3 4 5 6 7 8 9");
    System.out.println();
    for (int row = 0; row < 9; row++){
      System.out.print((row + 1) + "");
      for (int column = 0; column < 9; column++){
        if (bord[row][column] == -1){
          System.out.print(" " + "~");
        }
        else if (bord[row][column] == 0){
          System.out.print(" " + "*");
        }
        else if (bord[row][column] == 1){
          System.out.print(" " + "X");
        }
      }
      System.out.println();
    }
}

public static void initShips(int[][] ships){
    Random random = new Random();
    for (int ship = 0; ship < 5; ship++){
      ships[ship][0] = random.nextInt(9);
      ships[ship][1] = random.nextInt(9);

      for (int last = 0; last < ship; last++){
        if ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]))
          do{
          ships[ship][0] = random.nextInt(9);
          ships[ship][1] = random.nextInt(9);
        }
        while ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]));
      }
    }
}

public static void shoot(int[] shoot){
    Scanner input = new Scanner(System.in);
    System.out.print("Row: ");
    shoot[0] = input.nextInt();
    shoot[0]--;
    System.out.print("Column: ");
    shoot[1] = input.nextInt();
    shoot[1]--;
}

public static boolean hit(int[] shoot, int[][] ships){
    for (int ship = 0; ship < ships.length; ship++){
      if (shoot[0] == ships[ship][0] && shoot[1] == ships[ship][1]){
        System.out.printf("You hit a ship located in (%d,%d) ", shoot[0] + 1, shoot[1] + 1);
        return true;
      }
    }
    return false;
}

public static void hint(int[] shoot, int[][] ships, int attempt){
    int row = 0,
      column = 0;
    for (int line = 0; line < ships.length; line++){
      if (ships[line][0] == shoot[0])
        row++;
      if (ships[line][1] == shoot[1])
        column++;
    }
    System.out.printf(" Hint %d: Row %d -> %d ships " +
                      "Column %d -> %d ships ", attempt, shoot[0] + 1, row, shoot[1] + 1, column);
}

public static void changeboard(int[] shoot, int[][] ships, int[][] bord){
    if (hit(shoot, ships))
      bord[shoot[0]][shoot[1]] = 1;
    else
      bord[shoot[0]][shoot[1]] = 0;
}
public static boolean isCornerAdj(int columnI, int rowI, int testC, int testR){
    boolean isCornerAdj = false;
    if(rowI == 0){
      if((testR == rowI + 1 && testC == columnI - 1) || (testR == rowI + 1 && testC == columnI + 1)){
        isCornerAdj = true;
      }
    }
    else if(rowI == game.length - 1){
      if((testR == rowI -1 && testC == columnI -1) || (testR == rowI -1 && testC == columnI+ 1)){
        isCornerAdj = true;
      }
    }
    else{
      // In all other cases
      if((testR == rowI + 1 && testC == columnI -1) || (testR == rowI + 1 && testC == columnI + 1) ||
         (testR == rowI -1 && testC == columnI - 1) || (testR == rowI -1 && testC == columnI + 1)){
        isCornerAdj = true;
      }
    }
  
    return isCornerAdj;
  
}
public static boolean isEdgeAdj(int columnI, int rowI, int testC, int testR){
    boolean isEdgeAdj = false;
    if(rowI == 0){
      if((testR==rowI&&testC==columnI -1)||(testR == rowI && testC == columnI + 1)
           ||(testR == rowI + 1 && testC == columnI)){
        isEdgeAdj = true;
      }
    }
    else if(rowI == game.length -1){
      if((testR == rowI && testC == columnI -1) ||(testR == rowI && testC == columnI + 1)
           || (testR == rowI - 1 && testC == columnI)){
        isEdgeAdj = true;
      }
    }
    else{
      // In all other cases, we check left right, above and below the selected index
      if((testR == rowI && testC == columnI -1) || (testR == rowI && testC == columnI + 1) ||
         (testR == rowI -1 && testC == columnI) || (testR == rowI +1 && testC == columnI)){
        isEdgeAdj= true;
      }
    }
  
    return isEdgeAdj;
  
}
public static void adjacenyOutput(int rowI,int columnI){
    if(rowI == 0 || rowI == game.length-1){
      rowI = Math.abs(rowI - (game.length-1));
    }
    if(columnI == 0 || columnI == game.length-1){
      columnI = Math.abs(rowI - (game.length-1));
    }
    ArrayList edgeAdj = new ArrayList();
    ArrayList cornerAdj = new ArrayList();
    ArrayList nonAdj = new ArrayList();
    for(int i = 0; i < game.length; i++){
      for(int j = 0; j < game.length; j++){
        if(isCornerAdj(rowI,columnI,i,j)){
          cornerAdj.add("(" + String.valueOf(i) + "," + String.valueOf(j) + ")");
        }
        if (isEdgeAdj(rowI,columnI,i,j)) {
          edgeAdj.add("(" + String.valueOf(i) + "," + String.valueOf(j) + ")");
        
        }
        else{
          nonAdj.add("(" + String.valueOf(i) + "," + String.valueOf(j) + ")");
        }
      }
    }
    if(nonAdj.size() == game.length * game.length){
      System.out.println("Value non existent on BattleShip board");
      return;
    }
    System.out.println("Edge adjacency: ");
    for(String edge: edgeAdj){
      System.out.print(edge + " ");
    }
    System.out.println();
    System.out.println("Corner adjacency: ");
    for(String corner: cornerAdj){
      System.out.print(corner + " ");
    }
    System.out.println();
    System.out.println("None adjacency:");
    for(String noneAdj: nonAdj){
      System.out.print(noneAdj + " ");
    }
}

}

Explanation / Answer

import java.util.Random;

import java.util.Scanner;

public class battleShip

{

    public static void main(String[] args)

    {

        int[][] bord = new int[9][9];

        int[][] ships = new int[5][4];

        int[] shoot = new int[4];

        int attempt = 0,

            shoothit = 0;

        initBoard(bord);

        initShips(ships);

        System.out.println();

        do

        {

            showBoard(bord);

            shoot(shoot);

            attempt++;

            if (hit(shoot, ships))

            {

                hint(shoot, ships, attempt);

                shoothit++;

            }

            else

                hint(shoot, ships, attempt);

            changeboard(shoot, ships, bord);

        } while (shoothit != 5);

        System.out.println(" Battleship Java game finished! You hit 5 ships in " + attempt + " attempt");

        showBoard(bord);

    }

    public static void initBoard(int[][] bord)

    {

        for (int row = 0; row < 9; row++)

            for (int column = 0; column < 9; column++)

                bord[row][column] = -1;

    }

    public static void showBoard(int[][] bord)

    {

        System.out.println(" 1 2 5 2 9");

        System.out.println();

        for (int row = 0; row < 9; row++)

        {

            System.out.print((row + 1) + "");

            for (int column = 0; column < 9; column++)

            {

                if (bord[row][column] == -1)

                {

                    System.out.print(" " + "~");

                }

                else if (bord[row][column] == 0)

                {

                    System.out.print(" " + "*");

                }

                else if (bord[row][column] == 1)

                {

                    System.out.print(" " + "X");

                }

            }

            System.out.println();

        }

    }

    public static void initShips(int[][] ships)

    {

        Random random = new Random();

        for (int ship = 0; ship < 5; ship++)

        {

            ships[ship][0] = random.nextInt(9);

            ships[ship][1] = random.nextInt(9);

            //let's check if that shot was already tried

            //if it was, just finish the do...while when a new pair was randomly selected

            for (int last = 0; last < ship; last++)

            {

                if ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]))

                    do

                    {

                        ships[ship][0] = random.nextInt(9);

                        ships[ship][1] = random.nextInt(9);

                    } while ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]));

            }

        }

    }

    public static void shoot(int[] shoot)

    {

        Scanner input = new Scanner(System.in);

        System.out.print("Row: ");

        shoot[0] = input.nextInt();

        shoot[0]--;

        System.out.print("Column: ");

        shoot[1] = input.nextInt();

        shoot[1]--;

    }

    public static boolean hit(int[] shoot, int[][] ships)

    {

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

        {

            if (shoot[0] == ships[ship][0] && shoot[1] == ships[ship][1])

            {

                System.out.printf("You hit a ship located in (%d,%d) ", shoot[0] + 1, shoot[1] + 1);

                return true;

            }

        }

        return false;

    }

    public static void hint(int[] shoot, int[][] ships, int attempt)

    {

        int row = 0,

            column = 0;

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

        {

            if (ships[line][0] == shoot[0])

                row++;

            if (ships[line][1] == shoot[1])

                column++;

        }

        System.out.printf(" Hint %d: Row %d -> %d ships " +

                                 "Column %d -> %d ships ", attempt, shoot[0] + 1, row, shoot[1] + 1, column);

    }

    public static void changeboard(int[] shoot, int[][] ships, int[][] bord)

    {

        if (hit(shoot, ships))

            bord[shoot[0]][shoot[1]] = 1;

        else

            bord[shoot[0]][shoot[1]] = 0;

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote