(Java) Content of text file example: GameOfLife4.txt Assignment John H. Conway,
ID: 3876166 • Letter: #
Question
(Java) Content of text file example: GameOfLife4.txt
Assignment John H. Conway, a Cambridge mathematician, invented the Game of Life. The simulation runs on a two-dimensional arrav. Each cell in the arrav is in one of two states: occupied by a creature or empty. In this assignment we will populate the initial grid by reading data from a text file Once the initial grid has been created, the program loops. Each iteration of the loop represents a tick or time step (in other words, a new generation) in the environment. Cells interact with their eight neighbors. Neighbors are the cells that are horizontally, vertically, or diagonally adjacent. Creatures are created and die based on the following rules: 1. Any living creature (occupied cell) with fewer than two live neighbors dies of 2. Any living creature with two or three live neighbors lives on to the next 3. Any living creature with more than three live neighbors dies of 4. Any empty cell with exactly three live neighbors becomes an occupied cell. loneliness. generation. overcrowding This represents a birth. It is important to note that all births and deaths in each tick occur simultaneously.Explanation / Answer
Cells.java : ---------------------->>>>>>>>>>>>>>>>>
public class Cell{
private boolean occupied;
public Cell(boolean oc){
occupied = oc;
}
public boolean getOccupied(){
return occupied;
}
public void setOccupied(boolean b){
occupied = b;
}
}
Environment.java : -------------------->>>>>>>>>>>>>>>>>>
import java.io.File;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
public class Environment{
private int row;
private int col;
private Cell[][] cells;
public Environment(String fn) throws Exception{
Scanner sc = new Scanner(new File(fn));
row = sc.nextInt();
col = sc.nextInt();
cells = new Cell[row][col];
for(int i = 0;i<row;i++){
for(int j = 0;j<col;j++){
cells[i][j] = new Cell(sc.nextInt() == 1);
}
}
}
private void display(){
System.out.println("------------------GRID LIFES----------------- ");
for(int i = 0;i<row;i++){
for(int j = 0;j<col;j++){
if(cells[i][j].getOccupied()){
System.out.print(" 1 ");
}else{
System.out.print(" 0 ");
}
}
System.out.println();
}
}
private int noOfCells(int r,int c){
int count = 0;
if((r-1) >= 0){
if(cells[r-1][c].getOccupied()){
count++;
}
}
if((c-1) >= 0){
if(cells[r][c-1].getOccupied()){
count++;
}
}
if((r+1) <= row-1){
if(cells[r+1][c].getOccupied()){
count++;
}
}
if((c+1) <= col-1){
if(cells[r][c+1].getOccupied()){
count++;
}
}
return count;
}
private void createNextGrid(){
int n = 0;
for(int i = 0;i<row;i++){
for(int j = 0;j<col;j++){
n = noOfCells(i,j);
if(n < 2 || n > 3){
cells[i][j].setOccupied(false);
}
if(n == 3){
cells[i][j].setOccupied(true);
}
}
}
}
private boolean check(){
int n = 0;
for(int i = 0;i<row;i++){
for(int j = 0;j<col;j++){
if(!cells[i][j].getOccupied()){
n++;
}
}
}
if(n == (row*col)){
return false;
}
else{
return true;
}
}
public void runSimulation() throws Exception{
while(check()){
TimeUnit.SECONDS.sleep(3);
display();
createNextGrid();
}
display();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.