因此,我正在为一款战舰游戏制作一种方法,该游戏由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.

推荐答案

请注意,描述中特别指出船舶方位为horizontally,所以我们不需要担心垂直船舶.

来self 上面的 comments :

在我看来,描述不够详细 构成沉船!这排沉没了多少艘船?{'*', '*', '*', '*', '*', '*', '.', '.', '.', '.'}是3个两个长度吗 船?是2艘三长船吗?是1个两个长度的组合 船后面跟着一艘三长船(或者组合颠倒)?是1吗 两长船,后面是一艘四长船(或该组合 颠倒)?或者只是一艘五长船?有很多 像这样的组合.

如果我们假设所有沉船都向左排列,并且始终安全地乘坐尽可能长的船,那么这里有一个简单的答案:

  public static int sunkenShips(char boardArray[][]) {
    int streak;
    int sunkShips = 0;
    for(char[] row : board) {
      streak = 0;
      for(char mark : row) {
        if (mark == '*') {
          streak++;
          // max length hit; reset in case ships are up against each other
          if (streak == 5) {
            sunkShips++;
            streak = 0;
          }
        }
        else {
          if (streak >=2) {
            sunkShips++;  
          }
          streak = 0;
        }
      }
      // Needed if the ship is against the right edge:
      if (streak >= 2) {
        sunkShips++;  
      }
    }
    return sunkShips;
  }

我们所做的就是每当遇到*分时增加连胜次数.如果我们达到最长的船长度5,那么我们会增加沉船计数,并将当前的连胜计数从零开始.每当我们击中一个不是*的角色时,我们都会查看当前的连胜是否至少有两个长度,并相应增加船数,然后将当前的连胜数重置为零.

最后,如果船靠在棋盘的右侧边缘,那么我们就不会击中非*字符,需要判断至少两个长度的船,因此我们执行最后一次判断.

Java相关问答推荐

在正确位置绘制多边形的PFA问题

如何用Java表示C++类以通过FFI使用?

试图弄清楚资源未能在我的Android应用程序中调用关闭警告

Kotlin ReadWriteProperty:无法使用T作为具体化类型参数.改为使用类

int Array Stream System. out. print方法在打印Java8时在末尾添加% sign

Java:根据4象限中添加的行数均匀分布行的公式

我的scala文件失败了Scala.g4 ANTLR语法

@从类文件中删除JsonProperty—Java

ApachePOI:不带换行的新行

如何找到MongoDB文档并进行本地化?

由于我在Main方法中关闭了 scanner ,但在该方法中创建了一个新的 scanner ,因此出现了错误

如何让JVM在SIGSEGV崩溃后快速退出?

如何从日期中截取时间并将其传递给组件?

如何仅使用键/ID的一部分(组合)高效地返回映射值?

在添加AdMob时无法为Google Play构建应用程序包:JVM垃圾收集器崩溃和JVM内存耗尽

Android Java:已设置但未读取SharedPreferences

FETCH类型设置为LAZY,但它仍会发送第二个请求

Java KeyListener不工作或被添加

控制器建议异常处理

[jdk21][Foreign Function&;Memory API]MemoryLayout::varHandle通过可变数组进行 struct 化的问题