Write in BASIC Java the Game of Life program by Conway. (I am not an advanced pr
ID: 3820707 • Letter: W
Question
Write in BASIC Java the Game of Life program by Conway. (I am not an advanced programmer so keeping it basic as possible will help me understand the program better.)
The program must be implemented under a single class.
Represent a 20 x 20 grid.
When calculating the results of the rules:
o Squares off the edges of the board are assumed to be always empty
o All ‘births’ and ‘deaths’ happen simultaneously. In other words, the number of live neighbors for a given cell is always based on the cells before any rule was applied.
At the start :
The program will output 1) the current round # (round 1) and the initial layout of the board.
Then:
The program will wait for user input. Inputting “n” will calculate and display the next round board, along with the number of births and deaths.
i.e. look at picture.
And program to terminate if any other input in inserted.
Births: 43 Deaths: 122 Round #2 0000 0000 00 000 o 00 000 0 0000 000 00 00 0 00 00000 00 00 0 0 00 0000 0 00 00 00 00 0000 000 00 0 00 00 0 0000 000 00 00 0 2-00 00000 000 0000 0 s-00 0000 000 h2 t# 000 000 ed 000 Dn 30 4R0 0 ---00 00 00 h_0 000 r-00 00 nB 00 0000Explanation / Answer
LifeGame.java:
import java.util.Random;
import java.util.Scanner;
public class LifeGame {
// to keep track of deaths and births in a round
static int born, dies;
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Random r = new Random();
int[][] cells = new int[20][20];
// fill cells with random values
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if (i == 0 || j == 0 || i == 19 || j == 19)
cells[i][j] = 0;
else {
cells[i][j] = r.nextInt(2);
}
}
}
char choice = 'n';
int round = 1;
System.out.println("Starting game with below configuration");
print(cells);
System.out.println();
while (choice == 'n' || choice == 'N') {
// simulate one round
simulate(cells);
System.out.println("Births: " + born + " Deaths: " + dies);
System.out.println("---------------------------------- Round :"
+ round++);
// print information
print(cells);
// ask user for next
System.out.print("Enter choice for next round: ");
choice = s.next().charAt(0);
}
s.close();
}
// this method simulates a round
private static void simulate(int[][] cells) {
// marking cells to be deleted with -1
// marking cells for generation with 2
// after visiting all cells, we will convert the above marked cells with
// 0s or 1s as appropriate
// to keep track born and dies
int b = 0, d = 0;
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
// find no of live neighbors
int n = findActiveNeighbours(cells, i, j);
if (cells[i][j] == 1) { // if cell is live
if (n == 0 || n == 1) {
cells[i][j] = -1; // mark for deletion
d++;
} else if (n >= 4) {
cells[i][j] = -1; // mark for deletion
d++;
}
} else { // if cell is dead
if (n == 3) {
cells[i][j] = 2; // mark for generation
b++;
}
}
}
}
born = b;
dies = d;
// convert -1(dead) to 0 and 2(living) to 1 in cells
restoreCells(cells);
}
// convert -1(dead) to 0 and 2(living) to 1 in cells
private static void restoreCells(int[][] cells) {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if (cells[i][j] == -1) {
cells[i][j] = 0;
} else if (cells[i][j] == 2) {
cells[i][j] = 1; // mark for generation
}
}
}
}
// find no of live neighbors
private static int findActiveNeighbours(int[][] cells, int i, int j) {
int activeNeighbours = 0;
if (isValid(i - 1, j - 1) && (Math.abs(cells[i - 1][j - 1]) == 1))
activeNeighbours++;
if (isValid(i - 1, j) && (Math.abs(cells[i - 1][j]) == 1))
activeNeighbours++;
if (isValid(i - 1, j + 1) && (Math.abs(cells[i - 1][j + 1]) == 1))
activeNeighbours++;
if (isValid(i, j - 1) && (Math.abs(cells[i][j - 1]) == 1))
activeNeighbours++;
if (isValid(i, j + 1) && (Math.abs(cells[i][j + 1]) == 1))
activeNeighbours++;
if (isValid(i + 1, j - 1) && (Math.abs(cells[i + 1][j - 1]) == 1))
activeNeighbours++;
if (isValid(i + 1, j) && (Math.abs(cells[i + 1][j]) == 1))
activeNeighbours++;
if (isValid(i + 1, j + 1) && (Math.abs(cells[i + 1][j + 1]) == 1))
activeNeighbours++;
return activeNeighbours;
}
// check if i, j is valid indices for grid
private static boolean isValid(int i, int j) {
if (i >= 0 && i < 20 && j >= 0 && j < 20)
return true;
else
return false;
}
// to print the grid
public static void print(int[][] cells) {
for (int i = 0; i < 20; i++) {
System.out
.println("---------------------------------------------------------------------------------");
for (int j = 0; j < 20; j++) {
String s = (cells[i][j] == 1) ? "| 0 " : "| ";
System.out.print(s);
}
System.out.println("|");
}
System.out
.println("---------------------------------------------------------------------------------");
}
}
Sample Output:
Starting game with below configuration
---------------------------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | |
---------------------------------------------------------------------------------
| | 0 | | | 0 | 0 | | | | | 0 | | | | | | 0 | | | |
---------------------------------------------------------------------------------
| | | 0 | | 0 | | 0 | | 0 | 0 | 0 | | 0 | 0 | 0 | | | | | |
---------------------------------------------------------------------------------
| | 0 | | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | | | | 0 | | 0 | 0 | 0 | |
---------------------------------------------------------------------------------
| | | 0 | | | | 0 | 0 | | | | 0 | | 0 | 0 | | | 0 | | |
---------------------------------------------------------------------------------
| | | 0 | 0 | 0 | 0 | | | | 0 | | 0 | 0 | | | | | 0 | 0 | |
---------------------------------------------------------------------------------
| | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | | | 0 | | | 0 | | | | 0 | |
---------------------------------------------------------------------------------
| | | 0 | | | | 0 | | 0 | | | 0 | 0 | 0 | | 0 | | 0 | | |
---------------------------------------------------------------------------------
| | 0 | 0 | 0 | 0 | | | 0 | 0 | 0 | | | | 0 | 0 | 0 | | | | |
---------------------------------------------------------------------------------
| | | | | 0 | | 0 | | 0 | 0 | 0 | 0 | | 0 | | 0 | | 0 | 0 | |
---------------------------------------------------------------------------------
| | | | 0 | | 0 | | 0 | 0 | 0 | | | | | 0 | | | | 0 | |
---------------------------------------------------------------------------------
| | 0 | 0 | 0 | 0 | 0 | | 0 | 0 | | 0 | 0 | | 0 | 0 | | | | | |
---------------------------------------------------------------------------------
| | 0 | | | | 0 | | 0 | | 0 | 0 | | 0 | | | 0 | 0 | | 0 | |
---------------------------------------------------------------------------------
| | | 0 | 0 | 0 | 0 | | 0 | 0 | | | | 0 | | | | | | | |
---------------------------------------------------------------------------------
| | | | 0 | | 0 | | 0 | | | 0 | | 0 | 0 | | | 0 | 0 | | |
---------------------------------------------------------------------------------
| | 0 | 0 | | 0 | 0 | 0 | | | | | 0 | 0 | 0 | | | 0 | 0 | | |
---------------------------------------------------------------------------------
| | | | 0 | 0 | | | | 0 | | | 0 | 0 | 0 | | | 0 | 0 | | |
---------------------------------------------------------------------------------
| | 0 | | 0 | 0 | 0 | 0 | | 0 | | 0 | | 0 | | | 0 | 0 | | | |
---------------------------------------------------------------------------------
| | | | | | 0 | | 0 | 0 | | | | | 0 | 0 | | 0 | 0 | | |
---------------------------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | |
---------------------------------------------------------------------------------
Births: 42 Deaths: 93
---------------------------------- Round :1
---------------------------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | |
---------------------------------------------------------------------------------
| | | | 0 | 0 | 0 | | | | | 0 | 0 | | 0 | | | | | | |
---------------------------------------------------------------------------------
| | 0 | 0 | | | | | | | | 0 | 0 | | 0 | 0 | | 0 | | | |
---------------------------------------------------------------------------------
| | 0 | | | 0 | | | | | | | 0 | | | | | 0 | 0 | 0 | |
---------------------------------------------------------------------------------
| | 0 | | | | | | | | 0 | | 0 | | 0 | 0 | 0 | | | | |
---------------------------------------------------------------------------------
| | | | | | | | | 0 | | | 0 | | | 0 | | | 0 | 0 | |
---------------------------------------------------------------------------------
| | 0 | | | | | | 0 | 0 | | | | | | 0 | | 0 | | 0 | |
---------------------------------------------------------------------------------
| | | | | | | | | | 0 | 0 | 0 | | | | 0 | 0 | | | |
---------------------------------------------------------------------------------
| | 0 | 0 | | 0 | | 0 | | | | | | | | | 0 | | 0 | 0 | |
---------------------------------------------------------------------------------
| | | | | | | 0 | | | | | | 0 | 0 | | 0 | 0 | 0 | 0 | |
---------------------------------------------------------------------------------
| | | | | | | | | | | | | | | | 0 | | 0 | 0 | |
---------------------------------------------------------------------------------
| | 0 | | 0 | | 0 | | | | | | 0 | 0 | 0 | 0 | | | 0 | | |
---------------------------------------------------------------------------------
| | 0 | | | | | | | | | 0 | | 0 | | 0 | 0 | | | | |
---------------------------------------------------------------------------------
| | | 0 | 0 | | 0 | | 0 | | | 0 | | 0 | | | 0 | | | | |
---------------------------------------------------------------------------------
| | 0 | | | | | | 0 | 0 | | | | | | | | 0 | 0 | | |
---------------------------------------------------------------------------------
| | | 0 | | | | 0 | 0 | | | 0 | | | | 0 | 0 | | | 0 | |
---------------------------------------------------------------------------------
| | 0 | | | | | | | | 0 | 0 | | | | 0 | | | | | |
---------------------------------------------------------------------------------
| | | 0 | 0 | | | 0 | | 0 | | | | | | | | | | | |
---------------------------------------------------------------------------------
| | | | | | 0 | | 0 | 0 | 0 | | | | 0 | 0 | | 0 | 0 | | |
---------------------------------------------------------------------------------
| | | | | | | | | | | | | | | | | | | | |
---------------------------------------------------------------------------------
Enter choice for next round: n
Births: 53 Deaths: 47
---------------------------------- Round :2
---------------------------------------------------------------------------------
| | | | | 0 | | | | | | | | | | | | | | | |
---------------------------------------------------------------------------------
| | | 0 | 0 | 0 | | | | | | 0 | 0 | | 0 | 0 | | | | | |
---------------------------------------------------------------------------------
| | 0 | 0 | | | 0 | | | | | | | | 0 | 0 | 0 | 0 | | | |
---------------------------------------------------------------------------------
| 0 | 0 | | | | | | | | | | 0 | | | | | 0 | 0 | | |
---------------------------------------------------------------------------------
| | | | | | | | | | | | 0 | | 0 | 0 | 0 | | | | |
---------------------------------------------------------------------------------
| | | | | | | | 0 | 0 | 0 | 0 | | 0 | | | | 0 | 0 | 0 | |
---------------------------------------------------------------------------------
| | | | | | | | 0 | 0 | | | 0 | | | 0 | | 0 | | 0 | |
---------------------------------------------------------------------------------
| | 0 | 0 | | | | | 0 | 0 | 0 | 0 | | | | 0 | | | | 0 | |
---------------------------------------------------------------------------------
| | | | | | 0 | | | | | 0 | 0 | 0 | | | | | | 0 | |
---------------------------------------------------------------------------------
| | | | | | 0 | | | | | | | | | | 0 | | | | 0 |
---------------------------------------------------------------------------------
| | | | | | | | | | | | 0 | | | | 0 | | | | |
---------------------------------------------------------------------------------
| | | 0 | | | | | | | | | 0 | 0 | | | | | 0 | 0 | |
---------------------------------------------------------------------------------
| | 0 | | 0 | | | 0 | | | | 0 | | | | | 0 | 0 | | | |
---------------------------------------------------------------------------------
| | 0 | 0 | | | | 0 | 0 | 0 | 0 | | | | 0 | 0 | 0 | | | | |
---------------------------------------------------------------------------------
| | 0 | | 0 | | | | | 0 | 0 | | 0 | | | 0 | | 0 | 0 | | |
---------------------------------------------------------------------------------
| | 0 | 0 | | | | 0 | 0 | | | 0 | | | | 0 | 0 | 0 | 0 | | |
---------------------------------------------------------------------------------
| | 0 | | 0 | | | 0 | | 0 | 0 | 0 | | | | 0 | 0 | | | | |
---------------------------------------------------------------------------------
| | | 0 | | | | 0 | | | | 0 | | | 0 | 0 | 0 | | | | |
---------------------------------------------------------------------------------
| | | | | | | 0 | 0 | 0 | 0 | | | | | | | | | | |
---------------------------------------------------------------------------------
| | | | | | | | | 0 | | | | | | | | | | | |
---------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.