英文:
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 < battleField.length; i++){
for ( int j = 0; j < battleField[i].length; j++){
battleField[i][j] = '-';
}
}
battleField[((int)(Math.random()*10))][((int)(Math.random()*10))] = 'S';//assigns a random number between 1-10 and assigns it to either soldier/target
battleField[((int)(Math.random()*10))][((int)(Math.random()*10))] = 'T';
battleField[((int)(Math.random()*10))][((int)(Math.random()*10))] = '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();
}
}//end of fillGrid method
public static void main(String[] args) {
int columns = 10;
int rows = 10;
System.out.println("----------GRID----------");
//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] == '-') { // 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('T');
addChar('T');
addChar('S');
and none of the chosen spots will be duplicates
专注分享java语言的经验与见解,让所有开发者获益!
评论