| C:\upload\java\Life\source\java\Initializations.java |
< Vars >
import java.awt.*;
final class Initializations implements Constants, Strings
{
private Vars vars;
private PopupWindow error;
Initializations(Objects objects)
{
String errorString = (objects != null) ?
NULL_STRING : "objects = " + objects;
if (!errorString.equals(NULL_STRING))
{
System.out.println("Error, null pointer, Initializations " + errorString);
return;
}
error = new PopupWindow(objects.GetParentFrame(),
objects.GetScreenLocations(),
ERROR_WINDOW_NAME);
}
< boardWidth > < boardHeight >
protected void SetDefaults(Vars vars)
{
if ((vars.GetBoardWidth() == GARBAGE_VALUE) ||
(vars.GetBoardHeight() == GARBAGE_VALUE))
{
error.ShowMessage(BOARD_WIDTH_OR_HEIGHT_IS_GARBAGE + ". " +
" < Initializations : SetDefaults() >, " + WE_CANNOT_INTERPRET_THIS + ".",
OK);
}
else
{
vars.SetPixelsPerCell(DEFAULT_PIXELS_PER_CELL);
vars.SetColumns(vars.GetBoardWidth() /vars.GetPixelsPerCell());
vars.SetRows(vars.GetBoardHeight()/vars.GetPixelsPerCell());
vars.SetMaxNumberOfColumns(vars.GetBoardWidth() / MIN_PIXELS_PER_CELL);
vars.SetMaxNumberOfRows(vars.GetBoardHeight() / MIN_PIXELS_PER_CELL);
int rowMaxPixels = vars.GetBoardHeight() / MIN_ROWS;
int colMaxPixels = vars.GetBoardWidth() / MIN_COLS;
vars.SetMaxPixelsPerCell(Math.min(rowMaxPixels, colMaxPixels));
vars.SetMinPixelsPerCell(MIN_PIXELS_PER_CELL);
vars.SetMinNumberOfRows(MIN_ROWS);
vars.SetMinNumberOfColumns(MIN_COLS);
vars.SetTimeBetweenGenerations(HALF_SECOND);
vars.SetProbability(DEFAULT_PROBABILITY_OF_ALIVE);
vars.SetWaitForTimeDelay(true);
vars.SetClickLiveCellsViaMouse(true);
}
}
protected void InitializeEveryone(Vars vars, Objects objects)
{
PaintBoardAndComputeNextGeneration
paintBoardAndComputeNextGeneration
= objects.GetPaintBoardAndComputeNextGeneration();
GenerationNumberCanvas genNum = objects.GetGenNum();
BoardUtilities boardUtilities = objects.GetBoardUtilities();
ManageBoardCoordinates boardCoordinates = objects.GetBoardCoordinates();
if ((vars.GetRows() >= vars.GetMinNumberOfRows()) &&
(vars.GetRows() <= vars.GetMaxNumberOfRows()) &&
(vars.GetColumns() >= vars.GetMinNumberOfColumns()) &&
(vars.GetColumns() <= vars.GetMaxNumberOfColumns()))
{
vars.MakeBoards();
}
else
{
String errorStr = BAD_VALUES_FOR_ROWS_OR_COLUMNS + " " +
ROWS + " = " + vars.GetRows() + " " + COLUMNS + " = " +
vars.GetColumns() + MUST_HAVE_COLON +
vars.GetMinNumberOfRows() +
" <= vars.GetRows() <= " +
vars.GetMaxNumberOfRows() + AND + ", " +
vars.GetMinNumberOfColumns() +
" <= columns <= " + vars.GetMaxNumberOfColumns() + ".";
error.ShowMessage(errorStr, OK);
}
try
{
paintBoardAndComputeNextGeneration.setAliveColor(COLOR_ALIVE);
paintBoardAndComputeNextGeneration.setSoonDeadColor(COLOR_SOON_TO_DIE);
paintBoardAndComputeNextGeneration.setAboutToComeToLifeColor
(COLOR_ABOUT_TO_COME_TO_LIFE);
NullOutBoards(vars.GetThisGeneration(),
vars.GetNextGeneration(),
vars.GetOldBoards());
}
catch (NullPointerException e)
{
String whoFailed;
whoFailed = "" + ((boardUtilities == null) ? " boardUtilities null " : "") +
((boardCoordinates == null) ? " boardCoordinates null " : "") +
((paintBoardAndComputeNextGeneration == null ) ?
"paintBoard null " : "") +
((genNum == null) ? " genNum null " : "") + ".";
error.ShowMessage(POINTER_NULL +
"< Initializations() initializeEveryone >?!?! " +
whoFailed, OK);
}
}
protected final void Probalistic(int [][] board,
int p,
int rows, int columns)
{
int i = 0;
int j = 0;
try
{
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= columns; j++)
{
if (Math.random() <= (float) (p) / 100)
{
board[i][j] = 1;
}
else
{
board[i][j] = 0;
}
}
}
}
catch (ArrayIndexOutOfBoundsException e)
{
String errorStr = e.getMessage();
errorStr = errorStr +
ARRAY_BOUNDS_EXCEPTION + " routine Probalistic: " +
" " + ROWS + " = " + rows +
COLUMNS + " = " + columns +
", i = " + i + ", and j = " + j;
error.ShowMessage(errorStr, OK);
}
}
protected final void NullOutOldBoards(Vars vars)
{
int [][][] oldBoards = vars.GetOldBoards();
for (int k = 0; k < oldBoards.length; k++)
{
for (int i = 0; i < oldBoards[0].length; i++)
{
for (int j = 0; j < oldBoards[0][0].length; j++)
{
oldBoards[k][i][j] = GARBAGE_VALUE;
}
}
}
}
protected final void NullOutBoards(int[][] A, int [][] B)
{
nullBoards(A, B, null);
}
protected final void NullOutBoards(int [][] A, int [][] B,
int [][][] oldBoards)
{
nullBoards(A, B, oldBoards);
}
private final void nullBoards(int [][] A, int [][] B,
int [][][] oldBoards)
{
int i, j, k;
boolean dimensionsMakeSense = ((A.length == B.length) &&
(A[0].length == B[0].length ));
if (oldBoards != null)
{
dimensionsMakeSense &= (A.length == oldBoards[0].length) &&
(A[0].length == oldBoards[0][0].length);
}
if (!dimensionsMakeSense)
{
String errorString = "Routine < nullBoards >, class " +
"BoardUtilities.java: " +
DIMENSIONS_OF_BOARD_DO_NOT_MAKE_SENSE +
"A.length = " + A.length +
"; B.length = " + B.length +
"A[0].length = " + A[0].length + "; B[0].length = " +
B[0].length + ".";
if (oldBoards != null)
{
errorString += AND + "oldBoards[0].length = " +
oldBoards[0].length +
"; oldBoards[0][0].length = " +
oldBoards[0][0].length + ".";
}
error.ShowMessage(errorString, OK);
}
else
{
for (i = 0; i < A.length; i++)
{
for (j = 0; j < A[0].length; j++)
{
A[i][j] = B[i][j] = 0;
if (oldBoards != null)
{
for (k = 0; k < oldBoards.length; k++)
{
oldBoards[k][i][j] = GARBAGE_VALUE;
}
}
}
}
}
}
}