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

Thread: Anyone know how to do this game of life problem

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Anyone know how to do this game of life problem

    can anyone help with this game of life problem
    my professor gave us uncompleted code and i added a few parts but i dont know what im doing wrong
    this is the first class he gave us:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;

    public class Program8 extends JPanel implements ActionListener
    {
    private LifeCell[][] board; // Board of life cells.
    private JButton next; // Press for next generation.
    private JFrame frame; // The program frame.
    private Color purple;// i added
    private LifeCell cell;// i added
    int row,col;
    public static void main(String[] args) {new Program8();}

    public Program8()
    {
    // The usual boilerplate constructor that pastes the main
    // panel into a frame and displays the frame. It should
    // invoke the "init" method before packing the frame.
    frame = new JFrame("Game of Life");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setContentPane(this);
    this.init();
    frame.pack();
    frame.setVisible(true);
    }

    public void init()
    {
    // Create the user interface on the main panel. Construct
    // the LifeCell widgets, add them to the panel, and store
    // them in the two-dimensional array "board". Create the
    // "next" button that will show the next generation
    this.setPreferredSize(new Dimension(270, 350));
    purple = new Color(186,60,204);
    this.setBackground(purple);
    this.setLayout(null);
    next = new JButton("Next");
    next.addActionListener(this);
    next.setBounds(80,310,120,25);
    this.add(next);
    board = new LifeCell[10][10];
    for(row=0; row < 10; row++)
    { int x = 1;
    for(col=0; col < 10; col++)
    {
    cell= new LifeCell(board,row,col);
    board[row][col]= cell;
    }
    }

    }

    public void actionPerformed(ActionEvent event)
    {
    // Responds when the "next" button is pressed. It should
    // first ask all life cells to count their living neighbors.
    // It should then ask all life cells to update their states
    // for the new generation.
    }
    }

    this is the second class:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.geom.*;

    public class LifeCell extends JPanel implements MouseListener
    {
    private LifeCell[][] board; // A reference to the board array.
    private boolean alive; // Stores the state of the cell.
    private int row,col; // Position of the cell on the board.
    private int count; // Stores number of living neighbors.
    Rectangle2D box;
    int x;
    int y;
    public LifeCell(LifeCell[][] b, int r, int c)
    {
    // Initialize the life cell as dead. Store the reference
    // to the board array and the board position passed as
    // arguments. Initialize the neighbor count to zero.
    // Register the cell as listener to its own mouse events
    board =b;
    row = r;
    col = c;

    x= 5 + (35 * col);
    y = 5 + (35 * row);
    box = new Rectangle2D.Float(x,y,30,30);
    count = 0;
    alive = false;


    }

    public void countNeighbors()
    {
    // Set "count" to the number of living neighbors of this cell.

    }

    public void updateState()
    {
    // Examine "alive" and "count" to determine the state of this
    // cell in the next generation. If the state changes in the
    // next generation, invoke "toggle" to update the state.
    }

    public void paintComponent(Graphics g)
    {
    // Paint the cell. The cell must be painted differently
    // when alive than when dead, so the user can clearly see
    // the state of the cell.
    Graphics2D gr =(Graphics2D) g;
    super.paintComponent(g);

    if(alive =!alive)
    { gr.setPaint(Color.yellow);
    gr.fill(box);
    }
    else
    {gr.setPaint(Color.WHITE);
    gr.fill(box);
    }


    }

    public boolean isAlive() {return alive;}

    public void toggle()
    {
    alive = !alive;
    this.repaint();
    }

    // Implement the MouseListener interface.
    public void mousePressed(MouseEvent event) {toggle();}

    public void mouseReleased(MouseEvent event) {}

    public void mouseClicked(MouseEvent event) {}

    public void mouseEntered(MouseEvent event) {}

    public void mouseExited(MouseEvent event) {}
    }

    and this is the prompt

    1. The neighbors of a cell are the cells that border it
    horizontally, vertically, or diagonally.
    2. If a cell is alive, and 0 or 1 neighboring cells are alive,
    then the cell dies of loneliness in the next generation.
    3. If a cell is alive, and 2 or 3 neighboring cells are alive,
    then the cell remains alive in the next generation.
    4. If a cell is alive, and 4 or more neighboring cells are alive,
    then the cell dies of overcrowding in the next generation.
    5. If a cell is dead, and exactly 3 neighboring cells are alive,
    then the cellcomes to life in the next generation. Otherwise
    the cell remains dead.

    Write a Java GUI application that plays the Game Of Life. The
    user interface should display an eight row by eight column grid
    of LifeCell widgets. It should also display a button which
    generates the next generation of the current configuration.
    The close button should terminate the application.

    Each LifeCell widget is in one of two states: alive or dead. It
    must paint itself differently when alive than when dead, so the
    user can clearly see the state of the cell. The cell must change
    state when the mouse button is pressed over the cell. This
    enables the user to edit the configuration of living cells.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Anyone know how to do this game of life problem

    Please read the announcements on top of every forum for instructions on formatting your code properly.

    Do you have a question about your code? Where are you stuck? What's the problem?

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Anyone know how to do this game of life problem

    I'm stuck on how to display the LifeCell widgets

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Anyone know how to do this game of life problem

    That is a statement and not a question. The improve your chances of getting help you need to narrow down the problem and ask a specific question. "I don't know how to do this" is vague and not a question.
    Improving the world one idiot at a time!

Similar Threads

  1. Need help with Game of Life generations
    By MarcusSt0ne in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 7th, 2012, 08:56 AM
  2. 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
  3. 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
  4. 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
  5. 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