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.