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

Using Java to code The Game of Life Place all the files for this project togethe

ID: 3764416 • Letter: U

Question

Using Java to code

The Game of Life

Place all the files for this project together into one .jar file or .zip file, including the ones you didn't change. Please following our naming style of LastNameHW6.extension When you create the .jar file, if you specify which class has the main method; then you can run it from your desktop by double clicking on the icon.

Life is a mathematical “game” invented by mathematician, John Conway. It became widely popular after it was published in a column in Scientific American in 1970. It is one of the most commonly programmed games on the computer.

Life is an example of a cellular automaton, a system in which rules are applied to cells and their neighbors in a regular grid. Life is played on a rectangular grid of square cells. Each of these cells is either dead or alive. This is its state. Here are the rules for Life:

Once started, the state of a cell depends on its current state and the current state of its neighbors.

These rules are applied to all of the cells at one time. That is, the new state of each cell is determined before changing the state of any of the cells.

Click here to see some additional notes on Life.

Objectives

Working with 2-dimensional arrays

Reading, understanding, and modifying existing code

Creating an executable jar file

Specification

Life is designed to run in a grid of infinite size. John Conway initially created Life on a Go board, with 19 rows and 19 columns. We will use this simplification for our implementation.

You are given starter code here: LifeStarter.zip. This .zip file contains 3 .java files; they should all compile. You will not have to write any new classes. Your task will be to implement the needed methods in the Life class in order for the program to run properly.

You will need to provide an initial state for the game. Your initial state must include some static patterns (like the block and beehive), some patterns that will die off , and some alternating patterns (like the blinker). You can include a glider as well, if you like. For more information about these patterns, see the additional notes about Life.

You need to determine the values for the next generation of all the cells completelybefore changing the state of the cells. Use two arrays, one for the current generation and another for the next generation.

Since our game is not of infinite size, we do have to think about the "boundary cells"; those on the edge of the grid. These are more challenging because they don't have 8 neighbors: the corners have only 3, the cells along the edges have just 5. For this assignment, all boundary cells are required to start off dead and stay dead.

Documentation

You get off easy this time. The JavaDocs are all in place (just make sure to add your name to the file you modify). What I'll be looking for here are good algorithm comments and good style. Don't forget them. Make sure to add your name to the Life class.

Suggestions

Draw pictures, not object diagrams, of the cells in the arrays.

Given a cell at some location [r][c] write down the indexes of the neighbors. You'll see a pattern here that can be translated into loops.

You will be dealing with nested loops (and nested loops within nested loops) so make sure you have an algorithm before you start typing. Staring at a computer screen can be frustrating...

Start with a simple initial state, perhaps just one pattern. Once you're satisfied that it works, then make additional patterns, one at a time, and test that they work.

Getting Started

You can compile and run the starter code I've given you (LifeGUI class has the main method). At this point all it does is display a blue/green screen with some grid lines on it. To make something else appear, you need to implement the newGame() and getCell() methods. newGame doesn't have to do anything special at this point: perhaps just a few cells set to ALIVE and the rest DEAD; you can enhance the initial board later on. Once you've implemented lifeCycle(), then things will change when you click the "Live One Cycle" button.

Life is a mathematical “game” invented by mathematician, John Conway. It became widely popular after it was published in a column in Scientific American in 1970. It is one of the most commonly programmed games on the computer.

Life is an example of a cellular automaton, a system in which rules are applied to cells and their neighbors in a regular grid. Life is played on a rectangular grid of square cells. Each of these cells is either dead or alive. This is its state. Here are the rules for Life:

Each cell has eight (8) neighbors.

Once started, the state of a cell depends on its current state and the current state of its neighbors.

If a dead cell has exactly three (3) live neighbors, it becomes alive. This is a birth.

If a dead cell does not have exactly three (3) live neighbors, it remains dead.

If a live cell has two (2) or three (3) live neighbors, it remains alive. This is survival.

If a live cell has zero (0) or one (1) live neighbors, it dies. This is loneliness.

If a live cell has four (4) or more live neighbors, it dies. This is overcrowding.

These rules are applied to all of the cells at one time. That is, the new state of each cell is determined before changing the state of any of the cells.

Click here to see some additional notes on Life.

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.GridBagLayout;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.*;
import java.util.Observer;
import java.util.Observable;
public class GameOfLifeGUI extends JFrame implements Observer
{
private JPanel mainPanel;
private JPanel gridPanel;
private JPanel startPanel;
private JPanel titlePanel;
private JButton start;
private Cell cell;
private Grid grid;
private MouseEvent mouseClicked;
private MouseEvent mouseDragged;
private MouseEvent mousePressed;
private MouseEvent mouseRelease;
private MouseListener mouseListener;
public GameOfLifeGUI()
{
super("");
JButton start = new JButton("Start");
grid = new Grid(75,75);
mainPanel = new JPanel();
gridPanel = new JPanel();
startPanel = new JPanel();
titlePanel = new JPanel();
gridPanel.setLayout(new GridLayout(75,75));
gridPanel.setBackground(Color.WHITE);
gridPanel.add(grid);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(gridPanel, BorderLayout.CENTER);
mainPanel.add(startPanel, BorderLayout.SOUTH);
mainPanel.add(titlePanel, BorderLayout.NORTH);
startPanel.add(start);
this.setTitle("Conway's Game of Life");
this.setSize(1000, 750);
this.setLocationRelativeTo(null);
this.add(mainPanel);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}

Grid

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Component;
import java.awt.Color;
import javax.swing.JPanel.*;
import java.util.Observer;
import java.util.Arrays;
import java.util.Observable;
public class Grid extends JPanel
{
private Cell[][] grid;
private int column;
private int row;
public Grid(int column, int row)
{
this.column = column;
this.row = row;
grid = new Cell[row][column];
for (int r = 0; r < row; r++)
{
for (int c = 0; c < column; c++)
{
grid[r][c] = new Cell(r,c);
}
}
for (int c = 0; c < column; c++)
{
grid[0][c] = new Cell(row, column);
}
for (int c = 0; c < column-1; c++)
{
grid[row-1][c] = new Cell(row, column);
}
for (int r = 0; r < row; r++)
{
grid[r][0] = new Cell(row, column);
}
for (int r = 0; r < row-1; r++)
{
grid[r][column - 1] = new Cell(row, column);
}
}

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