因此,我正在为一款战舰游戏制作一种方法,该游戏由Java中的10 x 10网格上水平分布2-5个方格的船只组成.该方法是统计网格上沉船数量,这些沉船是全"*"的船舶,一艘船将标记为"S"和"水".".我已经制定了这个方法,但在正确计算沉船数量时,我总是遇到一个问题,因为它要么将一排恒星沉船(不是有效船只)算作沉船,但最近我遇到了一个问题,它将所有单个恒星算作沉船.正如您在下面的板中看到的那样,它应该返回1艘船,因为这是板上唯一有效的船,但当我运行此方法时,它返回4艘船.这也是稍后计算船上/网格上没有多少船舶的方法中的问题.
这是板/网格:
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
}
我try 了对代码的这一部分进行了很多调整,迭代了接下来的4个空白来判断它是否有效,并使用一个判断if声明来判断它是否有效(至少2个空白长).但每次它要么没有运行if陈述,要么最近没有正确地运行for循环.
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.