Question

Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

Write this Game of Life program in Java.

The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is computed. A new cell is born on an empty square if it is surrounded by exactly three occupied neighbor cells. A cell dies of overcrowding if it is surrounded by four or more neighbors, and it dies of loneliness if it is surrounded by zero or one neighbor. A neighbor is an occupant of an adjacent square to the left, right, top, or bottom or in a diagonal direction.

Program the game to eliminate the drudgery of computing successive generations by hand. Use a two-dimensional array to store the rectangular configuration. Write a program that shows successive generations of the game on console (you may not use GUI). Ask the user to specify the original configuration, by taking in user input for for number of rows and columns; and then user input for the initial board configuration of 1's and 0's.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

OUTPUT SCREENSHOT:


COPY CODE:

#include <iostream>
#define HEIGHT 4
#define WIDTH 4

struct Shape {
public:
char xCoord;
char yCoord;
char height;
char width;
char **figure;
};

struct Glider : public Shape {
static const char GLIDER_SIZE = 3;
Glider( char x , char y );
~Glider();
};

struct Blinker : public Shape {
static const char BLINKER_HEIGHT = 3;
static const char BLINKER_WIDTH = 1;
Blinker( char x , char y );
~Blinker();
};

class GameOfLife {
public:
GameOfLife( Shape sh );
void print();
void update();
char getState( char state , char xCoord , char yCoord , bool toggle);
void iterate(unsigned int iterations);
private:
char world[HEIGHT][WIDTH];
char otherWorld[HEIGHT][WIDTH];
bool toggle;
Shape shape;
};

GameOfLife::GameOfLife( Shape sh ) :
shape(sh) ,
toggle(true)
{
for ( char i = 0; i < HEIGHT; i++ ) {
for ( char j = 0; j < WIDTH; j++ ) {
world[i][j] = '.';
}
}
for ( char i = shape.yCoord; i - shape.yCoord < shape.height; i++ ) {
for ( char j = shape.xCoord; j - shape.xCoord < shape.width; j++ ) {
if ( i < HEIGHT && j < WIDTH ) {
world[i][j] =
shape.figure[ i - shape.yCoord ][j - shape.xCoord ];
}
}
}
}

void GameOfLife::print() {
if ( toggle ) {
for ( char i = 0; i < HEIGHT; i++ ) {
for ( char j = 0; j < WIDTH; j++ ) {
std::cout << world[i][j];
}
std::cout << std::endl;
}
} else {
for ( char i = 0; i < HEIGHT; i++ ) {
for ( char j = 0; j < WIDTH; j++ ) {
std::cout << otherWorld[i][j];
}
std::cout << std::endl;
}
}
for ( char i = 0; i < WIDTH; i++ ) {
std::cout << '=';
}
std::cout << std::endl;
}

void GameOfLife::update() {
if (toggle) {
for ( char i = 0; i < HEIGHT; i++ ) {
for ( char j = 0; j < WIDTH; j++ ) {
otherWorld[i][j] =
GameOfLife::getState(world[i][j] , i , j , toggle);
}
}
toggle = !toggle;
} else {
for ( char i = 0; i < HEIGHT; i++ ) {
for ( char j = 0; j < WIDTH; j++ ) {
world[i][j] =
GameOfLife::getState(otherWorld[i][j] , i , j , toggle);
}
}
toggle = !toggle;
}
}

char GameOfLife::getState( char state, char yCoord, char xCoord, bool toggle ) {
char neighbors = 0;
if ( toggle ) {
for ( char i = yCoord - 1; i <= yCoord + 1; i++ ) {
for ( char j = xCoord - 1; j <= xCoord + 1; j++ ) {
if ( i == yCoord && j == xCoord ) {
continue;
}
if ( i > -1 && i < HEIGHT && j > -1 && j < WIDTH ) {
if ( world[i][j] == 'X' ) {
neighbors++;
}
}
}
}
} else {
for ( char i = yCoord - 1; i <= yCoord + 1; i++ ) {
for ( char j = xCoord - 1; j <= xCoord + 1; j++ ) {
if ( i == yCoord && j == xCoord ) {
continue;
}
if ( i > -1 && i < HEIGHT && j > -1 && j < WIDTH ) {
if ( otherWorld[i][j] == 'X' ) {
neighbors++;
}
}
}
}
}
if (state == 'X') {
return ( neighbors > 1 && neighbors < 4 ) ? 'X' : '.';
}
else {
return ( neighbors == 3 ) ? 'X' : '.';
}
}

void GameOfLife::iterate( unsigned int iterations ) {
for ( int i = 0; i < iterations; i++ ) {
print();
update();
}
}

Glider::Glider( char x , char y ) {
xCoord = x;
yCoord = y;
height = GLIDER_SIZE;
width = GLIDER_SIZE;
figure = new char*[GLIDER_SIZE];
for ( char i = 0; i < GLIDER_SIZE; i++ ) {
figure[i] = new char[GLIDER_SIZE];
}
for ( char i = 0; i < GLIDER_SIZE; i++ ) {
for ( char j = 0; j < GLIDER_SIZE; j++ ) {
figure[i][j] = '.';
}
}
figure[0][1] = 'X';
figure[1][2] = 'X';
figure[2][0] = 'X';
figure[2][1] = 'X';
figure[2][2] = 'X';
}

Glider::~Glider() {
for ( char i = 0; i < GLIDER_SIZE; i++ ) {
delete[] figure[i];
}
delete[] figure;
}

Blinker::Blinker( char x , char y ) {
xCoord = x;
yCoord = y;
height = BLINKER_HEIGHT;
width = BLINKER_WIDTH;
figure = new char*[BLINKER_HEIGHT];
for ( char i = 0; i < BLINKER_HEIGHT; i++ ) {
figure[i] = new char[BLINKER_WIDTH];
}
for ( char i = 0; i < BLINKER_HEIGHT; i++ ) {
for ( char j = 0; j < BLINKER_WIDTH; j++ ) {
figure[i][j] = 'X';
}
}
}

Blinker::~Blinker() {
for ( char i = 0; i < BLINKER_HEIGHT; i++ ) {
delete[] figure[i];
}
delete[] figure;
}

int main() {
Glider glider(0,0);
GameOfLife gol(glider);
gol.iterate(5);
Blinker blinker(1,0);
GameOfLife gol2(blinker);
gol2.iterate(4);
}

Add a comment
Know the answer?
Add Answer to:
Write this Game of Life program in Java. The Game of Life is a well-known mathematical...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • Write a program that consists of a 10 by 10 game board, with 10 generations. Project...

    Write a program that consists of a 10 by 10 game board, with 10 generations. Project The Game of Life The life game consists of a board with size of NxN cells and cells are occupied by creatures. Each cell can have at most one creature. The surrounding cells are called the neighbors of this cell. Each game state is called "generation". The game progresses from one generation to the next according to the following rules: A creature that has...

  • Please use C++, thank you! The life game consists of a board with size of NxN cells and cells are occupied by creatures. Each cell can have at most one creature. The surrounding cells are called the...

    Please use C++, thank you! The life game consists of a board with size of NxN cells and cells are occupied by creatures. Each cell can have at most one creature. The surrounding cells are called the neighbors of this cell Each game state is called "generation". The game progresses from one generation to the next according to the following rules: A creature that has more than 3 neighbors- dies of crowding. Its cell will be empty in the next...

  • JAVA. Threading the Game of Life Write a Java program to implement the Game of Life,...

    JAVA. Threading the Game of Life Write a Java program to implement the Game of Life, as defined by John Conway: Any live cell with fewer than two live neighbors dies, as if caused by under-population. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if by...

  • I am really struggling with how to approach this program. Conway's Game of life, whereby, 1....

    I am really struggling with how to approach this program. Conway's Game of life, whereby, 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population. 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 over-population. 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. Submit...

  • I have a question about my assignment. Could you please make program and give me some...

    I have a question about my assignment. Could you please make program and give me some explanation of codes? I need c++ codes. Conway's Game of Life is a game invented by mathematician John Conway in 1970. The rules are as follows: Each cell lives in a cell in a square (make it 10x10) grid. A cell can either be dead or alive. Before you start the game, you need to provide an initial state. You can do this by...

  • I have a question about my assignment. Could you please make program and give me some...

    I have a question about my assignment. Could you please make program and give me some explanation of codes? Conway's Game of Life is a game invented by mathematician John Conway in 1970. The rules are as follows: Each cell lives in a cell in a square (make it 10x10) grid. A cell can either be dead or alive. Before you start the game, you need to provide an initial state. You can do this by randomly setting 20 of...

  • Option 2: Conwav's Game of Life Your task is to write a program that plays Conway's...

    Option 2: Conwav's Game of Life Your task is to write a program that plays Conway's Game of Life (for details and ex amples, see https://en.vikipedia.org/wiki/Convay's.Game of Life). The basic gam ts played on each generation, each cell changes according to the following instructions: nal grid of square cells, each of which is either alive or dead. With 1. Any dead cell with exactly three live neighbors (out of its eight nearest neighbors) comes to life [reproduction]. 2. Any live...

  • Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques...

    Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques we have been using in class are allowed. Nothing else. The program should read the initial state of the board by reading in "alive" cells from a user input data file. Meaning your program should ask the user the name of the data file. Assume all the other cells are "dead." Make sure to use modular coding techniques. The main program should be pretty...

  • Objective: Write a program that implements the Game of Life cellular automata system invented by John...

    Objective: Write a program that implements the Game of Life cellular automata system invented by John Conway. 1. Create two game grids of size at least 50x50. These grid cells can be either Boolean or integer. In the following, I’ll refer to these as gridOne and gridTwo. 2. Set all cells in both grids to false. 3. Start by initializing gridOne. Allow the user to specify two different ways of initializing the grid: 1) by specifying a pattern file to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT