I am trying to create a 3x3 board using 2 dimensional array that randomly fills 0s and 1s into the board and finds the rows, columns, and diagonals with all 1s.
Please help!
Thank you
Printable View
I am trying to create a 3x3 board using 2 dimensional array that randomly fills 0s and 1s into the board and finds the rows, columns, and diagonals with all 1s.
Please help!
Thank you
What's your question? What have you tried? Where are you stuck? Please see the link in my signature on asking questions the smart way, as well as this article: http://www.javaprogrammingforums.com...e-posting.html
I have declared to generate a random number and assign to each row and column of the 3x3 array. I'm not sure if the below portion of the code is correct.
Random randomGenerator = new Random();
int[] [] array = new int [3][3];
array [1][1] = randomGenerator.nextInt(1);
array [1][2] = randomGenerator.nextInt(1);
array [1][3] = randomGenerator.nextInt(1);
array [2][1] = randomGenerator.nextInt(1);
array [2][2] = randomGenerator.nextInt(1);
array [2][3] = randomGenerator.nextInt(1);
array [3][1] = randomGenerator.nextInt(1);
array [3][2] = randomGenerator.nextInt(1);
array [3][3] = randomGenerator.nextInt(1);
Well, have you stepped through the program with a debugger, or simply added print statements, to figure out what exactly the code is doing?
I am trying to display the board using JOptionPane and it is giving me an exception error - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
Well, without an SSCCE, I'm only guessing, but I can tell you that arrays are 0-based. That means that an array with 3 indexes will have index 0, 1, and 2. Index 3 is actually the 4th index.
How do I randomly fill with 0s and 1s in the 3x3 board?
You have the right idea, you just aren't keeping in mind that arrays start at index 0. Zero is the first index, one is the second index, etc.
I understand what you are saying. Index are element - 1. I don't know how to go forward with filling the board with 0s and 1s in the 3x3 position. For example for each number of row and column such at (1 x 1) (1 x 2) (1 x 3) (2 x 1) (2 x 2) ( 2 x 3) so on and so forth.
If you understand that arrays start at zero, why are you starting your indexes at one? If you understand that they go to array.length - 1, why are you trying to access array.length?
Can you show me how I can fill the board with 0s and 1s?