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

Thread: "Game of Life" help

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default "Game of Life" help

    For my programming class, I have to recreate the "Game of Life" game. Right now, we are only building the foundation for it so it doesn't have to do as much as the real game quite yet. The program should ask the user for input: number of columns, number of rows, seed number for the random gen, birth high, birth low, deathHigh and deathLow. There should be a grid of -'s with random #'s on it to represent the grid (we are going to use graphics later) and each # represents a living cell. The rules are:
    1.Any live cell with fewer than deathLow live neighbours dies, as if caused by underpopulation.
    2.Any live cell with more than deathHigh live neighbours dies, as if by overcrowding.
    3.Any live cell with a number of live neighbours between deathLow and deathHigh lives on to the next generation.
    4.Any dead cell with a number of live neighbours between birthLow and birthHigh becomes a live cell.

    The "seed" input is to help test the program. If the seed if always the same, then the random gen will always be the same. We have to print out 5 generations. My program works. No errors and it compiles fine. But, my teacher gave us a sample output and mine does not match hers. There is an error somewhere in my alterMatrix method, but I can't find where. Can you guys find it? Thanks a lot.

    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 {
     
                            }
                        }
                    }
                }
            }
        }
    }


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: "Game of Life" help

    And what is your question?

  3. #3
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: "Game of Life" help

    Quote Originally Posted by nWeid1 View Post
    But, my teacher gave us a sample output and mine does not match hers. There is an error somewhere in my alterMatrix method, but I can't find where. Can you guys find it? Thanks a lot.
    Find a 9-cell neighbourhood in your GoL whose centre cell changes to a different next state from the same cell in the same neighbourhood state in your teacher's output. Is your teacher's update correct and yours wrong? Show us the 9 cells before the update and the 9 cells (with the 'wrong' state in the centre) afterwards. I suspect you'll know yourself what is wrong when you do that.

Similar Threads

  1. Writing a very simple "Poker" game in Java
    By omgar in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 7th, 2014, 05:21 PM
  2. Replies: 3
    Last Post: December 7th, 2011, 01:03 AM
  3. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM
  5. help help, ... :) "game of life"
    By sanfor in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2009, 11:40 AM