控制台上简易的海战棋:
在这里插入代码片
import java.util.Scanner;
public class Battleship {
public static int pB = 7;
public static int pC = 9;
public static int pP = 5;
public static int cB = 7;
public static int cC = 9;
public static int cP = 5;
public static String[][] drawBoard(int width, int height) {
String[][] board = new String[width][height];
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
board[i][j] = "_";
}
}
//draw the battleship
int finishBattleship = 1;
while(finishBattleship > 0) {
int x = (int)(Math.random() * (height - 1));
int y = (int)(Math.random() * (width - 4));
board[x + 1][y] = "B";
board[x + 1][y + 1] = "B";
board[x + 1][y + 2] = "B";
board[x + 1][y + 3] = "B";
board[x + 1][y + 4] = "B";
board[x][y + 1] = "B";
board[x][y + 2] = "B";
finishBattleship--;
}
//draw the chasers
int finishChaser = 3;
while (finishChaser > 0) {
int x = (int)(Math.random() * (width - 3));
int y = (int)(Math.random() * height);
if (board[x][y] == "_" && board[x + 1][y] == "_" && board[x