Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I asked this question 6 times already and each time it was incorrect. It is not giving me what i need. Which is for the

I asked this question 6 times already and each time it was incorrect. It is not giving me what i need. Which is for the game to print out the adjacent edges and corners and the none adjacent after the user inputs the column and the row. Each time the code that was given was one that is just the battleship game and all it does was give you hints. That is NOT what i need. I need a battleship game that will print out the adjacent edges and corners. I tried adding more to what was answered in previous questions and the result of what i have is in this question. Please help me finish the code i have to make the game print out the adjacent corners and edges and the not adjacent

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("\t1 \t2 \t3 \t4 \t5 \t6 \t7 \t8 \t9"); 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("\t" + "~"); } else if (bord[row][column] == 0){ System.out.print("\t" + "*"); } else if (bord[row][column] == 1){ System.out.print("\t" + "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 + " "); } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

More Books

Students also viewed these Databases questions

Question

5. Identify some ways that people resist popular culture.

Answered: 1 week ago

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago