如何使用 math.random 在网格上重新随机化图表。

huangapple 未分类评论44阅读模式
英文:

How to rerandomize plots on a grid with math.random

问题

import java.lang.Math;

public class PaintBall_Thomas {
    public static void fillGrid(char[][] battleField) {
        for (int i = 0; i < battleField.length; i++) {
            for (int j = 0; j < battleField[i].length; j++) {
                battleField[i][j] = '-';
            }
        }

        int soldierRow = (int) (Math.random() * 10);
        int soldierCol = (int) (Math.random() * 10);
        battleField[soldierRow][soldierCol] = 'S';

        int target1Row, target1Col;
        do {
            target1Row = (int) (Math.random() * 10);
            target1Col = (int) (Math.random() * 10);
        } while (target1Row == soldierRow && target1Col == soldierCol);
        battleField[target1Row][target1Col] = 'T';

        int target2Row, target2Col;
        do {
            target2Row = (int) (Math.random() * 10);
            target2Col = (int) (Math.random() * 10);
        } while ((target2Row == soldierRow && target2Col == soldierCol) || (target2Row == target1Row && target2Col == target1Col));
        battleField[target2Row][target2Col] = 'T';

        for (int i = 0; i < battleField.length; i++) {
            for (int j = 0; j < battleField[i].length; j++) {
                System.out.print(battleField[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int columns = 10;
        int rows = 10;
        System.out.println("----------GRID----------");
        char[][] battleField = new char[rows][columns];
        fillGrid(battleField);
    }
}
英文:

I am assigning 3 chars (two 'T's and one 'S') to a 10 x 10 grid. I have that down but I want to know how I could re randomize the char's positions if they end up on the same spot. Basically I am creating a little game where I am a soldier (S) and am on a grid with two Targets (T). Eventually I will have to eliminate the targets but I fist I want to make sure each one ends up in it's own spot. here is my code

import java.lang.Math;
public class PaintBall_Thomas{
    public static void fillGrid(char[][] battleField){
      for ( int i = 0; i &lt; battleField.length; i++){
        for ( int j = 0; j &lt; battleField[i].length; j++){
          battleField[i][j] = &#39;-&#39;;
        }
      }
      battleField[((int)(Math.random()*10))][((int)(Math.random()*10))] = &#39;S&#39;;//assigns a random number between 1-10 and assigns it to either soldier/target
      battleField[((int)(Math.random()*10))][((int)(Math.random()*10))] = &#39;T&#39;;
      battleField[((int)(Math.random()*10))][((int)(Math.random()*10))] = &#39;T&#39;;
      for ( int i = 0; i &lt; battleField.length; i++){
        for ( int j = 0; j &lt; battleField[i].length; j++){

          System.out.print(battleField[i][j] + &quot; &quot;);
        }
        System.out.println();
      }
    }//end of fillGrid method
    public static void main(String[] args) {
      int columns = 10;
      int rows = 10;
      System.out.println(&quot;----------GRID----------&quot;);
      //end of main
      char[][] battleField = new char [rows][columns];
      fillGrid(battleField);
  }
}

答案1

得分: 0

这可能是递归的一个很好的用法,就像这样:

public void addChar(char type) {
    int row = (int)(Math.random() * battleField.length); // 随机选择位置
    int col = (int)(Math.random() * battleField.length);

    if (battleField[row][col] == '-') { // 如果选择的位置是空的
        battleField[row][col] = type; // 添加进去
    } else {
        addChar(type); // 如果位置被占用,再尝试一次
    }
}

所以这可能会有一些问题... 比如如果棋盘已经满了可能会导致堆栈溢出... 但是如果这个方法的唯一预期用途是添加两个 'T' 和一个 'S',应该没问题。

所以你可以这样添加它们:

addChar('T');
addChar('T');
addChar('S');

选择的位置都不会重复。

英文:

this could be a good use for recursion, so like

public void addChar(char type) {
    int row = (int)(Math.random() * battleField.length); // choose rand spot
    int col = (int)(Math.random() * battleField.length);

    if (battleField[row][col] == &#39;-&#39;) { // if chosen space is free
        battleField[row][col] = type; // add it
    } else {
        addChar(type) // try again if space was occupied
    }
}

so this could have some problems... such as possibilities of getting a stack overflow if the board is already full... but if the method's only intended use is to add the two Ts and one S, it should be fine

so you could just add them like

addChar(&#39;T&#39;);
addChar(&#39;T&#39;);
addChar(&#39;S&#39;);

and none of the chosen spots will be duplicates

huangapple
  • 本文由 发表于 2020年4月4日 13:44:14
  • 转载请务必保留本文链接:https://java.coder-hub.com/61024249.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定