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

Game of Life The Game of Life is not a game in the conventional sense. There are

ID: 3641928 • Letter: G

Question

Game of Life

The Game of Life is not a game in the conventional sense. There are no players,
and no winning or losing. Once the "pieces" are placed in the starting position,
the rules determine everything that happen later (evolution).
The universe of the Game of Life is a two-dimensional grid of square cells, each
of which is in one of two possible states, alive or dead. Every cell interacts with
its eight neighbors (except the cells along the border), which are the cells that
are horizontally, vertically, or diagonally adjacent. At each step in time, the
following transitions occur:

1. A dead cell with exactly three live neighbors becomes a live cell (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 above rules
simultaneously to every cell in the seed. The rules continue to be applied
repeatedly to create further generations.

You will be provided with an input file “input.txt”. The first line of the input
contains two numbers separated by a space. The first number represents the
number of rows in the grid (let’s call it “r”). The second number shows the
number of columns in the grid (let’s call it “c”). The next r lines will contain c
characters, either a ‘.’ (period), representing a dead cell, or an ‘X’ representing a
live cell.

The following figure shows a sample input:

10 10
..XX..XXXX
.X.XX.XX..
XX....X..X
X...XX..XX
X..X.X....
..........
.XXX.....X
.......XXX
......XX..
X.X.X.X...

Figure 1 Sample Input.

You should create 3 output files; named as “output1.txt”, “output10.txt” and
“output100.txt”, each one containing the grid information after generation 1,
10, and 100, respectively. The output files format is similar to the 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;
    }
}