English 中文(简体)
计算战舰沉船/船只的问题(贾瓦)
原标题:Problem with counting Battleship sunken ships/ships (Java)

因此,我正在采用一种方法,在 Java10x10格格网上,由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个空间对法典的这一部分进行大量敲响,以检查其有效作战船舶是否有效,并检查其是否有效(至少2个空间)。 但是,无论何时,它都未能通过声明或最近未能妥善地通行。

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;
                        }
                    }

这里指的是任何想:

<编码> 采用一种方法,当给一家有效的委员会时(见上文——假定投入委员会有效)将沉船数目退回。 如果船上没有船舶(这仍是一个有效的船上),则应归还价值。

问题回答

确实,你应该有一个10x10网,供你游戏。 然而,我认为这有点不同:

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

由于 Java没有“主人”的概念,因此产生了 Java记录。

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

在申请的某个地方,你应当储存les。 Probable a Map<int[], Ship>。 我没有这样想。 这里指THE。 这比我在你的问答部分中所发表的评论更加完整。 尽管如此,我认为“我的评论”有足够的技术细节可以回答这种问题。

当你玩 game和onHit(>时,你可以看看(x,y)中的关键所在,并带上船(主机)。 “onHit()”方法应当能够确定协调(验证)的配对识别码,将电网中的识别码替换为x,并确定船舶是否沉降。 为此,指导员将提供指导,以便你能够研究在THAT DIRECTION(为“V”和“H”旁边的)电网中的邻近位置。 如果它发现更多的国际发展价值,该船就不是沉没的,而游戏则继续。 红十字与红新月联会船舶沉积,该方法应当从地图上删除这一条目。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签