【问题标题】:Implementing a second ship into one dimensional battleship game Java在一维战舰游戏Java中实现第二艘船
【发布时间】:2021-03-05 07:06:32
【问题描述】:

我设计的战舰游戏只隐藏了一艘船,现在我必须在游戏中实现另一艘船。我是 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);
    
        }
    }

【问题讨论】:

    标签: java arrays bluej


    【解决方案1】:

    您的逻辑似乎是数组中的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;
    }
    

    【讨论】:

    • 除了 Kayamans 注释,您可能还想确保用户猜测实际上是一个介于 1 和 riverLength 之间的数字,否则部分 displayRiver (shipArray, false); userGuess = promptForInt(String.format("猜猜,输入一个从1到"的位置" + riverLength));用户猜测 = 用户猜测 -1; if(shipArray[userGuess] == 1) 将导致 IndexOutOufBoundsException。祝你的 Java 之旅好运和神速:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2018-01-05
    • 2019-07-05
    相关资源
    最近更新 更多