Вот доска/сетка:
Код: Выделить всё
static char[][] board = {
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', 'S', '.', '.', '*', '*', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '*', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '*', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}
};
Код: Выделить всё
public static int sunkenShips(char boardArray[][]) {
board = boardArray;
int sunkenShips = 0; //counts the amount of sunken ships
boolean sunken = false; //used later to check if the the space is a sunken ship
for(char[] row : boardArray) { //loops through board array
for(int i = 0; i < row.length; i++) { //loops through each row
if(row[i] == '*') { //checks if current element is a sunken ship
sunken = true; //sets it to true then
int ii = 0; //resets so it can check if it is a one star (not a sunken ship) in each row
for(int j = i; j < row.length && j < i + 5; j++) { //if it is sunken it checks whether its a valid sunken ship (2-5 squares long), it will go on until the 5th square and/or until the end of the row
ii++;
if(row[j] != '*' && ii == 1) { //checks if the next square after the one is was on is a * and if not which would make it only 1 star and not a sunken ship, break out of the loop
sunken = false; //changes it to false therefore
break;
}
}
if(sunken == true) { //if the ship is sunken then increments to the sunken ship variable
sunkenShips++;
}
}
}
}
if (validBoard(boardArray) == 12 && sunken == true) { //checks if board is valid using other method and if board has sunken ships
return sunkenShips;
}
else if (validBoard(boardArray)==12) {
for(char[] row: boardArray) {
for(int i = 0; i < row.length; i++) { //goes through the array to check if there aren't any ships
if (row[i] == 'S') { //checks if there is a ship
boolean validShip = true; //used to checking if its a valid ship (2-5 squares in length)
int iii = 0; //resets so it can check if it is a one S (not a ship) in each row
for(int j = i; j < row.length && j < i + 5; j++) { // same logic as above
iii++;
if(row[j] != 'S' && iii == 1) { //same logic as above to check if its a ship
validShip = false;
break;
} else {
validShip = true;
}
}
if (validShip == false) { // if there aren't ships it should return this value
return 0;
}
}
}
}
}
return 2; //default return value
}
Код: Выделить всё
for(int j = i; j < row.length && j < i + 5; j++) { //if it is sunken it checks whether its a valid sunken ship (2-5 squares long), it will go on until the 5th square and/or until the end of the row
ii++;
if(row[j] != '*' && ii == 1) { //checks if the next square after the one is was on is a * and if not which would make it only 1 star and not a sunken ship, break out of the loop
sunken = false; //changes it to false therefore
break;
}
}
Код: Выделить всё
Write a method that when given a valid board (see above – assume the input board is valid) returns the number of sunk ships. If there are no ships on the board (this is still a valid board) a value should be returned.Подробнее здесь: https://stackoverflow.com/questions/783 ... ships-java