Write a recursive method that counts the number of creatures on a grid. A creatu
ID: 3759055 • Letter: W
Question
Write a recursive method that counts the number of creatures on a grid. A creature is defined as a contiguous collection of 1's (connected horizontally vertically and diagonally). (In JAVA)
Input
Max dimensions of the array
Distribution of organisms on the array
Output
The number of creatures and the layout of the grid showing the locations of the creatures.
Input File (in.txt)
The format of the input file is as follows: First line is the dimensions of the grid.. Then the grid containing 1s for the creatures and 0s for the spaces. The file terminates by 0 0.
5 5
0 0 0 0 0
0 1 1 1 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
5 5
0 0 0 0 1
0 1 1 1 0
1 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0
Output File (out.txt)
Experiment # 1
Number of Creatures: 1
0 0 0 0 0
0 1 1 1 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Experiment #2
Number of Creatures: 1
0 0 0 0 1
0 1 1 1 0
1 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Explanation / Answer
import java.io.*;
public class grid_view
{
private BufferedReader _reader = null;
private BufferedWriter _writer = null;
private int _maxGrid = 0;
private int _numGerneration = 0;
private int[][] _readData = null;
private int[][] _writeData = null;
public grid_view(String inFile, String outFile) throws FileNotFoundException,IOException //consrtuction have two parameters
{
File file = new File(inFile);
this._reader = new BufferedReader(new FileReader(file));
this._writer = new BufferedWriter(new FileWriter(outFile));
boolean state;
do
{
state = this.ReadGeneration();
for (int i = 1; i <= this._numGerneration; i++)
{
this.GenerateGeneration();
this.WriteGeneration(i);
this.CopyCurrentGeneration();
}
}
while (state);
{
this._reader.close();
this._writer.close();
}
}
private boolean ReadGeneration() throws IOException
{
int lineIndex = -1;
String line = this._reader.readLine();
System.out.println("first line is the dimensions of the grid that equal: " + line);
if (line == "00")
return false;
this._maxGrid = Integer.parseInt(line.valueOf(line.charAt(0)));
this._readData = new int[this._maxGrid][this._maxGrid];
System.out.println("maxGrid equal: " + _maxGrid);
line = this._reader.readLine();
System.out.println("second line " + line);
if (line == "00")
return false;
this._numGerneration = Integer.parseInt(line.valueOf(line.charAt(0)));
System.out.println("the number of generation equal: " + _numGerneration);
for (int i = 0; i < this._readData.length; i++)
{
line = this._reader.readLine();
if (line == "00")
return false;
for (int j = 0; j < this._readData.length; j++)
{
_readData[i][j] =
Integer.parseInt(line.valueOf(line.charAt(j)));
}
}
line = this._reader.readLine();
boolean x;
if (line.equals("00"))
{
x = false;
} else {
x = true;
}
return x;
}
private void GenerateGeneration()//generate method
{
this._writeData = new int[this._maxGrid][this._maxGrid];
for (int r = 0; r < this._maxGrid; r++)
{
for (int c = 0; c < this._maxGrid; c++)
{
if (this._readData[r][c] == 0 && this.IsBorn(r, c, this._readData))
this._writeData[r][c] = 1;
else if (this._readData[r][c] == 1 && this.IsDie(r, c, this._readData))
this._writeData[r][c] = 0;
else
this._writeData[r][c] = this._readData[r][c];
}
}
}
private void WriteGeneration(int generation) throws IOException //write method
{
this._writer.write(" ");
this._writer.newLine();
this._writer.write("Creature #" + generation);
String line = "";
for (int x = 0; x < this._maxGrid; x++)
{
this._writer.newLine();
line = "";
for (int y = 0; y < _maxGrid; y++)
{
line += this._writeData[x][y];
}
this._writer.write(line);
}
}
private void CopyCurrentGeneration() //to do it with the second generation and continue
{
for (int n = 0; n < this._maxGrid; n++)
{
for (int m = 0; m < this._maxGrid; m++)
{
this._readData[n][m] = this._writeData[n][m];
}
}
}
private boolean IsBorn(int x, int y, int[][] array) //check for born by the neighbors ruls
{
if (this.GetActiveNeighbors(x, y, array) == 3)
return true;
return false;
}
private boolean IsDie(int y, int x, int[][] array) //check for die by the neighbors ruls
{
int neighbors = this.GetActiveNeighbors(y, x, array);
if (neighbors < 2 || neighbors > 3)
return true;
return false;
}
/* a[0,0], a[0,1], a[0,2]
a[1,0], a[1,1], a[1,2]
a[2,0], a[2,1], a[2,2]*/
private int GetActiveNeighbors(int a, int b, int[][] array) // 9 cases to check
{
int result = 0;
if (a == 0 && b == 0)
{
if (array[0][1] == 1)
result++;
if (array[1][1] == 1)
result++;
if (array[1][0] == 1)
result++;
}
else if (a == this._maxGrid - 1 && b == this._maxGrid - 1)
{
if (array[a][b - 1] == 1)
result++;
if (array[a - 1][b - 1] == 1)
result++;
if (array[a - 1][b] == 1)
result++;
}
else if (a == 0 && b == this._maxGrid - 1)
{
if (array[0][b - 1] == 1)
result++;
if (array[1][b - 1] == 1)
result++;
if (array[1][b] == 1)
result++;
}
else if (a == this._maxGrid - 1 && b == 0)
{
if (array[a - 1][b] == 1)
result++;
if (array[a - 1][b + 1] == 1)
result++;
if (array[a][b + 1] == 1)
result++;
}
else if (a == 0 && (b > 0 && b < this._maxGrid - 1))
{
if (array[0][b - 1] == 1)
result++;
if (array[1][b - 1] == 1)
result++;
if (array[1][b] == 1)
result++;
if (array[1][b + 1] == 1)
result++;
if (array[0][b + 1] == 1)
result++;
}
else if (a == this._maxGrid - 1 && (b > 0 && b < this._maxGrid - 1))
{
if (array[a][b - 1] == 1)
result++;
if (array[a - 1][b - 1] == 1)
result++;
if (array[a - 1][b] == 1)
result++;
if (array[a - 1][b + 1] == 1)
result++;
if (array[a][b + 1] == 1)
result++;
}
else if (b == 0 && (a > 0 && a < this._maxGrid - 1))
{
if (array[a - 1][0] == 1)
result++;
if (array[a - 1][1] == 1)
result++;
if (array[a][1] == 1)
result++;
if (array[a + 1][1] == 1)
result++;
if (array[a + 1][0] == 1)
result++;
}
else if (b == this._maxGrid - 1 && (a > 0 && a < this._maxGrid - 1))
{
if (array[a + 1][b] == 1)
result++;
if (array[a + 1][b - 1] == 1)
result++;
if (array[a][b - 1] == 1)
result++;
if (array[a - 1][b - 1] == 1)
result++;
if (array[a - 1][b] == 1)
result++;
}
else
{
if (array[a - 1][b - 1] == 1)
result++;
if (array[a - 1][b] == 1)
result++;
if (array[a - 1][b + 1] == 1)
result++;
if (array[a][b + 1] == 1)
result++;
if (array[a + 1][b + 1] == 1)
result++;
if (array[a + 1][b] == 1)
result++;
if (array[a + 1][b - 1] == 1)
result++;
if (array[a][b - 1] == 1)
result++;
}
return result;
}
public static void main(String[] args) throws FileNotFoundException,IOException
{
grid_view g = new grid_view("c:\users\in.txt", "c:\users\out.txt");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.