我已经连续几周都参加了这艘战列舰的编码,我真的很难将多舱船放入其中。该程序工作正常,但我想添加包含2-4个单元格的船只。如果有人能给我提供非常好的帮助,我已经尽了所有努力无济于事。
这是我的代码,可能有点令人困惑:
import java.util.Random;
import java.util.Scanner;
public class Battleship
{
private static int counter = 0;
private static boolean flag = true;
public static void main(String[] args)
{
int[][] boardP1 = new int[10][10];
int[][] boardP2 = new int[10][10];
int[][] shipsP1 = new int[10][2];
int[][] shipsP2 = new int[10][2];
int[] shootP1 = new int[2];
int[] shootP2 = new int[2];
int shotHitP1 = 0, shotHitP2 = 0;
Scanner userInput = new Scanner(System.in);
String p1name = null, p2name = null;
// Welcome message
System.out.println("\033[2J\033[10;28f WELCOME TO BATTLESHIPS!");
System.out.print("\033[13;31f Player 1: ");
p1name = userInput.nextLine();
System.out.print("\033[15;31f Player 2: ");
p2name = userInput.nextLine();
// Clear screen
System.out.println("\033[2J");
// Initialize boards (random)
initBoardsP1(boardP1);
initBoardsP2(boardP2);
// Initialize ships (random)
initShips(shipsP1);
initShips(shipsP2);
do
{ // Display boards
showBoardP1(boardP1);
showBoardP2(boardP2);
// P1 ask for shot
shootP1(shootP1);
counter++;
if (hit(shootP2,shipsP2))
{
shotHitP1++;
if (shotHitP1 == 5)
{ System.out.println("\n\n\n" +p1name+ " has won the game!");
System.out.println(); }
}
else
{ System.out.println("\033[48;36f Miss!"); }
changeboardP2(shootP1, shipsP1, boardP1);
System.out.print("\033[2J");
// P2 Ask for shot
showBoardP1(boardP1);
showBoardP2(boardP2);
shootP2(shootP2);
counter++;
if (hit(shootP1,shipsP1))
{
shotHitP2++;
if (shotHitP2 == 5)
{ System.out.println("\n\n\n" +p2name+ " has won the game!");
System.out.println(); }
}
else
{ System.out.println();
System.out.println("You missed!");
System.out.println(); }
changeboardP1(shootP2, shipsP2, boardP2);
System.out.print("\033[2J");
} while (shotHitP1 != 5 || shotHitP2 != 5);
}
public static void initShips(int[][] ships)
{
Random random = new Random();
for (int ship = 0; ship < 10; ship++)
{
ships[ship][0] = random.nextInt(10); // Draws row coordinate
ships[ship][1] = random.nextInt(10); // Draws column coordinate
// Check to see if already used combo
for (int last = 0; last < ship; last++)
{
if ((ships[ship][0] == ships[last][0]) && (ships[ship][1] == ships[last][1]))
do
{ ships[ship][0] = random.nextInt(10);
ships[ship][1] = random.nextInt(10); }
while((ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]));
}
}
}
public static void initBoardsP1(int[][] boardP1)
{ for (int row = 0; row < 10; row++)
for (int column = 0; column < 10; column++)
boardP1[row][column] = -1; }
public static void initBoardsP2(int[][] boardP2)
{ for (int row = 0; row < 10; row++)
for (int column = 0; column < 10; column++)
boardP2[row][column] = -1; }
public static void showBoardP1(int[][] boardP1)
{
if (flag = true)
{ System.out.println("\033[9;15f PLAYER 2");
System.out.println("\033[12;5f 1 2 3 4 5 6 7 8 9 10");
System.out.println("\033[13;4f +-++-++-++-++-++-++-++-++-++-+"); }
else
{ System.out.println("\033[9;54f PLAYER 1");
System.out.println("\033[12;45f 1 2 3 4 5 6 7 8 9 10");
System.out.println("\033[13;44f +-++-++-++-++-++-++-++-++-++-+"); }
for (int row = 0; row < 10; row++)
{
if (flag = true)
{ System.out.print("\033[1C"); }
else
{ System.out.print("\033[40C"); }
if (row <= 8)
{ System.out.print(" " +(row+1)+ " "); }
else
{ System.out.print(" 10 "); }
for (int column = 0; column < 10; column++)
{
System.out.print("|");
if (boardP1[row][column] == -1)
{ System.out.print(" "); }
else if (boardP1[row][column] == 0)
{ System.out.print("0"); }
else if (boardP1[row][column] == 1)
{ System.out.print("\033[1;31mX\033[0m"); }
System.out.print("|");
}
if (flag = true)
{ System.out.println();
System.out.println(" +-++-++-++-++-++-++-++-++-++-+"); }
else
{ System.out.println();
System.out.println("\033[40C +-++-++-++-++-++-++-++-++-++-+"); }
if ((counter % 100) == 0)
{ flag = true; }
else
{ flag = false; }
}
}
public static void showBoardP2(int[][] boardP2)
{
if (flag = true)
{ System.out.println("\033[9;54f PLAYER 1");
System.out.println("\033[12;45f 1 2 3 4 5 6 7 8 9 10");
System.out.println("\033[13;44f +-++-++-++-++-++-++-++-++-++-+"); }
else
{ System.out.println("\033[9;15f PLAYER 2");
System.out.println("\033[12;5f 1 2 3 4 5 6 7 8 9 10");
System.out.println("\033[13;4f +-++-++-++-++-++-++-++-++-++-+"); }
for (int row = 0; row < 10; row++)
{
if (flag = true)
{ System.out.print("\033[40C"); }
else
{ System.out.print("\033[1C"); }
if (row <= 8)
{ System.out.print(" " +(row+1)+ " "); }
else
{ System.out.print(" 10 "); }
for (int column = 0; column < 10; column++)
{
System.out.print("|");
if (boardP2[row][column] == -1)
{ System.out.print(" "); }
else if (boardP2[row][column] == 0)
{ System.out.print("0"); }
else if (boardP2[row][column] == 1)
{ System.out.print("\033[1;31mX\033[0m"); }
System.out.print("|");
}
if (flag = true)
{ System.out.println();
System.out.println("\033[40C +-++-++-++-++-++-++-++-++-++-+"); }
else
{ System.out.println();
System.out.println(" +-++-++-++-++-++-++-++-++-++-+"); }
if ((counter % 100) == 0)
{ flag = true; }
else
{ flag = false; }
}
}
public static void shootP1(int[] shootP1)
{
Scanner userInput = new Scanner(System.in);
System.out.println("\033[38;26f ----- PLAYER ONE'S SHOT -----");
System.out.println("\033[40;27f (Legend: 0 - Miss, \033[1;31mX\033[0m - Hit)");
System.out.println("\033[55;21f Press any non-integer character to quit.");
System.out.print("\033[44;35f Row: ");
shootP1[0] = userInput.nextInt();
shootP1[0]--;
System.out.print("\033[45;32f Column: ");
shootP1[1] = userInput.nextInt();
shootP1[1]--;
}
public static void shootP2(int[] shootP2)
{
Scanner input = new Scanner(System.in);
System.out.println("\033[38;26f ----- PLAYER TWO'S SHOT -----");
System.out.println("\033[40;27f (Legend: 0 - Miss, \033[1;31mX\033[0m - Hit)");
System.out.println("\033[55;21f Press any non-integer character to quit.");
System.out.print("\033[44;35f Row: ");
shootP2[0] = input.nextInt();
shootP2[0]--;
System.out.print("\033[45;32f Column: ");
shootP2[1] = input.nextInt();
shootP2[1]--;
}
public static boolean hit(int[] shoot, int[][] ships)
{
for (int ship = 0; ship < ships.length; ship++)
{
if (shoot[0] == ships[ship][0] && shoot[1] == ships[ship][1])
{
System.out.println("\033[48;34f KABOOM!!");
System.out.printf("\033[50;23f You hit a ship located in (%d,%d)!\n\n", shoot[0]+1, shoot[1]+1);
return true;
}
}
return false;
}
public static void changeboardP1(int[] shoot, int[][] ships, int[][] boardP1)
{
if (hit(shoot,ships))
{ boardP1[shoot[0]][shoot[1]] = 1; }
else
{ boardP1[shoot[0]][shoot[1]] = 0; }
}
public static void changeboardP2(int[] shoot, int[][] ships, int[][] boardP2)
{
if (hit(shoot,ships))
{ boardP2[shoot[0]][shoot[1]] = 1; }
else
{ boardP2[shoot[0]][shoot[1]] = 0; }
}
}
答案 0 :(得分:0)
我不了解你代码中的板/船阵列 什么是额外的船舶阵列?
我建议你默认在创建时为每个玩家的棋盘制作一个0阵列,然后通过在船只位置填写1来添加船只?并且可能稍后在游戏2中用于错过积分,3用于击中。
您需要的唯一额外阵列将是一个2D阵列,其中包含5行用于5艘船舶,2列指定船舶的初始和终止坐标,稍后可用于检查整艘船舶是否已被销毁