Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

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

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    35
    My Mood
    Lurking
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Unhappy 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.
    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();
    		}
    		}
    		}
     
    }
    Attached Files Attached Files
    Last edited by helloworld922; September 3rd, 2012 at 05:29 PM. Reason: please use [code] tags


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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)

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    35
    My Mood
    Lurking
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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?
    Attached Files Attached Files

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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]
    Copy paste your code from your class file here.
    (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.

  5. The Following User Says Thank You to jps For This Useful Post:

    Eriosblood (September 3rd, 2012)

  6. #5
    Member
    Join Date
    Sep 2012
    Posts
    35
    My Mood
    Lurking
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Game of Life 2-D matrix random seed boolean array PLEASE

    ahhhh thank you! Your awesome.
    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];
     
    		    }
     
     
    		 }
    }
    }

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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:
    /** 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);
    }

  8. #7
    Member
    Join Date
    Sep 2012
    Posts
    35
    My Mood
    Lurking
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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?

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Game of Life 2-D matrix random seed boolean array PLEASE

    Your method:
    	/**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:
    /** 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.

  10. The Following User Says Thank You to jps For This Useful Post:

    Eriosblood (September 3rd, 2012)

  11. #9
    Member
    Join Date
    Sep 2012
    Posts
    35
    My Mood
    Lurking
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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.

  12. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Game of Life 2-D matrix random seed boolean array PLEASE

    Quote Originally Posted by Eriosblood View Post
    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?
    The idea of javadoc is to educate the user of your code about your code. To get a good understanding, do a search for javadoc and just spend twenty minutes looking over existing documents. The html pages you see describing other peoples code will give you an idea. But basically you want to provide an understanding of what the code is meant to do and how it is done. Now the "how it is done" part is not to say that you expose the secrets behind your code's private parts, but it is to say when you have a public method which will be directly used by others, you want the user to understand what the parameters are assumed to be and what return value will be produced from them. Take my sample method from post#6 as an example. Say I am the user of this block of code. I am not sure what sumMyIntsPlease will actually do. So I go read the javadoc for it. As a user, (unless I have the source code), I have no way to see that firstInt is added to secondInt and the sum is returned. I must be able to read the existing javadoc to see the intended purpose of the method and gain an understanding of what will be returned to me as a result of what I supplied as parameters.

  13. The Following User Says Thank You to jps For This Useful Post:

    Eriosblood (September 3rd, 2012)

  14. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Game of Life 2-D matrix random seed boolean array PLEASE

    Quote Originally Posted by Eriosblood View Post
    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);

  15. The Following User Says Thank You to jps For This Useful Post:

    Eriosblood (September 3rd, 2012)

  16. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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:
    		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.

  17. The Following User Says Thank You to jps For This Useful Post:

    Eriosblood (September 3rd, 2012)

  18. #13
    Member
    Join Date
    Sep 2012
    Posts
    35
    My Mood
    Lurking
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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.
    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];
     
    		    }
     
     
    		 }
    }
    }

  19. #14
    Member
    Join Date
    Sep 2012
    Posts
    35
    My Mood
    Lurking
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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.

  20. #15
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Game of Life 2-D matrix random seed boolean array PLEASE

    Quote Originally Posted by Eriosblood View Post
    OHH! Yes I understand ahh thank you. Then just adjust my comments yet and should be good to go for this.
    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...

  21. #16
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Game of Life 2-D matrix random seed boolean array PLEASE

    Quote Originally Posted by Eriosblood View Post
    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.

  22. #17
    Member
    Join Date
    Sep 2012
    Posts
    35
    My Mood
    Lurking
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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.

  23. #18
    Member
    Join Date
    Sep 2012
    Posts
    35
    My Mood
    Lurking
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Game of Life 2-D matrix random seed boolean array PLEASE

    Quote Originally Posted by jps View Post
    On a side note, in your readNums method you have a few lines of code I'd like to discuss:
    		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.

  24. #19
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Game of Life 2-D matrix random seed boolean array PLEASE

    Quote Originally Posted by Eriosblood View Post
    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.

  25. #20
    Member
    Join Date
    Sep 2012
    Posts
    35
    My Mood
    Lurking
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default 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?

  26. #21
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

Similar Threads

  1. Help with my game of life program
    By thatni**a in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 11th, 2012, 08:20 AM
  2. Can't run Game of Life programm
    By cutekill0 in forum What's Wrong With My Code?
    Replies: 24
    Last Post: September 13th, 2011, 08:30 AM
  3. two dimensional matrix that returns back a boolean.
    By Bighairjersey in forum Java Theory & Questions
    Replies: 1
    Last Post: July 23rd, 2011, 03:01 PM
  4. Read in file and store in 2D array start of The Game of Life
    By shipwills in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 2nd, 2011, 08:52 AM
  5. Replies: 1
    Last Post: March 28th, 2009, 07:21 AM