Answered You can hire a professional tutor to get the answer.

QUESTION

For this game of life code, Please do the following writing for the code: 1-program description: functionality and features 2-overall program design...

For this game of life code, Please do the following writing for the code:

1-program description: functionality and features

2-overall program design

3-user interface

4-major classes: functionality of each class

World.java

import java.util.Scanner;

public class World{

// members

private char[][] world;

private int row;

private int col;

private char[][] glider = {{' ','*',' '},

{' ',' ','*'},

{'*','*','*'}};

int gliderHeight = 3;

int gliderLength = 3;

private char[][] exploder = {{' ','*',' '},

{'*','*','*'},

{'*',' ','*'},

{' ','*',' '}};

int exploderHeight = 4;

int exploderLength = 3;

private char[][] tumbler = {{' ','*','*',' ','*','*',' '},

{' ','*','*',' ','*','*',' '},

{' ',' ','*',' ','*',' ',' '},

{'*',' ','*',' ','*',' ','*'},

{'*',' ','*',' ','*',' ','*'},

{'*','*',' ',' ',' ','*','*'}};

int tumblerHeight = 6;

int tumblerLength = 7;

private char[][] tencellRow = {{'*','*','*','*','*','*','*','*','*','*'}};

int tenCellHeight = 1;

int tenCellLength = 10;

// constructors

// no-arg

public World()

{

}

//arg

public World(int r, int c)

{

row = r;

col = c;

}

//method

public void initializeGrid()//pass in r & c

{

world = new char[row][col];

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

{

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

{

world[i][j] = ' ';

}

}

}

public void printGrid()

{

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

{

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

{

System.out.print(world[i][j]);

}

System.out.println();

}

}

//count neighbors

public void checkCells()

{

int neighbors = 0;

char[][] temporary = new char[row][col];

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

{

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

{

temporary[i][j] = world[i][j];

}

}

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

{

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

{

neighbors = 0;

if (j < row-1)

{

if(world[i][j + 1] == '*')

{

neighbors++;

}

}

if (j > 0)

{

if(world[i][j - 1] == '*')

{

neighbors++;

}

}

if (i < row-1)

{

if(world[i + 1][j] == '*')

{

neighbors++;

}

}

if (i > 0)

{

if(world[i - 1][j] == '*')

{

neighbors++;

}

}

if (i < row-1 && j < row-1)

{

if(world[i + 1][j + 1] == '*')

{

neighbors++;

}

}

if (i > 0 && j > 0)

{

if(world[i - 1][j -1] == '*')

{

neighbors++;

}

}

if (j > 0 && i < row-1)

{

if(world[i + 1][j - 1] == '*')

{

neighbors++;

}

}

if (i > 0 && j < row-1)

{

if(world[i - 1][j + 1] == '*')

{

neighbors++;

}

}

//rules for live cell

if (world[i][j] == '*'){

if (neighbors == 3 || neighbors == 2)

{

temporary[i][j] = '*';

}

else

{

temporary[i][j] = ' ';

}

}

//rules for dead cell

else if (world[i][j] == ' '){

if (neighbors == 3)

{

temporary[i][j] = '*';

}

}

}

}

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

{

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

{

world[i][j] = temporary[i][j];

}

}

}

public void placePattern(String patternName)

{

char[][] pattern = {};

int patternHeight = 0;

int patternLength = 0;

if(patternName.equals("glider"))

{

pattern = glider;

patternHeight = gliderHeight;

patternLength = gliderLength;

}

if(patternName.equals("exploder"))

{

pattern = exploder;

patternHeight = exploderHeight;

patternLength = exploderLength;

}

if(patternName.equals("tumbler"))

{

pattern = tumbler;

patternHeight = tumblerHeight;

patternLength = tumblerLength;

}

if(patternName.equals("ten row cell"))

{

pattern = tencellRow;

patternHeight = tenCellHeight;

patternLength = tenCellLength;

}

int place = (row / 2) -1;

for(int i = place; i < (place + patternHeight); i++)

{

for(int x = place; x < (place + patternLength); x++)

{

world[i][x] = pattern[i - place][x - place];

}

}

}

}

WorldDriver.java

import java.util.Scanner;

public class WorldDriver

{

public static void main(String[] args) throws InterruptedException

{

Scanner input = new Scanner(System.in); //scanner

System.out.print("Enter number of rows and columns: ");

int rows = input.nextInt(); //rows are equal to columns

int columns = rows;

Scanner input1 = new Scanner(System.in);

System.out.println("Enter what pattern you want to input: glider, exploder, tumbler, ten row cell, or random assignment: ");

String pattern = input1.nextLine();

World world1 = new World(rows, columns); //passes world class

if(pattern.equals("glider"))

{

if(rows < world1.gliderHeight)

{

System.out.println("Board is too small try again");

System.exit(0);

}

if(columns < world1.gliderLength)

{

System.out.println("Board is too small try again");

System.exit(0);

}

}

else if(pattern.equals("exploder"))

{

if(rows < world1.gliderHeight)

{

System.out.println("Board is too small try again");

System.exit(0);

}

if(columns < world1.gliderLength)

{

System.out.println("Board is too small try again");

System.exit(0);

}

}

else if(pattern.equals("tumbler"))

{

if(rows < world1.tumblerHeight)

{

System.out.println("Board is too small try again");

System.exit(0);

}

if(columns < world1.tumblerLength)

{

System.out.println("Board is too small try again");

System.exit(0);

}

}

else if(pattern.equals("ten row cell"))

{

if(rows < world1.tenCellHeight)

{

System.out.println("Board is too small try again");

System.exit(0);

}

if(columns < world1.tenCellLength)

{

System.out.println("Board is too small try again");

System.exit(0);

}

}

else

{

System.out.println("Invalid Pattern");

System.exit(0);

}

System.out.println("Enter number of generations:");

int gen = input.nextInt();

System.out.println("Enter wait time between generations(in milliseconds):");

int sleepTime = input.nextInt();

world1.initializeGrid(); //initialize grid

world1.placePattern(pattern);

world1.printGrid(); //print grid dont have to pass anything object holds the variable

int count = 0;

while(count < gen)

{

world1.checkCells(); // checks and prints live cells object holds the variable

count++;

Thread.sleep(sleepTime);

world1.printGrid();

}

input.close();

input1.close();

}

}

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question