我是Java编程的新手。在我的工作和学习场所,我要负责编写某种“战舰精简版”的程序。
到目前为止,我有以下代码:
import java.io.*;
public class temp {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
static boolean[][] playfield = new boolean[11][11];
public static void main(String[] args) throws IOException {
String[][] field = new String[15][15];
char[] topnumbers = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
char[] lownumbers = {'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
//------2XShip-----------------------------------------------------------------
for (int x2ships = 1; x2ships <= 3; x2ships++) {
int x2rnd = (int) (Math.random() * 10) + 1;
for (int y2ships = 1; y2ships <= 1; y2ships++) {
int y2rnd = (int) (Math.random() * 10) + 1;
playfield[x2rnd][y2rnd] = true;
int direction = (int) (Math.random() * 4) + 1;
switch (direction) {
case 1:
//Left
if (x2rnd == 1) {
x2rnd = x2rnd + 1;
playfield[x2rnd][y2rnd] = true;
} else {
x2rnd = x2rnd - 1;
playfield[x2rnd][y2rnd] = true;
}
break;
case 2:
//Right
if (x2rnd == 10) {
x2rnd = x2rnd - 1;
playfield[x2rnd][y2rnd] = true;
} else {
x2rnd = x2rnd + 1;
playfield[x2rnd][y2rnd] = true;
}
break;
case 3:
//Top
if (y2rnd == 1) {
y2rnd = y2rnd + 1;
playfield[x2rnd][y2rnd] = true;
} else {
y2rnd = y2rnd - 1;
playfield[x2rnd][y2rnd] = true;
}
break;
case 4:
//Bottom
if (y2rnd == 10) {
y2rnd = y2rnd - 1;
playfield[x2rnd][y2rnd] = true;
} else {
y2rnd = y2rnd + 1;
playfield[x2rnd][y2rnd] = true;
}
break;
}
}
}
//------3XShip-----------------------------------------------------------------
for (int x3ships = 1; x3ships <= 2; x3ships++) {
int x3rnd = (int) (Math.random() * 10) + 1;
for (int y3ships = 1; y3ships <= 1; y3ships++) {
int y3rnd = (int) (Math.random() * 10) + 1;
playfield[x3rnd][y3rnd] = true;
int direction = (int) (Math.random() * 4) + 1;
switch (direction) {
case 1:
//Left
if (x3rnd < 3) {
x3rnd = x3rnd + 1;
playfield[x3rnd][y3rnd] = true;
x3rnd = x3rnd + 1;
playfield[x3rnd][y3rnd] = true;
} else {
x3rnd = x3rnd - 1;
playfield[x3rnd][y3rnd] = true;
x3rnd = x3rnd - 1;
playfield[x3rnd][y3rnd] = true;
}
break;
case 2:
//Right
if (x3rnd > 8) {
x3rnd = x3rnd - 1;
playfield[x3rnd][y3rnd] = true;
x3rnd = x3rnd - 1;
playfield[x3rnd][y3rnd] = true;
} else {
x3rnd = x3rnd + 1;
playfield[x3rnd][y3rnd] = true;
x3rnd = x3rnd + 1;
playfield[x3rnd][y3rnd] = true;
}
break;
case 3:
//Top
if (y3rnd < 3) {
y3rnd = y3rnd + 1;
playfield[x3rnd][y3rnd] = true;
y3rnd = y3rnd + 1;
playfield[x3rnd][y3rnd] = true;
} else {
y3rnd = y3rnd - 1;
playfield[x3rnd][y3rnd] = true;
y3rnd = y3rnd - 1;
playfield[x3rnd][y3rnd] = true;
}
break;
case 4:
//Bottom
if (y3rnd > 8) {
y3rnd = y3rnd - 1;
playfield[x3rnd][y3rnd] = true;
y3rnd = y3rnd - 1;
playfield[x3rnd][y3rnd] = true;
} else {
y3rnd = y3rnd + 1;
playfield[x3rnd][y3rnd] = true;
y3rnd = y3rnd + 1;
playfield[x3rnd][y3rnd] = true;
}
break;
}
}
}
//------4XShip-----------------------------------------------------------------
for (int x4ships = 1; x4ships <= 1; x4ships++) {
int x4rnd = (int) (Math.random() * 10) + 1;
for (int y4ships = 1; y4ships <= 1; y4ships++) {
int y4rnd = (int) (Math.random() * 10) + 1;
playfield[x4rnd][y4rnd] = true;
int direction = (int) (Math.random() * 4) + 1;
switch (direction) {
case 1:
//Left
if (x4rnd < 4) {
x4rnd = x4rnd + 1;
playfield[x4rnd][y4rnd] = true;
x4rnd = x4rnd + 1;
playfield[x4rnd][y4rnd] = true;
x4rnd = x4rnd + 1;
playfield[x4rnd][y4rnd] = true;
} else {
x4rnd = x4rnd - 1;
playfield[x4rnd][y4rnd] = true;
x4rnd = x4rnd - 1;
playfield[x4rnd][y4rnd] = true;
x4rnd = x4rnd - 1;
playfield[x4rnd][y4rnd] = true;
}
break;
case 2:
//Right
if (x4rnd > 7) {
x4rnd = x4rnd - 1;
playfield[x4rnd][y4rnd] = true;
x4rnd = x4rnd - 1;
playfield[x4rnd][y4rnd] = true;
x4rnd = x4rnd - 1;
playfield[x4rnd][y4rnd] = true;
} else {
x4rnd = x4rnd + 1;
playfield[x4rnd][y4rnd] = true;
x4rnd = x4rnd + 1;
playfield[x4rnd][y4rnd] = true;
x4rnd = x4rnd + 1;
playfield[x4rnd][y4rnd] = true;
}
break;
case 3:
//Top
if (y4rnd < 4) {
y4rnd = y4rnd + 1;
playfield[x4rnd][y4rnd] = true;
y4rnd = y4rnd + 1;
playfield[x4rnd][y4rnd] = true;
y4rnd = y4rnd + 1;
playfield[x4rnd][y4rnd] = true;
} else {
y4rnd = y4rnd - 1;
playfield[x4rnd][y4rnd] = true;
y4rnd = y4rnd - 1;
playfield[x4rnd][y4rnd] = true;
y4rnd = y4rnd - 1;
playfield[x4rnd][y4rnd] = true;
}
break;
case 4:
//Bottom
if (y4rnd > 7) {
y4rnd = y4rnd - 1;
playfield[x4rnd][y4rnd] = true;
y4rnd = y4rnd - 1;
playfield[x4rnd][y4rnd] = true;
y4rnd = y4rnd - 1;
playfield[x4rnd][y4rnd] = true;
} else {
y4rnd = y4rnd + 1;
playfield[x4rnd][y4rnd] = true;
y4rnd = y4rnd + 1;
playfield[x4rnd][y4rnd] = true;
y4rnd = y4rnd + 1;
playfield[x4rnd][y4rnd] = true;
}
break;
}
}
}
System.out.print(" ");
System.out.print(" " + topnumbers[1] + " ");
System.out.print(" " + topnumbers[2] + " ");
System.out.print(" " + topnumbers[3] + " ");
System.out.print(" " + topnumbers[4] + " ");
System.out.print(" " + topnumbers[5] + " ");
System.out.print(" " + topnumbers[6] + " ");
System.out.print(" " + topnumbers[7] + " ");
System.out.print(" " + topnumbers[8] + " ");
System.out.print(" " + topnumbers[9] + " ");
System.out.print(" " + topnumbers[10] + " ");
System.out.println();
for (int y = 1; y <= 10; y++) {
System.out.print(lownumbers[y] + " ");
for (int x = 1; x <= 10; x++) {
field[x][y] = ANSI_BLUE + " \u25A0 " + ANSI_RESET; // Black squares
if (playfield[x][y]) {
System.out.print(ANSI_GREEN + " \u25A1 " + ANSI_RESET); // Green squares
} else {
System.out.print(field[x][y]);
}
}
System.out.println();
}
//Samples
field[5][5] = "\u25c9"; // Circle with circle
System.out.print(field[5][5]);
field[5][5] = "\u25cb"; // Circle empty
System.out.print(field[5][5]);
field[5][5] = "\u25c8"; // Rhombus with rhombus
System.out.print(field[5][5]);
field[5][5] = "\u25cc"; // Dotted Circle
System.out.print(field[5][5]);
}
}
但是我遇到了问题-我不知道如何检查我的船在哪里。 =>我无法检查它们之间的空间。 =>他们经常彼此呆在一起。
您是否有解决此问题的想法? 预先感谢!