Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***** BELOW IS MY CODE FOR LIFEGAME(GAME OF LIFE). BUT I NEED COUPLE OF THINGS TO GET DONE IN THAT, 1.) I NEED HORIZONTAL AND

***** BELOW IS MY CODE FOR LIFEGAME(GAME OF LIFE). BUT I NEED COUPLE OF THINGS TO GET DONE IN THAT,

1.) I NEED HORIZONTAL AND VERTICAL LINE GRID WHEN THE CODE STARTS.

2.) I NEED TO Allow the user to input the desired grid dimensions before the game begins.

3.) Write the updateCells subroutine in a general-enough way that one can easily change the parameters of the game: e.g. set it so that a cell must have 4, rather than 3, living neighbors to be born.

******************* I NEED THIS THREE THINGS DONE IN MY CODE. NEEP HELP!!

// LifeGame Class

public class LifeGame {

private int rows, cols;

private boolean[][] cells;

public LifeGame(int rows, int cols){

this.rows = rows;

this.cols = cols;

cells = new boolean[rows][cols];

initialize();

}

private void initialize(){

for(int i = 0; i < rows; i++)

for(int j = 0; j < cols; j++)

{

if(Math.random() <= 0.2) //20% of cells are alive

cells[i][j] = true;

}

}

public boolean isAlive(int row, int col){

return cells[row][col];

}

//checks if the row, col is valid grid position

private boolean isOnBoard(int row, int col){

if(row >= 0 && col >= 0 && row < rows && col < cols)

return true;

else

return false;

}

private int countLiveNeighbors(int row, int col){

int count = 0;

for(int r = row - 1; r <= row + 1 ; r++)

{

for(int c = col-1; c <= col+1; c++)

{

if(r == row && c == col) //don't count the cell itself for whom we are counting neighbors

continue;

if(isOnBoard(r, c) && isAlive(r,c))

count++;

}

}

return count;

}

public void nextGeneration(){

boolean[][] next = new boolean[rows][cols];

for(int r = 0; r < rows; r++)

{

for(int c = 0; c < cols; c++)

{

int count = countLiveNeighbors(r, c);

if(count > 3 || count < 2) //overcrowded or lonely

next[r][c] = false;

else if(count == 3) //cell with 3 neighbors, (live/dead) always is live in next generation

next[r][c] = true;

else if(count == 2 && isAlive(r,c)) //a live cell with 2 neighbours lives in next gen

next[r][c] = true;

}

}

cells = next;

}

public int getRows(){

return rows;

}

public int getCols(){

return cols;

}

public String toString(){

String str = "";

for(int r = 0; r < rows; r++)

{

for(int c = 0; c < cols; c++)

{

if(cells[r][c])

str += "0";

else

str += ".";

}

str += " ";

}

str += " ";

return str;

}

}

// SimpleAnimationStarter Class

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class SimpleAnimationStarter extends JPanel implements ActionListener {

private LifeGame game ;

public SimpleAnimationStarter()

{

game = new LifeGame(150,150); //20 rows, 20 cols;

}

public void drawFrame(Graphics g, int frameNumber, int width, int height) {

int x = 0, y = 0;

int rows = game.getRows(), cols = game.getCols();

int dx = width / cols;

int dy = height / rows;

Color color;

//System.out.println(game);

drawGrid(g, width, height);

for(int r = 0; r < rows; r++)

{

x = 0;

for(int c = 0; c < cols; c++)

{

if(game.isAlive(r, c))

color = Color.black;

else

color = Color.white;

g.setColor(color);

g.fillRect(x, y, dx, dy);

x += dx;

}

y += dy;

}

game.nextGeneration();

}

private void drawGrid(Graphics g, int width, int height)

{

int x = 0, y = 0;

int rows = game.getRows(), cols = game.getCols();

int dx = width / cols;

int dy = height / rows;

for(int i = 1; i <= rows; i++)

{

g.drawLine(0, y, width, y);

y += dy;

}

for(int i = 1; i <= cols; i++)

{

g.drawLine(x, 0, x, height);

x += dx;

}

}

//------ Implementation details: DO NOT EXPECT TO UNDERSTAND THIS ------

public static void main(String[] args) {

JFrame window = new JFrame("Simple Animation");

SimpleAnimationStarter drawingArea = new SimpleAnimationStarter();

drawingArea.setBackground(Color.WHITE);

window.setContentPane(drawingArea);

drawingArea.setPreferredSize(new Dimension(600,450));

window.pack();

window.setLocation(100,50);

window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

window.setResizable(true);

Timer frameTimer = new Timer(200,drawingArea);

window.setVisible(true);

frameTimer.start();

} // end main

private int frameNum;

public void actionPerformed(ActionEvent evt) {

frameNum++;

repaint();

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

drawFrame(g, frameNum, getWidth(), getHeight());

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions

Question

What is human nature?

Answered: 1 week ago

Question

6. Explain how to train managers to coach employees.

Answered: 1 week ago

Question

5. Tell how job experiences can be used for skill development.

Answered: 1 week ago