在一维战舰游戏java中实现第二艘战舰

ac1kyiln  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(385)

我已经设计了战舰游戏,只有一艘船隐藏,现在我必须实现另一艘船到游戏中。我是java新手,想知道是否有人能给我一个简单的方法来解决这个问题。我需要创建另一个方法还是调整display river方法?

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

        }
    }
jdzmm42g

jdzmm42g1#

你的逻辑似乎是 1 在数组中表示一艘飞船,而你的飞船的宽度显然永远不会超过一艘。
您当前使用以下命令创建一艘船

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

所以你可以把它转换成一个创建战舰的方法,然后对多艘战舰调用它。只是要确保你没有把一艘船放在另一艘船的上面,或者犯下另一个逻辑错误(比如试图把5艘船放进一条4号的河中,它会一直循环试图为船只寻找空间)。
伪代码和非伪代码:

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

相关问题