# Game of Life 2-D matrix random seed boolean array PLEASE

• September 3rd, 2012, 02:56 PM
Eriosblood
Game of Life 2-D matrix random seed boolean array PLEASE
I'm very new to Java and I do not want to fall behind on this first lab and be subjected to the "Snowball Effect", been working quite relentlessly on this program but I am quite unsure of "seeds" nor how to manipulate the inside of the array for the boolean type.

This is what I have so far. Please and thank you for any/all input.
Code java:

```import java.util.*; public class Life {     public static void main(String[] args) {   long seed = 0; Random rnd = new Random(seed);     }   public static void randomiseMatrix(Random rnd, boolean[][] rnMatrix) { for (int i = 1; i < rnMatrix.length - 1; i++) { for (int j = 1; j < rnMatrix[0].length - 1; j++) { rnMatrix[i][j] = rnd.nextBoolean(); } } }   }```
• September 3rd, 2012, 04:18 PM
jps
Re: Game of Life 2-D matrix random seed boolean array PLEASE
When pseudo-random numbers are generated a seed is used to determine a starting point. So say you make a random number generator and use seed 0. You start pulling nextBoolean one by one and say for example you get the following:
true false true true false true false true true true false false (in that order)
Any time you run your program with seed 0, you will get the same thing as above. When you test your code using random sometimes you want to get the same "random" sequence as before. One example is, say you found a bug in your code and you made a change which you think will correct it. The best way to compare apples to apples is to have the code run exactly as it did before when the bug was found. Seeding your random generator with a known seed does this for you. Seeding the generator with an unpredictable value helps to prevent predicting the pseudo-random sequence. Sometimes the system's time is used to get the seed.

On to your code. Please place [code=java] before your code and [/code] after your code. rnMatrix was declared as a parameter of your method randomiseMatrix. The problem with that is as soon as the method exits, rnMatrix is lost forever. You need to declare your variable outside the scope of the method in order to have access to it, or return rnMatrix to the code calling the method.

You built a nested loop to populate your 2D array one element at a time. To read or modify the elements you would do the same thing again with a nested loop. (there are other ways too)
• September 3rd, 2012, 04:41 PM
Eriosblood
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Thank you very much for the quick reply....wasn't sure how to post my program on here lol yeah I am a newb. Included the program in a txt file. It runs and executes...wondering if my methods and comments explaining the methods are correct?
• September 3rd, 2012, 04:45 PM
jps
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Much rather see the code on the forum rather than in an attachment.

Just type [code=java]
(ctrl+a to select all of your file followed by ctrl+c to copy to the clipboard and ctrl+v here to paste)
and type [/code] here to close the code block in your post.
• September 3rd, 2012, 04:47 PM
Eriosblood
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Code java:

```import java.util.*; public class Life {   /**Main method, user enters the number of rows first, column second, seed third*/ public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("Enter number of rows first, number of columns second and lastly a seed: "); boolean [][] matrix = readNums(console); printMatrix(matrix); nextMatrix(matrix);   }     /**Reads the number of columns/rows/seed sends * parameters to create the matrix and returns matrix */ private static boolean[][] readNums(Scanner console){ int numRows; int numCols; Random r = new Random();   numRows = console.nextInt(); numCols = console.nextInt(); r.setSeed(console.nextLong());   System.out.println(); boolean[][] matrix = new boolean[numRows][numCols];   createMatrix(matrix); for(int row = 1; row < matrix.length - 1; row ++ ) { for( int col = 1; col < matrix[row].length - 1; col++) {   matrix[row][col] = r.nextBoolean();   } }   return matrix; }             /** Prints our matrix with no organisms in first/last row/column * and fills with true/false boolean values */ private static void printMatrix(boolean[][] matrix) { System.out.println("Matrix with your specifications"); for(int r = 0; r < matrix.length; r++) { for(int c = 0; c < matrix[r].length; c++) { if(!matrix[r][c]) { System.out.print( " – " ); } else { System.out.print(" # "); } } System.out.println(); } System.out.println();   }     /**Creates a matrix with lengths entered and sets all values false*/ private static void createMatrix(boolean[][] matrix) { for(int r = 0; r < matrix.length; r++) { for(int c = 0; c < matrix[r].length; c++) { matrix[r][c] = false; }   }   }       /**This method leaves the first row and last row empty of organisms and also the first and last columns void of organisms**/ private static void nextMatrix(boolean[][] matrix) { boolean [][] newMatrix = matrix.clone(); for (int row = 1; row < matrix.length; row++) { for (int col = 1; col < matrix.length; col++) {   newMatrix[row][col] = matrix[row][col];   }     } } }```
• September 3rd, 2012, 04:55 PM
jps
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Your javadoc comments should be complete sentences describing the purpose and function of the block of code they refer to. You can use @return and @param to describe specific parts of methods. For example:
Code java:

```/** This method is used to add two integers together and retrun the sum. * @param firstInt The first integer to be added. * @param secondInt The second integer to be added. * @return The sum of firstInt and secondInt. */ public int sumMyIntsPlease(int firstInt, int secondInt) { return (firstInt + secondInt); }```
• September 3rd, 2012, 04:59 PM
Eriosblood
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Is there a certain protocol as to how long they should be? There's a lot going on in the methods, should I explain everything?
• September 3rd, 2012, 05:04 PM
jps
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Code java:

``` /**Creates a matrix with lengths entered and sets all values false*/ private static void createMatrix(boolean[][] matrix) { for(int r = 0; r < matrix.length; r++) { for(int c = 0; c < matrix[r].length; c++) { matrix[r][c] = false; }   }   }```
This method does not create a matrix, so the method name is misleading to its purpose. I might have gone for something like:
Code java:

```/** This method will set all values of the given matrix to the given boolean. * @param matrix The matrix to be modified. * @param booleanValue The value to set the elements of the matrix. */ private static void setMatrixValues(boolean[][] matrix, boolean value) { for(int r = 0; r < matrix.length; r++) { for(int c = 0; c < matrix[r].length; c++) { matrix[r][c] = value; } } }```
While your program may not need to ever set the matrix to all true, this is more reusable code than what you had. Use this as an example in your mind and try to think in a most general way when you write code. Try to improve on what you have so far, which is not bad.
• September 3rd, 2012, 05:11 PM
Eriosblood
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Oh okay I see what your saying, the boolean value kind of threw me off, not sure where it should get passed from then.
• September 3rd, 2012, 05:12 PM
jps
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Quote:

Originally Posted by Eriosblood
Is there a certain protocol as to how long they should be? There's a lot going on in the methods, should I explain everything?

• September 3rd, 2012, 05:15 PM
jps
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Quote:

Originally Posted by Eriosblood
Oh okay I see what your saying, the boolean value kind of threw me off, not sure where it should get passed from then.

Well you determined you wanted to set the matrix all to false. So when you call the method you would just do it that way. Just assume for a moment that your createMatrix method was replace with the sample I wrote in post#8.
Instead of calling the method like you currently do:
createMatrix(matrix);
You would just call it with the boolean you decided you wanted in each:
setMatrixValues(matrix, false);
• September 3rd, 2012, 05:19 PM
jps
Re: Game of Life 2-D matrix random seed boolean array PLEASE
On a side note, in your readNums method you have a few lines of code I'd like to discuss:
Code java:

``` boolean[][] matrix = new boolean[numRows][numCols];   createMatrix(matrix); for(int row = 1; row < matrix.length - 1; row ++ ) { for( int col = 1; col < matrix[row].length - 1; col++) {   matrix[row][col] = r.nextBoolean();   } }```
You call your createMatrix method, which sets all elements to false. Then you run through the nested loop setup and set each value based on your random generator's nextBoolean method. Setting all values to false in the line of code above setting their values to random values seems pointless.
• September 3rd, 2012, 05:20 PM
Eriosblood
Re: Game of Life 2-D matrix random seed boolean array PLEASE
OHH! Yes I understand ahh thank you. Then just adjust my comments yet and should be good to go for this.
Code java:

```import java.util.*; public class Life {   /**Main method, user enters the number of rows first, column second, seed third * Sends parameters to construct matrix with all false values, and then * constructs inner values of matrix with random boolean values * */ public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("Enter number of rows first, number of columns second and lastly a seed: "); boolean [][] matrix = readNums(console); printMatrix(matrix); nextMatrix(matrix);   }     /** @param console Reads the number of columns entered, rows entered and seed entered. * @createMatrix(matrix) Sends parameters to create the matrix method. */ private static boolean[][] readNums(Scanner console){ int numRows; int numCols; Random r = new Random();   numRows = console.nextInt(); numCols = console.nextInt(); r.setSeed(console.nextLong());   System.out.println(); boolean[][] matrix = new boolean[numRows][numCols];   setMatrixValues(matrix, false); for(int row = 1; row < matrix.length - 1; row ++ ) { for( int col = 1; col < matrix[row].length - 1; col++) {   matrix[row][col] = r.nextBoolean();   } }   return matrix; }             /** Prints our matrix with no organisms in first/last row/column * and fills with true/false boolean values. */ private static void printMatrix(boolean[][] matrix) { System.out.println("Matrix with your specifications"); for(int r = 0; r < matrix.length; r++) { for(int c = 0; c < matrix[r].length; c++) { if(!matrix[r][c]) { System.out.print( " – " ); } else { System.out.print(" # "); } } System.out.println(); } System.out.println();   }     /** This method will set all values of the given matrix to the given boolean. * @param matrix The matrix to be modified. */ private static void setMatrixValues(boolean[][] matrix, boolean value) { for(int r = 0; r < matrix.length; r++) { for(int c = 0; c < matrix[r].length; c++) { matrix[r][c] = value; }   }   }       /**This method leaves the first row and last row empty of organisms and also the first and last columns void of organisms.**/ private static void nextMatrix(boolean[][] matrix) { boolean [][] newMatrix = matrix.clone(); for (int row = 1; row < matrix.length; row++) { for (int col = 1; col < matrix.length; col++) {   newMatrix[row][col] = matrix[row][col];   }     } } }```
• September 3rd, 2012, 05:26 PM
Eriosblood
Re: Game of Life 2-D matrix random seed boolean array PLEASE
The inner values of the matrix are supposed to be random to either print a "-" or a "#" so I figured that's what I was supposed to write. Not quite sure.
• September 3rd, 2012, 05:31 PM
jps
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Quote:

Originally Posted by Eriosblood
OHH! Yes I understand ahh thank you. Then just adjust my comments yet and should be good to go for this.
Code java:

```import java.util.*; public class Life {   /**Main method, user enters the number of rows first, column second, seed third * Sends parameters to construct matrix with all false values, and then * constructs inner values of matrix with random boolean values * */ public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.println("Enter number of rows first, number of columns second and lastly a seed: "); boolean [][] matrix = readNums(console); printMatrix(matrix); nextMatrix(matrix);   }     /** @param console Reads the number of columns entered, rows entered and seed entered. * @createMatrix(matrix) Sends parameters to create the matrix method. */ private static boolean[][] readNums(Scanner console){ int numRows; int numCols; Random r = new Random();   numRows = console.nextInt(); numCols = console.nextInt(); r.setSeed(console.nextLong());   System.out.println(); boolean[][] matrix = new boolean[numRows][numCols];   setMatrixValues(matrix, false); for(int row = 1; row < matrix.length - 1; row ++ ) { for( int col = 1; col < matrix[row].length - 1; col++) {   matrix[row][col] = r.nextBoolean();   } }   return matrix; }             /** Prints our matrix with no organisms in first/last row/column * and fills with true/false boolean values. */ private static void printMatrix(boolean[][] matrix) { System.out.println("Matrix with your specifications"); for(int r = 0; r < matrix.length; r++) { for(int c = 0; c < matrix[r].length; c++) { if(!matrix[r][c]) { System.out.print( " – " ); } else { System.out.print(" # "); } } System.out.println(); } System.out.println();   }     /** This method will set all values of the given matrix to the given boolean. * @param matrix The matrix to be modified. */ private static void setMatrixValues(boolean[][] matrix, boolean value) { for(int r = 0; r < matrix.length; r++) { for(int c = 0; c < matrix[r].length; c++) { matrix[r][c] = value; }   }   }       /**This method leaves the first row and last row empty of organisms and also the first and last columns void of organisms.**/ private static void nextMatrix(boolean[][] matrix) { boolean [][] newMatrix = matrix.clone(); for (int row = 1; row < matrix.length; row++) { for (int col = 1; col < matrix.length; col++) {   newMatrix[row][col] = matrix[row][col];   }     } } }```

I am not sure allowing the user control over your random generator's seed is exactly what you want, maybe it is. Ask yourself, is this something the user of your code needs to be able to do?

I think your readNums method name could be improved, just my opinion. It is a private part of your code, not visible to users, but descriptive names should be used throughout your code and not just the public parts. After all, you as the author of the code are most likely to be the one who has to figure out what readNums means later on down the road when it is time to add-to/modify the code.

Your method nextMatrix. I don't understand what that is for. You clone your matrix supplied as a parameter, and at the end of the method you just discard it. There was no real reason to do such a thing that I can think of. Maybe you meant to return the newly created matrix so it can be used for something? ...although in your code when you call the method from main, you don't do anything with it there...
• September 3rd, 2012, 05:34 PM
jps
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Quote:

Originally Posted by Eriosblood
The inner values of the matrix are supposed to be random to either print a "-" or a "#" so I figured that's what I was supposed to write. Not quite sure.

I don't understand what you mean here. It is my fault for so many posts. When you have a question about a post, you can quote it or refer to it to make it easier to follow what you mean in your reply.
• September 3rd, 2012, 05:38 PM
Eriosblood
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Yeah, I just deleted the nextMatrix, didn't seem to do anything to the program. The seed is for the tester of the program to ensure we both get the same values.
• September 3rd, 2012, 05:42 PM
Eriosblood
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Quote:

Originally Posted by jps
On a side note, in your readNums method you have a few lines of code I'd like to discuss:
Code java:

``` boolean[][] matrix = new boolean[numRows][numCols];   createMatrix(matrix); for(int row = 1; row < matrix.length - 1; row ++ ) { for( int col = 1; col < matrix[row].length - 1; col++) {   matrix[row][col] = r.nextBoolean();   } }```
You call your createMatrix method, which sets all elements to false. Then you run through the nested loop setup and set each value based on your random generator's nextBoolean method. Setting all values to false in the line of code above setting their values to random values seems pointless.

The inner part of the matrix should be randomly a "-" or a "#" so that's why I used random I think.
• September 3rd, 2012, 05:47 PM
jps
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Quote:

Originally Posted by Eriosblood
The inner part of the matrix should be randomly a "-" or a "#" so that's why I used random I think.

Yes that is why you use random.
First you declare your variable and initialize it with the line of code:
boolean[][] matrix = new boolean[numRows][numCols];
Then there is no reason to set them all false, and immediately set them all r.nextBoolean()
The moral of my story was you are just wasting time and effort setting them all to false if you are just going to overwrite the value with a random boolean value.
• September 3rd, 2012, 05:49 PM
Eriosblood
Re: Game of Life 2-D matrix random seed boolean array PLEASE
I think I set them all to false because the first/last row and first/last column must be a "-". No?
• September 3rd, 2012, 06:10 PM
jps
Re: Game of Life 2-D matrix random seed boolean array PLEASE
Surely there is a way you can achieve your goal without doing the extra work of setting the value of most of your elements twice.