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 13 of 13

Thread: Game of Life Matrix...Needs troubleshoot, specific output

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

    Default Game of Life Matrix...Needs troubleshoot, specific output

    Well I have the program written, just have the problem that I'm not getting the output I desire.
    import java.util.Random;
    import java.util.Scanner;
     
     
    //This program creates a board of -'s with random #'s on it.
    public class Life {
        // This method asks for user input and calls the initializing and printing
        // methods
        public static void main(String[] args) {
     
            Scanner console = new Scanner(System.in); // Creates new scanner
            System.out.println("Number of rows?");
            int rows = console.nextInt(); // Stores user input as number of rows
     
            System.out.println("Number of columns?");
            int columns = console.nextInt(); // Stores user input as number of
            // columns
     
            System.out.println("Seed number?");
            long seed = console.nextLong(); // Stores user input as number of seed
     
            System.out.println("Birth minimum?");
            int birthLow = console.nextInt(); // Store user input as the minimum
                                                // number for birth
     
            System.out.println("Birth maximum?");
            int birthHigh = console.nextInt(); // Stores user input as maximum
                                                // number for birth
     
            System.out.println("Live minimum?");
            int liveLow = console.nextInt(); // Stores user input as minimum for
                                                // death
     
            System.out.println("Live maximum?");
            int liveHigh = console.nextInt(); // stores user input as maximum for
                                                // death
     
            boolean initMatrix[][] = new boolean[rows][columns]; // creates new
            // boolean array
            // sized
            // according to
            // user input
     
            array(initMatrix, rows, columns, seed); // calls the initializing method
     
            printArray(initMatrix, rows, columns); // calls the printing method
     
            System.out.println();
     
            for(int i=0; i<4; i++){
     
                alterMatrix(initMatrix, rows, columns, birthLow, birthHigh, liveLow, liveHigh);
                printArray(initMatrix, rows, columns);
                System.out.println();
            }
     
        }
     
        // This method initializes the array with trues and falses
        public static void array(boolean[][] Matrix, int rows, int columns,
                long seed) {
     
            Random generator = new Random(seed); // creates random number according
            // to user seed
     
            // loop goes through every row starting at 2nd and ending at 2nd to last
            for (int i = 1; i < rows - 1; i++) {
                // loop goes through every column starting at 2nd and ending at 2nd
                // to last
                for (int j = 1; j < columns - 1; j++) {
     
                    // generates random value
                    boolean x = generator.nextBoolean();
     
                    // if x is false, set that array spot as false
                    if (!x) {
                        Matrix[i][j] = false;
     
                    }
     
                    // if x is true, set that array spot as true
                    else {
                        Matrix[i][j] = true;
                    }
                }
            }
        }
     
        // This method prints the array
        public static void printArray(boolean[][] Matrix, int rows, int columns) {
     
            // these loops go through every value in the array
            for (int k = 0; k < rows; k++) {
                for (int m = 0; m < columns; m++) {
     
                    // if the array is false, print a -
                    if (!Matrix[k][m]) {
                        System.out.print("- ");
                    }
     
                    // if the array is true, print a #
                    else {
                        System.out.print("# ");
                    }
                }
                System.out.println(); // starts a new row
            }
        }
     
        public static void alterMatrix(boolean[][] initialMatrix, int rows,
                int columns, int birthLow, int birthHigh, int liveLow, int liveHigh) {
     
            boolean matrixUpdate[][] = initialMatrix.clone();
            for (int row = 0; row < initialMatrix.length; row++) {
                matrixUpdate[row] = matrixUpdate[row].clone();
     
                // loop goes through every row starting at 2nd and ending at 2nd to
                // last
                for (int i = 1; i < rows - 1; i++) {
                    // loop goes through every column starting at 2nd and ending at
                    // 2nd
                    // to last
                    for (int j = 1; j < columns - 1; j++) {
     
                        // if initMatrix was false, look to see if life can be born
                        if (!initialMatrix[i][j]) {
                            int counter = 0;
                            // These if statements test each neighboring spot for
                            // life
                            // if life is there, counter will increase by 1
                            if (initialMatrix[i - 1][j - 1] == true) {
                                counter = counter + 1;
                            }
                            if (initialMatrix[i - 1][j] == true) {
                                counter = counter + 1;
                            }
                            if (initialMatrix[i - 1][j + 1] == true) {
                                counter = counter + 1;
                            }
                            if (initialMatrix[i][j - 1] == true) {
                                counter = counter + 1;
                            }
                            if (initialMatrix[i][j + 1] == true) {
                                counter = counter + 1;
                            }
                            if (initialMatrix[i + 1][j + 1] == true) {
                                counter = counter + 1;
                            }
                            if (initialMatrix[i + 1][j - 1] == true) {
                                counter = counter + 1;
                            }
                            if (initialMatrix[i + 1][j] == true) {
                                counter = counter + 1;
                            } else {
     
                            }
     
                            // if the counter is in the birth range, set that spot
                            // as true
                            if (counter >= birthLow && counter <= birthHigh) {
                                matrixUpdate[i][j] = true;
                            }
     
                        }
     
                        // if initMatrix was true, look to see if life will die
                        else {
     
                            int counter2 = 0;
     
                            // these if statements test each spot neighboring spot
                            // for
                            // life
                            // if life is found, counter will increase by 1
                            if (initialMatrix[i - 1][j - 1] == true) {
                                counter2 = counter2 + 1;
                            }
                            if (initialMatrix[i - 1][j] == true) {
                                counter2 = counter2 + 1;
                            }
                            if (initialMatrix[i - 1][j + 1] == true) {
                                counter2 = counter2 + 1;
                            }
                            if (initialMatrix[i][j - 1] == true) {
                                counter2 = counter2 + 1;
                            }
                            if (initialMatrix[i][j + 1] == true) {
                                counter2 = counter2 + 1;
                            }
                            if (initialMatrix[i + 1][j + 1] == true) {
                                counter2 = counter2 + 1;
                            }
                            if (initialMatrix[i + 1][j - 1] == true) {
                                counter2 = counter2 + 1;
                            }
                            if (initialMatrix[i + 1][j] == true) {
                                counter2 = counter2 + 1;
                            }
                            // if counter is outside of the death range, life is
                            // eliminated
                            if (counter2 >= liveHigh || counter2 <= liveLow) {
                                matrixUpdate[i][j] = false;
                            } else {
     
                            }
                        }
                    }
                }
            }
        }
    }
    I should be getting this instead.
    Sample Input
    6 8
    7
    3 8
    3 8

    Sample Output
    - - - - - - - -
    - # # # - - - -
    - # # # # - # -
    - - - # # - # -
    - # - # - # # -
    - - - - - - - -

    - - - - - - - -
    - # # # # - - -
    - # # # # # - -
    - # # # # # # -
    - - # # # # # -
    - - - - - - - -

    - - - - - - - -
    - # # # # # - -
    - # - - # # # -
    - # # - - # # -
    - # # # # # # -
    - - - - - - - -

    - - - - - - - -
    - # # # # # # -
    - # # # # # # -
    - # # # # # # -
    - # # # # # # -
    - - - - - - - -

    - - - - - - - -
    - # # # # # # -
    - # - - - - # -
    - # - - - - # -
    - # # # # # # -
    - - - - - - - -

    - - - - - - - -
    - # # # # # # -
    - # # # # # # -
    - # # # # # # -
    - # # # # # # -
    - - - - - - - -


  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 Matrix...Needs troubleshoot, specific output

    I do not understand your question or your problem. Can you give more details about what you want, vs what you are getting?

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Game of Life Matrix...Needs troubleshoot, specific output

    Quote Originally Posted by Eriosblood View Post
    ...I'm not getting the output I desire....
    I should be getting this instead.
    Sample Input
    6 8
    7
    3 8
    3 8

    Sample Output
    - - - - - - - -
    - # # # - - - -
    - # # # # - # -
    - - - # # - # -
    - # - # - # # -
    - - - - - - - -

    - - - - - - - -
    - # # # # - - -
    - # # # # # - -
    - # # # # # # -
    - - # # # # # -
    - - - - - - - -
    ...
    When I run your code, the starting and next generation look like this:

    Number of rows?
    6 8
    Number of columns?
    Seed number?
    7
    Birth minimum?
    3 8
    Birth maximum?
    Live minimum?
    3 8
    Live maximum?
    - - - - - - - -
    - # # # - - - -
    - # # # # - # -
    - - - # # - # -
    - # - # - # # -
    - - - - - - - -

    - - - - - - - -
    - - # # # # - -
    - # # - # # # -
    - # # # # - # -
    - # # # # - - -
    - - - - - - - -


    Now, compare the very first place where it disagrees with your expected output. It's the live cell at (1,1) that died, right? (The '-' that I colored red.) It had three neighbors in the starting generation, and according to your rules, it should have died, as it did.

    Here's the way that I see things (at least as far as this cell is concerned): When it executes your second loop in alterMatirix(): Values of liveLow and liveHigh are 3 and 8, right? Then, according to your program, any live cell with number of neighbors greater than or equal to 8 or less than or equal to 3 will die. But your expected output printout shows it still alive. What's up with that?

    My conclusion: Maybe your user input is not consistent with your expectations.

    Solution: Either change the input values or change your expectations or change the program to make them consistent.

    Now, it may very well be that, after you get that first discrepancy fixed, you will find other problems. Work through them one at a time, using a manually generated next generation to have something to compare with. (While debugging, just print out the first new generation, and get that one absolutely correct. I suggest that you not try to "spot" something in the program that causes all of the observed errors. Just fix one at a time...)


    As a side note, I always played the game according to Conway's original rules. I was totally fascinated with the way things floated across the screen (I used a "live" graphics program with a lot more rows and columns), and I never found alterations of the rules to be very interesting. I mean, he really had it set up to give lots (and lots and lots) of interesting possibilities.

    Anyhow...

    Allowing the program to change the rules might re-awaken my long-dormant interest...

    Just so we can understand your rules, how about stating your intent.

    Conway's original rules go like this:
    1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
    2. Any live cell with two or three live neighbors lives on to the next generation.
    3. Any live cell with more than three live neighbors dies, as if by overcrowding.
    4. Any dead cell with fewer than three live neighbors remains dead on to the next generation.
    5. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
    6. Any dead cell with or more than three live neighbors remains dead on to the next generation.


    Several variations of the game are possible, depending on the kind of surface the grid is laid out on. (Torus, Klein bottle, Box, Mirror, etc.)

    What happens at the edges? I perceive that your game is in a "box" with no wrap-around. In other words the outer two rows and columns are permanently dead zones.


    If we see your rules, perhaps we can reconcile your posted expected values with a "correct" program for your rules.

    Cheers!

    Z

    NOTE---NOTE---NOTE


    [edit]
    The statement of rule six has an extra 'or' in there. I meant to write

    6. Any dead cell with more than three live neighbors remains dead on to the next generation.

    Sorry.
    [/edit]
    Last edited by Zaphod_b; September 11th, 2012 at 10:13 AM. Reason: Clarification of typographical error

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

    Default Re: Game of Life Matrix...Needs troubleshoot, specific output

    Wow thank you very much, very insightful! Here are my rules.
    Attached Files Attached Files

  5. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Game of Life Matrix...Needs troubleshoot, specific output

    OK. So go through your code and go through the instructions and see what the output for the first generation should be for the values that you give it. If you can't see why the code is giving you what it is showing at the first generation, put print statements that show what count values cause particular cell values to be what they are.

    I think your handling of the cloned matrix in alterMatrix is really bassackwards.

    Make a copy of the incoming matrix with clone. Clone all of the rows.

    Then update the incoming matrix by counting neighbors in the copy.

    Maybe something like...

        public static void alterMatrix(boolean[][] ioMatrix, int rows,
                int columns, int birthLow, int birthHigh, int liveLow, int liveHigh) {
     
            boolean oldMatrix[][] = ioMatrix.clone();
            for (int row = 0; row < ioMatrix.length; row++) {
                oldMatrix[row] = ioMatrix[row].clone();
            }
     
        // Now go through all of the rows and columns of oldMatrix[][], counting neighbors.  Update
        // ioMatrix elements according to your rules.


    Cheers!

    Z
    Last edited by Zaphod_b; September 10th, 2012 at 06:35 PM.

  6. The Following User Says Thank You to Zaphod_b For This Useful Post:

    Eriosblood (September 10th, 2012)

  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 Matrix...Needs troubleshoot, specific output

    Quote Originally Posted by Zaphod_b View Post
    Conway's original rules go like this:
    1. Any live cell with fewer than two live neighbors dies, as if caused by under-population.
    2. Any live cell with two or three live neighbors lives on to the next generation.
    3. Any live cell with more than three live neighbors dies, as if by overcrowding.
    4. Any dead cell with fewer than three live neighbors remains dead on to the next generation.
    5. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
    6. Any dead cell with or more than three live neighbors remains dead on to the next generation.

    Cheers!

    Z
    Quote Originally Posted by jps View Post
    I do not understand your question or your problem. Can you give more details about what you want, vs what you are getting?
    Oh now i get it. I guess it helps to be familiar with what is going on. Never heard of this before.
    I do have to complain about rule 6. Rule 6 would disallow any dead cell from ever being live. If it takes exactly 3 live neighbors to birth the cell, but any cell with 3 or more live neighbors remains dead, no cell will ever spawn life.

  8. #7
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Game of Life Matrix...Needs troubleshoot, specific output

    Quote Originally Posted by Zaphod_b
    Any dead cell with or more than three live neighbors remains dead on to the next generation.

    [edit]
    My statement of rule six has an extra "or" that may have confused people. Sorry. Kill the "or" so it becomes

    6. Any dead cell with more than three live neighbors remains dead on to the next generation.
    [/edit]

    Anyhow...

    Many patterns "float" across the grid and things that were dead become alive. Not all patterns act this way. The game is completely deterministic, but can give rise to endless experimentation. I played and played (and played) with this stuff many years ago. I will say that "random" starting patterns didn't give me much satisfaction, but studying the results led to ways of creating many "beautiful" patterns. (Maybe it's just me: I'm funny that way. I think some of them are beautiful.)

    Now with the tiny grid that your example uses, maybe not so many "interesting" things happen, but there is the possibility of oscillation and certain other things that might be neat to explore.


    If you are interested, look at this reference: Conway's Game of Life - wikipedia There are some animated examples and many references.

    Love that Glider Gun! How about those Breeders?


    Cheers!

    Z
    Last edited by Zaphod_b; September 11th, 2012 at 10:09 AM. Reason: Clarification regarding typographical error.

  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 Matrix...Needs troubleshoot, specific output

    How does it not mean that?
    livingNeighbors = 3;
    if(livingNeighbors == 3) {//rule 5
       myLifeStarts();
    }
    //but then...
    if(livingNeighbors >= 3) {//rule 6
       myLifeRemainsDead();
    }
    Life can never start. Rule 6 should be > 3, not >= 3 or every time livingNeighbors is 3, rule 6 overrides rule 5.

  10. #9
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Game of Life Matrix...Needs troubleshoot, specific output

    Quote Originally Posted by jps View Post
    if(livingNeighbors >= 3) {//rule 6

    How does it not mean that?
    Quote Originally Posted by Zaphod_d
    6. Any dead cell with or more than three live neighbors remains dead on to the next generation.
    My rule six was intended to imply the condition "greater than," not "greater than or equal to." (A typographical error results in the extra "or" in the sentence. Sorry. I had to lay off my proofreader. Kill the 'or.')
    So it becomes

    6. Any dead cell with more than three live neighbors remains dead on to the next generation.


    In fact, in my enumeration of the rules, there is some redundancy (That is, there are not six independent conditions.) The listing was not intended as a logical road map for a program, and an implementation does not have to check all six conditions in six separate program statements.

    The rules of the original Game of Life can be boiled down to two:
    1. A live cell remains live if it has either two or three live neighbors, otherwise it dies.

    2. A dead cell becomes alive if it has exactly three live neighbors, otherwise it remains dead.


    The rules for the game proposed by the Original Poster in this thread have user-defined thresholds for how many or how few live neighbors will cause live cells to die or dead cells to come to life in the next generation.

    Cheers!

    Z
    Last edited by Zaphod_b; September 11th, 2012 at 10:14 AM.

  11. #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 Matrix...Needs troubleshoot, specific output

    Quote Originally Posted by Zaphod_b View Post
    My rule six was intended to imply the condition "greater than," not "greater than or equal to." (A typographical error results in the extra "or" in the sentence. Sorry. I had to lay off my proofreader. Kill the 'or.')
    Those lovely typos just go and change the meaning sometimes.

  12. #11
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Game of Life Matrix...Needs troubleshoot, specific output

    Quote Originally Posted by jps View Post
    Those lovely typos just go and change the meaning sometimes.
    Well, I didn't say, "A dead cell with three or more live neighbors...." That would have been unequivocally wrong.

    The way that it ended up: "A dead cell or more than three live neighbors..." is just plain nonsense.


    Bottom line:
    Caveat lector!


    Cheers!

    Z

  13. #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 Matrix...Needs troubleshoot, specific output

    Quote Originally Posted by Zaphod_b View Post
    The way that it ended up: "A dead cell or more than three live neighbors..." is just plain nonsense.
    Hate to be picky, but that is not how it ended up either.
    Any dead cell with or more than three live neighbors remains dead on to the next generation.
    That is how it come out, and to me it works out >=, and I only mentioned it because I for one was unfamiliar with this "Game of Life" thing until this thread come along, and as I started to understand through the posted rules, and seeing the code in my head, there was a conflict I could not understand. So I hope you do not feel you are under attack Z.

    Edit:I think my current frustration with At&t (with whom I am still on hold exceeding an hour and twenty minutes) caused me to be rude in my way of wording things, so I changed it.
    Last edited by jps; September 11th, 2012 at 06:35 PM.

  14. #13
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Game of Life Matrix...Needs troubleshoot, specific output

    Quote Originally Posted by jps View Post
    Hate to be picky...
    There is no excuse for my leaving things in a state that can be unnecessarily confusing. (I realize that my machine-gun-like stream-of-consciousness style can be confusing even in the best conditions. It's a curse.)

    I thought your posts were completely appropriate, and I appreciate the feedback.

    Cheers!

    Z

Similar Threads

  1. Game of Life 2-D matrix random seed boolean array PLEASE
    By Eriosblood in forum Collections and Generics
    Replies: 20
    Last Post: September 3rd, 2012, 06:10 PM
  2. 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, 09:20 AM
  3. 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
  4. Game of Life GUI Error
    By Lavace in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 3rd, 2011, 09:15 AM
  5. Replies: 1
    Last Post: March 28th, 2009, 07:21 AM