Game of Life The Game of Life is not a game in the conventional sense. Thereare
ID: 3641927 • Letter: G
Question
Game of Life
The Game of Life is not a game in the conventional sense. Thereare no players,
and no winning or losing. Once the "pieces" are placed in thestarting position,
the rules determine everything that happen later(evolution).
The universe of the Game of Life is a two-dimensional grid ofsquare cells, each
of which is in one of two possible states, alive or dead. Everycell interacts with
its eight neighbors (except the cells along the border), whichare the cells that
are horizontally, vertically, or diagonally adjacent. Ateach step in time, the
following transitions occur:
1. A dead cell with exactly three live neighbors becomes a livecell (birth).
2. A live cell with two or three live neighbors stays alive(survival).
3. A live cell, with four or more neighbors dies(overcrowding).
4. A live cell, with one or less neighbors, dies(loneliness).
The initial pattern (provided as an input file to you)constitutes the seed of the
system. The first generation is created by applying the aboverules
simultaneously to every cell in the seed. The rules continue tobe applied
repeatedly to create further generations.
You will be provided with an input file “input.txt”. The firstline of the input
contains two numbers separated by a space. The firstnumber represents the
number of rows in the grid (let’s call it “r”). Thesecond number shows the
number of columns in the grid (let’s call it “c”). Thenext r lines will contain c
characters, either a ‘.’ (period), representing a dead cell, oran ‘X’ representing alive cell.
The following figure shows a sample input:
You should create 3 output files; named as“output1.txt”, “output10.txt” and
“output100.txt”, each one containing the grid informationafter generation 1,
10, and 100, respectively. The output files format is similar tothe input file (no
row and column number at the first line).
Explanation / Answer
import java.io.*;
import java.util.Scanner;
public class GameOfLife {
public static void main(String[] args) throws FileNotFoundException, IOException {
Scanner fileInput = new Scanner(new FileReader("input.txt"));
PrintWriter out1 = new PrintWriter(new FileWriter("out1.txt"));
PrintWriter out10 = new PrintWriter(new FileWriter("out10.txt"));
PrintWriter out100 = new PrintWriter(new FileWriter("out100.txt"));
int row = fileInput.nextInt();
int col = fileInput.nextInt();
char[][] grid = new char[row + 2][col + 2];
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
grid[r][c] = '.';
}
}
for (int r = 1; r <= row; r++) {
String lineBuff = fileInput.next();
for (int c = 1; c <= col; c++) {
grid[r][c] = lineBuff.charAt(c - 1);
}
}
for (int step = 1; step <= 100; step++) {
nextGeneration(grid, row, col);
if (step == 1) {
for (int r = 1; r <= row; r++) {
for (int c = 1; c <= col; c++) {
out1.print(grid[r][c]);
}
out1.println();
}
}
else if (step == 10) {
for (int r = 1; r <= row; r++) {
for (int c = 1; c <= col; c++) {
out10.print(grid[r][c]);
}
out10.println();
}
}
else if (step == 100) {
for (int r = 1; r <= row; r++) {
for (int c = 1; c <= col; c++) {
out100.print(grid[r][c]);
}
out100.println();
}
}
}
fileInput.close();
out1.close();
out10.close();
out100.close();
}
private static void nextGeneration(char[][] grid, int row, int col) {
char[][] gridCpy = new char[row + 2][col + 2];
for (int r = 0; r < gridCpy.length; r++) {
for (int c = 0; c < gridCpy[r].length; c++) {
gridCpy[r][c] = '.';
}
}
for (int r = 1; r <= row; r++) {
for (int c = 1; c <= col; c++) {
gridCpy[r][c] = nextCellStatus(grid, r, c) ? 'X' : '.';
}
}
for (int r = 1; r <= row; r++) {
for (int c = 1; c <= col; c++) {
grid[r][c] = gridCpy[r][c];
}
}
}
private static boolean nextCellStatus(char[][] grid, int cellRow, int cellCol) {
int neighborCount = grid[cellRow][cellCol] == 'X' ? -1 : 0;
for (int r = cellRow - 1; r <= cellRow + 1; r++) {
for (int c = cellCol - 1; c <= cellCol + 1; c++) {
neighborCount += grid[r][c] == 'X' ? 1 : 0;
}
}
if (neighborCount == 3) {
return true;
}
if (neighborCount == 2 && grid[cellRow][cellCol] == 'X') {
return true;
}
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.