Well once again I have some trouble getting my lab to work. I am currently having trouble on an algorithm that is supposed to count the neighbors next to a cell in a Two-dimensional array which is supposed to resemble with the algorithm of Conway's Game of Life. For some reason becuase reason it doesnt seem to be working. I have everything else done and would gladly appriciate some help.
import apcslib.*; import chn.util.*;
public class Life
{
boolean alive = false;
boolean[][] board = new boolean[21][21];
boolean[][] nextBoard = new boolean[21][21];
FileInput inFile;
FileOutput outFile;
public Life(String input, String output)
{
inFile = new FileInput(input);
outFile = new FileOutput(output);
}
public void cellConstruct()
{
int pairNumbers = inFile.readInt();
int count = 0;
int x, y;
int width = 20;
while (inFile.hasMoreTokens())
{
x = inFile.readInt();
y = inFile.readInt();
count++;
board[x][y] = true;
}
}
public void setAlive(int x, int y, boolean alive)
{
board[x][y] = true;
}
public boolean CellIsAlive(int i, int j)
{
return board[i][j];
}
public int countLiveNeighbors (int i, int j)
{
int limit = 21 - 1;
int count = 0;
for (int ii = i - 1; ii <= i + 1; ii++)
{
for (int jj = j - 1; jj <= j + 1; jj++)
{
if (ii == i && jj == j) continue;
if (ii < 0 || ii > limit) continue;
if (jj < 0 || jj > limit) continue;
if (CellIsAlive (ii, jj) == true) count++;
}
}
return count;
}
public void execute(int numberSteps)
{
int numberOfSteps = numberSteps;
int numberOfNeighbors;
for (int step = 0; step < numberOfSteps; step++)
{
for (int row = 0; row < nextBoard.length; row++)
{
for (int col = 0; col < nextBoard[row].length; col++)
{
nextBoard[row][col] = board[row][col];
}
}
// Set up an empty board and fill it with value for next step
for (int i = 0; i < 21; i++)
{
for (int j = 0; j < 21; j++)
{
numberOfNeighbors = countLiveNeighbors(i, j);
if (CellIsAlive(i, j))
{
if (numberOfNeighbors == 2 || numberOfNeighbors == 3)
nextBoard[i][j] = true;
}
else // cell is dead
if (numberOfNeighbors != 3 || numberOfNeighbors != 2)
nextBoard[i][j] = false;
}
}
for (int row = 0; row < board.length; row++)
{
for (int col = 0; col < board[row].length; col++)
{
board[row][col] = nextBoard[row][col];
}
}
print();
outFile.close();
}
}
public void print ()
{
outFile.println(Format.left(" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",4));
outFile.println("");
for (int row = 1; row < board.length; row++)
{
outFile.print(Format.left(row,4));
for (int col = 1; col < board[row].length; col++)
{
if (board[row][col] == true)
{
outFile.print(Format.right("*",3));
}
else
{
outFile.print(Format.right(" ",3));
}
}
outFile.println();
}
}
}
class LifeOutput
{
public static void main (String args[])
{
ConsoleIO input = new ConsoleIO();
int numberOfSteps;
String fileInput;
System.out.println("Welcome to Life.");
System.out.println("");
System.out.println("Please enter name of the file to be read: ");
fileInput = input.readLine();
System.out.println("Please enter the # of generations to elaspe: ");
numberOfSteps = input.readInt();
System.out.println("");
System.out.println("Executing....");
System.out.println("");
Life lifeS = new Life(fileInput,"fileout.txt");
lifeS.cellConstruct();
lifeS.execute(numberOfSteps);
}
}
The lab to this program can be found 'Here'
Once again thanks in advance,
Lord Felix
PS: I figured out the error with my previous post so thanks anyways.