将第二艘船实现为一维战舰游戏Java

Implementing a second ship into one dimensional battleship game Java

我将战舰游戏设计为只隐藏一艘船,现在我必须在游戏中实现另一艘船。我是 Java 的新手,想知道是否有人可以为我提供一个简单的方法来解决这个问题。我需要创建另一个方法还是只调整显示河流方法?

        public static void displayRiver(int[] river, boolean showShip) {
            System.out.println();
            System.out.print("|");
            for (int val : river) {
                switch (val) {
                    case -1: // No Ship
                    System.out.print("x");
                    break;
                    case 0: // Unknown
                    System.out.print(" ");
                    break;
                    case 1: // Ship Found
                    System.out.print(showShip ? "Y" : " ");
                    break;
                }//switch
                System.out.print("|");
            }//for
            System.out.println();
            System.out.println();
        }//displayRiver
    
        // main method
        public static void main(String[] arg) {
            int riverLength = promptForInt("Please, enter the river lenght"); 
            int [] shipArray = new int[riverLength]; 
         
            int randomBattleshipLocation = new Random().nextInt(riverLength); 
            shipArray[randomBattleshipLocation] = 1; 
         
            boolean showShip = false ; 
            int userGuess; 
    
            do
            {
                displayRiver (shipArray, false);
                userGuess = promptForInt(String.format("Guess, enter a location from 1 to " + riverLength));
                userGuess = userGuess -1; 
    
                if(shipArray[userGuess] == 1) 
                {
                    System.out.println("Boom! ");
                    showShip = true; 
                    displayRiver(shipArray, true);
                }
                else if(shipArray[userGuess] == -1)
                {
                    System.out.println("Location was already hit, try again! ");
                }
                else if(shipArray[userGuess] == 0) 
                {
                    System.out.println("Splash...");
                    shipArray[userGuess] = -1 ; 
                }
    
            } while(!showShip); 
    
            System.exit(0);
    
        }
    }

你的逻辑似乎是数组中的 1 表示一艘船,而你的船显然宽度永远不会超过一个。

您目前使用以下创建一个 ship

int randomBattleshipLocation = new Random().nextInt(riverLength); 
shipArray[randomBattleshipLocation] = 1;

所以你可以把它变成一个创建战舰的方法,然后为多艘船调用它多次。只要确保你没有在另一艘船的顶部分配一艘船,或者犯另一个逻辑错误(比如试图将 5 艘船放入一条 4 号河流,它会永远循环寻找 space for船)。

伪代码和非伪代码:

for(int i = 0;i < shipsToAdd; i++) {
    addShip(shipArray);
}

// Use a shared class-level Random object, don't do new Random().nextInt();
private static Random rnd = new Random();
private static addShip(int[] array) {
    // Here you should loop to check if the array is already full of ships
    // otherwise it's a design flaw that will result in an infinite loop with bad input

    // loop until we find a shipless array index
    int index = rnd.nextInt(array);
    while(array[index] == 1)
       index = rnd.nextInt(array);
    array[index] = 1;
}