-1

This question is for Java. I keep on trying to create a Block[][] of size N, where the numbers 0 - 9 are placed randomly. I've created an array and then shuffled it to prevent numbers from being repeated. When I iterate over the array and assign the numbers to the two dimensional array, I keep on finding that all of the numbers in the two dimensional array are the same. The block keeps on coming out as:

222

222

222

I'd greatly appreciate your help!

public class Board {

int N = 3; 
static int [][] copy; 

//construct a board from an N-by-N array of blocks 
//(where blocks[i][j] = block in row i, column j) 
public Board(int[][] blocks){
    blocks =  new int[N][N]; //creates array of size N
    //generates random numbers 0 inclusive to # exclusive 
    List<Integer> randomList = new ArrayList<>(); 
    for (int i = 0; i < blocks.length; i++){
        randomList.add(i); 
    }
    int randomNum = 0; 
    for (int i = 0; i < blocks.length; i++){
        randomNum = randomList.get(i); 
    }
    Collections.shuffle(randomList);
    for (int i = 0; i < blocks.length; i++){
        for (int j = 0; j < blocks[i].length; j++){
            blocks[i][j] = randomNum; 
        }
    }
    copy = blocks.clone(); 
}

4 Answers4

0

The problem is you're assigning the value from randomNum to all of your cells. This variable is defined before your second for-loop and is left at the highest value at which that loop terminated (in your example the value 2).

You probably want to add a value from your randomList and then reshuffle it when the i index changes.

jhkuperus
  • 1,439
  • 11
  • 15
0

Your code for selecting a random number has no randomness in it at all:

int randomNum = 0; 
for (int i = 0; i < blocks.length; i++){
    randomNum = randomList.get(i); 
}

You do shuffle randomList afterwards, but then make no use of the shuffled list.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
0
   int counter = 0;
   Collections.shuffle(randomList);
    for (int i = 0; i < blocks.length; i++){
        for (int j = 0; j < blocks[i].length; j++){
            blocks[i][j] = randomList.get(counter);
            counter++; 
        }
    }

You should create a counter that will loop through each element of your original arraylist and assign to the multi-dimensional arraylist

Also change:

 for (int i = 0; i < 10; i++){
        randomList.add(i); 
    }

You say you want 0-9 the above loop will give you 0-9. The loop you have will only give you 0-2.

brso05
  • 13,142
  • 2
  • 21
  • 40
  • this code will produce an indexoutofbounds at the 2nd line (when i = 1) – DeiAndrei Oct 08 '14 at 13:16
  • I assumed he was initializing the randomList correctly with 0-9 numbers in it. – brso05 Oct 08 '14 at 13:21
  • it will still crash for N >= 4 – DeiAndrei Oct 08 '14 at 13:22
  • Look at the example we aren't asking what will happen if he completly changes his code we are asking what he wants which is 3!!! @DeiAndrei – brso05 Oct 08 '14 at 13:23
  • where the numbers "0 - 9" are placed randomly. I've created an array and then shuffled it "to prevent numbers from being repeated" - read the question how can you do this with a board greater than 3 X 3? – brso05 Oct 08 '14 at 13:26
0

You need to use the value in your random list of the position you are setting. In addition to this, if you want 0-9 values to be inserted, you need to modify the random list creation:

public Board(int[][] blocks){
    blocks =  new int[N][N]; //creates array of size N
    //generates random numbers 0 inclusive to # exclusive 
    List<Integer> randomList = new ArrayList<>();

    for (int i = 0; i < 10; i++){
        randomList.add(i); 
    }

    Collections.shuffle(randomList);
    for (int i = 0; i < blocks.length; i++){
        for (int j = 0; j < blocks[i].length; j++){
            blocks[i][j] = randomList.get(j); 
        }
        // Shuffle the list again to get different values for the next line
        Collections.shuffle(randomList);
    }
    copy = blocks.clone(); 
}
David SN
  • 3,389
  • 1
  • 17
  • 21