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

Thread: Need help with Game of Life generations

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Location
    West Virginia
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with Game of Life generations

    I'm making a simple game of life 10x10 board using dashes, and filling them in with x's when the user inputs the corresponding row and column. I have everything I need for this except option 3, which is to 'show the next generation' by these rules:
    - A new cell is born on an empty square if it is surrounded by exactly three neighbors
    - A cell dies of overcrowding if it is surrounded by four or more neighbors
    - It dies of loneliness if it is surrounded by zero or one neighbor
    - If a cell has two or three neighbors, it survives to the next generation

    import java.util.Scanner;
    public class GameOfLife
    {
        public static void main (String[] args) {
     
            final int ROWS = 10;
            final int COLUMNS = 10;
            Scanner scan = new Scanner(System.in);
            String[][] board = new String[ROWS][COLUMNS];
            for (int i = 0; i < ROWS; i++) 
                for (int j = 0; j < COLUMNS; j++)
                    board[i][j] = "-";
     
            boolean done = false;
            while (!done) {
     
                System.out.println("1 - Add a being");
                System.out.println("2 - Show current board");
                System.out.println("3 - Show next generation");
                System.out.println("4 - Clear board");
                System.out.println("5 - Exit");
                int option = scan.nextInt();
     
                if (option == 1) {                
                    System.out.print("Enter row for being (1 through 9): ");
                    int i = scan.nextInt();
                    System.out.print("Enter column for being: ");
                    int j = scan.nextInt();
                    board[i][j] = "x";        
                }
     
                else if (option == 2) {
                    for (int i = 0; i < ROWS; i++) {
                        for (int j = 0; j < COLUMNS; j++)
                            System.out.print(board[i][j]);
                        System.out.println();
                    }
                }
     
                else if (option == 3) {
                    int neighbors = 0;
                    int x = ROWS, y = COLUMNS;                
                    if (x >= 1 && y >=1 && x < COLUMNS && y < ROWS) {
                        if (board[x][y++].equals(1)) {neighbors++;}
                        if (board[x][y--].equals(1)) {neighbors++;}
                        if (board[x++][y].equals(1)) {neighbors++;}
                        if (board[x++][y++].equals(1)) {neighbors++;}
                        if (board[x++][y--].equals(1)) {neighbors++;}
                        if (board[x--][y--].equals(1)) {neighbors++;}
                        if (board[x--][y++].equals(1)) {neighbors++;}
                    }
     
                }
     
                else if (option == 4) {
                    for (int i = 0; i < ROWS; i++)
                        for (int j = 0; j < COLUMNS; j++)
                            board[i][j] = "-";
                }
                else if (option == 5) {
                    done = true;
                }
     
                else System.out.println("Invalid input.");            
            }
     
            System.exit(0);
        }
    }

    I have it so it finds the neighbors but I'm not sure how to go about deleting or keeping certain x's.

    For example, how to go from this:
    ----------
    ----------
    ----------
    ---xxx----
    ----------
    ----------
    ----------
    ----------
    ----------
    ----------

    To this:

    ----------
    ----------
    ----x-----
    ----x-----
    ----x-----
    ----------
    ----------
    ----------
    ----------
    ----------


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,137
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Need help with Game of Life generations

    how to go about deleting or keeping certain x's.
    Use the rules of the game to change the contents of a slot depending on what neighbors it has.
    Perhaps you need two boards: current and next. Use the current board to set the xs in the next board. Then set current to next.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Location
    West Virginia
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Game of Life generations

    Quote Originally Posted by Norm View Post
    Use the rules of the game to change the contents of a slot depending on what neighbors it has.
    Perhaps you need two boards: current and next. Use the current board to set the xs in the next board. Then set current to next.
    I have a current board (which is actually just option 2), and I tried just copying that again and using if statements according to the rules, but that's where I'm not sure of how to start it.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,137
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Need help with Game of Life generations

    Use the items on the current board to set the items on the next board.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Location
    West Virginia
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with Game of Life generations

    Changed my else if for option 3 and now I'm getting this error on the if statements inside of option 3:
    java.lang.ArrayIndexOutOfBoundsException: 10
    /**
     * Program the mathematical game called The Game of Life.
     * 
     * @author Marcus Stone 
     * @version 11-27-2012
     */
    import java.util.Scanner;
    public class GameOfLife
    {
        public static void main (String[] args) {
            final int ROWS = 10;
            final int COLUMNS = 10;
            Scanner scan = new Scanner(System.in);
            String[][] board = new String[ROWS][COLUMNS];
            String[][] nextGen = new String[ROWS][COLUMNS];
            for (int i = 0; i < ROWS; i++) 
                for (int j = 0; j < COLUMNS; j++)
                    board[i][j] = "-";
            boolean done = false;
            while (!done) {
                System.out.println("1 - Add a being");
                System.out.println("2 - Show current board");
                System.out.println("3 - Show next generation");
                System.out.println("4 - Clear board");
                System.out.println("5 - Exit");
                int option = scan.nextInt();
     
                if (option == 1) {                
                    System.out.print("Enter row for being (1 through 8): ");
                    int i = scan.nextInt();
                    System.out.print("Enter column for being (1 through 8): ");
                    int j = scan.nextInt();
                    board[i][j] = "x";        
                }
                else if (option == 2) {
                    for (int i = 0; i < ROWS; i++) {
                        for (int j = 0; j < COLUMNS; j++)
                            System.out.print(board[i][j]);
                        System.out.println();
                    }
                }
                else if (option == 3) {
                    for (int i = 1; i <= ROWS; i++) {
                        for (int j = 1; j <= COLUMNS; j++) {
                            if (board[i][j] == "x") {
                                int count = 0; {
                                    if (board[i][j - 1] == "x")
                                    count++;
                                    if (board[i][j + 1] == "x")
                                    count++;
                                    if (board[i - 1][j] == "x")
                                    count++;
                                    if (board[i - 1][j - 1] == "x")
                                    count++;
                                    if (board[i - 1][j + 1] == "x")
                                    count++;
                                    if (board[i + 1][j - 1] == "x")
                                    count++;
                                    if (board[i + 1][j] == "x")
                                    count++;
                                    if (board[i + 1][j + 1] == "x")
                                    count++;
                                }
                                if (count == 2 || count == 3)
                                    board[i][j] = "x";
                                else
                                    board[i][j] = "-";
                            }
                            if (board[i][j] == "-") {
                                int nextCount = 0; {
                                    if (board[i][j - 1] == "x")
                                    nextCount++;
                                    if (board[i][j + 1] == "x")
                                    nextCount++;
                                    if (board[i - 1][j] == "x")
                                    nextCount++;
                                    if (board[i - 1][j - 1] == "x")
                                    nextCount++;
                                    if (board[i - 1][j + 1] == "x")
                                    nextCount++;
                                    if (board[i + 1][j - 1] == "x")
                                    nextCount++;
                                    if (board[i + 1][j] == "x")
                                    nextCount++;
                                    if (board[i + 1][j + 1] == "x")
                                    nextCount++;
                                }
                                if (nextCount == 3)
                                    board[i][j] = "x";
                            }
                            System.out.print(board[i][j]);
                        }
                    }
                }
                else if (option == 4) {
                    for (int i = 0; i < ROWS; i++)
                        for (int j = 0; j < COLUMNS; j++)
                            board[i][j] = "-";
                }
                else if (option == 5) {
                    done = true;
                }
                else System.out.println("Invalid input.");            
            }
            System.exit(0);
        }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,137
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Need help with Game of Life generations

    java.lang.ArrayIndexOutOfBoundsException: 10
    That means that program is using an index that is past the end of the array.
    Remember that array indexes go from 0 to the array length-1
    Make sure the index's value never goes past the array's length-1
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Game of Life, help with Array Object? Quite puzzled
    By Eriosblood in forum Collections and Generics
    Replies: 33
    Last Post: September 17th, 2012, 09:58 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, 08: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. Conways Game of Life. Long Death option. Please help..
    By byako in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 9th, 2011, 07:49 AM
  5. Game of Life GUI Error
    By Lavace in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 3rd, 2011, 08:15 AM