Need help with this little assignment using Java language. Taking a programming
ID: 3742628 • Letter: N
Question
Need help with this little assignment using Java language. Taking a programming class because of my minor. I’m stuck need help, thanks!
Aim: Write a simple stand-alone 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: 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 lo 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 ft square is (0,0). (5,5)30 4,1 a7 b 2 An A-level program will handle answers, be clearly written, and be well designed. all the input formats illustrated output correctExplanation / Answer
/* package codechef; // don't place package name! */
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
class CodeChef{
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;
}
}