簡體   English   中英

將第二艘船實現為一維戰艦游戲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表示一艘船,而您的船的寬度顯然永遠不會超過 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM