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