我正在创建一款战舰游戏,目前正在将战舰放置在 9x9 的阵列字段中。我在设置船只的尺寸和字母时遇到问题。我需要我的随机生成器来创建具有以下功能的船只: 10% 的机会是两个字母。 30% 的机会是三个字母。 在适合阵列网格的任何水平或横向位置上有 50% 的机会出现五个字母。我需要有关实现此功能的方法的指导或提示。 例如:
0 1 2 3 4 5 6 7 8 9
0 x . x . . . . . . x
1 . x * * * x . . x .
2 . x * * * * * x . .
3 . . * x . . . . . .
4 . . * E E E . . . .
5 . . * . F F F F . .
6 . A A A A . G . . .
7 . . x . . . G . . .
8 . . . . . x . . . .
9 x . C C . . . . . .
任何有关我的问题所在的方法的帮助将不胜感激。这对我来说是一次学习经历,所以如果可能的话,我想要指导而不是精确的解决方案。
import java.util.*;
import java.util.Scanner;
public class BattleshipLab {
public static void main(String[] args){
int shotHit = 0;
int size = 0;
System.out.println("Welcome to the Ship Sinking Game");
String[][] gameBoard = new String[11][11];
makeBoard(gameBoard);
for(size == 0; Math.random() < gameBoad.length){
size = Math.random();
}
while(shotHit < 25){
showGameBoard(gameBoard);
shotHit = playerTurn(gameBoard, size);
}
}
// Creates a board with ".".
public static void makeBoard(String[][] gameBoard) {
for (int row = 0; row < gameBoard.length; row++) {
for (int col = 0; col < gameBoard[0].length; col++) {
gameBoard[row][col] = ".";
}
}
}
public static void showGameBoard(String[][] gameBoard) {
for (int row = 0; row < gameBoard.length; row++) {
for (int col = 0; col < gameBoard[0].length; col++) {
gameBoard[0][0] = " ";
// Row numbers
gameBoard[0][1] = "0";
gameBoard[0][2] = "1";
gameBoard[0][3] = "2";
gameBoard[0][4] = "3";
gameBoard[0][5] = "4";
gameBoard[0][6] = "5";
gameBoard[0][7] = "6";
gameBoard[0][8] = "7";
gameBoard[0][9] = "8";
gameBoard[0][10] = "9";
// Col numbers
gameBoard[1][0] = "0";
gameBoard[2][0] = "1";
gameBoard[3][0] = "2";
gameBoard[4][0] = "3";
gameBoard[5][0] = "4";
gameBoard[6][0] = "5";
gameBoard[7][0] = "6";
gameBoard[8][0] = "7";
gameBoard[9][0] = "8";
gameBoard[10][0] = "9";
if (gameBoard[row][col].equals("A")) {
System.out.print(" " + ".");
} else {
System.out.print(" " + gameBoard[row][col]);
}
}
System.out.println("");
}
}
public static void ship(String[][] gameBoard, int size) {
if (Math.random() < 1) {
int col = (int) (Math.random());
int row = (int) (Math.random());
for (int i = 0; i < size; i++) {
gameBoard[row][col] = "A";
}
}
}
public static int playerTurn(String[][] gameBoard, int shotHit) {
Scanner input = new Scanner(System.in);
int row;
int col;
System.out.println("Enter a coordinate(0..9) for target:");
row = input.nextInt();
System.out.println("Enter a coordinate(0..9) for target:");
col = input.nextInt();
if (gameBoard[row + 1][col + 1].equals("A")) {
shotHit++;
System.out.println("Good shot! A ship was hit.\n");
gameBoard[row + 1][col + 1] = "*";
} else {
System.out.println("\t No ships were hit.\n");
gameBoard[row + 1][col + 1] = "x";
}
return shotHit;
}
}
最佳答案
我们可以通过检查随机生成的数字(例如 10)来获得概率。 Random
生成的数字与其先前的值无关,因此,
- 生成数字的概率变为
0
= 1/10 = 0.1 = 10% - 生成数字的概率变为
1或2或3
= 3/10 = 0.3 = 30% - 生成数字的概率变为
4 或 5 .. 9
= 6/10 = 0.6 = 60%
请注意,所有给定概率的总和加起来不等于 100% (50+30+10 = 90)。
这是上面的代码。
Random r = new Random(System.currentTimeMillis()); // change this seed value to a desired but reproducible number
int N = 5; // number of ships
for (int i = 0; i < N; i++) {
int shipP = r.nextInt(10);
if (shipP < 1) {
// only 0 (10% probability)
// ship = "AA";
} else if (shipP < 4) {
// works when 1, 2, 3
// ship = "BBB";
} else {
// works when 4, 5 .. 9
// ship = "CCCCC";
}
// generate orientation randomly (probability 0.5)
boolean vertical = r.nextInt(2) == 0;
// now find a suitable position in grid and place the ship.
}
其他:了解 Official Java doc. 中的 seed
值的含义。
关于java - 二维数组的随机生成器,简单游戏的累加字符串字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53587176/