Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program that cotains the following requirements: An intial start pa

ID: 3758302 • Letter: W

Question

Write a java program that cotains the following requirements: An intial start pattern will be passed in. The game of Life (not the board game) is intended to model life in a society of organisms. Given a rectangular grid of cells (i.e. a two dimensional array), a cell (individual square of the grid) may contain an organism. Except for cells on the border of the grid, each cell in the grid has eight neighbors(above, below, right, left, above and to the right, above and to the left, below and to the right, below and to the left.) Organism are born and die according to the following rules: a) an organism is born in an empty cell that has exactly three neighbors b) an organism will die from isolation if it has fewer than two neighbors c) an organism will die from overcrowding if it has more than three neighbors The following display shows the first five generations of a particular configuration of organisms: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 1st 2nd 3rd 4th 5th Write a program to play the game of Life. Specifics: 1. Use methods. 2. The grid is 10 by 8. 3. Create a method of displaying the grid with the cells in their respective location. 4. Show the user the first generation and ask if they like to see the next generation. Your program should continue until the user decides to quit. 5. You’ll need to consider boundary areas. Make sure you don’t go outside the bounds of your grid. Tips: 1. Use two arrays, one that holds the old grid configuration and one that holds the next (new) generation configuration. Create a method to copy the contents of new configuration to the old. 2. Get the display method working right away. This way you can tell if the game rules are being applied correctly. 3. To save time, you may hardcode the initial organism coordinates.

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Life extends JPanel implements ActionListener, MouseListener, MouseMotionListener
{
public static void main(String[] args)
{
JFrame f = new JFrame("Life");
JPanel p = new Life();
f.setContentPane(p);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setLocation(100,50);
f.setVisible(true);
}
private final int GRID_SIZE = 100;
private boolean[][] alive;   
private MosaicPanel display;
private Timer timer;
private JButton stopGoButton;
private JButton nextButton;   
private JButton randomButton;
private JButton clearButton;   
private JButton quitButton;
public Life()
{
alive = new boolean[GRID_SIZE][GRID_SIZE];
setLayout(new BorderLayout(3,3));
setBackground(Color.GRAY);
setBorder(BorderFactory.createLineBorder(Color.GRAY,3));
int cellSize = 600/GRID_SIZE;
display = new MosaicPanel(GRID_SIZE,GRID_SIZE,cellSize,cellSize);
if (cellSize < 5)
display.setGroutingColor(null);
display.setUse3D(false);
add(display,BorderLayout.CENTER);
JPanel bottom = new JPanel();
add(bottom,BorderLayout.SOUTH);
clearButton = new JButton("Clear");
stopGoButton = new JButton("Start");
quitButton = new JButton("Quit");
nextButton = new JButton("One Step");
randomButton = new JButton("Random Fill");
bottom.add(stopGoButton);
bottom.add(nextButton);
bottom.add(randomButton);
bottom.add(clearButton);
bottom.add(quitButton);
stopGoButton.addActionListener(this);
clearButton.addActionListener(this);
quitButton.addActionListener(this);
randomButton.addActionListener(this);
nextButton.addActionListener(this);
display.addMouseListener(this);
display.addMouseMotionListener(this);
timer = new Timer(50,this);
}
private void doFrame()
{
boolean[][] newboard = new boolean[GRID_SIZE][GRID_SIZE];
for ( int r = 0; r < GRID_SIZE; r++ )
{
int above, below;
int left, right;
above = r > 0 ? r-1 : GRID_SIZE-1;
below = r < GRID_SIZE-1 ? r+1 : 0;
for ( int c = 0; c < GRID_SIZE; c++ )
{
left = c > 0 ? c-1 : GRID_SIZE-1;
right = c < GRID_SIZE-1 ? c+1 : 0;
int n = 0;
if (alive[above][left])
n++;
if (alive[above][c])
n++;
if (alive[above][right])
n++;
if (alive[r][left])
n++;
if (alive[r][right])
n++;
if (alive[below][left])
n++;
if (alive[below][c])
n++;
if (alive[below][right])
n++;
if (n == 3 || (alive[r][c] && n == 2))
newboard[r][c] = true;
else
newboard[r][c] = false;
}
}
alive = newboard;
}
private void showBoard()
{
display.setAutopaint(false);
for (int r = 0; r < GRID_SIZE; r++)
{
for (int c = 0; c < GRID_SIZE; c++)
{
if (alive[r][c])
display.setColor(r,c,Color.WHITE);
else
display.setColor(r,c,null);
}
}
display.setAutopaint(true);
}
public void actionPerformed(ActionEvent e)
{
Object src = e.getSource();
if (src == quitButton)
{
System.exit(0);
}
else if (src == clearButton)
{
alive = new boolean[GRID_SIZE][GRID_SIZE];
display.clear();
}
else if (src == nextButton)
{
doFrame();
showBoard();
}
else if (src == stopGoButton)
{
if (timer.isRunning())
{
timer.stop();
clearButton.setEnabled(true);
randomButton.setEnabled(true);
nextButton.setEnabled(true);
stopGoButton.setText("Start");
}
else
{
timer.start();
clearButton.setEnabled(false);
randomButton.setEnabled(false);
nextButton.setEnabled(false);
stopGoButton.setText("Stop");
}
}
else if (src == randomButton)
{
for (int r = 0; r < GRID_SIZE; r++)
{
for (int c = 0; c < GRID_SIZE; c++)
alive[r][c] = (Math.random() < 0.25);
}
showBoard();
}
else if (src == timer)
{
doFrame();
showBoard();
}
}
public void mousePressed(MouseEvent e)
{
if (timer.isRunning())
return;
int row = display.yCoordToRowNumber(e.getY());
int col = display.yCoordToRowNumber(e.getX());
if (row >= 0 && row < display.getRowCount() && col >= 0 && col < display.getColumnCount())
{
if (e.isMetaDown() || e.isControlDown())
{
display.setColor(row,col,null);
alive[row][col] = false;
}
else
{
display.setColor(row,col,Color.WHITE);
alive[row][col] = true;
}
}
}
public void mouseDragged(MouseEvent e)
{
mousePressed(e);
}
public void mouseClicked(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseMoved(MouseEvent e) { }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote