我正在创建一个战舰游戏,目前正在将飞船放置在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;
}
}
答案 0 :(得分:1)
我们可以通过检查随机生成的数字(例如10)来获得概率。 Random
生成的数字与其先前的值无关,因此,
0
= 1/10 = 0.1 = 10%1 or 2 or 3
= 3/10 = 0.3 = 30%4 or 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
值