簡體   English   中英

二維數組的隨機生成器,用於簡單游戲的包含字符串字母

[英]Random generator for two dimensional array, Incriminating string letters for simple game

我正在創建一個戰艦游戲,當前正在將飛船放置在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 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值的含義

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM