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

Write a java program that implements Conway’s Game of Life, where StdDraw is use

ID: 3736636 • Letter: W

Question

Write a java program that implements Conway’s Game of Life, where StdDraw is used to visualize how the conditions change, creating an animation. You should implement the solution using at least two classes: GameOfLife og GameOfLifeMain.

1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.

2. Any live cell with two or three live neighbours lives on to the next generation.

3. Any live cell with more than three live neighbours dies, as if by overpopulation.

4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

The class GameOfLife should define GameOfLife-objects that represents a condition in a GameOfLife simulation. The class has to have methods to manipulate the conditions, fx change the value of a cell, simulate a step forward ect. In the class GameOfLife the condition is represented in a 2-dimensional array of integers (int[][]), where 1 indicates a living cell and 0 indicates a dead cell. Hence the game is represented by a matrix.

Your GameOfLife-class must at least have the two constructors:

public GameOfLife(int n). A constructor to generate a Game of Life with the startcondition being a n times n grid of randomly initialized cells.

public GameOfLife(int[][] initialState). A constructor to generate a Game of Life with the startcondition being represented in the array initialState

You will also need to use several methods on the pa GameOfLife-objects. There should be a method nextState() that generates the following condition in the current GameOfLife. When writing the method nextState() it can be a help to create another method liveNeighbours(int x, int y) which counts how many living neighbours the cell (x,y) has i in the current game.

The class GameOfLifeMain have the main method, which initializes a GameOfLife-object and uses the methods related to the object. To ensure the animation runs smoothly, you should use the method StdDraw.show(n), where n is an integer. This method draws the image og waits n milliseconds. Following calls of drawing methods will not be shown until next call of StdDraw.show(n). The method StdDraw.clear() can be used to erase the drawn.

Finally the class GameOfLifeMain should be able to read the starting conditions from a file (.gol). The format of the file is a matrix consisting of ones and zeros fx.

100

Explanation / Answer

package com.zetcode; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.geom.Ellipse2D; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; class Surface extends JPanel implements ActionListener { private int pos_x = 8; private int pos_y = 8; private final int RADIUS = 90; private final int DELAY = 35; private Timer timer; private Image image; private final double delta[] = { 3, 3 }; public Surface() { loadImage(); determineAndSetImageSize(); initTimer(); } private void loadImage() { image = new ImageIcon("mushrooms.jpg").getImage(); } private void determineAndSetImageSize() { int h = image.getHeight(this); int w = image.getWidth(this); setPreferredSize(new Dimension(w, h)); } private void initTimer() { timer = new Timer(DELAY, this); timer.start(); } private void doDrawing(Graphics g) { Graphics2D g2d = (Graphics2D) g.create(); g2d.clip(new Ellipse2D.Double(pos_x, pos_y, RADIUS, RADIUS)); g2d.drawImage(image, 0, 0, null); g2d.dispose(); } @Override public void paintComponent(Graphics g) { super.paintComponent(g); doDrawing(g); } @Override public void actionPerformed(ActionEvent e) { moveCircle(); repaint(); } private void moveCircle() { int w = getWidth(); int h = getHeight(); if (pos_x < 0) { delta[0] = Math.random() % 4 + 5; } else if (pos_x > w - RADIUS) { delta[0] = -(Math.random() % 4 + 5); } if (pos_y < 0 ) { delta[1] = Math.random() % 4 + 5; } else if (pos_y > h - RADIUS) { delta[1] = -(Math.random() % 4 + 5); } pos_x += delta[0]; pos_y += delta[1]; } } public class ClippingEx extends JFrame { public ClippingEx() { initUI(); } private void initUI() { setTitle("Clipping"); add(new Surface()); pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { ClippingEx cl = new ClippingEx(); cl.setVisible(true); } }); } }
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