java - 在一维游戏中实现第二艘战舰 "River Battleship"

标签 java

是否有人能够为我提供一种简单的方法,在我设计的只有一个敌人的游戏中实现第二艘敌方战舰,这是我目前的代码:

import java.util.*; //Scanner that reads user input

public static void main(String[] arg) 
    {
        int riverLength = promptForInt("Select Length for River ") ; //Variable that stores the user input when prompted
        int [] shipArray = new int[riverLength] ; //User input is used to create an Array, i.e. River Size
        int battleshipCoordinates = new Random().nextInt(riverLength) ; //Random number is found within the Array (Where the enemy ship will hide) 

        shipArray[battleshipCoordinates] = 1 ; 
        boolean hit = false ; //Statement created for ship hit and default to false
        int playerSelection; //int Variable declared

        do
       {
           displayRiver (shipArray, false);
           playerSelection = promptForInt(String.format("Select location to launch torpedo (1 - %d) ", riverLength));
           playerSelection = playerSelection -1 ; 

           if(shipArray[playerSelection] == 1 ) //if a user strikes the Enemy (Case 1) correctly they system informs them of a strike
           {
            System.out.println("Battleship Sunk!");
            hit = true; 
            displayRiver(shipArray, true);
           }
           else if(shipArray[playerSelection] == -1)
           {
            System.out.println("Location already Hit! Try again.");
           }
           else if(shipArray[playerSelection] == 0)
           {
            System.out.println("Miss!");
            shipArray[playerSelection] = -1 ; 
           }

       } while(!hit); 




    }
}

最佳答案

第 0 步 - 创建一艘新战舰并将它们像第一艘战舰一样放置在阵列上。

第 1 步 - 将 boolean 命中更改为等于 2 的 int。

第 2 步 - 不要切换命中,而是在击中船只时将其减少 1,然后设置 数组上的该位置为 0。

第 3 步 - 调整你的逻辑,这样除非击中 <1,否则你不会获胜

关于java - 在一维游戏中实现第二艘战舰 "River Battleship",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58920194/

相关文章:

java - 无法从某些网址获取图像内容

java - 有最小消息队列吗?

java - 如何通过测试解决 API 调用上的错误代码 "405"?

java - 无法直接调用 TextToSpeech 实例的 speak()

java - 编写任务执行(预/后)脚本 [Gradle + IntelliJ]

java - 在方法中创建Hashset对象会减少java中的堆栈内存

java - libgdx 无法将纹理加载到数组

java - 解析xml以查找java中的元素

java - 从.html页面java解析文本

java - 如何简单地读取我创建的 .txt 文件并计算其行数