2016-08-10 11 views
1

特定の量の要素を持つ20×20 Char 2D 2D配列を作成しようとしています。たとえば、 A x 20 B X 10 Cが残りです。Java:特定の量の要素を持つ20x20 2D配列の方法

20として、ランダムに私はMath.randomでこれを達成しようとしています、20×20のグリッドの周りにランダムに20×20のグリッドの周囲に配置され10のBsを置き、そしてC.

ですべての空のスペースを埋める(があるでしょう

)しかしそうすることはできません。

package quest; 
import java.util.*; 
public class Quest 
{ 
    public static void main(String[] args) 
    { 
    char [][] board = new char [20][20]; 
    int j = 0; 
    for(int x = 0; x < 20; x++) 
    { 
    for(int y = 0; y < 20; y++) 
    { 
    for(int a = 0; a < 20; a++) 
    {  
    int chances = (int)(Math.random()*20)+1;  
    if (chances == 1) 
    {  
    board[x][y] = 'A'; 
    } 
    }  
    for(int b = 0; b < 10; b++) 
    {  
    int chances = (int)(Math.random()*20)+1;  
    if (chances == 1) 
    {  
    board[x][y] = 'B'; 
    } 
    if(board[x][y] == 0) 
    { 
    board[x][y] = 'C'; 
    }  
    }   
    System.out.print(board[x][y]+" "); 
    } 
    System.out.println(); 
    } 

} 

} 

答えて

0

ここでは、どのようにAsを追加できるかを示します。 Bsの場合は同じことをします。 Csの場合は、単にボードを通過し、空のセルにCsを挿入します。

char[][] board = new Char[20][20]; 
//Place As 
for (int i = 1; i <= 20; i++) { //20 As 
    int randX = (int) (Math.random() * 20); //random number between 0-19 
    int randY = (int) (Math.random() * 20); 
    while (board[randX][randY] != null) { //find a free cell 
     randX = (int) (Math.random() * 20); 
     randY = (int) (Math.random() * 20); 
     } 
     board[randX][randY] = 'A'; //insert A 
    } 

別の選択肢は、第1のCSと基板を充填し、次いで、細胞内のCがあるかのようにBsを挿入することです。これを行うには、board[randX][randY] != nullboard[randX][randY] != 'C'に変更する必要があります。

+0

ありがとう、これは非常に簡単で、私が探していた答えでした! –

0

そうB、あなたは、単に乱数を生成することができ、それが0.5未満であれば、あなたは割り当て、A。その後、別の乱数を生成して文字の位置を決定します。場所がすでに取得されている場合は、別の場所を生成します。

手紙が足りなくなったとき、たとえばすでに20 Aが割り当てられている場合は、ランダムに場所を生成し、その場所にBを割り当てます。

完了したら、単に配列を調べ、空白が見つかった場合はCを割り当てます。

2

この解決策では、20 x 20グリッド内の各スロットをカバーする0から399までの一意の乱数400(= 20 x 20)のシーケンスを生成します。その後、20 As、10 Bs、残りのCsをランダムに取り込みます。

Math.randomを使用する際の問題は、同じ乱数を複数回生成するリスクがあることです。より良いアプローチは、2D配列の各スロットをカバーする一連の数字にCollections.shuffle()を使用することです。

int WIDTH = 20; 
int HEIGHT = 20; 
char[][] array = new char[WIDTH][HEIGHT]; 

// generate a random sequence of 400 (= 20 x 20) unique numbers 
ArrayList<Integer> numbers = new ArrayList<Integer>(); 

for (int i = 0; i < WIDTH*HEIGHT; i++) { 
    numbers.add(i); 
} 
Collections.shuffle(numbers); 

// add 20 randomly placed As 
for (int i=0; i < 20; ++i) { 
    int r = numbers.get(i)/WIDTH; 
    int c = numbers.get(i) % HEIGHT; 

    array[r][c] = 'A'; 
} 

// add 10 randomly placed Bs 
for (int i=20; i < 30; ++i) { 
    int r = numbers.get(i)/WIDTH; 
    int c = numbers.get(i) % HEIGHT; 

    array[r][c] = 'B'; 
} 

// fill the rest with Cs 
for (int i=30; i < WIDTH*HEIGHT; ++i) { 
    int r = numbers.get(i)/WIDTH; 
    int c = numbers.get(i) % HEIGHT; 

    array[r][c] = 'C'; 
} 
0

あなたは400要素1Dアレイとして20x20の2D配列を考えることができます。少しの数学を使って、値を置く座標を得ることができます。その後、AとBの文字が配置されるポジションの乱数を生成することができます。すでに設定されているAまたはBの文字を上書きしないようにチェックする必要があります。

final char[][] matrix = new char[20][20]; 
    for (int i = 0; i < 20; i++) { 
     for (int j = 0; j < 20; j++) { 
      matrix[i][j] = 'c'; 
     } 
    } 
    final Random random = new Random(); 
    for (int i = 0; i < 20; i++) { 
     while(true) { 
      int position = random.nextInt(400); 
      if (matrix[position/20][position % 20] == 'c') { 
       matrix[position/20][position % 20] = 'a'; 
       break; 
      } 
     } 
    } 
    for (int i = 0; i < 10; i++) { 
     while(true) { 
      int position = random.nextInt(400); 
      if (matrix[position/20][position % 20] == 'c') { 
       matrix[position/20][position % 20] = 'b'; 
       break; 
      } 
     } 
    } 
関連する問題