计算战列舰沉没船只/船只的问题(Java)

问题描述 投票:0回答:1

所以我正在为战舰游戏制作一种方法,该游戏由 Java 中 10x10 网格上水平 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
    }

我尝试了很多调整,这部分代码会迭代接下来的 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.

java arrays for-loop if-statement multidimensional-array
1个回答
0
投票

事实上,您的游戏应该有一个 10x10 的网格。不过,我觉得有点不同:

static char[][] board = {
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
    {'.', '.', '1', '.', '.', '*', '*', '.', '.', '.'},
    {'.', '.', '1', '.', '.', '.', '.', '.', '.', '.'},
    {'.', '.', '.', '2', '2', '2', '.', '.', '.', '.'},
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
    {'.', '.', '.', '.', '.', '*', '.', '.', '.', '.'},
    {'.', '.', '.', '.', '.', '*', '.', '.', '.', '.'},
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
    {'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}
};

由于 Java 没有“元组”的概念,因此创建 Java 记录。

public record Ship (int id, int length, char orientation)

您应该在应用程序的某个位置存储元组。可能是

Map<int[], Ship>
。我还没有想清楚这一点。这并不是“THE”的答案。这意味着比我在您的问题的评论部分中发布的内容更完整的评论。也就是说,我认为“我的评论”有足够的技术细节来作为某种答案。 玩游戏时,

onHit()

,您可以在地图中查找

(x,y)
键,并获取船舶(元组)。
onHit()
方法应该能够从该坐标确定匹配的 ID(验证),将网格中的 ID 替换为“x”,并确定船舶是否沉没。为此,元组将提供方向,以便您可以在该方向上查找网格中的相邻位置(“V”为上下,“H”为左右)。如果它找到更多 ID 值,则船不会沉没,游戏继续。如果船沉没,该方法应从地图中删除该条目。
    

© www.soinside.com 2019 - 2024. All rights reserved.