我想制作两人一战的战舰游戏,一个人,另一个电脑。游戏板为10x10。每个玩家有5艘船,这些船由计算机随机放置在棋盘上。船不能水平或垂直彼此相邻。每个玩家通过给出行号和列号在其他玩家区域射击一个空间。行号和列号从0开始。如果拍摄成功,即其中一艘船受到打击,则在屏幕上显示“ *”字符。否则,将显示为带有“ x”字母的缺失。射击成功后,随着该船沉没,还必须提供信息。船上的所有位置都将显示为“ s”字符。当玩家的所有船只沉没时,游戏结束。我以这种方式编写了代码,但是缺少了一些东西。它必须获得x和y坐标5次,但它需要超过5次并且不会放置飞船。我需要帮助。
import java.util.Scanner;
public class amiralbatti {
public static int numRows = 10;
public static int numCols = 10;
public static int playerShips;
public static int computerShips;
public static String[][] grid = new String[numRows][numCols];
public static int[][] missedGuesses = new int[numRows][numCols];
public static void main(String[] args){
System.out.println("**** Welcome to Amiral Batti game ****");
//Step 1 – Create the ocean map
createOceanMap();
//Step 2 – Deploy player’s ships
deployPlayerShips();
//Step 3 - Deploy computer's ships
deployComputerShips();
//Step 4 Battle
do {
Battle();
}
while(amiralbatti.playerShips != 0 && amiralbatti.computerShips!= 0);
//Step 5 - Game over
gameOver();
}
public static void createOceanMap(){
//First section of Ocean Map
System.out.print(" ");
System.out.println("\t0 \t1 \t2 \t3 \t4 \t5 \t6 \t7 \t8 \t9");
//Middle section of Ocean Map
for(int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
grid[i][j] = "\t" + ".";
if (j == 0)
System.out.print(i + grid[i][j]);
else
System.out.print(grid[i][j]);
}
System.out.println();
}
}
public static void deployPlayerShips(){
Scanner scn = new Scanner(System.in);
System.out.println("\nDeploy your ships:");
//Deploying five ships for player
amiralbatti.playerShips = 5;
for (int i = 1; i <= amiralbatti.playerShips; ) {
System.out.print("Enter X coordinate for your " + i + " ship: ");
int x = scn.nextInt();
System.out.print("Enter Y coordinate for your " + i + " ship: ");
int y = scn.nextInt();
if((x >= 0 && x < numRows) && (y >= 0 && y < numCols) && (grid[x][y] == "."))
{
grid[x][y] = "o";
i++;
}
else if((x >= 0 && x < numRows) && (y >= 0 && y < numCols) && grid[x][y] == "o")
System.out.println("You can't place two or more ships on the same location");
else if((x < 0 || x >= numRows) || (y < 0 || y >= numCols))
System.out.println("You can't place ships outside the " + numRows + " by " + numCols + " grid");
}
printOceanMap();
}
public static void deployComputerShips(){
System.out.println("\nComputer is deploying ships");
//Deploying five ships for computer
amiralbatti.computerShips = 5;
for (int i = 1; i <= amiralbatti.computerShips; ) {
int x = (int)(Math.random() * 10);
int y = (int)(Math.random() * 10);
if((x >= 0 && x < numRows) && (y >= 0 && y < numCols) && (grid[x][y] == "."))
{
grid[x][y] = "x";
System.out.println(i + ". ship DEPLOYED");
i++;
}
}
printOceanMap();
}
public static void Battle(){
playerTurn();
computerTurn();
printOceanMap();
System.out.println();
System.out.println("Your ships: " + amiralbatti.playerShips + " | Computer ships: " + amiralbatti.computerShips);
System.out.println();
}
public static void playerTurn(){
System.out.println("\nYOUR TURN");
int x = -1, y = -1;
do {
Scanner input = new Scanner(System.in);
System.out.print("Enter X coordinate: ");
x = input.nextInt();
System.out.print("Enter Y coordinate: ");
y = input.nextInt();
if ((x >= 0 && x < numRows) && (y >= 0 && y < numCols)) //valid guess
{
if (grid[x][y] == "x") //if computer ship is already there; computer loses ship
{
System.out.println("Boom! You sunk the ship!");
grid[x][y] = "s"; //Hit mark
--amiralbatti.computerShips;
}
else if (grid[x][y] == "o") {
System.out.println("Oh no, you sunk your own ship :(");
grid[x][y] = "x";
--amiralbatti.playerShips;
++amiralbatti.computerShips;
}
else if (grid[x][y] == " ") {
System.out.println("Sorry, you missed");
grid[x][y] = "-";
}
}
else if ((x < 0 || x >= numRows) || (y < 0 || y >= numCols)) //invalid guess
System.out.println("You can't place ships outside the " + numRows + " by " + numCols + " grid");
}while((x < 0 || x >= numRows) || (y < 0 || y >= numCols)); //keep re-prompting till valid guess
}
public static void computerTurn(){
System.out.println("\nCOMPUTER'S TURN");
//Guess co-ordinates
int x = -1, y = -1;
do {
x = (int)(Math.random() * 10);
y = (int)(Math.random() * 10);
if ((x >= 0 && x < numRows) && (y >= 0 && y < numCols)) //valid guess
{
if (grid[x][y] == "o") //if player ship is already there; player loses ship
{
System.out.println("The Computer sunk one of your ships!");
grid[x][y] = "x";
--amiralbatti.playerShips;
++amiralbatti.computerShips;
}
else if (grid[x][y] == "x") {
System.out.println("The Computer sunk one of its own ships");
grid[x][y] = "s";
}
else if (grid[x][y] == " ") {
System.out.println("Computer missed");
//Saving missed guesses for computer
if(missedGuesses[x][y] != 1)
missedGuesses[x][y] = 1;
}
}
}while((x < 0 || x >= numRows) || (y < 0 || y >= numCols)); //keep re-prompting till valid guess
}
public static void gameOver(){
System.out.println("Your ships: " + amiralbatti.playerShips + " | Computer ships: " + amiralbatti.computerShips);
if(amiralbatti.playerShips > 0 && amiralbatti.computerShips <= 0)
System.out.println("Hooray! You won the battle :)");
else
System.out.println("Sorry, you lost the battle");
System.out.println();
}
public static void printOceanMap(){
System.out.print(" ");
System.out.println("\t0 \t1 \t2 \t3 \t4 \t5 \t6 \t7 \t8 \t9");
//Middle section of Ocean Map
for(int x = 0; x < grid.length; x++) {
System.out.print(x);
for (int y = 0; y < grid[x].length; y++){
System.out.print(grid[x][y]);
}
}
}
}
在for循环中,您可以像这样进行比较:
grid[x][y] == "."
但是您可以像这样初始化网格:
grid[i][j] = "\t" + ".";
由于那永远不会匹配,因此您永远不会输入使i递增并放置飞船的if条件。
通常,我会说将制表符放在网格中是不明智的。最佳实践是将游戏模型(您的海洋/船舶网格)和该模型的可视化(屏幕上的输出)分开。
哦,而且,也可以equals
用来比较字符串==
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句