Sample output: You will write a Java program that implements Conway\'s Game of L
ID: 3764334 • Letter: S
Question
Sample output:You will write a Java program that implements Conway's Game of Life, a simple cellular automaton discussed in class. See for example http://www.bitstorm.org/gameoflifel Our version has a 10 x 10 grid, numbered like this: 01 2345 678 9 The grid is represented by a 10 x 10 2-dimensional integer array. If the grid point Gi, j) is "populated", the array element [ilG] contains 1; otherwise it contains 0. Elements along the edges, i = 0 or 9, or j = 0 or 9, are always unpopulated. , an un indicated by a space. What your program should do: Prompt the user to enter a list of (ij) pairs, both non-negative integers (stop when a negative integer is read for either i or j) .
Explanation / Answer
Answer :
import java.util.*;
import java.io.*;
public class GameOfLyf
{
int l=20,b=60;
public static void main(String[] args)
{
GameOfLyf n=new GameOfLyf();
n.setGame();
}
void setGame()
{
char[][] config=new char[l][b];
beginGame(config,l,b);
}
void beginGame(char[][] mat,int l, int b)
{
Scanner s=new Scanner(System.in);
String ch="";
float per=0;
while(!ch.equals("y"))
{
per=setConfig(mat);
setCustomisedConfig(mat,"exam.txt");
display2D(mat);
System.out.println((per*100)+"% of grid filled.");
System.out.println("Begin? y/n");
ch=s.nextLine();
}
while(!ch.equals("x"))
{
mat=change(mat,l,b);
display2D(mat);
System.out.println("Ctrl+Z to stop.");
try
{
Thread.sleep(100);
}
catch(Exception e)
{
System.out.println("Something went horribly wrong.");
}
//ch=s.nextLine();
}
s.close();
System.out.println("Game Is Completed");
}
char[][] change(char[][] mat,int l, int b)
{
char[][] newmat=new char[l][b];
for(int i=0;i<l;i++)
for(int j=0;j<b;j++)
newmat[i][j]=jump(mat,i,j);
return newmat;
}
char jump(char[][] mat,int i, int j)
{
int count=towards(mat,i,j);
if(mat[i][j]=='*')
{
if(count<2||count>3)
return '_';
return '*';
}
else
{
if(count==3)
return '*';
return '_';
}
}
int towards(char[][] mat, int i, int j)
{
int count=0;
for(int x=i-1;x<=i+1;x++)
for(int y=j-1;y<=j+1;y++)
{
if(x==i&&y==j)
continue;
count+=evaluate(mat,x,y);
}
return count;
}
int evaluate(char[][] mat, int i, int j)
{
if(i<0||j<0||i==l||j==b)
return 0;
if(mat[i][j]=='*')
return 1;
return 0;
}
float setCustomisedConfig(char[][] arr,String infile)
{
try
{
BufferedReader br=new BufferedReader(new FileReader(infile));
String line;
for(int i=0;i<arr.length;i++)
{
line=br.readLine();
for(int j=0;j<arr[0].length;j++)
arr[i][j]=line.charAt(j);
}
br.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
return 0;
}
float setConfig(char[][] arr)
{
float per=0.10f;//(float)Math.random();
for(int i=0;i<arr.length;i++)
setConfigOnedimen(arr[i],per);
return per;
}
void setConfigOnedimen(char[] arr,float per)
{
for(int i=0;i<arr.length;i++)
{
if(Math.random()<per)
arr[i]='*';
else
arr[i]='_';
}
}
void display2D(char[][] arr)
{
for(int i=0;i<arr.length;i++)
displayOneDimen(arr[i]);
System.out.println();
}
void displayOneDimen(char[] arr)
{
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]);
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.