Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help changing a few things on this code. The output print this: [ ] [0] [1] [2] [3] [4] [5] [6] [7] [8]

I need help changing a few things on this code.

The output print this:

[ ] [0] [1] [2] [3] [4] [5] [6] [7] [8] [8] [-] [-] [-] [-] [-] [-] [-] [-] [-] [7] [-] [-] [-] [-] [-] [-] [-] [-] [-] [6] [-] [-] [-] [-] [-] [-] [-] [-] [-] [5] [-] [-] [-] [-] [-] [-] [-] [-] [-] [4] [-] [-] [-] [-] [-] [-] [-] [-] [-] [3] [-] [-] [-] [-] [-] [-] [-] [-] [-] [2] [-] [-] [-] [-] [-] [-] [-] [-] [-] [1] [-] [-] [-] [-] [-] [-] [-] [-] [-] [0] [-] [-] [-] [-] [-] [-] [-] [-] [-]

but i would like it to print this:

[ ] [1] [2] [3] [4] [5] [6] [7] [8] [9] [1] [-] [-] [-] [-] [-] [-] [-] [-] [-] [2] [-] [-] [-] [-] [-] [-] [-] [-] [-] [3] [-] [-] [-] [-] [-] [-] [-] [-] [-] [4] [-] [-] [-] [-] [-] [-] [-] [-] [-] [5] [-] [-] [-] [-] [-] [-] [-] [-] [-] [6] [-] [-] [-] [-] [-] [-] [-] [-] [-] [7] [-] [-] [-] [-] [-] [-] [-] [-] [-] [8] [-] [-] [-] [-] [-] [-] [-] [-] [-] [9] [-] [-] [-] [-] [-] [-] [-] [-] [-]

Another thing is when you input the the row and the column, it outputs the adjacent data and asks "Try Again? Y/N" and instead of doing that i wanted it to just show the game again and put a "*" where a shot was shot. Like so:

[ ] [1] [2] [3] [4] [5] [6] [7] [8] [9] [1] [-] [-] [-] [-] [-] [-] [-] [-] [-] [2] [-] [-] [-] [-] [-] [-] [-] [-] [-] [3] [-] [-] [-] [-] [-] [-] [-] [-] [-] [4] [-] [-] [-] [-] [-] [-] [-] [-] [-] [5] [-] [-] [*] [-] [-] [-] [-] [-] [-] [6] [-] [-] [-] [-] [-] [-] [-] [-] [-] [7] [-] [-] [-] [-] [-] [-] [-] [-] [-] [8] [-] [-] [-] [-] [-] [-] [-] [-] [-] [9] [-] [-] [-] [-] [-] [-] [-] [-] [-]

This is my code: please help me input these changes without affecting the adjacent outputs

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

public class battleShip{ private static String[][] board; public battleShip(){ board = new String[9][9]; for(int i = 0; i < board.length; i++){ for(int j = 0; j < board.length; j++){ board[i][j] = "-"; } } } public static void main(String[] args){ battleShip game = new battleShip(); game.simulate(); }

public static boolean isCornerAdjacent(int rowIndex, int colIndex, int testRow, int testColumn){

boolean isCornerAdjacent = false; if(rowIndex == 0){

if((testRow == rowIndex + 1 && testColumn == colIndex - 1) || (testRow == rowIndex + 1 && testColumn == colIndex + 1)){ isCornerAdjacent = true; } } else if(rowIndex == board.length - 1){ if((testRow == rowIndex -1 && testColumn == colIndex -1) || (testRow == rowIndex -1 && testColumn == colIndex + 1)){ isCornerAdjacent = true; } } else{ // In all other cases if((testRow == rowIndex + 1 && testColumn == colIndex -1) || (testRow == rowIndex + 1 && testColumn == colIndex + 1) || (testRow == rowIndex -1 && testColumn == colIndex - 1) || (testRow == rowIndex -1 && testColumn == colIndex + 1)){ isCornerAdjacent = true; } }

return isCornerAdjacent;

}

public static boolean isEdgeAdjacent(int rowIndex, int colIndex, int testRow, int testColumn){

boolean isEdgeAdjacent = false; if(rowIndex == 0){

if((testRow == rowIndex && testColumn == colIndex -1) ||(testRow == rowIndex && testColumn == colIndex + 1) || (testRow == rowIndex + 1 && testColumn == colIndex)){ isEdgeAdjacent = true; } } else if(rowIndex == board.length -1){

if((testRow == rowIndex && testColumn == colIndex -1) ||(testRow == rowIndex && testColumn == colIndex + 1) || (testRow == rowIndex - 1 && testColumn == colIndex)){ isEdgeAdjacent = true; } } else{

if((testRow == rowIndex && testColumn == colIndex -1) || (testRow == rowIndex && testColumn == colIndex + 1) || (testRow == rowIndex -1 && testColumn == colIndex) || (testRow == rowIndex +1 && testColumn == colIndex)){ isEdgeAdjacent = true; } }

return isEdgeAdjacent;

}

public static void displayAdjacenyData(int rowIndex,int colIndex){

if(rowIndex == 0 || rowIndex == board.length-1){ rowIndex = Math.abs(rowIndex - (board.length-1)); } if(colIndex == 0 || colIndex == board.length-1){ colIndex = Math.abs(rowIndex - (board.length-1)); }

ArrayList edgeAdjacencyList = new ArrayList(); ArrayList cornerAdjacencyList = new ArrayList(); ArrayList nonAdjacentValueList = new ArrayList(); for(int i = 0; i < board.length; i++){ for(int j = 0; j < board.length; j++){

if(isCornerAdjacent(rowIndex,colIndex,i,j)){ cornerAdjacencyList.add("(" + String.valueOf(i) + "," + String.valueOf(j) + ")"); } if (isEdgeAdjacent(rowIndex,colIndex,i,j)) { edgeAdjacencyList.add("(" + String.valueOf(i) + "," + String.valueOf(j) + ")");

} else{ nonAdjacentValueList.add("(" + String.valueOf(i) + "," + String.valueOf(j) + ")"); } } } if(nonAdjacentValueList.size() == board.length * board.length){ System.out.println("Value non existent on BattleShip board"); return; }

System.out.println("Current edge data"); for(String edgePair: edgeAdjacencyList){ System.out.print(edgePair + " "); } System.out.println(); System.out.println("Current corner data"); for(String cornerPair: cornerAdjacencyList){ System.out.print(cornerPair + " "); } System.out.println(); System.out.println("Current non adjacent data"); for(String nonAdjacentPair: nonAdjacentValueList){ System.out.print(nonAdjacentPair + " "); }

} public void revert(){ for(int i = 0; i < board.length; i++){ for(int j = 0; j < board.length; j++){ board[i][j] = "*"; } } } public static void displayMatrix(){ // This method will display a matrix in a battleship like fashion for(int i = 0; i <= board.length; i++){ if(i == 0){

System.out.print("[ ]" + " "); } else{ System.out.print("["+(i -1)+"]" + " ");

} } System.out.println();

for(int i = 0; i < board.length; i++){

if(i == board.length){ System.out.print("[ ]" + " "); } else{ System.out.print("["+(Math.abs(i-(board.length-1))+"]" + " "));

}

for(int j = 0; j < board.length; j++){ System.out.print("["+board[i][j]+"]" + " "); } System.out.println(); } }

public static void simulate(){

Scanner battleshipComputer = new Scanner(System.in); displayMatrix(); System.out.println("Hello user, welcome to the battleship simulation"); boolean userStillPlaying = true; while(userStillPlaying){ System.out.println("Please enter a row and column index from (0/a) to (8/i)" + " " + "Accepted formats include: (5,5), 3 0, 4,1, a7, b 2"); String userResponse = battleshipComputer.nextLine(); processInput(userResponse); System.out.println("Try Again? Y/N"); String stillPlaying = battleshipComputer.nextLine(); if(stillPlaying.equals("N")){ userStillPlaying = false; } } }

public static boolean isValidInteger(Character digit){

return Character.isDigit(digit);

}

public static boolean isValidCharacter(Character letter){

char[] validChars = {'a','b','c','d','e','f','g','h','i'}; for(int i = 0; i < validChars.length; i++){ if(letter.equals(validChars[i])){ return true; } } return false; }

public static int handleCharacters(Character digit){

char[] validChars = {'a','b','c','d','e','f','g','h','i'}; for(int i = 0; i <= validChars.length; i++){ if(digit.equals(validChars[i])){ return i; } } return -1;

}

public static boolean isValidUserInput(String userResponse){ char[] expression = userResponse.toCharArray();

boolean isValidUserInput = false; if(userResponse.charAt(0) == '(' && userResponse.charAt(userResponse.length() -1) == ')'){ if(isValidInteger(userResponse.charAt(1)) || ! isValidInteger(userResponse.charAt(userResponse.length()-2)) || !isValidCharacter(userResponse.charAt(1)) || !isValidCharacter(userResponse.charAt(userResponse.length()-2))){ return isValidUserInput; } } else if(Character.isWhitespace(expression[1]) || expression[1] == ','){ if(expression[1] == ','){ if(!isValidInteger(expression[0]) || !isValidInteger(expression[2]) || !isValidCharacter(expression[0]) || !isValidCharacter(expression[2])){ return isValidUserInput; } } else if(Character.isWhitespace(expression[1])){ String[] data = userResponse.split(" "); String str = data[0] + data[1]; if(!isValidInteger(str.charAt(0)) || !isValidInteger(str.charAt(1)) || !isValidCharacter(str.charAt(0)) || !isValidCharacter(str.charAt(1))){ return isValidUserInput; } } } else if(!Character.isWhitespace(expression[1]) && expression[1] != ','){ if(!isValidInteger(userResponse.charAt(0)) || ! isValidInteger(userResponse.charAt(userResponse.length()-1)) || !isValidCharacter(userResponse.charAt(0)) || !isValidCharacter(userResponse.charAt(userResponse.length()-1))){ return isValidUserInput; } } else{ isValidUserInput = true; } return isValidUserInput;

}

public static void processInput(String userResponse){

char[] expression = userResponse.toCharArray(); if(userResponse.charAt(0) == '(' && userResponse.charAt(userResponse.length() -1) == ')'){

if(isValidCharacter(userResponse.charAt(1)) && isValidCharacter(userResponse.charAt(userResponse.length()-2))){ int rowIndex = handleCharacters(userResponse.charAt(1)); int colIndex = handleCharacters(userResponse.charAt(userResponse.length()-2)); displayAdjacenyData(rowIndex,colIndex); } else if(isValidCharacter(userResponse.charAt(1)) && isValidInteger(userResponse.charAt(userResponse.length()-2))){ int rowIndex = handleCharacters(userResponse.charAt(1)); int colIndex = Character.getNumericValue(userResponse.charAt(userResponse.length()-2)); displayAdjacenyData(rowIndex,colIndex); } else if(isValidInteger(userResponse.charAt(1)) && isValidCharacter(userResponse.charAt(userResponse.length()-2))){ int rowIndex = Character.getNumericValue(userResponse.charAt(1)); int colIndex = handleCharacters(userResponse.charAt(userResponse.length()-2)); displayAdjacenyData(rowIndex,colIndex); } else{ int rowIndex = Character.getNumericValue(userResponse.charAt(1)); int colIndex = Character.getNumericValue(userResponse.charAt(userResponse.length()-2)); displayAdjacenyData(rowIndex,colIndex);

} } // This handles either spaces or commas else if(Character.isWhitespace(expression[1]) || expression[1] == ','){

// The four cases beneath handle the order in which either a number or character appear or whether // we recieve two characters or two integers if ((expression[1]) == ',') { if(isValidCharacter(expression[0]) && isValidCharacter(expression[2])){ int rowIndex = handleCharacters(expression[0]); int colIndex = handleCharacters(expression[2]); displayAdjacenyData(rowIndex,colIndex); } else if(isValidCharacter(expression[0]) && isValidInteger(expression[2])){ int rowIndex = handleCharacters(expression[0]); int colIndex = Character.getNumericValue(expression[2]); displayAdjacenyData(rowIndex,colIndex); } else if(isValidInteger(expression[0]) && isValidCharacter(expression[2])){ int rowIndex = Character.getNumericValue(expression[0]); int colIndex = handleCharacters(expression[2]); displayAdjacenyData(rowIndex,colIndex); } else{ int rowIndex = Character.getNumericValue(expression[0]); int colIndex = Character.getNumericValue(expression[2]); displayAdjacenyData(rowIndex,colIndex);

} } else if(userResponse.contains(" ")){ String[] data = userResponse.split(" "); String s = data[0] + data[1]; if(isValidCharacter(s.charAt(0)) && isValidCharacter(s.charAt(1))){ int rowIndex = handleCharacters(s.charAt(0)); int colIndex = handleCharacters(s.charAt(1)); displayAdjacenyData(rowIndex,colIndex); } else if(isValidCharacter(s.charAt(0)) && isValidInteger(s.charAt(1))){ int rowIndex = handleCharacters(s.charAt(0)); int colIndex = Character.getNumericValue(s.charAt(1)); displayAdjacenyData(rowIndex,colIndex); } else if(isValidInteger(s.charAt(0)) && isValidCharacter(s.charAt(1))){ int rowIndex = Character.getNumericValue(s.charAt(0)); int colIndex = handleCharacters(s.charAt(1)); displayAdjacenyData(rowIndex,colIndex); } else{ int rowIndex = Character.getNumericValue(s.charAt(0)); int colIndex = Character.getNumericValue(s.charAt(1)); displayAdjacenyData(rowIndex,colIndex);

}

} } else{

if(isValidCharacter(userResponse.charAt(0)) && isValidCharacter(userResponse.charAt(userResponse.length()-1))){ int rowIndex = handleCharacters(userResponse.charAt(0)); int colIndex = handleCharacters(userResponse.charAt(userResponse.length()-1)); displayAdjacenyData(rowIndex,colIndex); } else if(isValidCharacter(userResponse.charAt(0)) && isValidInteger(userResponse.charAt(userResponse.length()-1))){ int rowIndex = handleCharacters(userResponse.charAt(0)); int colIndex = Character.getNumericValue(userResponse.charAt(userResponse.length()-1)); displayAdjacenyData(rowIndex,colIndex); } else if(isValidInteger(userResponse.charAt(0)) && isValidCharacter(userResponse.charAt(userResponse.length()-1))){ int rowIndex = Character.getNumericValue(userResponse.charAt(0)); int colIndex = handleCharacters(userResponse.charAt(userResponse.length()-1)); displayAdjacenyData(rowIndex,colIndex); } else{ int rowIndex = Character.getNumericValue(userResponse.charAt(0)); int colIndex = Character.getNumericValue(userResponse.charAt(userResponse.length()-1)); displayAdjacenyData(rowIndex,colIndex); } } } }

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

Databases Theory And Applications 27th Australasian Database Conference Adc 20 Sydney Nsw September 28 29 20 Proceedings Lncs 9877

Authors: Muhammad Aamir Cheema ,Wenjie Zhang ,Lijun Chang

1st Edition

3319469215, 978-3319469218

More Books

Students also viewed these Databases questions

Question

.

Answered: 1 week ago