java - Random generator for two dimensional array, Incriminating string letters for simple game
问题描述
I am creating a Battleship game and currently in the process of placing the ships on my array field that is 9x9. I am having trouble setting the size and letter of my ships. I need my random generator to create ships that have: A 10% chance of being two letters. A 30% chance of being three letters. A 50% chance of being five letters in any horizontal or landscape position that fits on the array grid. I need guidance or tips on a way to implement this feature. Ex:
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 . . . . . .
Any help as to what method my problem is in would be appreciate. This is a learning experience for me so Id like guidance not exact solutions if possible.
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 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.
}
附加:阅读官方 Java 文档seed
中的值意味着什么。
推荐阅读
- sql - 如何在组合查询中编辑字段
- google-app-maker - 如何在谷歌应用程序制造商中使用@expr 数据进行绑定转换器?
- vb.net - 如何将对象传递给默认表单实例
- java - 来自 ExpandableListView 的子元素中的 setOnClickListener
- r - 根据用户输入日期添加新的计算列
- jenkins - Jenkins 多分支与不同仓库中的 Jenkinsfile
- tomcat - Web 应用程序 [gestionMines] 似乎已经启动了一个名为 [Abandoned connection cleanup thread] 的线程,但未能停止它
- git - 如何确定您的默认远程 git 存储库?
- docker - /var/run/docker.sock:连接:ecs 集群上的 Jenkins 从属权限被拒绝
- java - 在更新到 SpringBoot2 后监听会话事件不起作用