`import java.util.Random;
public class Main {
public static void main(String[] args) {
int[][] board = new int[5][5];
int sNA = 5;
// new GUI();
sequenceMaker(board);
drawBoard(board);
}
//
public static void drawBoard(int[][] board2D) {
int m=1;
for(int i = 0; i < board2D.length; i ){
for(int j=0; j < board2D.length; j ){
System.out.print(board2D[i][j] " ");
}
System.out.println();
}
}
public static void sequenceMaker(int[][] board2D) {
Random rand = new Random();
int m = 1;
int x = 0;
while(x < 24){
int columnRandom = rand.nextInt(5);
int rowsRandom = rand.nextInt(5);
x = 1;
if(board2D[columnRandom][rowsRandom] == 0) {
board2D[columnRandom][rowsRandom] = m;
m = 1;
}
else if(board2D[columnRandom][rowsRandom] == m) {
while(board2D[columnRandom][rowsRandom] == m) {
columnRandom = rand.nextInt(5);
rowsRandom = rand.nextInt(5);
board2D[columnRandom][rowsRandom] = m;
m =1;
}
}
}
}
}`
This is what I wrote to this point, but the output doesn't include every index, maximally going to 15-16ish.
I tried a while loop, so that if another integer is in the place of randomally generated number, it generates those number once again. I don't know why my output is incomplete though.
CodePudding user response:
I have solved my own question (I think).
What I have changed is that in the sequenceMaker() function, I have moved the x =1 into the if statements. The code looks like this :
public static void sequenceMaker(int[][] board2D) {
Random rand = new Random();
int m = 1;
int x = 0;
while(x < 25){
int columnRandom = rand.nextInt(5); // random column
int rowsRandom = rand.nextInt(5); // random row
if(board2D[columnRandom][rowsRandom] == 0) {
board2D[columnRandom][rowsRandom] = m;
m = 1;
x = 1;
}
else if(board2D[columnRandom][rowsRandom] != 0) {
while(board2D[columnRandom][rowsRandom] == 0) {
columnRandom = rand.nextInt(5);
rowsRandom = rand.nextInt(5);
board2D[columnRandom][rowsRandom] = m;
m =1;
x = 1;
}
}
}
}
I hope my mediocrity helped someone!
CodePudding user response:
public static void sequenceMaker(int[][] board2D) {
//it will initialize your array with random integers
Random rand = new Random();
for(int i=0; i<board2D.length; i ){
for(int j=0; j < board2D[i].length; j ){
int randomNo = rand.nextInt(20); //you can change the bound as required
board2D[i][j] = randomNo;
}
}
}